Skip to content

Commit 9d28ab7

Browse files
authored
[web] Fix VoiceOver child focus direction (flutter#188820)
Fixes flutter#168458 **Problem** When VoiceOver focuses a child semantics node inside a Flutter web view, `ViewFocusBinding` reports it as forward tab traversal. The framework then moves focus back to the first focusable widget instead of keeping VoiceOver's target. **Fix** `ViewFocusBinding` now uses the keyboard direction only when the Flutter view root is focused. Child focus reports `ViewFocusDirection.undefined`, preserving VoiceOver's selected element. **Demo** | Before | After | |---|---| | https://flutter-demo-46-before.web.app | https://flutter-demo-46-after.web.app | Steps: with VoiceOver on, open the rotor or context menu(CTRL+OPTION+U) and jump between form controls. Before: focus snaps back to the first form control after each VoiceOver jump. After: focus stays on the control VoiceOver navigated to.
1 parent 326d90b commit 9d28ab7

3 files changed

Lines changed: 204 additions & 3 deletions

File tree

‎engine/src/flutter/lib/web_ui/lib/src/engine/platform_dispatcher/view_focus_binding.dart‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ final class ViewFocusBinding {
103103
event = ui.ViewFocusEvent(
104104
viewId: viewId,
105105
state: ui.ViewFocusState.focused,
106-
direction: _viewFocusDirection,
106+
direction: _isViewRoot(viewId, focusedElement)
107+
? _viewFocusDirection
108+
: ui.ViewFocusDirection.undefined,
107109
);
108110
}
109111
_updateViewKeyboardReachability(_lastViewId, reachable: true);
@@ -117,6 +119,11 @@ final class ViewFocusBinding {
117119
return viewManager.findViewForElement(element)?.viewId;
118120
}
119121

122+
bool _isViewRoot(int viewId, DomElement? element) {
123+
final DomElement? rootElement = _viewManager[viewId]?.dom.rootElement;
124+
return element != null && element == rootElement;
125+
}
126+
120127
void _handleViewCreated(int viewId) {
121128
final DomElement? rootElement = _viewManager[viewId]?.dom.rootElement;
122129

‎engine/src/flutter/lib/web_ui/test/engine/platform_dispatcher/view_focus_binding_test.dart‎

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,62 @@ void testMain() {
7676
expect(dispatchedViewFocusEvents[0].direction, ui.ViewFocusDirection.forward);
7777
});
7878

79+
test('fires a focus event with undefined direction when a child is focused', () async {
80+
final EngineFlutterView view = createAndRegisterView(dispatcher);
81+
final DomElement button = createDomElement('button');
82+
83+
view.dom.rootElement.append(button);
84+
button.focusWithoutScroll();
85+
86+
expect(dispatchedViewFocusEvents, hasLength(1));
87+
88+
expect(dispatchedViewFocusEvents[0].viewId, view.viewId);
89+
expect(dispatchedViewFocusEvents[0].state, ui.ViewFocusState.focused);
90+
expect(dispatchedViewFocusEvents[0].direction, ui.ViewFocusDirection.undefined);
91+
});
92+
93+
test('fires a refocus event with undefined direction when child focus returns', () async {
94+
final EngineFlutterView view = createAndRegisterView(dispatcher);
95+
final DomElement button1 = createDomElement('button');
96+
final DomElement button2 = createDomElement('button');
97+
98+
view.dom.rootElement.append(button1);
99+
view.dom.rootElement.append(button2);
100+
101+
button1.focusWithoutScroll();
102+
button1.blur();
103+
await Future<void>.delayed(Duration.zero);
104+
button2.focusWithoutScroll();
105+
106+
expect(dispatchedViewFocusEvents, hasLength(3));
107+
108+
expect(dispatchedViewFocusEvents[0].viewId, view.viewId);
109+
expect(dispatchedViewFocusEvents[0].state, ui.ViewFocusState.focused);
110+
expect(dispatchedViewFocusEvents[0].direction, ui.ViewFocusDirection.undefined);
111+
112+
expect(dispatchedViewFocusEvents[1].viewId, view.viewId);
113+
expect(dispatchedViewFocusEvents[1].state, ui.ViewFocusState.unfocused);
114+
expect(dispatchedViewFocusEvents[1].direction, ui.ViewFocusDirection.undefined);
115+
116+
expect(dispatchedViewFocusEvents[2].viewId, view.viewId);
117+
expect(dispatchedViewFocusEvents[2].state, ui.ViewFocusState.focused);
118+
expect(dispatchedViewFocusEvents[2].direction, ui.ViewFocusDirection.undefined);
119+
});
120+
121+
test('fires a focus event with backward direction when shift tab focuses the root', () async {
122+
final EngineFlutterView view = createAndRegisterView(dispatcher);
123+
124+
domDocument.body!.pressTabKey(shift: true);
125+
view.dom.rootElement.focusWithoutScroll();
126+
domDocument.body!.releaseTabKey();
127+
128+
expect(dispatchedViewFocusEvents, hasLength(1));
129+
130+
expect(dispatchedViewFocusEvents[0].viewId, view.viewId);
131+
expect(dispatchedViewFocusEvents[0].state, ui.ViewFocusState.focused);
132+
expect(dispatchedViewFocusEvents[0].direction, ui.ViewFocusDirection.backward);
133+
});
134+
79135
test('fires a focus event - a view was unfocused', () async {
80136
final EngineFlutterView view = createAndRegisterView(dispatcher);
81137

@@ -143,6 +199,34 @@ void testMain() {
143199
expect(dispatchedViewFocusEvents[2].direction, ui.ViewFocusDirection.undefined);
144200
});
145201

202+
test('fires a focus event - focus transitions between children of two views', () async {
203+
final EngineFlutterView view1 = createAndRegisterView(dispatcher);
204+
final EngineFlutterView view2 = createAndRegisterView(dispatcher);
205+
final DomElement button1 = createDomElement('button');
206+
final DomElement button2 = createDomElement('button');
207+
208+
view1.dom.rootElement.append(button1);
209+
view2.dom.rootElement.append(button2);
210+
211+
button1.focusWithoutScroll();
212+
button2.focusWithoutScroll();
213+
214+
expect(dispatchedViewFocusEvents, hasLength(2));
215+
216+
expect(dispatchedViewFocusEvents[0].viewId, view1.viewId);
217+
expect(dispatchedViewFocusEvents[0].state, ui.ViewFocusState.focused);
218+
expect(dispatchedViewFocusEvents[0].direction, ui.ViewFocusDirection.undefined);
219+
220+
expect(dispatchedViewFocusEvents[1].viewId, view2.viewId);
221+
expect(dispatchedViewFocusEvents[1].state, ui.ViewFocusState.focused);
222+
expect(dispatchedViewFocusEvents[1].direction, ui.ViewFocusDirection.undefined);
223+
224+
// The view that lost the focus becomes reachable by the keyboard again,
225+
// and the view that gained it is taken out of the tab order.
226+
expect(view1.dom.rootElement.getAttribute('tabindex'), '0');
227+
expect(view2.dom.rootElement.getAttribute('tabindex'), '-1');
228+
});
229+
146230
test('requestViewFocusChange focuses the view', () {
147231
final EngineFlutterView view = createAndRegisterView(dispatcher);
148232

@@ -242,7 +326,7 @@ void testMain() {
242326

243327
expect(dispatchedViewFocusEvents[0].viewId, view.viewId);
244328
expect(dispatchedViewFocusEvents[0].state, ui.ViewFocusState.focused);
245-
expect(dispatchedViewFocusEvents[0].direction, ui.ViewFocusDirection.forward);
329+
expect(dispatchedViewFocusEvents[0].direction, ui.ViewFocusDirection.undefined);
246330
});
247331

248332
test('works even if focus is changed in the middle of a blur call', () {
@@ -266,7 +350,7 @@ void testMain() {
266350

267351
expect(dispatchedViewFocusEvents[0].viewId, view.viewId);
268352
expect(dispatchedViewFocusEvents[0].state, ui.ViewFocusState.focused);
269-
expect(dispatchedViewFocusEvents[0].direction, ui.ViewFocusDirection.forward);
353+
expect(dispatchedViewFocusEvents[0].direction, ui.ViewFocusDirection.undefined);
270354
});
271355
});
272356
}

‎packages/flutter/test/widgets/view_test.dart‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,116 @@ void main() {
559559
expect(FocusManager.instance.rootScope.hasPrimaryFocus, isFalse);
560560
});
561561

562+
// A screen reader can move focus straight to a control instead of tabbing
563+
// into the view. On web that arrives as a SemanticsAction.focus for the
564+
// control, followed by a focus event for the view with an undefined
565+
// direction, and the already focused control must survive it.
566+
// See https://github.com/flutter/flutter/issues/168458
567+
testWidgets('ViewFocusEvent with an undefined direction keeps the focused child', (
568+
WidgetTester tester,
569+
) async {
570+
final nodeA = FocusNode(debugLabel: 'a');
571+
addTearDown(nodeA.dispose);
572+
final nodeB = FocusNode(debugLabel: 'b');
573+
addTearDown(nodeB.dispose);
574+
575+
late FlutterView view;
576+
await tester.pumpWidget(
577+
Directionality(
578+
textDirection: TextDirection.ltr,
579+
child: Column(
580+
children: <Widget>[
581+
Focus(focusNode: nodeA, child: const Text('a')),
582+
Focus(focusNode: nodeB, child: const Text('b')),
583+
Builder(
584+
builder: (BuildContext context) {
585+
view = View.of(context);
586+
return const SizedBox.shrink();
587+
},
588+
),
589+
],
590+
),
591+
),
592+
);
593+
594+
// Focus leaves the view entirely, which parks the focus on the root scope.
595+
ServicesBinding.instance.platformDispatcher.onViewFocusChange?.call(
596+
ViewFocusEvent(
597+
viewId: view.viewId,
598+
state: ViewFocusState.unfocused,
599+
direction: ViewFocusDirection.undefined,
600+
),
601+
);
602+
await tester.pump();
603+
604+
expect(FocusManager.instance.rootScope.hasPrimaryFocus, isTrue);
605+
606+
// The screen reader picks the second control. Both the semantics driven
607+
// focus request and the view focus event land in the same frame.
608+
nodeB.requestFocus();
609+
ServicesBinding.instance.platformDispatcher.onViewFocusChange?.call(
610+
ViewFocusEvent(
611+
viewId: view.viewId,
612+
state: ViewFocusState.focused,
613+
direction: ViewFocusDirection.undefined,
614+
),
615+
);
616+
await tester.pump();
617+
618+
expect(nodeB.hasPrimaryFocus, isTrue);
619+
expect(nodeA.hasPrimaryFocus, isFalse);
620+
});
621+
622+
testWidgets('ViewFocusEvent with a forward direction moves focus to the first child', (
623+
WidgetTester tester,
624+
) async {
625+
final nodeA = FocusNode(debugLabel: 'a');
626+
addTearDown(nodeA.dispose);
627+
final nodeB = FocusNode(debugLabel: 'b');
628+
addTearDown(nodeB.dispose);
629+
630+
late FlutterView view;
631+
await tester.pumpWidget(
632+
Directionality(
633+
textDirection: TextDirection.ltr,
634+
child: Column(
635+
children: <Widget>[
636+
Focus(focusNode: nodeA, child: const Text('a')),
637+
Focus(focusNode: nodeB, child: const Text('b')),
638+
Builder(
639+
builder: (BuildContext context) {
640+
view = View.of(context);
641+
return const SizedBox.shrink();
642+
},
643+
),
644+
],
645+
),
646+
),
647+
);
648+
649+
ServicesBinding.instance.platformDispatcher.onViewFocusChange?.call(
650+
ViewFocusEvent(
651+
viewId: view.viewId,
652+
state: ViewFocusState.unfocused,
653+
direction: ViewFocusDirection.undefined,
654+
),
655+
);
656+
await tester.pump();
657+
658+
nodeB.requestFocus();
659+
ServicesBinding.instance.platformDispatcher.onViewFocusChange?.call(
660+
ViewFocusEvent(
661+
viewId: view.viewId,
662+
state: ViewFocusState.focused,
663+
direction: ViewFocusDirection.forward,
664+
),
665+
);
666+
await tester.pump();
667+
668+
expect(nodeA.hasPrimaryFocus, isTrue);
669+
expect(nodeB.hasPrimaryFocus, isFalse);
670+
});
671+
562672
testWidgets(
563673
'View notifies engine that a view should have focus when a widget focus change occurs.',
564674
(WidgetTester tester) async {

0 commit comments

Comments
 (0)