[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
This commit is contained in:
Yegor
2024-03-19 16:10:32 -07:00
committed by GitHub
parent e7b49194d2
commit 15eb786641
2 changed files with 139 additions and 28 deletions

View File

@@ -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 = <String?>[label, hint, value]
.whereType<String>() // poor man's null filter
.where((String element) => element.trim().isNotEmpty)
.join(' ');
return combinedValue.isNotEmpty ? combinedValue : null;
}

View File

@@ -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 {