Skip to content

Commit d155a33

Browse files
authored
[material_ui] Fix MenuAnchor non-Unicode shortcut labels on JS web (flutter#13370)
Delegate unlocalized trigger fallback in `_LocalizedShortcutLabeler.getShortcutLabel` directly to `trigger.keyLabel` instead of masking `keyId` with `LogicalKeyboardKey.planeMask` (`0x0ff00000000`), which truncates to `0` under 32-bit JS bitwise `&` and renders keys like `Tab` and `F1` as raw char codes (`\t`, `\u0801`). Fixes flutter/flutter#195003 ## Pre-Review Checklist [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent d2e97da commit d155a33

3 files changed

Lines changed: 49 additions & 10 deletions

File tree

‎packages/material_ui/lib/src/menu_anchor.dart‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,21 +2622,12 @@ class _LocalizedShortcutLabeler {
26222622
],
26232623
];
26242624
String? shortcutTrigger;
2625-
final int logicalKeyId = trigger.keyId;
26262625
if (_shortcutGraphicEquivalents.containsKey(trigger)) {
26272626
shortcutTrigger = _shortcutGraphicEquivalents[trigger];
26282627
} else {
26292628
// Otherwise, look it up, and if we don't have a translation for it,
26302629
// then fall back to the key label.
2631-
shortcutTrigger = _getLocalizedName(trigger, localizations);
2632-
if (shortcutTrigger == null && logicalKeyId & LogicalKeyboardKey.planeMask == 0x0) {
2633-
// If the trigger is a Unicode-character-producing key, then use the
2634-
// character.
2635-
shortcutTrigger = String.fromCharCode(logicalKeyId & LogicalKeyboardKey.valueMask)
2636-
.toUpperCase();
2637-
}
2638-
// Fall back to the key label if all else fails.
2639-
shortcutTrigger ??= trigger.keyLabel;
2630+
shortcutTrigger = _getLocalizedName(trigger, localizations) ?? trigger.keyLabel;
26402631
}
26412632
return <String>[
26422633
...modifiers,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Fixes `MenuAnchor` and `MenuBar` shortcut labels for unlocalized non-Unicode keys (such as `Tab` and `F1`) on JavaScript web builds.
3+
version: patch

‎packages/material_ui/test/menu_anchor_test.dart‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4461,6 +4461,51 @@ void main() {
44614461
expect(find.text(allExpected), findsOneWidget);
44624462
expect(find.text(charExpected), findsOneWidget);
44634463
}, variant: TargetPlatformVariant.all());
4464+
4465+
testWidgets(
4466+
'getShortcutLabel formats unlocalized non-Unicode keys',
4467+
(WidgetTester tester) async {
4468+
final (String expectedCtrl, String expectedSeparator) = switch (defaultTargetPlatform) {
4469+
TargetPlatform.android ||
4470+
TargetPlatform.fuchsia ||
4471+
TargetPlatform.linux ||
4472+
TargetPlatform.windows => ('Ctrl', '+'),
4473+
TargetPlatform.iOS || TargetPlatform.macOS => ('⌃', ' '),
4474+
};
4475+
4476+
await tester.pumpWidget(
4477+
MaterialApp(
4478+
home: Material(
4479+
child: MenuBar(
4480+
controller: controller,
4481+
children: <Widget>[
4482+
SubmenuButton(
4483+
menuChildren: <Widget>[
4484+
MenuItemButton(
4485+
shortcut: const SingleActivator(LogicalKeyboardKey.tab, control: true),
4486+
child: Text(TestMenu.subMenu10.label),
4487+
),
4488+
MenuItemButton(
4489+
shortcut: const SingleActivator(LogicalKeyboardKey.f1, control: true),
4490+
child: Text(TestMenu.subMenu11.label),
4491+
),
4492+
],
4493+
child: Text(TestMenu.mainMenu0.label),
4494+
),
4495+
],
4496+
),
4497+
),
4498+
),
4499+
);
4500+
await tester.tap(find.text(TestMenu.mainMenu0.label));
4501+
await tester.pump();
4502+
4503+
expect(find.text('$expectedCtrl${expectedSeparator}Tab'), findsOneWidget);
4504+
expect(find.text('$expectedCtrl${expectedSeparator}F1'), findsOneWidget);
4505+
},
4506+
variant: TargetPlatformVariant.all(),
4507+
tags: 'reduced-web-test-set',
4508+
);
44644509
});
44654510

44664511
group('CheckboxMenuButton', () {

0 commit comments

Comments
 (0)