From 15eb7866415214faa967e04cdd513a7d073bea5f Mon Sep 17 00:00:00 2001 From: Yegor Date: Tue, 19 Mar 2024 16:10:32 -0700 Subject: [PATCH] [web] include the hint field in semantics labels (flutter/engine#51529) Add hint to the semantic label. A label containing all field will now look like this: ``` tooltip label hint value ``` Fixes https://github.com/flutter/flutter/issues/143852 --- .../src/engine/semantics/label_and_value.dart | 80 +++++++++++------ .../test/engine/semantics/semantics_test.dart | 87 +++++++++++++++++++ 2 files changed, 139 insertions(+), 28 deletions(-) diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/label_and_value.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/label_and_value.dart index 375da2787f..1d8045bf70 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/label_and_value.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/label_and_value.dart @@ -70,38 +70,21 @@ class LabelAndValue extends RoleManager { } } + /// Computes the final label to be assigned to the node. + /// + /// The label is a concatenation of tooltip, label, hint, and value, whichever + /// combination is present. String? _computeLabel() { - final bool hasValue = semanticsObject.hasValue; - final bool hasLabel = semanticsObject.hasLabel; - final bool hasTooltip = semanticsObject.hasTooltip; - // If the node is incrementable the value is reported to the browser via // the respective role manager. We do not need to also render it again here. - final bool shouldDisplayValue = hasValue && !semanticsObject.isIncrementable; + final bool shouldDisplayValue = !semanticsObject.isIncrementable && semanticsObject.hasValue; - if (!hasLabel && !shouldDisplayValue && !hasTooltip) { - return null; - } - - final StringBuffer combinedValue = StringBuffer(); - if (hasTooltip) { - combinedValue.write(semanticsObject.tooltip); - if (hasLabel || shouldDisplayValue) { - combinedValue.write('\n'); - } - } - if (hasLabel) { - combinedValue.write(semanticsObject.label); - if (shouldDisplayValue) { - combinedValue.write(' '); - } - } - - if (shouldDisplayValue) { - combinedValue.write(semanticsObject.value); - } - - return combinedValue.toString(); + return computeDomSemanticsLabel( + tooltip: semanticsObject.hasTooltip ? semanticsObject.tooltip : null, + label: semanticsObject.hasLabel ? semanticsObject.label : null, + hint: semanticsObject.hint, + value: shouldDisplayValue ? semanticsObject.value : null, + ); } void _cleanUpDom() { @@ -115,3 +98,44 @@ class LabelAndValue extends RoleManager { _cleanUpDom(); } } + +String? computeDomSemanticsLabel({ + String? tooltip, + String? label, + String? hint, + String? value, +}) { + final String? labelHintValue = _computeLabelHintValue(label: label, hint: hint, value: value); + + if (tooltip == null && labelHintValue == null) { + return null; + } + + final StringBuffer combinedValue = StringBuffer(); + if (tooltip != null) { + combinedValue.write(tooltip); + + // Separate the tooltip from the rest via a line-break (if the rest exists). + if (labelHintValue != null) { + combinedValue.writeln(); + } + } + + if (labelHintValue != null) { + combinedValue.write(labelHintValue); + } + + return combinedValue.isNotEmpty ? combinedValue.toString() : null; +} + +String? _computeLabelHintValue({ + String? label, + String? hint, + String? value, +}) { + final String combinedValue = [label, hint, value] + .whereType() // poor man's null filter + .where((String element) => element.trim().isNotEmpty) + .join(' '); + return combinedValue.isNotEmpty ? combinedValue : null; +} diff --git a/engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_test.dart b/engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_test.dart index 34dd6c79b8..e44a85c462 100644 --- a/engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_test.dart +++ b/engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_test.dart @@ -54,6 +54,9 @@ void runSemanticsTests() { group('Text', () { _testText(); }); + group('labels', () { + _testLabels(); + }); group('container', () { _testContainer(); }); @@ -789,6 +792,90 @@ void _testText() { }); } +void _testLabels() { + test('computeDomSemanticsLabel combines tooltip, label, value, and hint', () { + expect( + computeDomSemanticsLabel(tooltip: 'tooltip'), + 'tooltip', + ); + expect( + computeDomSemanticsLabel(label: 'label'), + 'label', + ); + expect( + computeDomSemanticsLabel(value: 'value'), + 'value', + ); + expect( + computeDomSemanticsLabel(hint: 'hint'), + 'hint', + ); + expect( + computeDomSemanticsLabel(tooltip: 'tooltip', label: 'label', hint: 'hint', value: 'value'), + ''' +tooltip +label hint value''' + ); + expect( + computeDomSemanticsLabel(tooltip: 'tooltip', hint: 'hint', value: 'value'), + ''' +tooltip +hint value''' + ); + expect( + computeDomSemanticsLabel(tooltip: 'tooltip', label: 'label', value: 'value'), + ''' +tooltip +label value''' + ); + expect( + computeDomSemanticsLabel(tooltip: 'tooltip', label: 'label', hint: 'hint'), + ''' +tooltip +label hint''' + ); + }); + + test('computeDomSemanticsLabel collapses empty labels to null', () { + expect( + computeDomSemanticsLabel(), + isNull, + ); + expect( + computeDomSemanticsLabel(tooltip: ''), + isNull, + ); + expect( + computeDomSemanticsLabel(label: ''), + isNull, + ); + expect( + computeDomSemanticsLabel(value: ''), + isNull, + ); + expect( + computeDomSemanticsLabel(hint: ''), + isNull, + ); + expect( + computeDomSemanticsLabel(tooltip: '', label: '', hint: '', value: ''), + isNull, + ); + expect( + computeDomSemanticsLabel(tooltip: '', hint: '', value: ''), + isNull, + ); + expect( + computeDomSemanticsLabel(tooltip: '', label: '', value: ''), + isNull, + ); + expect( + computeDomSemanticsLabel(tooltip: '', label: '', hint: ''), + isNull, + ); + }); +} + void _testContainer() { test('container node has no transform when there is no rect offset', () async {