Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 21b8224

Browse files
authored
Send AccessibilityEvent.TYPE_VIEW_FOCUSED when input focus is set. (#12746)
When the focus changes, we should be sending a TYPE_VIEW_FOCUSED event. This enables that.
1 parent 2f352d6 commit 21b8224

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,14 @@ public class AccessibilityBridge extends AccessibilityNodeProvider {
190190
@Nullable
191191
private SemanticsNode inputFocusedSemanticsNode;
192192

193+
// Keeps track of the last semantics node that had the input focus.
194+
//
195+
// This is used to determine if the input focus has changed since the last time the
196+
// {@code inputFocusSemanticsNode} has been set, so that we can send a {@code TYPE_VIEW_FOCUSED}
197+
// event when it changes.
198+
@Nullable
199+
private SemanticsNode lastInputFocusedSemanticsNode;
200+
193201
// The widget within Flutter that currently sits beneath a cursor, e.g,
194202
// beneath a stylus or mouse cursor.
195203
@Nullable
@@ -1377,6 +1385,20 @@ void updateSemantics(@NonNull ByteBuffer buffer, @NonNull String[] strings) {
13771385
event.getText().add(object.label);
13781386
sendAccessibilityEvent(event);
13791387
}
1388+
1389+
// If the object is the input-focused node, then tell the reader about it, but only if
1390+
// it has changed since the last update.
1391+
if (inputFocusedSemanticsNode != null && inputFocusedSemanticsNode.id == object.id &&
1392+
(lastInputFocusedSemanticsNode == null || lastInputFocusedSemanticsNode.id != inputFocusedSemanticsNode.id)) {
1393+
lastInputFocusedSemanticsNode = inputFocusedSemanticsNode;
1394+
sendAccessibilityEvent(obtainAccessibilityEvent(object.id, AccessibilityEvent.TYPE_VIEW_FOCUSED));
1395+
} else if (inputFocusedSemanticsNode == null) {
1396+
// There's no TYPE_VIEW_CLEAR_FOCUSED event, so if the current input focus becomes
1397+
// null, then we just set the last one to null too, so that it sends the event again
1398+
// when something regains focus.
1399+
lastInputFocusedSemanticsNode = null;
1400+
}
1401+
13801402
if (inputFocusedSemanticsNode != null && inputFocusedSemanticsNode.id == object.id
13811403
&& object.hadFlag(Flag.IS_TEXT_FIELD) && object.hasFlag(Flag.IS_TEXT_FIELD)
13821404
// If we have a TextField that has InputFocus, we should avoid announcing it if something

0 commit comments

Comments
 (0)