Skip to content

Commit e4cabae

Browse files
authored
Add a11y support for selected text (flutter#4584)
1 parent d715860 commit e4cabae

6 files changed

Lines changed: 29 additions & 2 deletions

File tree

‎lib/ui/semantics.dart‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,9 @@ class SemanticsUpdateBuilder extends NativeFieldWrapperClass2 {
293293
/// string describes what result an action performed on this node has. The
294294
/// reading direction of all these strings is given by `textDirection`.
295295
///
296+
/// The fields 'textSelectionStart' and 'textSelectionEnd' describe the
297+
/// currently selected text within `value`.
298+
///
296299
/// The `rect` is the region occupied by this node in its own coordinate
297300
/// system.
298301
///
@@ -302,6 +305,8 @@ class SemanticsUpdateBuilder extends NativeFieldWrapperClass2 {
302305
int id,
303306
int flags,
304307
int actions,
308+
int textSelectionStart,
309+
int textSelectionEnd,
305310
Rect rect,
306311
String label,
307312
String hint,
@@ -318,6 +323,8 @@ class SemanticsUpdateBuilder extends NativeFieldWrapperClass2 {
318323
id,
319324
flags,
320325
actions,
326+
textSelectionStart,
327+
textSelectionEnd,
321328
rect.left,
322329
rect.top,
323330
rect.right,
@@ -336,6 +343,8 @@ class SemanticsUpdateBuilder extends NativeFieldWrapperClass2 {
336343
int id,
337344
int flags,
338345
int actions,
346+
int textSelectionStart,
347+
int textSelectionEnd,
339348
double left,
340349
double top,
341350
double right,

‎lib/ui/semantics/semantics_node.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ struct SemanticsNode {
5555
int32_t id = 0;
5656
int32_t flags = 0;
5757
int32_t actions = 0;
58+
int32_t textSelectionStart = -1;
59+
int32_t textSelectionEnd = -1;
5860
std::string label;
5961
std::string hint;
6062
std::string value;

‎lib/ui/semantics/semantics_update_builder.cc‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ SemanticsUpdateBuilder::~SemanticsUpdateBuilder() = default;
3737
void SemanticsUpdateBuilder::updateNode(int id,
3838
int flags,
3939
int actions,
40+
int textSelectionStart,
41+
int textSelectionEnd,
4042
double left,
4143
double top,
4244
double right,
@@ -53,6 +55,8 @@ void SemanticsUpdateBuilder::updateNode(int id,
5355
node.id = id;
5456
node.flags = flags;
5557
node.actions = actions;
58+
node.textSelectionStart = textSelectionStart;
59+
node.textSelectionEnd = textSelectionEnd;
5660
node.rect = SkRect::MakeLTRB(left, top, right, bottom);
5761
node.label = label;
5862
node.hint = hint;

‎lib/ui/semantics/semantics_update_builder.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class SemanticsUpdateBuilder
2828
void updateNode(int id,
2929
int flags,
3030
int actions,
31+
int textSelectionStart,
32+
int textSelectionEnd,
3133
double left,
3234
double top,
3335
double right,

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,12 @@ public AccessibilityNodeInfo createAccessibilityNodeInfo(int virtualViewId) {
123123

124124
if (object.hasFlag(Flag.IS_TEXT_FIELD)) {
125125
result.setClassName("android.widget.EditText");
126-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
126+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
127127
result.setEditable(true);
128+
if (object.textSelectionStart != -1 && object.textSelectionEnd != -1) {
129+
result.setTextSelection(object.textSelectionStart, object.textSelectionEnd);
130+
}
131+
}
128132

129133
// Cursor movements
130134
int granularities = 0;
@@ -599,6 +603,8 @@ private class SemanticsObject {
599603

600604
int flags;
601605
int actions;
606+
int textSelectionStart;
607+
int textSelectionEnd;
602608
String label;
603609
String value;
604610
String increasedValue;
@@ -658,6 +664,8 @@ void updateWith(ByteBuffer buffer, String[] strings) {
658664

659665
flags = buffer.getInt();
660666
actions = buffer.getInt();
667+
textSelectionStart = buffer.getInt();
668+
textSelectionEnd = buffer.getInt();
661669

662670
int stringIndex = buffer.getInt();
663671
label = stringIndex == -1 ? null : strings[stringIndex];

‎shell/platform/android/platform_view_android.cc‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ bool PlatformViewAndroid::ResourceContextMakeCurrent() {
461461

462462
void PlatformViewAndroid::UpdateSemantics(
463463
std::vector<blink::SemanticsNode> update) {
464-
constexpr size_t kBytesPerNode = 30 * sizeof(int32_t);
464+
constexpr size_t kBytesPerNode = 32 * sizeof(int32_t);
465465
constexpr size_t kBytesPerChild = sizeof(int32_t);
466466

467467
JNIEnv* env = fml::jni::AttachCurrentThread();
@@ -488,6 +488,8 @@ void PlatformViewAndroid::UpdateSemantics(
488488
buffer_int32[position++] = node.id;
489489
buffer_int32[position++] = node.flags;
490490
buffer_int32[position++] = node.actions;
491+
buffer_int32[position++] = node.textSelectionStart;
492+
buffer_int32[position++] = node.textSelectionEnd;
491493
if (node.label.empty()) {
492494
buffer_int32[position++] = -1;
493495
} else {

0 commit comments

Comments
 (0)