diff --git a/packages/flutter/lib/src/rendering/proxy_box.dart b/packages/flutter/lib/src/rendering/proxy_box.dart index 0fe6ad1623..3058ecd346 100644 --- a/packages/flutter/lib/src/rendering/proxy_box.dart +++ b/packages/flutter/lib/src/rendering/proxy_box.dart @@ -1895,7 +1895,7 @@ class RenderTransform extends RenderProxyBox { this.origin = origin; } - /// The origin of the coordinate system (relative to the upper left corder of + /// The origin of the coordinate system (relative to the upper left corner of /// this render object) in which to apply the matrix. /// /// Setting an origin is equivalent to conjugating the transform matrix by a @@ -1907,6 +1907,7 @@ class RenderTransform extends RenderProxyBox { return; _origin = value; markNeedsPaint(); + markNeedsSemanticsUpdate(); } /// The alignment of the origin, relative to the size of the box. @@ -1927,6 +1928,7 @@ class RenderTransform extends RenderProxyBox { return; _alignment = value; markNeedsPaint(); + markNeedsSemanticsUpdate(); } /// The text direction with which to resolve [alignment]. @@ -1940,6 +1942,7 @@ class RenderTransform extends RenderProxyBox { return; _textDirection = value; markNeedsPaint(); + markNeedsSemanticsUpdate(); } /// When set to true, hit tests are performed based on the position of the @@ -1960,42 +1963,49 @@ class RenderTransform extends RenderProxyBox { return; _transform = new Matrix4.copy(value); markNeedsPaint(); + markNeedsSemanticsUpdate(); } /// Sets the transform to the identity matrix. void setIdentity() { _transform.setIdentity(); markNeedsPaint(); + markNeedsSemanticsUpdate(); } /// Concatenates a rotation about the x axis into the transform. void rotateX(double radians) { _transform.rotateX(radians); markNeedsPaint(); + markNeedsSemanticsUpdate(); } /// Concatenates a rotation about the y axis into the transform. void rotateY(double radians) { _transform.rotateY(radians); markNeedsPaint(); + markNeedsSemanticsUpdate(); } /// Concatenates a rotation about the z axis into the transform. void rotateZ(double radians) { _transform.rotateZ(radians); markNeedsPaint(); + markNeedsSemanticsUpdate(); } /// Concatenates a translation by (x, y, z) into the transform. void translate(double x, [double y = 0.0, double z = 0.0]) { _transform.translate(x, y, z); markNeedsPaint(); + markNeedsSemanticsUpdate(); } /// Concatenates a scale into the transform. void scale(double x, [double y, double z]) { _transform.scale(x, y, z); markNeedsPaint(); + markNeedsSemanticsUpdate(); } Matrix4 get _effectiveTransform { diff --git a/packages/flutter/lib/src/semantics/semantics.dart b/packages/flutter/lib/src/semantics/semantics.dart index 9f2116f065..5852abec02 100644 --- a/packages/flutter/lib/src/semantics/semantics.dart +++ b/packages/flutter/lib/src/semantics/semantics.dart @@ -1431,18 +1431,74 @@ class SemanticsNode extends AbstractNode with DiagnosticableTreeMixin { } } -/// A helper class to contain a semantics node and the effective -/// sort order of that node during the traversal of the tree. This is -/// what is actually sorted when semantics nodes are sorted. +/// This class defines the comparison that is used to sort [SemanticsNode]s +/// before sending them to the platform side. +/// +/// This is a helper class used to contain a [node], the effective +/// [order], the globally transformed starting corner [globalStartCorner], +/// and the containing node's [containerTextDirection] during the traversal of +/// the semantics node tree. A null value is allowed for [containerTextDirection], +/// because in that case we want to fall back to ordering by child insertion +/// order for nodes that are equal after sorting from top to bottom. class _TraversalSortNode implements Comparable<_TraversalSortNode> { - _TraversalSortNode(this.node, this.order); + _TraversalSortNode(this.node, this.order, this.containerTextDirection, Matrix4 transform) + : assert(node != null) { + // When containerTextDirection is null, this is set to topLeft, but the x + // coordinate is also ignored when doing the comparison in that case, so + // this isn't actually expressing a directionality opinion. + globalStartCorner = _transformPoint( + containerTextDirection == TextDirection.rtl ? node.rect.topRight : node.rect.topLeft, + transform, + ); + } + + /// The node that this sort node represents. SemanticsNode node; + + /// The effective text direction for this node is the directionality that + /// its container has. + TextDirection containerTextDirection; + + /// This is the effective sort order for this node, taking into account its + /// parents. SemanticsSortOrder order; + /// The is the starting corner for the rectangle on this semantics node in + /// global coordinates. When the container has the directionality + /// [TextDirection.ltr], this is the upper left corner. When the container + /// has the directionality [TextDirection.rtl], this is the upper right + /// corner. When the container has no directionality, this is set, but the + /// x coordinate is ignored. + Offset globalStartCorner; + + static Offset _transformPoint(Offset point, Matrix4 matrix) { + final Vector3 result = matrix.transform3(new Vector3(point.dx, point.dy, 0.0)); + return new Offset(result.x, result.y); + } + + /// Compares the node's start corner with that of `other`. + /// + /// Sorts top to bottom, and then start to end. + /// + /// This takes into account the container text direction, since the + /// coordinate system has zero on the left, and we need to compare + /// differently for different text directions. + /// + /// If no text direction is available (i.e. [containerTextDirection] is + /// null), then we sort by vertical position first, and then by child + /// insertion order. int _compareGeometry(_TraversalSortNode other) { - // TODO(gspencer): Move the geometric comparison from the platform side to here. - // This involves calculating the globally-transformed quad for the semantics node rect - // and then sorting by its bounding box, based on the container's directionality. + final int verticalDiff = globalStartCorner.dy.compareTo(other.globalStartCorner.dy); + if (verticalDiff != 0) { + return verticalDiff; + } + switch (containerTextDirection) { + case TextDirection.rtl: + return other.globalStartCorner.dx.compareTo(globalStartCorner.dx); + case TextDirection.ltr: + return globalStartCorner.dx.compareTo(other.globalStartCorner.dx); + } + // In case containerTextDirection is null we fall back to child insertion order. return 0; } @@ -1490,17 +1546,34 @@ class SemanticsOwner extends ChangeNotifier { void _updateTraversalOrder() { final List<_TraversalSortNode> nodesInSemanticsTraversalOrder = <_TraversalSortNode>[]; SemanticsSortOrder currentSortOrder = new SemanticsSortOrder(keys: []); + Matrix4 currentTransform = new Matrix4.identity(); + TextDirection currentTextDirection = rootSemanticsNode.textDirection; bool visitor(SemanticsNode node) { final SemanticsSortOrder previousOrder = currentSortOrder; + final Matrix4 previousTransform = currentTransform.clone(); if (node.sortOrder != null) { currentSortOrder = currentSortOrder.merge(node.sortOrder); } - final _TraversalSortNode traversalNode = new _TraversalSortNode(node, currentSortOrder); + if (node.transform != null) { + currentTransform.multiply(node.transform); + } + final _TraversalSortNode traversalNode = new _TraversalSortNode( + node, + currentSortOrder, + currentTextDirection, + currentTransform, + ); + // The text direction in force here is the parent's text direction. nodesInSemanticsTraversalOrder.add(traversalNode); if (node.hasChildren) { + final TextDirection previousTextDirection = currentTextDirection; + currentTextDirection = node.textDirection; + // Now visit the children with this node's text direction in force. node.visitChildren(visitor); + currentTextDirection = previousTextDirection; } currentSortOrder = previousOrder; + currentTransform = previousTransform; return true; } rootSemanticsNode.visitChildren(visitor); @@ -2522,7 +2595,9 @@ String _concatStrings({ /// of keys can co-exist at the same level and not interfere with each other, /// allowing for sorting into groups. Keys that evaluate as equal, or when /// compared with Widgets that don't have [Semantics], fall back to the default -/// upper-start-to-lower-end geometric ordering. +/// upper-start-to-lower-end geometric ordering if a text directionality +/// exists, and they sort from top to bottom followed by child insertion order +/// when no directionality is present. /// /// Since widgets are globally sorted by their sort key, the order does not have /// to conform to the widget hierarchy. diff --git a/packages/flutter/test/widgets/semantics_test.dart b/packages/flutter/test/widgets/semantics_test.dart index c523186916..9c11b5a462 100644 --- a/packages/flutter/test/widgets/semantics_test.dart +++ b/packages/flutter/test/widgets/semantics_test.dart @@ -2,6 +2,9 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:math'; +import 'dart:ui'; + import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/semantics.dart'; @@ -119,21 +122,21 @@ void main() { expect(semantics, hasSemantics( new TestSemantics.root( - children: [ - new TestSemantics.rootChild( - label: 'test1', + children: [ + new TestSemantics.rootChild( + label: 'test1', + children: [ + new TestSemantics( + label: 'middle', children: [ new TestSemantics( - label: 'middle', - children: [ - new TestSemantics( - label: 'test2b', - ), - ], - ) - ] - ) - ] + label: 'test2b', + ), + ], + ) + ] + ) + ] ), ignoreId: true, ignoreRect: true, @@ -205,12 +208,12 @@ void main() { final SemanticsTester semantics = new SemanticsTester(tester); final TestSemantics expectedSemantics = new TestSemantics.root( - children: [ - new TestSemantics.rootChild( - label: 'test1', - textDirection: TextDirection.rtl, - ) - ] + children: [ + new TestSemantics.rootChild( + label: 'test1', + textDirection: TextDirection.rtl, + ) + ] ); await tester.pumpWidget( @@ -243,14 +246,14 @@ void main() { ); final TestSemantics expectedSemantics = new TestSemantics.root( - children: [ - new TestSemantics.rootChild( - label: 'label', - hint: 'hint', - value: 'value', - textDirection: TextDirection.ltr, - ) - ] + children: [ + new TestSemantics.rootChild( + label: 'label', + hint: 'hint', + value: 'value', + textDirection: TextDirection.ltr, + ) + ] ); expect(semantics, hasSemantics(expectedSemantics, ignoreTransform: true, ignoreRect: true, ignoreId: true)); @@ -280,12 +283,12 @@ void main() { ); final TestSemantics expectedSemantics = new TestSemantics.root( - children: [ - new TestSemantics.rootChild( - hint: 'hint one\nhint two', - textDirection: TextDirection.ltr, - ) - ] + children: [ + new TestSemantics.rootChild( + hint: 'hint one\nhint two', + textDirection: TextDirection.ltr, + ) + ] ); expect(semantics, hasSemantics(expectedSemantics, ignoreTransform: true, ignoreRect: true, ignoreId: true)); @@ -364,13 +367,13 @@ void main() { ); final TestSemantics expectedSemantics = new TestSemantics.root( - children: [ - new TestSemantics.rootChild( - hint: 'hint', - value: 'value', - textDirection: TextDirection.ltr, - ) - ] + children: [ + new TestSemantics.rootChild( + hint: 'hint', + value: 'value', + textDirection: TextDirection.ltr, + ) + ] ); expect(semantics, hasSemantics(expectedSemantics, ignoreTransform: true, ignoreRect: true, ignoreId: true)); @@ -647,8 +650,8 @@ void main() { ], ), ], - ), ignoreTransform: true, ignoreRect: true, ignoreId: true)); - + ), ignoreTransform: true, ignoreRect: true, ignoreId: true), + ); semantics.dispose(); }); @@ -829,7 +832,239 @@ void main() { ], ), ], - ), ignoreTransform: true, ignoreRect: true, ignoreId: true)); + ), ignoreTransform: true, ignoreRect: true, ignoreId: true), + ); + semantics.dispose(); + }); + + testWidgets('Semantics widgets without sort orders are sorted properly', (WidgetTester tester) async { + final SemanticsTester semantics = new SemanticsTester(tester); + int semanticsUpdateCount = 0; + tester.binding.pipelineOwner.ensureSemantics( + listener: () { + semanticsUpdateCount += 1; + } + ); + await tester.pumpWidget( + new Directionality( + textDirection: TextDirection.ltr, + child: new Column( + children: [ + const Text('Label 1'), + const Text('Label 2'), + new Row( + children: [ + const Text('Label 3'), + const Text('Label 4'), + const Text('Label 5'), + ], + ), + ], + ), + ), + ); + expect(semanticsUpdateCount, 1); + expect(semantics, hasSemantics( + new TestSemantics( + children: [ + new TestSemantics( + label: r'Label 1', + textDirection: TextDirection.ltr, + previousNodeId: -1, + ), + new TestSemantics( + label: r'Label 2', + textDirection: TextDirection.ltr, + previousNodeId: 2, + ), + new TestSemantics( + label: r'Label 3', + textDirection: TextDirection.ltr, + previousNodeId: 3, + ), + new TestSemantics( + label: r'Label 4', + textDirection: TextDirection.ltr, + previousNodeId: 4, + ), + new TestSemantics( + label: r'Label 5', + textDirection: TextDirection.ltr, + previousNodeId: 5, + ), + ], + ), ignoreTransform: true, ignoreRect: true, ignoreId: true), + ); + semantics.dispose(); + }); + + testWidgets('Semantics widgets that are transformed are sorted properly', (WidgetTester tester) async { + final SemanticsTester semantics = new SemanticsTester(tester); + int semanticsUpdateCount = 0; + tester.binding.pipelineOwner.ensureSemantics( + listener: () { + semanticsUpdateCount += 1; + } + ); + await tester.pumpWidget( + new Directionality( + textDirection: TextDirection.ltr, + child: new Column( + children: [ + const Text('Label 1'), + const Text('Label 2'), + new Transform.rotate( + angle: pi / 2.0, + child: new Row( + children: [ + const Text('Label 3'), + const Text('Label 4'), + const Text('Label 5'), + ], + ), + ), + ], + ), + ), + ); + expect(semanticsUpdateCount, 1); + expect(semantics, hasSemantics( + new TestSemantics( + children: [ + new TestSemantics( + label: r'Label 1', + textDirection: TextDirection.ltr, + previousNodeId: 6, + ), + new TestSemantics( + label: r'Label 2', + textDirection: TextDirection.ltr, + previousNodeId: 2, + ), + new TestSemantics( + label: r'Label 3', + textDirection: TextDirection.ltr, + previousNodeId: -1, + ), + new TestSemantics( + label: r'Label 4', + textDirection: TextDirection.ltr, + previousNodeId: 4, + ), + new TestSemantics( + label: r'Label 5', + textDirection: TextDirection.ltr, + previousNodeId: 5, + ), + ], + ), ignoreTransform: true, ignoreRect: true, ignoreId: true), + ); + semantics.dispose(); + }); + + testWidgets( + 'Semantics widgets without sort orders are sorted properly when no Directionality is present', + (WidgetTester tester) async { + final SemanticsTester semantics = new SemanticsTester(tester); + int semanticsUpdateCount = 0; + tester.binding.pipelineOwner.ensureSemantics(listener: () { + semanticsUpdateCount += 1; + }); + await tester.pumpWidget( + new Stack( + alignment: Alignment.center, + children: [ + // Set this up so that the placeholder takes up the whole screen, + // and place the positioned boxes so that if we traverse in the + // geometric order, we would go from box [4, 3, 2, 1, 0], but if we + // go in child order, then we go from box [4, 1, 2, 3, 0]. We're verifying + // that we go in child order here, not geometric order, since there + // is no directionality, so we don't have a geometric opinion about + // horizontal order. We do still want to sort vertically, however, + // which is why the order isn't [0, 1, 2, 3, 4]. + new Semantics( + button: true, + child: const Placeholder(), + ), + new Positioned( + top: 200.0, + left: 100.0, + child: new Semantics( // Box 0 + button: true, + child: const SizedBox(width: 30.0, height: 30.0), + ), + ), + new Positioned( + top: 100.0, + left: 200.0, + child: new Semantics( // Box 1 + button: true, + child: const SizedBox(width: 30.0, height: 30.0), + ), + ), + new Positioned( + top: 100.0, + left: 100.0, + child: new Semantics( // Box 2 + button: true, + child: const SizedBox(width: 30.0, height: 30.0), + ), + ), + new Positioned( + top: 100.0, + left: 0.0, + child: new Semantics( // Box 3 + button: true, + child: const SizedBox(width: 30.0, height: 30.0), + ), + ), + new Positioned( + top: 10.0, + left: 100.0, + child: new Semantics( // Box 4 + button: true, + child: const SizedBox(width: 30.0, height: 30.0), + ), + ), + ], + ), + ); + expect(semanticsUpdateCount, 1); + expect( + semantics, + hasSemantics( + new TestSemantics( + children: [ + new TestSemantics( + flags: [SemanticsFlag.isButton], + previousNodeId: -1, + ), + new TestSemantics( + flags: [SemanticsFlag.isButton], + previousNodeId: 6, + ), + new TestSemantics( + flags: [SemanticsFlag.isButton], + previousNodeId: 7, + ), + new TestSemantics( + flags: [SemanticsFlag.isButton], + previousNodeId: 4, + ), + new TestSemantics( + flags: [SemanticsFlag.isButton], + previousNodeId: 5, + ), + new TestSemantics( + flags: [SemanticsFlag.isButton], + previousNodeId: 2, + ), + ], + ), + ignoreTransform: true, + ignoreRect: true, + ignoreId: true), + ); semantics.dispose(); }); }