Skip to content

Commit 3c96406

Browse files
authored
Detached traversal child should update its traversal parent (#186118)
Fixes #185784 When a traversal child is detached, its traversal parent was not marked dirty. So the framework didn't send an updated childrenInTraversalOrder for traversal parent. On the engine side, the out-of-dated subtree is still reachable, so Talkback still read invisible items after the menu closed. This PR is to update the _dirtyNodes list when a traversal child is detached. When a traversal child is detached, its traversal parent will be marked dirty and added to the _dirtyNodes list to update its childrenInTraversalOrder. ## 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.
1 parent 58dd014 commit 3c96406

2 files changed

Lines changed: 124 additions & 57 deletions

File tree

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

Lines changed: 48 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3276,12 +3276,19 @@ class SemanticsNode with DiagnosticableTreeMixin {
32763276
owner!._nodes.remove(id);
32773277
owner!._detachedNodes.add(this);
32783278

3279+
if (_traversalChildIdentifier case final Object identifier?) {
3280+
owner!._traversalParentNodes[identifier]?._markDirty();
3281+
}
3282+
32793283
// Clean up the according entry in owner._traversalParentNodes map.
32803284
owner!._traversalParentNodes.removeWhere((Object key, SemanticsNode node) => node == this);
32813285
// Clean up this node from the value set in owner._traversalChildNodes map.
32823286
for (final Set<SemanticsNode> childSet in owner!._traversalChildNodes.values) {
32833287
childSet.removeWhere((SemanticsNode node) => node == this);
32843288
}
3289+
owner!._traversalChildNodes.removeWhere(
3290+
(Object key, Set<SemanticsNode> value) => value.isEmpty,
3291+
);
32853292

32863293
_owner = null;
32873294
assert(parent == null || attached == parent!.attached);
@@ -4049,7 +4056,7 @@ class SemanticsNode with DiagnosticableTreeMixin {
40494056
}
40504057

40514058
void _addToUpdate(SemanticsUpdateBuilder builder, Set<int> customSemanticsActionIdsUpdate) {
4052-
assert(_dirty || _isTraversalParent);
4059+
assert(_dirty);
40534060
final SemanticsData data = getSemanticsData();
40544061
assert(() {
40554062
final FlutterError? error = _DebugSemanticsRoleChecks._checkSemanticsData(this);
@@ -4974,69 +4981,53 @@ class SemanticsOwner extends ChangeNotifier {
49744981
for (final Set<SemanticsNode> childSet in _traversalChildNodes.values) {
49754982
childSet.removeWhere((SemanticsNode oldNode) => node == oldNode);
49764983
}
4977-
}
4978-
}
4979-
4980-
visitedNodes.sort((SemanticsNode a, SemanticsNode b) => a.depth - b.depth);
4981-
final SemanticsUpdateBuilder builder = SemanticsBinding.instance.createSemanticsUpdateBuilder();
4982-
4983-
final updatedVisitedNodes = <SemanticsNode>[];
4984-
4985-
for (final node in visitedNodes) {
4986-
final bool isTraversalParent = node._isTraversalParent;
4987-
final bool isTraversalChild = node._isTraversalChild;
4988-
4989-
if (kIsWeb) {
4990-
updatedVisitedNodes.add(node);
4991-
} else {
4992-
if (!isTraversalParent && !isTraversalChild) {
4993-
updatedVisitedNodes.add(node);
4994-
continue;
4984+
_traversalChildNodes.removeWhere((Object key, Set<SemanticsNode> value) => value.isEmpty);
4985+
final bool isTraversalParent = node._isTraversalParent;
4986+
final bool isTraversalChild = node._isTraversalChild;
4987+
4988+
// If the node is a traversal parent, then add it to the
4989+
// _traversalParentNodes map for later grafting. Similarly, add the node
4990+
// to the _traversalChildNodes map if it is a traversal child.
4991+
if (isTraversalParent) {
4992+
assert(
4993+
!_traversalParentNodes.containsKey(node._traversalParentIdentifier) ||
4994+
_traversalParentNodes[node.traversalParentIdentifier!] == node,
4995+
'The traversalParentIdentifier must be unique. No two semantics nodes can share the same traversalParentIdentifier.',
4996+
);
4997+
_traversalParentNodes[node.traversalParentIdentifier!] = node;
4998+
} else if (isTraversalChild) {
4999+
_traversalChildNodes
5000+
.putIfAbsent(node.traversalChildIdentifier!, () => <SemanticsNode>{})
5001+
.add(node);
49955002
}
49965003

4997-
if (isTraversalChild) {
4998-
// If the node has a non-null `_traversalChildIdentifier`, it indicates
4999-
// that its hit-test parent and traversal parent are different, and
5000-
// its traversal parent should update its children to include this node.
5001-
// Therefore, its traversal parent node should be added to the
5002-
// `updatedVisitedNodes` list for later grafting, in order to generate
5003-
// a correct `childrenIntraversalOrder`. This is typically used in
5004-
// `OverlayPortal` widget.
5005-
final SemanticsNode? parentNode = _traversalParentNodes[node.traversalChildIdentifier];
5006-
if (parentNode != null && !updatedVisitedNodes.contains(parentNode)) {
5007-
updatedVisitedNodes.add(parentNode);
5004+
if (!kIsWeb) {
5005+
if (node._isTraversalChild) {
5006+
// If the node has a non-null `_traversalChildIdentifier`, it indicates
5007+
// that its hit-test parent and traversal parent are different, and
5008+
// its traversal parent should update its children to include this node.
5009+
// Therefore, its traversal parent node should be visited for later
5010+
// grafting, in order to generate a correct `childrenInTraversalOrder`.
5011+
// This is typically used in `OverlayPortal` widget.
5012+
final SemanticsNode? parentNode = _traversalParentNodes[node.traversalChildIdentifier];
5013+
if (parentNode != null && !visitedNodes.contains(parentNode)) {
5014+
parentNode._markDirty();
5015+
}
50085016
}
50095017
}
5010-
5011-
updatedVisitedNodes.add(node);
5012-
}
5013-
5014-
// If the node is a traversal parent, then add it to the
5015-
// _traversalParentNodes map for later grafting. Similarly, add the node
5016-
// to the _traversalChildNodes map if it is a traversal child.
5017-
if (isTraversalParent) {
5018-
assert(
5019-
!_traversalParentNodes.containsKey(node._traversalParentIdentifier) ||
5020-
_traversalParentNodes[node.traversalParentIdentifier!] == node,
5021-
'The traversalParentIdentifier must be unique. No two semantics nodes can share the same traversalParentIdentifier.',
5022-
);
5023-
_traversalParentNodes[node.traversalParentIdentifier!] = node;
5024-
} else if (isTraversalChild) {
5025-
_traversalChildNodes[node.traversalChildIdentifier!] ??= <SemanticsNode>{};
5026-
_traversalChildNodes[node.traversalChildIdentifier!]!.add(node);
50275018
}
50285019
}
50295020

5030-
for (final node in updatedVisitedNodes) {
5031-
assert(
5032-
node.parent?._dirty != true || node._isTraversalParent,
5033-
); // could be null (no parent) or false (not dirty)
5021+
visitedNodes.sort((SemanticsNode a, SemanticsNode b) => a.depth - b.depth);
5022+
final SemanticsUpdateBuilder builder = SemanticsBinding.instance.createSemanticsUpdateBuilder();
50345023

5035-
// The traversalParentNode is added to updatedVisitedNodes for later
5036-
// grafting; its traversalChildren should be grafted to its children in
5037-
// the traversal order. This grafting process is skipped on web because
5038-
// the traversal order will be handled in the web engine.
5039-
final bool needUpdateTraversalParent = !kIsWeb && node._isTraversalParent;
5024+
for (final node in visitedNodes) {
5025+
// TODO(QuncCccccc): Add an assert that each non-root node has a
5026+
// non-dirty parent. This currently fails in view-related tests such as
5027+
// test/widgets/view_test.dart's "correctly switches between view
5028+
// configurations" when switching view configurations during semantics
5029+
// updates.
5030+
assert(node.parent?._dirty != true); // could be null (no parent) or false (not dirty)
50405031
// The _serialize() method marks the node as not dirty, and
50415032
// recurses through the tree to do a deep serialization of all
50425033
// contiguous dirty nodes. This means that when we return here,
@@ -5047,7 +5038,7 @@ class SemanticsOwner extends ChangeNotifier {
50475038
// calls reset() on its SemanticsNode if onlyChanges isn't set,
50485039
// which happens e.g. when the node is no longer contributing
50495040
// semantics).
5050-
if ((node._dirty || needUpdateTraversalParent) && node.attached) {
5041+
if (node._dirty && node.attached) {
50515042
node._addToUpdate(builder, customSemanticsActionIds);
50525043
}
50535044
}

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,82 @@ void main() {
266266
SemanticsUpdateBuilderSpy.observations.clear();
267267
handle.dispose();
268268
}, skip: kIsWeb); // intended: the web engine handles the transform calculation itself.
269+
270+
testWidgets('Semantics update removes detached OverlayPortal traversal child', (
271+
WidgetTester tester,
272+
) async {
273+
final SemanticsHandle handle = tester.ensureSemantics();
274+
await tester.pumpWidget(const Placeholder(), phase: EnginePhase.build);
275+
SemanticsUpdateBuilderSpy.observations.clear();
276+
277+
final controller = OverlayPortalController()..show();
278+
final entry = OverlayEntry(
279+
builder: (BuildContext context) {
280+
return OverlayPortal(
281+
controller: controller,
282+
child: TestButton(onPressed: () {}, child: const Text('anchor')),
283+
overlayChildBuilder: (BuildContext context) {
284+
return TestButton(onPressed: () {}, child: const Text('menu item'));
285+
},
286+
);
287+
},
288+
);
289+
addTearDown(() {
290+
entry
291+
..remove()
292+
..dispose();
293+
});
294+
295+
await tester.pumpWidget(
296+
Directionality(
297+
textDirection: TextDirection.ltr,
298+
child: Overlay(initialEntries: <OverlayEntry>[entry]),
299+
),
300+
);
301+
302+
final int anchorId = SemanticsUpdateBuilderSpy.observations.entries.singleWhere((
303+
MapEntry<int, SemanticsNodeUpdateObservation> entry,
304+
) {
305+
return entry.value.label == 'anchor';
306+
}).key;
307+
final int menuItemId = SemanticsUpdateBuilderSpy.observations.entries.singleWhere((
308+
MapEntry<int, SemanticsNodeUpdateObservation> entry,
309+
) {
310+
return entry.value.label == 'menu item';
311+
}).key;
312+
expect(
313+
SemanticsUpdateBuilderSpy.observations.values.any((
314+
SemanticsNodeUpdateObservation observation,
315+
) {
316+
return observation.childrenInTraversalOrder.contains(menuItemId);
317+
}),
318+
isTrue,
319+
);
320+
321+
SemanticsUpdateBuilderSpy.observations.clear();
322+
controller.hide();
323+
await tester.pump();
324+
325+
expect(SemanticsUpdateBuilderSpy.observations.containsKey(menuItemId), isFalse);
326+
expect(
327+
SemanticsUpdateBuilderSpy.observations.values.any((
328+
SemanticsNodeUpdateObservation observation,
329+
) {
330+
return listEquals(observation.childrenInTraversalOrder, <int>[anchorId]);
331+
}),
332+
isTrue,
333+
);
334+
expect(
335+
SemanticsUpdateBuilderSpy.observations.values.any((
336+
SemanticsNodeUpdateObservation observation,
337+
) {
338+
return observation.childrenInTraversalOrder.contains(menuItemId);
339+
}),
340+
isFalse,
341+
);
342+
SemanticsUpdateBuilderSpy.observations.clear();
343+
handle.dispose();
344+
}, skip: kIsWeb); // intended: the web engine handles the traversal order itself.
269345
}
270346

271347
class SemanticsUpdateTestBinding extends AutomatedTestWidgetsFlutterBinding {

0 commit comments

Comments
 (0)