Unverified Commit c8d876c5 authored by Greg Spencer's avatar Greg Spencer Committed by GitHub

Fix CallbackShortcuts to only call shortcuts when triggered (#93693)

parent 819823e7
...@@ -1005,9 +1005,11 @@ class CallbackShortcuts extends StatelessWidget { ...@@ -1005,9 +1005,11 @@ class CallbackShortcuts extends StatelessWidget {
// throws, by providing the activator and event as arguments that will appear // throws, by providing the activator and event as arguments that will appear
// in the stack trace. // in the stack trace.
bool _applyKeyBinding(ShortcutActivator activator, RawKeyEvent event) { bool _applyKeyBinding(ShortcutActivator activator, RawKeyEvent event) {
if (activator.accepts(event, RawKeyboard.instance)) { if (activator.triggers?.contains(event.logicalKey) ?? true) {
bindings[activator]!.call(); if (activator.accepts(event, RawKeyboard.instance)) {
return true; bindings[activator]!.call();
return true;
}
} }
return false; return false;
} }
......
...@@ -1126,12 +1126,16 @@ void main() { ...@@ -1126,12 +1126,16 @@ void main() {
group('CallbackShortcuts', () { group('CallbackShortcuts', () {
testWidgets('trigger on key events', (WidgetTester tester) async { testWidgets('trigger on key events', (WidgetTester tester) async {
int invoked = 0; int invokedA = 0;
int invokedB = 0;
await tester.pumpWidget( await tester.pumpWidget(
CallbackShortcuts( CallbackShortcuts(
bindings: <ShortcutActivator, VoidCallback>{ bindings: <ShortcutActivator, VoidCallback>{
const SingleActivator(LogicalKeyboardKey.keyA): () { const SingleActivator(LogicalKeyboardKey.keyA): () {
invoked += 1; invokedA += 1;
},
const SingleActivator(LogicalKeyboardKey.keyB): () {
invokedB += 1;
}, },
}, },
child: const Focus( child: const Focus(
...@@ -1143,9 +1147,20 @@ void main() { ...@@ -1143,9 +1147,20 @@ void main() {
await tester.pump(); await tester.pump();
await tester.sendKeyDownEvent(LogicalKeyboardKey.keyA); await tester.sendKeyDownEvent(LogicalKeyboardKey.keyA);
expect(invoked, equals(1)); await tester.pump();
expect(invokedA, equals(1));
expect(invokedB, equals(0));
await tester.sendKeyUpEvent(LogicalKeyboardKey.keyA); await tester.sendKeyUpEvent(LogicalKeyboardKey.keyA);
expect(invoked, equals(1)); expect(invokedA, equals(1));
expect(invokedB, equals(0));
invokedA = 0;
invokedB = 0;
await tester.sendKeyDownEvent(LogicalKeyboardKey.keyB);
expect(invokedA, equals(0));
expect(invokedB, equals(1));
await tester.sendKeyUpEvent(LogicalKeyboardKey.keyB);
expect(invokedA, equals(0));
expect(invokedB, equals(1));
}); });
testWidgets('nested CallbackShortcuts stop propagation', (WidgetTester tester) async { testWidgets('nested CallbackShortcuts stop propagation', (WidgetTester tester) async {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment