From ce4a45779df6ba4931443172684fa7b3738b5bb8 Mon Sep 17 00:00:00 2001 From: amirh Date: Mon, 23 Oct 2017 14:52:41 -0700 Subject: [PATCH] Add an isButton flag to the Semantic widget,use it for MaterialButton (#12657) https://github.com/flutter/flutter/issues/11992 --- packages/flutter/lib/src/material/button.dart | 2 +- .../flutter/lib/src/rendering/proxy_box.dart | 15 +++++++ .../flutter/lib/src/rendering/semantics.dart | 6 +++ packages/flutter/lib/src/widgets/basic.dart | 8 ++++ .../flutter/test/material/buttons_test.dart | 43 +++++++++++++++++-- packages/flutter/test/material/card_test.dart | 3 ++ .../test/rendering/semantics_test.dart | 7 +-- 7 files changed, 77 insertions(+), 7 deletions(-) diff --git a/packages/flutter/lib/src/material/button.dart b/packages/flutter/lib/src/material/button.dart index 5b3459e16f..465e22b3f2 100644 --- a/packages/flutter/lib/src/material/button.dart +++ b/packages/flutter/lib/src/material/button.dart @@ -330,7 +330,7 @@ class _MaterialButtonState extends State { child: new Center( widthFactor: 1.0, heightFactor: 1.0, - child: widget.child + child: new Semantics(button: true, child: widget.child), ) ) ) diff --git a/packages/flutter/lib/src/rendering/proxy_box.dart b/packages/flutter/lib/src/rendering/proxy_box.dart index 05d38312ed..3920927a77 100644 --- a/packages/flutter/lib/src/rendering/proxy_box.dart +++ b/packages/flutter/lib/src/rendering/proxy_box.dart @@ -3175,6 +3175,7 @@ class RenderSemanticsAnnotations extends RenderProxyBox { bool explicitChildNodes, bool checked, bool selected, + bool button, String label, TextDirection textDirection, }) : assert(container != null), @@ -3182,6 +3183,7 @@ class RenderSemanticsAnnotations extends RenderProxyBox { _explicitChildNodes = explicitChildNodes, _checked = checked, _selected = selected, + _button = button, _label = label, _textDirection = textDirection, super(child); @@ -3260,6 +3262,17 @@ class RenderSemanticsAnnotations extends RenderProxyBox { markNeedsSemanticsUpdate(onlyLocalUpdates: (value != null) == hadValue); } + /// If non-null, sets the [SemanticsNode.isButton] semantic to the given value. + bool get button => _button; + bool _button; + set button(bool value) { + if (button == value) + return; + final bool hadValue = button != null; + _button = value; + markNeedsSemanticsUpdate(onlyLocalUpdates: (value != null) == hadValue); + } + /// If non-null, sets the [SemanticsNode.textDirection] semantic to the given value. /// /// This must not be null if [label] is not null. @@ -3286,6 +3299,8 @@ class RenderSemanticsAnnotations extends RenderProxyBox { config.label = label; if (textDirection != null) config.textDirection = textDirection; + if (button != null) + config.isButton = button; } } diff --git a/packages/flutter/lib/src/rendering/semantics.dart b/packages/flutter/lib/src/rendering/semantics.dart index 1feb1ae485..73430f1b01 100644 --- a/packages/flutter/lib/src/rendering/semantics.dart +++ b/packages/flutter/lib/src/rendering/semantics.dart @@ -680,6 +680,7 @@ class SemanticsNode extends AbstractNode with DiagnosticableTreeMixin { properties.add(new FlagProperty('isChecked', value: _hasFlag(SemanticsFlags.isChecked), ifTrue: 'checked', ifFalse: 'unchecked')); properties.add(new FlagProperty('isSelected', value: _hasFlag(SemanticsFlags.isSelected), ifTrue: 'selected')); properties.add(new StringProperty('label', _label, defaultValue: '')); + properties.add(new FlagProperty('isButton', value: _hasFlag(SemanticsFlags.isButton), ifTrue: 'button')); properties.add(new EnumProperty('textDirection', _textDirection, defaultValue: null)); } @@ -1045,6 +1046,11 @@ class SemanticsConfiguration { _setFlag(SemanticsFlags.isChecked, value); } + /// Whether the owning [RenderObject] is a button (true) or not (false). + set isButton(bool value) { + _setFlag(SemanticsFlags.isButton, value); + } + // TAGS Iterable get tagsForChildren => _tagsForChildren; diff --git a/packages/flutter/lib/src/widgets/basic.dart b/packages/flutter/lib/src/widgets/basic.dart index fe6a99893f..9d91bb6e7b 100644 --- a/packages/flutter/lib/src/widgets/basic.dart +++ b/packages/flutter/lib/src/widgets/basic.dart @@ -4451,6 +4451,7 @@ class Semantics extends SingleChildRenderObjectWidget { this.explicitChildNodes: false, this.checked, this.selected, + this.button, this.label, this.textDirection, }) : assert(container != null), @@ -4491,6 +4492,12 @@ class Semantics extends SingleChildRenderObjectWidget { /// all other tabs are unselected. final bool selected; + /// If non-null, indicates that this subtree represents a button. + /// + /// TalkBack/VoiceOver provides users with the hint "button" when a button + /// is focused. + final bool button; + /// Provides a textual description of the widget. /// /// If a label is provided, there must either by an ambient [Directionality] @@ -4514,6 +4521,7 @@ class Semantics extends SingleChildRenderObjectWidget { checked: checked, selected: selected, label: label, + button: button, textDirection: _getTextDirection(context), ); } diff --git a/packages/flutter/test/material/buttons_test.dart b/packages/flutter/test/material/buttons_test.dart index 1f09eefa24..22f213a938 100644 --- a/packages/flutter/test/material/buttons_test.dart +++ b/packages/flutter/test/material/buttons_test.dart @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:ui' show SemanticsFlags; + import 'package:flutter/rendering.dart'; import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; @@ -31,14 +33,49 @@ void main() { new TestSemantics.root( children: [ new TestSemantics.rootChild( - id: 1, actions: SemanticsAction.tap.index, label: 'ABC', rect: new Rect.fromLTRB(0.0, 0.0, 88.0, 36.0), - transform: new Matrix4.translationValues(356.0, 282.0, 0.0) + transform: new Matrix4.translationValues(356.0, 282.0, 0.0), + flags: SemanticsFlags.isButton.index, + ) + ], + ), + ignoreId: true, + )); + + semantics.dispose(); + }); + + testWidgets('Does RaisedButton contribute semantics', (WidgetTester tester) async { + final SemanticsTester semantics = new SemanticsTester(tester); + await tester.pumpWidget( + new Directionality( + textDirection: TextDirection.ltr, + child: new Material( + child: new Center( + child: new RaisedButton( + onPressed: () { }, + child: const Text('ABC') + ), + ), + ), + ), + ); + + expect(semantics, hasSemantics( + new TestSemantics.root( + children: [ + new TestSemantics.rootChild( + actions: SemanticsAction.tap.index, + label: 'ABC', + rect: new Rect.fromLTRB(0.0, 0.0, 88.0, 36.0), + transform: new Matrix4.translationValues(356.0, 282.0, 0.0), + flags: SemanticsFlags.isButton.index, ) ] - ) + ), + ignoreId: true, )); semantics.dispose(); diff --git a/packages/flutter/test/material/card_test.dart b/packages/flutter/test/material/card_test.dart index f182286c5b..8118fddcc9 100644 --- a/packages/flutter/test/material/card_test.dart +++ b/packages/flutter/test/material/card_test.dart @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:ui' show SemanticsFlags; + import 'package:flutter/rendering.dart'; import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; @@ -47,6 +49,7 @@ void main() { label: 'Button', textDirection: TextDirection.ltr, actions: SemanticsAction.tap.index, + flags: SemanticsFlags.isButton.index, ), ], ), diff --git a/packages/flutter/test/rendering/semantics_test.dart b/packages/flutter/test/rendering/semantics_test.dart index 30aa85df44..d223416962 100644 --- a/packages/flutter/test/rendering/semantics_test.dart +++ b/packages/flutter/test/rendering/semantics_test.dart @@ -198,7 +198,7 @@ void main() { expect( minimalProperties.toStringDeep(minLevel: DiagnosticLevel.hidden), - 'SemanticsNode#16(owner: null, isPartOfNodeMerging: false, Rect.fromLTRB(0.0, 0.0, 0.0, 0.0), wasAffectedByClip: false, actions: [], isSelected: false, label: "", textDirection: null)\n', + 'SemanticsNode#16(owner: null, isPartOfNodeMerging: false, Rect.fromLTRB(0.0, 0.0, 0.0, 0.0), wasAffectedByClip: false, actions: [], isSelected: false, label: "", isButton: false, textDirection: null)\n', ); final SemanticsConfiguration config = new SemanticsConfiguration() @@ -208,6 +208,7 @@ void main() { ..addAction(SemanticsAction.showOnScreen, () { }) ..isChecked = false ..isSelected = true + ..isButton = true ..label = 'Use all the properties' ..textDirection = TextDirection.rtl; final SemanticsNode allProperties = new SemanticsNode() @@ -217,11 +218,11 @@ void main() { ..updateWith(config: config, childrenInInversePaintOrder: null); expect( allProperties.toStringDeep(), - 'SemanticsNode#17(STALE, owner: null, leaf merge, Rect.fromLTRB(60.0, 20.0, 80.0, 50.0), clipped, actions: [longPress, scrollUp, showOnScreen], unchecked, selected, label: "Use all the properties", textDirection: rtl)\n', + 'SemanticsNode#17(STALE, owner: null, leaf merge, Rect.fromLTRB(60.0, 20.0, 80.0, 50.0), clipped, actions: [longPress, scrollUp, showOnScreen], unchecked, selected, label: "Use all the properties", button, textDirection: rtl)\n', ); expect( allProperties.getSemanticsData().toString(), - 'SemanticsData(Rect.fromLTRB(50.0, 10.0, 70.0, 40.0), [1.0,0.0,0.0,10.0; 0.0,1.0,0.0,10.0; 0.0,0.0,1.0,0.0; 0.0,0.0,0.0,1.0], actions: [longPress, scrollUp, showOnScreen], flags: [hasCheckedState, isSelected], label: "Use all the properties", textDirection: rtl)', + 'SemanticsData(Rect.fromLTRB(50.0, 10.0, 70.0, 40.0), [1.0,0.0,0.0,10.0; 0.0,1.0,0.0,10.0; 0.0,0.0,1.0,0.0; 0.0,0.0,0.0,1.0], actions: [longPress, scrollUp, showOnScreen], flags: [hasCheckedState, isSelected, isButton], label: "Use all the properties", textDirection: rtl)', ); final SemanticsNode scaled = new SemanticsNode()