Skip to content

Commit 97db29f

Browse files
authored
[web] Skip non-focusable containers in Focusable.focusAsRouteDefault (flutter#194138)
Guards `Focusable.focusAsRouteDefault()` with `if (!semanticsObject.isFocusable) return false;` so non-focusable container roles (`SemanticScrollable`, `SemanticForm`, `SemanticList`, `SemanticTable`) do not swallow initial route focus when dialogs or drawers open on Web. Verified: `felt test --suite chrome-dart2js-canvaskit-engine test/engine/semantics/semantics_test.dart` (271 passed). Part of flutter#138371 (related to flutter#122876, flutter#120894). - `engine/src/flutter/lib/web_ui/lib/src/engine/semantics/focusable.dart`: Returns `false` from `focusAsRouteDefault()` when `!semanticsObject.isFocusable` (matching `Focusable.update()`, which only sets `tabindex="0"` when `isFocusable` is `true`). - `engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_test.dart`: Resets `onSemanticsActionEvent` in suite `tearDown` and verifies that `SemanticRoute` skips non-focusable `SemanticScrollable`, `SemanticForm`, `SemanticList`, and `SemanticTable` containers to focus the first inner focusable descendant. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- 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/CONTRIBUTING.ai.md [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 [flutter/website]: https://github.com/flutter/website [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 38785ef commit 97db29f

2 files changed

Lines changed: 187 additions & 0 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ class Focusable extends SemanticBehavior {
4747
/// not take the focus. The return value can be used to decide whether to stop
4848
/// searching for a node that should take focus.
4949
bool focusAsRouteDefault() {
50+
if (!semanticsObject.isFocusable) {
51+
return false;
52+
}
5053
_focusManager._lastEvent = AccessibilityFocusManagerEvent.requestedFocus;
5154
owner.element.focusWithoutScroll();
5255
return true;

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

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ void runSemanticsTests() {
5151
EngineSemantics.debugResetSemantics();
5252
});
5353

54+
tearDown(() {
55+
EnginePlatformDispatcher.instance.onSemanticsActionEvent = null;
56+
});
57+
5458
group(EngineSemanticsOwner, () {
5559
_testEngineSemanticsOwner();
5660
});
@@ -4712,6 +4716,186 @@ void _testRoute() {
47124716

47134717
semantics().semanticsEnabled = false;
47144718
});
4719+
4720+
test('skips non-focusable Focusable containers to focus on inner descendant', () async {
4721+
Future<void> checkContainerRouteDefaultFocus({
4722+
required SemanticsNodeUpdate Function(
4723+
SemanticsTester tester,
4724+
List<SemanticsNodeUpdate> leafChildren,
4725+
)
4726+
buildContainer,
4727+
required Map<int, EngineSemanticsRole> expectedContainerRoles,
4728+
required int firstLeafId,
4729+
}) async {
4730+
// 1. Container wrapping a non-focusable leaf ('Heading') + focusable button:
4731+
// route default focus should skip the container and focus the leaf <span>.
4732+
EngineSemantics.debugResetSemantics();
4733+
semantics()
4734+
..debugOverrideTimestampFunction(() => _testTime)
4735+
..semanticsEnabled = true;
4736+
4737+
final capturedActions = <CapturedAction>[];
4738+
EnginePlatformDispatcher.instance.onSemanticsActionEvent = (ui.SemanticsActionEvent event) {
4739+
capturedActions.add((event.nodeId, event.type, event.arguments));
4740+
};
4741+
4742+
var tester = SemanticsTester(owner());
4743+
tester.updateNode(
4744+
id: 0,
4745+
flags: const ui.SemanticsFlags(scopesRoute: true),
4746+
transform: Matrix4.identity().toFloat64(),
4747+
children: <SemanticsNodeUpdate>[
4748+
buildContainer(tester, <SemanticsNodeUpdate>[
4749+
tester.updateNode(
4750+
id: firstLeafId,
4751+
label: 'Heading',
4752+
rect: const ui.Rect.fromLTRB(0, 0, 100, 50),
4753+
),
4754+
tester.updateNode(
4755+
id: firstLeafId + 1,
4756+
label: 'Click me!',
4757+
flags: const ui.SemanticsFlags(
4758+
isEnabled: ui.Tristate.isTrue,
4759+
isButton: true,
4760+
isFocused: ui.Tristate.isFalse,
4761+
),
4762+
hasTap: true,
4763+
rect: const ui.Rect.fromLTRB(0, 50, 100, 100),
4764+
),
4765+
]),
4766+
],
4767+
);
4768+
tester.apply();
4769+
4770+
expect(capturedActions, isEmpty);
4771+
for (final MapEntry<int, EngineSemanticsRole>(:key, :value)
4772+
in expectedContainerRoles.entries) {
4773+
expect(tester.getSemanticsObject(key).semanticRole?.kind, value);
4774+
}
4775+
final DomElement span = owner().debugSemanticsTree![firstLeafId]!.element
4776+
.querySelectorAll('span')
4777+
.single;
4778+
expect(span.tabIndex, -1);
4779+
expect(domDocument.activeElement, span);
4780+
4781+
// 2. Container wrapping only a focusable button:
4782+
// route default focus should skip the container and focus the button.
4783+
EngineSemantics.debugResetSemantics();
4784+
semantics()
4785+
..debugOverrideTimestampFunction(() => _testTime)
4786+
..semanticsEnabled = true;
4787+
4788+
tester = SemanticsTester(owner());
4789+
tester.updateNode(
4790+
id: 0,
4791+
flags: const ui.SemanticsFlags(scopesRoute: true),
4792+
transform: Matrix4.identity().toFloat64(),
4793+
children: <SemanticsNodeUpdate>[
4794+
buildContainer(tester, <SemanticsNodeUpdate>[
4795+
tester.updateNode(
4796+
id: firstLeafId,
4797+
label: 'Click me!',
4798+
flags: const ui.SemanticsFlags(
4799+
isEnabled: ui.Tristate.isTrue,
4800+
isButton: true,
4801+
isFocused: ui.Tristate.isFalse,
4802+
),
4803+
hasTap: true,
4804+
rect: const ui.Rect.fromLTRB(0, 0, 100, 50),
4805+
),
4806+
]),
4807+
],
4808+
);
4809+
tester.apply();
4810+
4811+
expect(capturedActions, isEmpty);
4812+
final DomElement button = tester.getSemanticsObject(firstLeafId).element;
4813+
expect(button.tabIndex, 0);
4814+
expect(domDocument.activeElement, button);
4815+
}
4816+
4817+
// SemanticScrollable (hasImplicitScrolling: true)
4818+
await checkContainerRouteDefaultFocus(
4819+
buildContainer: (SemanticsTester tester, List<SemanticsNodeUpdate> children) =>
4820+
tester.updateNode(
4821+
id: 1,
4822+
flags: const ui.SemanticsFlags(hasImplicitScrolling: true),
4823+
rect: const ui.Rect.fromLTRB(0, 0, 100, 100),
4824+
children: children,
4825+
),
4826+
expectedContainerRoles: <int, EngineSemanticsRole>{1: EngineSemanticsRole.scrollable},
4827+
firstLeafId: 2,
4828+
);
4829+
4830+
// SemanticForm (ui.SemanticsRole.form)
4831+
await checkContainerRouteDefaultFocus(
4832+
buildContainer: (SemanticsTester tester, List<SemanticsNodeUpdate> children) =>
4833+
tester.updateNode(
4834+
id: 1,
4835+
role: ui.SemanticsRole.form,
4836+
rect: const ui.Rect.fromLTRB(0, 0, 100, 100),
4837+
children: children,
4838+
),
4839+
expectedContainerRoles: <int, EngineSemanticsRole>{1: EngineSemanticsRole.form},
4840+
firstLeafId: 2,
4841+
);
4842+
4843+
// SemanticList -> SemanticListItem
4844+
await checkContainerRouteDefaultFocus(
4845+
buildContainer: (SemanticsTester tester, List<SemanticsNodeUpdate> children) =>
4846+
tester.updateNode(
4847+
id: 1,
4848+
role: ui.SemanticsRole.list,
4849+
rect: const ui.Rect.fromLTRB(0, 0, 100, 100),
4850+
children: <SemanticsNodeUpdate>[
4851+
tester.updateNode(
4852+
id: 2,
4853+
role: ui.SemanticsRole.listItem,
4854+
rect: const ui.Rect.fromLTRB(0, 0, 100, 100),
4855+
children: children,
4856+
),
4857+
],
4858+
),
4859+
expectedContainerRoles: <int, EngineSemanticsRole>{
4860+
1: EngineSemanticsRole.list,
4861+
2: EngineSemanticsRole.listItem,
4862+
},
4863+
firstLeafId: 3,
4864+
);
4865+
4866+
// SemanticTable -> SemanticRow -> SemanticCell
4867+
await checkContainerRouteDefaultFocus(
4868+
buildContainer: (SemanticsTester tester, List<SemanticsNodeUpdate> children) =>
4869+
tester.updateNode(
4870+
id: 1,
4871+
role: ui.SemanticsRole.table,
4872+
rect: const ui.Rect.fromLTRB(0, 0, 100, 100),
4873+
children: <SemanticsNodeUpdate>[
4874+
tester.updateNode(
4875+
id: 2,
4876+
role: ui.SemanticsRole.row,
4877+
rect: const ui.Rect.fromLTRB(0, 0, 100, 100),
4878+
children: <SemanticsNodeUpdate>[
4879+
tester.updateNode(
4880+
id: 3,
4881+
role: ui.SemanticsRole.cell,
4882+
rect: const ui.Rect.fromLTRB(0, 0, 100, 100),
4883+
children: children,
4884+
),
4885+
],
4886+
),
4887+
],
4888+
),
4889+
expectedContainerRoles: <int, EngineSemanticsRole>{
4890+
1: EngineSemanticsRole.table,
4891+
2: EngineSemanticsRole.row,
4892+
3: EngineSemanticsRole.cell,
4893+
},
4894+
firstLeafId: 4,
4895+
);
4896+
4897+
semantics().semanticsEnabled = false;
4898+
});
47154899
}
47164900

47174901
void _testDialogs() {

0 commit comments

Comments
 (0)