Clean up semantics code (#191620)
<!--
Thanks for filing a pull request!
Reviewers are typically assigned within a week of filing a request.
To learn more about code review, see our documentation on Tree Hygiene:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
-->
Some bug I found during implementing accessibilityOpaque. Separated them
out to this pr for easier review
## Pre-launch Checklist
- [ ] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [ ] I read the [AI contribution guidelines] and understand my
responsibilities, or I am not using AI tools.
- [ ] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [ ] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [ ] I signed the [CLA].
- [ ] I listed at least one issue that this PR fixes in the description
above.
- [ ] I updated/added relevant documentation (doc comments with `///`).
- [ ] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [ ] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [ ] All existing and new tests are passing.
If you need help, consider asking for advice on the #hackers-new channel
on [Discord].
If this change needs to override an active code freeze, provide a
comment explaining why. The code freeze workflow can be overridden by
code reviewers. See pinned issues for any active code freezes with
guidance.
**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.
<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[AI contribution guidelines]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
diff --git a/packages/flutter/lib/src/rendering/object.dart b/packages/flutter/lib/src/rendering/object.dart
index 620d8ec..7112bfd 100644
--- a/packages/flutter/lib/src/rendering/object.dart
+++ b/packages/flutter/lib/src/rendering/object.dart
@@ -6088,9 +6088,13 @@
if (parentData == newParentData) {
return;
}
+ final bool wasParentDataDirty = parentDataDirty;
// Parent data changes may result in node formation changes.
markNeedsBuild();
parentData = newParentData;
+ if (wasParentDataDirty) {
+ geometry = null;
+ }
updateChildren();
}
diff --git a/packages/flutter/lib/src/semantics/semantics.dart b/packages/flutter/lib/src/semantics/semantics.dart
index 13c34fb..7b4152d 100644
--- a/packages/flutter/lib/src/semantics/semantics.dart
+++ b/packages/flutter/lib/src/semantics/semantics.dart
@@ -112,9 +112,8 @@
///
/// Use [ChildSemanticsConfigurationsResultBuilder] to generate the return
/// value.
-typedef ChildSemanticsConfigurationsDelegate = ChildSemanticsConfigurationsResult Function(
- List<SemanticsConfiguration>,
-);
+typedef ChildSemanticsConfigurationsDelegate =
+ ChildSemanticsConfigurationsResult Function(List<SemanticsConfiguration>);
/// Controls how accessibility focus is blocked.
///
@@ -6461,6 +6460,9 @@
/// is blocked in the a11y focus (different from input focus).
AccessibilityFocusBlockType get accessibilityFocusBlockType => _accessibilityFocusBlockType;
set accessibilityFocusBlockType(AccessibilityFocusBlockType value) {
+ if (_accessibilityFocusBlockType == value) {
+ return;
+ }
_accessibilityFocusBlockType = value;
_flags = _flags.copyWith(
isAccessibilityFocusBlocked: value != AccessibilityFocusBlockType.none,
diff --git a/packages/flutter/test/rendering/semantics_layout_sized_test.dart b/packages/flutter/test/rendering/semantics_layout_sized_test.dart
index 2cf0e4f..cca99b1 100644
--- a/packages/flutter/test/rendering/semantics_layout_sized_test.dart
+++ b/packages/flutter/test/rendering/semantics_layout_sized_test.dart
@@ -158,6 +158,120 @@
pumpFrame(phase: EnginePhase.flushSemantics);
});
+
+ test(
+ 'semantics geometry updates when dropped child with dirty parent data rejoins the semantics tree',
+ () {
+ final greatGrandChild = RenderTestParent();
+ greatGrandChild.explicitChildNode = true;
+
+ final grandChild = RenderTestTransformParent(child: greatGrandChild);
+ grandChild.explicitChildNode = true;
+
+ final child = RenderTestParent(child: grandChild);
+ child.explicitChildNode = true;
+
+ final container = RenderTestParent(child: child);
+ container.explicitChildNode = true;
+
+ final root = RenderTestLastChildSemanticsMultiChildParent(children: <RenderBox>[container]);
+ root.explicitChildNode = true;
+
+ TestRenderingFlutterBinding.instance.pipelineOwner.ensureSemantics();
+ layout(root, phase: EnginePhase.flushSemantics);
+
+ // Initial state: root visits container, child, grandChild, and greatGrandChild.
+ expect(root.debugSemantics!.childrenCount, 1);
+ expect(greatGrandChild.debugSemantics!.transform, null);
+
+ // To trigger the scenario:
+ // Mark greatGrandChild as needing semantics update (setting parentData = null, parentDataDirty = true).
+ greatGrandChild.markNeedsSemanticsUpdate();
+
+ // Adding a new child to root makes it drop container and its subtree from semantics tree.
+ final newChild = RenderTestLayoutSemanticsBoundary();
+ newChild.isSemanticBoundary = true;
+ root.add(newChild);
+
+ pumpFrame(phase: EnginePhase.flushSemantics);
+
+ // Verify container and subtree were dropped.
+ expect(root.debugSemantics!.childrenCount, 1);
+ final children = <SemanticsNode>[];
+ root.debugSemantics!.visitChildren((SemanticsNode child) {
+ children.add(child);
+ return true;
+ });
+ expect(children.single, newChild.debugSemantics);
+
+ // Update grandChild's transform and remove newChild so subtree rejoins the semantics tree.
+ grandChild.transform = Matrix4.translationValues(20.0, 30.0, 0.0);
+ root.remove(newChild);
+
+ pumpFrame(phase: EnginePhase.flushSemantics);
+
+ // Verify greatGrandChild is back in the semantics tree and its geometry was updated with transform.
+ expect(root.debugSemantics!.childrenCount, 1);
+ expect(greatGrandChild.debugSemantics!.transform, Matrix4.translationValues(20.0, 30.0, 0.0));
+ },
+ );
+}
+
+class RenderTestTransformParent extends RenderBox with RenderObjectWithChildMixin<RenderBox> {
+ RenderTestTransformParent({RenderBox? child}) {
+ this.child = child;
+ }
+
+ bool isSemanticBoundary = false;
+ bool explicitChildNode = false;
+ Matrix4? transform = Matrix4.identity();
+
+ @override
+ void describeSemanticsConfiguration(SemanticsConfiguration config) {
+ super.describeSemanticsConfiguration(config);
+ config.isSemanticBoundary = isSemanticBoundary;
+ config.explicitChildNodes = explicitChildNode;
+ config.label = 'Transform Parent';
+ config.textDirection = TextDirection.ltr;
+ }
+
+ @override
+ void setupParentData(RenderObject child) {
+ if (child.parentData is! BoxParentData) {
+ child.parentData = BoxParentData();
+ }
+ }
+
+ @override
+ void performLayout() {
+ if (child != null) {
+ child!.layout(constraints.loosen());
+ }
+ size = constraints.biggest;
+ }
+
+ @override
+ void applyPaintTransform(RenderBox child, Matrix4 transform) {
+ if (this.transform != null) {
+ transform.multiply(this.transform!);
+ }
+ }
+
+ @override
+ void paint(PaintingContext context, Offset offset) {
+ if (child != null) {
+ if (transform != null) {
+ context.pushTransform(needsCompositing, offset, transform!, (
+ PaintingContext context,
+ Offset offset,
+ ) {
+ context.paintChild(child!, offset);
+ });
+ } else {
+ context.paintChild(child!, offset);
+ }
+ }
+ }
}
class RenderTestParentUsesSize extends RenderTestParent {
diff --git a/packages/flutter/test/semantics/semantics_test.dart b/packages/flutter/test/semantics/semantics_test.dart
index 5225f62..cac46f6 100644
--- a/packages/flutter/test/semantics/semantics_test.dart
+++ b/packages/flutter/test/semantics/semantics_test.dart
@@ -1064,6 +1064,23 @@
expect(config.customSemanticsActions[customAction], same(onCustomAction));
});
+ test(
+ 'SemanticsConfiguration accessibilityFocusBlockType does not mark hasBeenAnnotated when unchanged',
+ () {
+ final config = SemanticsConfiguration();
+ expect(config.accessibilityFocusBlockType, AccessibilityFocusBlockType.none);
+ expect(config.hasBeenAnnotated, isFalse);
+
+ config.accessibilityFocusBlockType = AccessibilityFocusBlockType.none;
+ expect(config.accessibilityFocusBlockType, AccessibilityFocusBlockType.none);
+ expect(config.hasBeenAnnotated, isFalse);
+
+ config.accessibilityFocusBlockType = AccessibilityFocusBlockType.blockSubtree;
+ expect(config.accessibilityFocusBlockType, AccessibilityFocusBlockType.blockSubtree);
+ expect(config.hasBeenAnnotated, isTrue);
+ },
+ );
+
test('SemanticsConfiguration.copy() preserves hitTestBehavior', () {
final config = SemanticsConfiguration()
..isSemanticBoundary = true