Skip to content

Commit b0da3c2

Browse files
authored
[a11y] Add a semantics role for slider (flutter#184029)
fix: flutter#184030 ## Pre-launch Checklist - [ ] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [ ] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [ ] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [ ] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [ ] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [ ] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 6f0e0db commit b0da3c2

12 files changed

Lines changed: 356 additions & 4 deletions

File tree

‎engine/src/flutter/lib/ui/semantics.dart‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,12 @@ enum SemanticsRole {
553553
/// For more information, see:
554554
/// https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Roles/region_role
555555
region,
556+
557+
/// The slider role is for range input widgets where the user selects a value from within given minimum and maximum values.
558+
///
559+
/// For more information, see:
560+
/// https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Roles/slider_role
561+
slider,
556562
}
557563

558564
/// Describe the type of data for an input field.

‎engine/src/flutter/lib/ui/semantics/semantics_node.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ enum class SemanticsRole : int32_t {
105105
kMain = 30,
106106
kNavigation = 31,
107107
kRegion = 32,
108+
kSlider = 33,
108109
};
109110

110111
/// C/C++ representation of `SemanticsValidationResult` defined in

‎engine/src/flutter/lib/web_ui/lib/semantics.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,7 @@ enum SemanticsRole {
652652
main,
653653
navigation,
654654
region,
655+
slider,
655656
}
656657

657658
// Mirrors engine/src/flutter/lib/ui/semantics.dart

‎engine/src/flutter/lib/web_ui/lib/src/engine/semantics/incrementable.dart‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ import 'semantics.dart';
2020
/// events. This is to prevent the browser from taking over drag gestures. Drag
2121
/// gestures must be interpreted by the Flutter framework.
2222
class SemanticIncrementable extends SemanticRole {
23-
SemanticIncrementable(SemanticsObject semanticsObject)
24-
: _focusManager = AccessibilityFocusManager(semanticsObject.owner),
25-
super.blank(EngineSemanticsRole.incrementable, semanticsObject) {
23+
SemanticIncrementable(
24+
SemanticsObject semanticsObject, [
25+
EngineSemanticsRole kind = EngineSemanticsRole.incrementable,
26+
]) : _focusManager = AccessibilityFocusManager(semanticsObject.owner),
27+
super.blank(kind, semanticsObject) {
2628
// The following generic roles can coexist with incrementables. Generic focus
2729
// management is not used by this role because the root DOM element is not
2830
// the one being focused on, but the internal `<input>` element.

‎engine/src/flutter/lib/web_ui/lib/src/engine/semantics/semantics.dart‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,9 @@ enum EngineSemanticsRole {
625625

626626
/// An area that represents a form.
627627
form,
628+
629+
/// A graphic object that can be incremented or decremented as a slider.
630+
slider,
628631
}
629632

630633
/// Responsible for setting the `role` ARIA attribute, for attaching
@@ -2264,6 +2267,8 @@ class SemanticsObject {
22642267
return EngineSemanticsRole.loadingSpinner;
22652268
case ui.SemanticsRole.progressBar:
22662269
return EngineSemanticsRole.progressBar;
2270+
case ui.SemanticsRole.slider:
2271+
return EngineSemanticsRole.slider;
22672272
// TODO(chunhtai): implement these roles.
22682273
// https://github.com/flutter/flutter/issues/159741.
22692274
case ui.SemanticsRole.dragHandle:
@@ -2308,6 +2313,7 @@ class SemanticsObject {
23082313
return switch (role) {
23092314
EngineSemanticsRole.textField => SemanticTextField(this),
23102315
EngineSemanticsRole.scrollable => SemanticScrollable(this),
2316+
EngineSemanticsRole.slider => SemanticIncrementable(this, EngineSemanticsRole.slider),
23112317
EngineSemanticsRole.incrementable => SemanticIncrementable(this),
23122318
EngineSemanticsRole.button => SemanticButton(this),
23132319
EngineSemanticsRole.radioGroup => SemanticRadioGroup(this),

‎engine/src/flutter/shell/platform/android/BUILD.gn‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ android_java_sources = [
392392
"io/flutter/view/MenuRoleConfigurator.java",
393393
"io/flutter/view/ProgressBarRoleConfigurator.java",
394394
"io/flutter/view/RoleConfiguratorFactory.java",
395+
"io/flutter/view/SliderRoleConfigurator.java",
395396
"io/flutter/view/TextureRegistry.java",
396397
"io/flutter/view/VsyncWaiter.java",
397398
]

‎engine/src/flutter/shell/platform/android/io/flutter/view/AccessibilityBridge.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2070,7 +2070,8 @@ enum Role {
20702070
CONTENT_INFO(29),
20712071
MAIN(30),
20722072
NAVIGATION(31),
2073-
REGION(32);
2073+
REGION(32),
2074+
SLIDER(33);
20742075

20752076
final int value;
20762077

‎engine/src/flutter/shell/platform/android/io/flutter/view/RoleConfiguratorFactory.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
public class RoleConfiguratorFactory {
1212
private static final AccessibilityNodeConfigurator progressBarConfigurator =
1313
new ProgressBarRoleConfigurator();
14+
private static final AccessibilityNodeConfigurator sliderConfigurator =
15+
new SliderRoleConfigurator();
1416
private static final AccessibilityNodeConfigurator comboBoxConfigurator =
1517
new ComboBoxRoleConfigurator();
1618
private static final AccessibilityNodeConfigurator menuConfigurator = new MenuRoleConfigurator();
@@ -27,6 +29,8 @@ public static AccessibilityNodeConfigurator getConfigurator(AccessibilityBridge.
2729
switch (role) {
2830
case PROGRESS_BAR:
2931
return progressBarConfigurator;
32+
case SLIDER:
33+
return sliderConfigurator;
3034
case COMBO_BOX:
3135
return comboBoxConfigurator;
3236
case MENU:
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
package io.flutter.view;
6+
7+
import android.os.Build;
8+
import android.view.accessibility.AccessibilityNodeInfo;
9+
import io.flutter.Build.API_LEVELS;
10+
11+
/**
12+
* Configurator for the {@link AccessibilityBridge.Role#SLIDER} role. Sets the class name to
13+
* SeekBar, adds progress action, and handles range info.
14+
*/
15+
public class SliderRoleConfigurator extends BaseRoleConfigurator {
16+
@Override
17+
protected void configureRole(
18+
AccessibilityNodeInfo result, AccessibilityBridge.SemanticsNode node) {
19+
result.setClassName("android.widget.SeekBar");
20+
result.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_SET_PROGRESS);
21+
if (node.value != null) {
22+
float min = Float.NEGATIVE_INFINITY;
23+
float max = Float.POSITIVE_INFINITY;
24+
if (node.minValue != null) {
25+
try {
26+
min = Float.parseFloat(node.minValue);
27+
} catch (NumberFormatException e) {
28+
// Fallback to default min.
29+
}
30+
}
31+
if (node.maxValue != null) {
32+
try {
33+
max = Float.parseFloat(node.maxValue);
34+
} catch (NumberFormatException e) {
35+
// Fallback to default max.
36+
}
37+
}
38+
try {
39+
String valueString = node.value;
40+
boolean isPercentage = valueString != null && valueString.endsWith("%");
41+
if (isPercentage) {
42+
valueString = valueString.substring(0, valueString.length() - 1);
43+
}
44+
float parsedValue = Float.parseFloat(valueString);
45+
if (isPercentage) {
46+
// Convert the percentage to a value between min and max.
47+
if (max != Float.POSITIVE_INFINITY && min != Float.NEGATIVE_INFINITY) {
48+
parsedValue = (parsedValue / 100.0f) * (max - min) + min;
49+
}
50+
}
51+
result.setRangeInfo(
52+
AccessibilityNodeInfo.RangeInfo.obtain(
53+
AccessibilityNodeInfo.RangeInfo.RANGE_TYPE_FLOAT, min, max, parsedValue));
54+
} catch (NumberFormatException e) {
55+
if (Build.VERSION.SDK_INT >= API_LEVELS.API_36) {
56+
result.setRangeInfo(
57+
AccessibilityNodeInfo.RangeInfo.obtain(
58+
AccessibilityNodeInfo.RangeInfo.RANGE_TYPE_INDETERMINATE, 0.0f, 0.0f, 0.0f));
59+
} else {
60+
// Fallback to RANGE_TYPE_FLOAT with 0.0.
61+
result.setRangeInfo(
62+
AccessibilityNodeInfo.RangeInfo.obtain(
63+
AccessibilityNodeInfo.RangeInfo.RANGE_TYPE_FLOAT, 0.0f, 0.0f, 0.0f));
64+
}
65+
}
66+
}
67+
}
68+
}

‎engine/src/flutter/shell/platform/android/test/io/flutter/view/AccessibilityBridgeTest.java‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2747,6 +2747,58 @@ public void itAddsRangeInfoToProgressBar() {
27472747
assertEquals(50.0f, nodeInfo.getRangeInfo().getCurrent(), 1e-4f);
27482748
}
27492749

2750+
@Test
2751+
public void itAddsRangeInfoToSlider_withPercentage() {
2752+
AccessibilityBridge accessibilityBridge = setUpBridge();
2753+
TestSemanticsNode testSemanticsNode = new TestSemanticsNode();
2754+
testSemanticsNode.role = AccessibilityBridge.Role.SLIDER.value;
2755+
testSemanticsNode.value = "50%";
2756+
testSemanticsNode.minValue = "0.0";
2757+
testSemanticsNode.maxValue = "5.0";
2758+
TestSemanticsUpdate testSemanticsUpdate = testSemanticsNode.toUpdate();
2759+
testSemanticsUpdate.sendUpdateToBridge(accessibilityBridge);
2760+
AccessibilityNodeInfo nodeInfo = accessibilityBridge.createAccessibilityNodeInfo(0);
2761+
assertEquals("android.widget.SeekBar", nodeInfo.getClassName().toString());
2762+
assertNotNull(nodeInfo.getRangeInfo());
2763+
assertEquals(0.0f, nodeInfo.getRangeInfo().getMin(), 1e-4f);
2764+
assertEquals(5.0f, nodeInfo.getRangeInfo().getMax(), 1e-4f);
2765+
assertEquals(2.5f, nodeInfo.getRangeInfo().getCurrent(), 1e-4f);
2766+
}
2767+
2768+
@Test
2769+
@Config(sdk = 24)
2770+
public void itAddsActionSetProgressForSlider() {
2771+
AccessibilityBridge accessibilityBridge = setUpBridge();
2772+
TestSemanticsNode testSemanticsNode = new TestSemanticsNode();
2773+
testSemanticsNode.role = AccessibilityBridge.Role.SLIDER.value;
2774+
testSemanticsNode.value = "50%";
2775+
TestSemanticsUpdate testSemanticsUpdate = testSemanticsNode.toUpdate();
2776+
testSemanticsUpdate.sendUpdateToBridge(accessibilityBridge);
2777+
AccessibilityNodeInfo nodeInfo = accessibilityBridge.createAccessibilityNodeInfo(0);
2778+
assertEquals("android.widget.SeekBar", nodeInfo.getClassName().toString());
2779+
assertTrue(
2780+
nodeInfo
2781+
.getActionList()
2782+
.contains(AccessibilityNodeInfo.AccessibilityAction.ACTION_SET_PROGRESS));
2783+
}
2784+
2785+
@Test
2786+
@Config(sdk = 24)
2787+
public void itOmitsActionSetProgressForProgressBar() {
2788+
AccessibilityBridge accessibilityBridge = setUpBridge();
2789+
TestSemanticsNode testSemanticsNode = new TestSemanticsNode();
2790+
testSemanticsNode.role = AccessibilityBridge.Role.PROGRESS_BAR.value;
2791+
testSemanticsNode.value = "50";
2792+
TestSemanticsUpdate testSemanticsUpdate = testSemanticsNode.toUpdate();
2793+
testSemanticsUpdate.sendUpdateToBridge(accessibilityBridge);
2794+
AccessibilityNodeInfo nodeInfo = accessibilityBridge.createAccessibilityNodeInfo(0);
2795+
assertEquals("android.widget.ProgressBar", nodeInfo.getClassName().toString());
2796+
assertFalse(
2797+
nodeInfo
2798+
.getActionList()
2799+
.contains(AccessibilityNodeInfo.AccessibilityAction.ACTION_SET_PROGRESS));
2800+
}
2801+
27502802
@Test
27512803
public void itAddsRangeInfoToProgressBar_missingMinAndMaxValue() {
27522804
AccessibilityBridge accessibilityBridge = setUpBridge();

0 commit comments

Comments
 (0)