From 7d525ea3183fe18febb99b03486665b39d1714ad Mon Sep 17 00:00:00 2001 From: victorgalo Date: Thu, 6 Jun 2024 23:24:26 +0200 Subject: [PATCH] Add support for setting the heading level for web semantics (#97894) (#125771) This change adds a new property in Semantics widget that would take an integer corresponding to the heading levels defined by the ARIA heading role. This is necessary in order to get proper accessibility and usability in a website for users who rely on screen readers and other assistive technologies. Issue fixed by this PR: fixes https://github.com/flutter/flutter/issues/97894 Engine part: https://github.com/flutter/engine/pull/41435 --- .../lib/src/rendering/custom_paint.dart | 3 + .../flutter/lib/src/rendering/proxy_box.dart | 3 + .../flutter/lib/src/semantics/semantics.dart | 64 +++++++++++++++++-- packages/flutter/lib/src/widgets/basic.dart | 2 + packages/flutter_test/test/matchers_test.dart | 6 ++ 5 files changed, 73 insertions(+), 5 deletions(-) diff --git a/packages/flutter/lib/src/rendering/custom_paint.dart b/packages/flutter/lib/src/rendering/custom_paint.dart index 852d01bf12..036de13358 100644 --- a/packages/flutter/lib/src/rendering/custom_paint.dart +++ b/packages/flutter/lib/src/rendering/custom_paint.dart @@ -940,6 +940,9 @@ class RenderCustomPaint extends RenderProxyBox { if (properties.header != null) { config.isHeader = properties.header!; } + if (properties.headingLevel != null) { + config.headingLevel = properties.headingLevel!; + } if (properties.scopesRoute != null) { config.scopesRoute = properties.scopesRoute!; } diff --git a/packages/flutter/lib/src/rendering/proxy_box.dart b/packages/flutter/lib/src/rendering/proxy_box.dart index 6782a433cc..49670a3aa6 100644 --- a/packages/flutter/lib/src/rendering/proxy_box.dart +++ b/packages/flutter/lib/src/rendering/proxy_box.dart @@ -4329,6 +4329,9 @@ class RenderSemanticsAnnotations extends RenderProxyBox { if (_properties.header != null) { config.isHeader = _properties.header!; } + if (_properties.headingLevel != null) { + config.headingLevel = _properties.headingLevel!; + } if (_properties.textField != null) { config.isTextField = _properties.textField!; } diff --git a/packages/flutter/lib/src/semantics/semantics.dart b/packages/flutter/lib/src/semantics/semantics.dart index 5f622a33f6..3008e513c8 100644 --- a/packages/flutter/lib/src/semantics/semantics.dart +++ b/packages/flutter/lib/src/semantics/semantics.dart @@ -446,6 +446,7 @@ class SemanticsData with Diagnosticable { required this.platformViewId, required this.maxValueLength, required this.currentValueLength, + required this.headingLevel, this.tags, this.transform, this.customSemanticsActionIds, @@ -454,7 +455,8 @@ class SemanticsData with Diagnosticable { assert(attributedValue.string == '' || textDirection != null, 'A SemanticsData object with value "${attributedValue.string}" had a null textDirection.'), assert(attributedDecreasedValue.string == '' || textDirection != null, 'A SemanticsData object with decreasedValue "${attributedDecreasedValue.string}" had a null textDirection.'), assert(attributedIncreasedValue.string == '' || textDirection != null, 'A SemanticsData object with increasedValue "${attributedIncreasedValue.string}" had a null textDirection.'), - assert(attributedHint.string == '' || textDirection != null, 'A SemanticsData object with hint "${attributedHint.string}" had a null textDirection.'); + assert(attributedHint.string == '' || textDirection != null, 'A SemanticsData object with hint "${attributedHint.string}" had a null textDirection.'), + assert(headingLevel >= 0 && headingLevel <= 6, 'Heading level must be between 0 and 6'); /// A bit field of [SemanticsFlag]s that apply to this node. final int flags; @@ -547,6 +549,12 @@ class SemanticsData with Diagnosticable { /// The reading direction is given by [textDirection]. final String tooltip; + /// Indicates that this subtree represents a heading. + /// + /// A value of 0 indicates that it is not a heading. The value should be a + /// number between 1 and 6, indicating the hierarchical level as a heading. + final int headingLevel; + /// The reading direction for the text in [label], [value], /// [increasedValue], [decreasedValue], and [hint]. final TextDirection? textDirection; @@ -719,6 +727,7 @@ class SemanticsData with Diagnosticable { properties.add(DoubleProperty('scrollExtentMin', scrollExtentMin, defaultValue: null)); properties.add(DoubleProperty('scrollPosition', scrollPosition, defaultValue: null)); properties.add(DoubleProperty('scrollExtentMax', scrollExtentMax, defaultValue: null)); + properties.add(IntProperty('headingLevel', headingLevel, defaultValue: 0)); } @override @@ -748,6 +757,7 @@ class SemanticsData with Diagnosticable { && other.transform == transform && other.elevation == elevation && other.thickness == thickness + && other.headingLevel == headingLevel && _sortedListsEqual(other.customSemanticsActionIds, customSemanticsActionIds); } @@ -778,6 +788,7 @@ class SemanticsData with Diagnosticable { transform, elevation, thickness, + headingLevel, customSemanticsActionIds == null ? null : Object.hashAll(customSemanticsActionIds!), ), ); @@ -892,6 +903,7 @@ class SemanticsProperties extends DiagnosticableTree { this.button, this.link, this.header, + this.headingLevel, this.textField, this.slider, this.keyboardKey, @@ -950,7 +962,8 @@ class SemanticsProperties extends DiagnosticableTree { assert(value == null || attributedValue == null, 'Only one of value or attributedValue should be provided'), assert(increasedValue == null || attributedIncreasedValue == null, 'Only one of increasedValue or attributedIncreasedValue should be provided'), assert(decreasedValue == null || attributedDecreasedValue == null, 'Only one of decreasedValue or attributedDecreasedValue should be provided'), - assert(hint == null || attributedHint == null, 'Only one of hint or attributedHint should be provided'); + assert(hint == null || attributedHint == null, 'Only one of hint or attributedHint should be provided'), + assert(headingLevel == null || (headingLevel > 0 && headingLevel <= 6), 'Heading level must be between 1 and 6'); /// If non-null, indicates that this subtree represents something that can be /// in an enabled or disabled state. @@ -1362,6 +1375,17 @@ class SemanticsProperties extends DiagnosticableTree { /// [Directionality] or an explicit [textDirection] should be provided. final String? tooltip; + /// The heading level in the DOM document structure. + /// + /// This is only applied to web semantics and is ignored on other platforms. + /// + /// Screen readers will use this value to determine which part of the page + /// structure this heading represents. A level 1 heading, indicated + /// with aria-level="1", usually indicates the main heading of a page, + /// a level 2 heading, defined with aria-level="2" the first subsection, + /// a level 3 is a subsection of that, and so on. + final int? headingLevel; + /// Provides hint values which override the default hints on supported /// platforms. /// @@ -2236,7 +2260,8 @@ class SemanticsNode with DiagnosticableTreeMixin { || _maxValueLength != config._maxValueLength || _currentValueLength != config._currentValueLength || _mergeAllDescendantsIntoThisNode != config.isMergingSemanticsOfDescendants - || _areUserActionsBlocked != config.isBlockingUserActions; + || _areUserActionsBlocked != config.isBlockingUserActions + || _headingLevel != config._headingLevel; } // TAGS, LABELS, ACTIONS @@ -2540,7 +2565,14 @@ class SemanticsNode with DiagnosticableTreeMixin { int? get currentValueLength => _currentValueLength; int? _currentValueLength; - bool _canPerformAction(SemanticsAction action) => _actions.containsKey(action); + /// The level of the widget as a heading within the structural hierarchy + /// of the screen. A value of 1 indicates the highest level of structural + /// hierarchy. A value of 2 indicates the next level, and so on. + int get headingLevel => _headingLevel; + int _headingLevel = _kEmptyConfig._headingLevel; + + bool _canPerformAction(SemanticsAction action) => + _actions.containsKey(action); static final SemanticsConfiguration _kEmptyConfig = SemanticsConfiguration(); @@ -2598,6 +2630,7 @@ class SemanticsNode with DiagnosticableTreeMixin { _maxValueLength = config._maxValueLength; _currentValueLength = config._currentValueLength; _areUserActionsBlocked = config.isBlockingUserActions; + _headingLevel = config._headingLevel; _replaceChildren(childrenInInversePaintOrder ?? const []); if (mergeAllDescendantsIntoThisNodeValueChanged) { @@ -2643,6 +2676,7 @@ class SemanticsNode with DiagnosticableTreeMixin { int? platformViewId = _platformViewId; int? maxValueLength = _maxValueLength; int? currentValueLength = _currentValueLength; + int headingLevel = _headingLevel; final double elevation = _elevation; double thickness = _thickness; final Set customSemanticsActionIds = {}; @@ -2682,6 +2716,8 @@ class SemanticsNode with DiagnosticableTreeMixin { platformViewId ??= node._platformViewId; maxValueLength ??= node._maxValueLength; currentValueLength ??= node._currentValueLength; + headingLevel = node._headingLevel; + if (identifier == '') { identifier = node._identifier; } @@ -2765,6 +2801,7 @@ class SemanticsNode with DiagnosticableTreeMixin { maxValueLength: maxValueLength, currentValueLength: currentValueLength, customSemanticsActionIds: customSemanticsActionIds.toList()..sort(), + headingLevel: headingLevel, ); } @@ -2840,6 +2877,7 @@ class SemanticsNode with DiagnosticableTreeMixin { childrenInTraversalOrder: childrenInTraversalOrder, childrenInHitTestOrder: childrenInHitTestOrder, additionalActions: customSemanticsActionIds ?? _kEmptyCustomSemanticsActionsList, + headingLevel: data.headingLevel, ); _dirty = false; } @@ -4711,6 +4749,21 @@ class SemanticsConfiguration { _setFlag(SemanticsFlag.isHeader, value); } + /// Indicates the heading level in the document structure. + /// + /// This is only used for web semantics, and is ignored on other platforms. + int get headingLevel => _headingLevel; + int _headingLevel = 0; + + set headingLevel(int value) { + assert(value >= 0 && value <= 6); + if (value == headingLevel) { + return; + } + _headingLevel = value; + _hasBeenAnnotated = true; + } + /// Whether the owning [RenderObject] is a slider (true) or not (false). bool get isSlider => _hasFlag(SemanticsFlag.isSlider); set isSlider(bool value) { @@ -5044,7 +5097,8 @@ class SemanticsConfiguration { .._currentValueLength = _currentValueLength .._actions.addAll(_actions) .._customSemanticsActions.addAll(_customSemanticsActions) - ..isBlockingUserActions = isBlockingUserActions; + ..isBlockingUserActions = isBlockingUserActions + .._headingLevel = _headingLevel; } } diff --git a/packages/flutter/lib/src/widgets/basic.dart b/packages/flutter/lib/src/widgets/basic.dart index 1fce06ad28..de471615f0 100644 --- a/packages/flutter/lib/src/widgets/basic.dart +++ b/packages/flutter/lib/src/widgets/basic.dart @@ -7101,6 +7101,7 @@ class Semantics extends SingleChildRenderObjectWidget { bool? keyboardKey, bool? link, bool? header, + int? headingLevel, bool? textField, bool? readOnly, bool? focusable, @@ -7172,6 +7173,7 @@ class Semantics extends SingleChildRenderObjectWidget { keyboardKey: keyboardKey, link: link, header: header, + headingLevel: headingLevel, textField: textField, readOnly: readOnly, focusable: focusable, diff --git a/packages/flutter_test/test/matchers_test.dart b/packages/flutter_test/test/matchers_test.dart index dd3f08f707..1825755da8 100644 --- a/packages/flutter_test/test/matchers_test.dart +++ b/packages/flutter_test/test/matchers_test.dart @@ -684,6 +684,7 @@ void main() { customSemanticsActionIds: [CustomSemanticsAction.getIdentifier(action)], currentValueLength: 10, maxValueLength: 15, + headingLevel: 0, ); final _FakeSemanticsNode node = _FakeSemanticsNode(data); @@ -970,6 +971,7 @@ void main() { customSemanticsActionIds: [CustomSemanticsAction.getIdentifier(action)], currentValueLength: 10, maxValueLength: 15, + headingLevel: 0, ); final _FakeSemanticsNode node = _FakeSemanticsNode(data); @@ -1062,6 +1064,7 @@ void main() { platformViewId: 105, currentValueLength: 10, maxValueLength: 15, + headingLevel: 0, ); final _FakeSemanticsNode node = _FakeSemanticsNode(data); @@ -1161,6 +1164,7 @@ void main() { platformViewId: 105, currentValueLength: 10, maxValueLength: 15, + headingLevel: 0, ); final _FakeSemanticsNode emptyNode = _FakeSemanticsNode(emptyData); @@ -1189,6 +1193,7 @@ void main() { currentValueLength: 10, maxValueLength: 15, customSemanticsActionIds: [CustomSemanticsAction.getIdentifier(action)], + headingLevel: 0, ); final _FakeSemanticsNode fullNode = _FakeSemanticsNode(fullData); @@ -1279,6 +1284,7 @@ void main() { currentValueLength: 10, maxValueLength: 15, customSemanticsActionIds: [CustomSemanticsAction.getIdentifier(action)], + headingLevel: 0, ); final _FakeSemanticsNode node = _FakeSemanticsNode(data);