Handle null primary focus when receiving key events (#80302)
This allows the key handling code in the shortcut manager to function when there is a null primaryFocus. The primary focus can be null only in some special circumstances (e.g. when an app hasn't set any focus at all), but that shouldn't cause a crash.
This commit is contained in:
parent
a0abf4109c
commit
6b06943c32
@ -356,8 +356,8 @@ class ShortcutManager extends ChangeNotifier with Diagnosticable {
|
|||||||
);
|
);
|
||||||
final Intent? matchedIntent = _find(keysPressed: keysPressed);
|
final Intent? matchedIntent = _find(keysPressed: keysPressed);
|
||||||
if (matchedIntent != null) {
|
if (matchedIntent != null) {
|
||||||
final BuildContext primaryContext = primaryFocus!.context!;
|
final BuildContext? primaryContext = primaryFocus?.context;
|
||||||
assert (primaryContext != null);
|
if (primaryContext != null) {
|
||||||
final Action<Intent>? action = Actions.maybeFind<Intent>(
|
final Action<Intent>? action = Actions.maybeFind<Intent>(
|
||||||
primaryContext,
|
primaryContext,
|
||||||
intent: matchedIntent,
|
intent: matchedIntent,
|
||||||
@ -369,6 +369,7 @@ class ShortcutManager extends ChangeNotifier with Diagnosticable {
|
|||||||
: KeyEventResult.skipRemainingHandlers;
|
: KeyEventResult.skipRemainingHandlers;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return modal ? KeyEventResult.skipRemainingHandlers : KeyEventResult.ignored;
|
return modal ? KeyEventResult.skipRemainingHandlers : KeyEventResult.ignored;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -290,6 +290,37 @@ void main() {
|
|||||||
expect(invoked, isTrue);
|
expect(invoked, isTrue);
|
||||||
expect(pressedKeys, equals(<LogicalKeyboardKey>[LogicalKeyboardKey.shiftLeft]));
|
expect(pressedKeys, equals(<LogicalKeyboardKey>[LogicalKeyboardKey.shiftLeft]));
|
||||||
});
|
});
|
||||||
|
testWidgets('ShortcutManager ignores keypresses with no primary focus', (WidgetTester tester) async {
|
||||||
|
final GlobalKey containerKey = GlobalKey();
|
||||||
|
final List<LogicalKeyboardKey> pressedKeys = <LogicalKeyboardKey>[];
|
||||||
|
final TestShortcutManager testManager = TestShortcutManager(pressedKeys);
|
||||||
|
bool invoked = false;
|
||||||
|
await tester.pumpWidget(
|
||||||
|
Actions(
|
||||||
|
actions: <Type, Action<Intent>>{
|
||||||
|
TestIntent: TestAction(
|
||||||
|
onInvoke: (Intent intent) {
|
||||||
|
invoked = true;
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
),
|
||||||
|
},
|
||||||
|
child: Shortcuts(
|
||||||
|
manager: testManager,
|
||||||
|
shortcuts: <LogicalKeySet, Intent>{
|
||||||
|
LogicalKeySet(LogicalKeyboardKey.shift): const TestIntent(),
|
||||||
|
},
|
||||||
|
child: SizedBox(key: containerKey, width: 100, height: 100),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
await tester.pump();
|
||||||
|
expect(primaryFocus, isNull);
|
||||||
|
expect(Shortcuts.of(containerKey.currentContext!), isNotNull);
|
||||||
|
await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft);
|
||||||
|
expect(invoked, isFalse);
|
||||||
|
expect(pressedKeys, isEmpty);
|
||||||
|
});
|
||||||
testWidgets("Shortcuts passes to the next Shortcuts widget if it doesn't map the key", (WidgetTester tester) async {
|
testWidgets("Shortcuts passes to the next Shortcuts widget if it doesn't map the key", (WidgetTester tester) async {
|
||||||
final GlobalKey containerKey = GlobalKey();
|
final GlobalKey containerKey = GlobalKey();
|
||||||
final List<LogicalKeyboardKey> pressedKeys = <LogicalKeyboardKey>[];
|
final List<LogicalKeyboardKey> pressedKeys = <LogicalKeyboardKey>[];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user