Commit 4c1150dd authored by amirh's avatar amirh Committed by GitHub

Construct the accessibility channel's events by SemanticsEvent. (#12638)

This refactoring allows us to have SemanticsEvent object for events that are not
associated with an accessibility node id.
And allow https://github.com/flutter/flutter/pull/12594 to be a bit
cleaner with a single place for accessibility channel documentation (the
SemanticsEvent classes documentation).
parent 79f13c36
...@@ -643,12 +643,7 @@ class SemanticsNode extends AbstractNode with DiagnosticableTreeMixin { ...@@ -643,12 +643,7 @@ class SemanticsNode extends AbstractNode with DiagnosticableTreeMixin {
void sendEvent(SemanticsEvent event) { void sendEvent(SemanticsEvent event) {
if (!attached) if (!attached)
return; return;
final Map<String, dynamic> annotatedEvent = <String, dynamic>{ SystemChannels.accessibility.send(event.toMap(nodeId: id));
'nodeId': id,
'type': event.type,
'data': event.toMap(),
};
SystemChannels.accessibility.send(annotatedEvent);
} }
@override @override
......
...@@ -25,15 +25,30 @@ abstract class SemanticsEvent { ...@@ -25,15 +25,30 @@ abstract class SemanticsEvent {
/// Converts this event to a Map that can be encoded with /// Converts this event to a Map that can be encoded with
/// [StandardMessageCodec]. /// [StandardMessageCodec].
Map<String, dynamic> toMap(); ///
/// [nodeId] is the unique identifier of the semantics node associated with
/// the event, or null if the event is not associated with a semantics node.
Map<String, dynamic> toMap({ int nodeId }) {
final Map<String, dynamic> event = <String, dynamic>{
'type': type,
'data': getDataMap(),
};
if (nodeId != null)
event['nodeId'] = nodeId;
return event;
}
/// Returns the event's data object.
Map<String, dynamic> getDataMap();
@override @override
String toString() { String toString() {
final List<String> pairs = <String>[]; final List<String> pairs = <String>[];
final Map<String, dynamic> map = toMap(); final Map<String, dynamic> dataMap = getDataMap();
final List<String> sortedKeys = map.keys.toList()..sort(); final List<String> sortedKeys = dataMap.keys.toList()..sort();
for (String key in sortedKeys) for (String key in sortedKeys)
pairs.add('$key: ${map[key]}'); pairs.add('$key: ${dataMap[key]}');
return '$runtimeType(${pairs.join(', ')})'; return '$runtimeType(${pairs.join(', ')})';
} }
} }
...@@ -87,7 +102,7 @@ class ScrollCompletedSemanticsEvent extends SemanticsEvent { ...@@ -87,7 +102,7 @@ class ScrollCompletedSemanticsEvent extends SemanticsEvent {
final double maxScrollExtent; final double maxScrollExtent;
@override @override
Map<String, dynamic> toMap() { Map<String, dynamic> getDataMap() {
final Map<String, dynamic> map = <String, dynamic>{ final Map<String, dynamic> map = <String, dynamic>{
'pixels': pixels.clamp(minScrollExtent, maxScrollExtent), 'pixels': pixels.clamp(minScrollExtent, maxScrollExtent),
'minScrollExtent': minScrollExtent, 'minScrollExtent': minScrollExtent,
......
...@@ -58,7 +58,7 @@ class SystemChannels { ...@@ -58,7 +58,7 @@ class SystemChannels {
/// ///
/// See also: /// See also:
/// ///
/// * [SemanticsEvents] and its subclasses for a list of valid accessibility /// * [SemanticsEvent] and its subclasses for a list of valid accessibility
/// events that can be sent over this channel. /// events that can be sent over this channel.
static const BasicMessageChannel<dynamic> accessibility = const BasicMessageChannel<dynamic>( static const BasicMessageChannel<dynamic> accessibility = const BasicMessageChannel<dynamic>(
'flutter/accessibility', 'flutter/accessibility',
......
...@@ -24,6 +24,29 @@ void main() { ...@@ -24,6 +24,29 @@ void main() {
'TestSemanticsEvent(number: 10, text: hello)', 'TestSemanticsEvent(number: 10, text: hello)',
); );
}); });
test('SemanticsEvent.toMap', () {
expect(
new TestSemanticsEvent(text: 'hi', number: 11).toMap(),
<String, dynamic> {
'type': 'TestEvent',
'data': <String, dynamic> {
'text': 'hi',
'number': 11
}
}
);
expect(
new TestSemanticsEvent(text: 'hi', number: 11).toMap(nodeId: 123),
<String, dynamic> {
'type': 'TestEvent',
'nodeId': 123,
'data': <String, dynamic> {
'text': 'hi',
'number': 11
}
}
);
});
} }
class TestSemanticsEvent extends SemanticsEvent { class TestSemanticsEvent extends SemanticsEvent {
...@@ -33,7 +56,7 @@ class TestSemanticsEvent extends SemanticsEvent { ...@@ -33,7 +56,7 @@ class TestSemanticsEvent extends SemanticsEvent {
final int number; final int number;
@override @override
Map<String, dynamic> toMap() { Map<String, dynamic> getDataMap() {
final Map<String, dynamic> result = <String, dynamic>{}; final Map<String, dynamic> result = <String, dynamic>{};
if (text != null) if (text != null)
result['text'] = text; result['text'] = text;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment