Skip to content

Commit c7f49fe

Browse files
QuncCcccccchunhtai
andauthored
Fix mismatch between hit-test tree and traversal tree (#186826)
This PR is to update hit-test tree to make sure traversal tree and hit-test tree have the same number of nodes, even though they can have different shapes. In OverlayPortal, if the child node is a traversal child, but the current node is not a traversal parent, it means the child node should be grafted to be a child of a traversal parent node that has the same identifier as the child. However, if the traversal parent is not in the tree, after grafting, the child will not be in the tree as well. To keep the hit-test tree match the traversal tree, we also skip this node in childrenInHitTestOrder. Otherwise, the number of nodes in two trees will be different and user might accidentally hit test a node that will never be traversed. Fixes #184898 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. --------- Co-authored-by: chunhtai <[email protected]>
1 parent d742b87 commit c7f49fe

2 files changed

Lines changed: 73 additions & 11 deletions

File tree

‎packages/flutter/lib/src/semantics/semantics.dart‎

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ typedef SemanticsUpdateCallback = void Function(SemanticsUpdate update);
112112
///
113113
/// Use [ChildSemanticsConfigurationsResultBuilder] to generate the return
114114
/// value.
115-
typedef ChildSemanticsConfigurationsDelegate =
116-
ChildSemanticsConfigurationsResult Function(List<SemanticsConfiguration>);
115+
typedef ChildSemanticsConfigurationsDelegate = ChildSemanticsConfigurationsResult Function(
116+
List<SemanticsConfiguration>,
117+
);
117118

118119
/// Controls how accessibility focus is blocked.
119120
///
@@ -4055,6 +4056,35 @@ class SemanticsNode with DiagnosticableTreeMixin {
40554056
return childrenInTraversalOrder;
40564057
}
40574058

4059+
List<SemanticsNode> _childrenInHitTestOrder() {
4060+
if (_children == null) {
4061+
return const <SemanticsNode>[];
4062+
}
4063+
if (kIsWeb || _isTraversalParent) {
4064+
return _children!;
4065+
}
4066+
4067+
// If a child node is not in the traversal tree after grafting, it is
4068+
// dropped from the hit-test tree to keep the two trees in sync.
4069+
// Otherwise, the user might accidentally hit test a node that cannot
4070+
// be traversed.
4071+
bool shouldNotSkipInHitTest(SemanticsNode child) {
4072+
if (child._isTraversalChild) {
4073+
final SemanticsNode? traversalParent =
4074+
owner!._traversalParentNodes[child.getSemanticsData().traversalChildIdentifier];
4075+
return traversalParent != null;
4076+
}
4077+
return true;
4078+
}
4079+
4080+
return _children!.where(shouldNotSkipInHitTest).toList();
4081+
}
4082+
4083+
Int32List _childrenIdInHitTestOrder() {
4084+
final List<SemanticsNode> children = _childrenInHitTestOrder();
4085+
return Int32List.fromList(children.reversed.map<int>((SemanticsNode node) => node.id).toList());
4086+
}
4087+
40584088
void _addToUpdate(SemanticsUpdateBuilder builder, Set<int> customSemanticsActionIdsUpdate) {
40594089
assert(_dirty);
40604090
final SemanticsData data = getSemanticsData();
@@ -4093,14 +4123,7 @@ class SemanticsNode with DiagnosticableTreeMixin {
40934123
}
40944124
} else {
40954125
childrenInTraversalOrder = _childrenIdInTraversalOrder();
4096-
4097-
final int childCount = _children!.length;
4098-
// _children is sorted in paint order, so we invert it to get the hit test
4099-
// order.
4100-
childrenInHitTestOrder = Int32List(childCount);
4101-
for (int i = childCount - 1; i >= 0; i -= 1) {
4102-
childrenInHitTestOrder[i] = _children![childCount - i - 1].id;
4103-
}
4126+
childrenInHitTestOrder = _childrenIdInHitTestOrder();
41044127
}
41054128

41064129
Int32List? customSemanticsActionIds;
@@ -4539,7 +4562,7 @@ class SemanticsNode with DiagnosticableTreeMixin {
45394562
}
45404563

45414564
return switch (childOrder) {
4542-
DebugSemanticsDumpOrder.inverseHitTest => _children!,
4565+
DebugSemanticsDumpOrder.inverseHitTest => _childrenInHitTestOrder(),
45434566
DebugSemanticsDumpOrder.traversalOrder => _childrenInTraversalOrder(),
45444567
};
45454568
}

‎packages/flutter/test/semantics/semantics_update_test.dart‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ typedef SemanticsNodeUpdateObservation = ({
1919
String hint,
2020
List<StringAttribute>? hintAttributes,
2121
Int32List childrenInTraversalOrder,
22+
Int32List childrenInHitTestOrder,
2223
Float64List transform,
2324
});
2425

@@ -267,6 +268,43 @@ void main() {
267268
handle.dispose();
268269
}, skip: kIsWeb); // intended: the web engine handles the transform calculation itself.
269270

271+
testWidgets(
272+
'Semantics update does not leak nodes to hit-test order when traversal parent is missing',
273+
(WidgetTester tester) async {
274+
final SemanticsHandle handle = tester.ensureSemantics();
275+
// Pumps a placeholder to trigger the warm up frame.
276+
await tester.pumpWidget(const Placeholder(), phase: EnginePhase.build);
277+
SemanticsUpdateBuilderSpy.observations.clear();
278+
279+
const identifier = '111';
280+
await tester.pumpWidget(
281+
Directionality(
282+
textDirection: TextDirection.ltr,
283+
child: Column(
284+
children: <Widget>[
285+
Semantics(
286+
traversalChildIdentifier: identifier,
287+
child: const SizedBox.square(dimension: 10),
288+
),
289+
const SizedBox.square(dimension: 10),
290+
],
291+
),
292+
),
293+
);
294+
295+
// SemanticsNode#0 (root) should have 0 children in both traversal order and hit-test order.
296+
final SemanticsNodeUpdateObservation? rootObservation =
297+
SemanticsUpdateBuilderSpy.observations[0];
298+
expect(rootObservation, isNotNull);
299+
expect(rootObservation!.childrenInTraversalOrder, isEmpty);
300+
expect(rootObservation.childrenInHitTestOrder, isEmpty);
301+
302+
SemanticsUpdateBuilderSpy.observations.clear();
303+
handle.dispose();
304+
},
305+
skip: kIsWeb, // [intended] the web engine handles the tree grafting itself.
306+
);
307+
270308
testWidgets('Semantics update removes detached OverlayPortal traversal child', (
271309
WidgetTester tester,
272310
) async {
@@ -413,6 +451,7 @@ class SemanticsUpdateBuilderSpy extends Fake implements ui.SemanticsUpdateBuilde
413451
value: value,
414452
valueAttributes: valueAttributes,
415453
childrenInTraversalOrder: childrenInTraversalOrder,
454+
childrenInHitTestOrder: childrenInHitTestOrder,
416455
transform: transform,
417456
);
418457
}

0 commit comments

Comments
 (0)