diff --git a/app/lib/features/settings/keyboard_shortcuts_registry.dart b/app/lib/features/settings/keyboard_shortcuts_registry.dart index 7e0aa75a..6dc477c8 100644 --- a/app/lib/features/settings/keyboard_shortcuts_registry.dart +++ b/app/lib/features/settings/keyboard_shortcuts_registry.dart @@ -97,7 +97,7 @@ class KeyboardShortcutRegistry { keys: shellChatShortcutLabels(platform).join(', '), caveat: web ? 'Shows the open chat and focuses its message input. The browser may reserve Ctrl+K.' - : 'Shows the open chat and focuses its message input. In the terminal Ctrl+K stays kill-line; Alt+K still opens Chat.', + : 'Shows the open chat and focuses its message input. In the terminal Ctrl+K stays kill-line; ${_apple ? 'Option+K and Cmd+K still open' : 'Alt+K still opens'} Chat.', ), const KeyboardShortcutEntry( group: KeyboardShortcutGroup.navigation, diff --git a/app/lib/shell/adaptive_shell.dart b/app/lib/shell/adaptive_shell.dart index b3ed85c2..24e48822 100644 --- a/app/lib/shell/adaptive_shell.dart +++ b/app/lib/shell/adaptive_shell.dart @@ -416,9 +416,11 @@ class _AdaptiveShellState extends State { return true; } // Ctrl+K is the terminal's kill-line while the terminal has the keyboard; - // Alt+K still opens Chat there. + // Alt+K and Cmd+K still open Chat there. if (shellChatShortcut(event, keyboard) && - (keyboard.isAltPressed || !primaryFocusIsTypingSurface())) { + (keyboard.isAltPressed || + keyboard.isMetaPressed || + !primaryFocusIsTypingSurface())) { if (event is KeyDownEvent) _openChat(); return true; } diff --git a/app/lib/shell/shell_chat_shortcut.dart b/app/lib/shell/shell_chat_shortcut.dart index 52cb1de4..4dabcee9 100644 --- a/app/lib/shell/shell_chat_shortcut.dart +++ b/app/lib/shell/shell_chat_shortcut.dart @@ -2,28 +2,29 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; /// Whether [event] opens the Chat destination from any destination: Ctrl+K, -/// or Alt+K (Option+K on Apple platforms). Alt+K is recognised by key position -/// too, because Option+K yields a dead-key symbol on a Mac. Shift, Cmd and -/// chords with both Ctrl and Alt are not the shortcut. Repeats of a held chord +/// Cmd+K (Meta+K), or Alt+K (Option+K on Apple platforms). Alt+K is +/// recognised by key position too, because Option+K yields a dead-key symbol +/// on a Mac. Shift and chords of more than one of Ctrl, Cmd and Alt are not +/// the shortcut. Repeats of a held chord /// match as well, so the caller can swallow them; only its first press acts. bool shellChatShortcut(KeyEvent event, HardwareKeyboard keyboard) { - if (event is KeyUpEvent || - keyboard.isShiftPressed || - keyboard.isMetaPressed) { - return false; - } - final control = keyboard.isControlPressed; + if (event is KeyUpEvent || keyboard.isShiftPressed) return false; final alt = keyboard.isAltPressed; - if (control == alt) return false; + final held = [ + keyboard.isControlPressed, + keyboard.isMetaPressed, + alt, + ].where((pressed) => pressed).length; + if (held != 1) return false; if (event.logicalKey == LogicalKeyboardKey.keyK) return true; return alt && event.physicalKey == PhysicalKeyboardKey.keyK; } /// The chords that open Chat as they read on this platform, for the help /// page. Alt (Option on Apple platforms) leads, as it does for the floating -/// chats; Ctrl+K is the other chord everywhere. +/// chats; Cmd+K follows on Apple platforms and Ctrl+K works everywhere. List shellChatShortcutLabels([TargetPlatform? platform]) { final target = platform ?? defaultTargetPlatform; final apple = target == TargetPlatform.macOS || target == TargetPlatform.iOS; - return [apple ? 'Option+K' : 'Alt+K', 'Ctrl+K']; + return apple ? ['Option+K', 'Cmd+K', 'Ctrl+K'] : ['Alt+K', 'Ctrl+K']; } diff --git a/app/test/features/settings/keyboard_shortcuts_help_test.dart b/app/test/features/settings/keyboard_shortcuts_help_test.dart index 4cfc6412..e7a0f6fb 100644 --- a/app/test/features/settings/keyboard_shortcuts_help_test.dart +++ b/app/test/features/settings/keyboard_shortcuts_help_test.dart @@ -33,7 +33,7 @@ void main() { web: false, ).entries.firstWhere((entry) => entry.action == 'Open Chat'); - expect(openChat(TargetPlatform.macOS).keys, 'Option+K, Ctrl+K'); + expect(openChat(TargetPlatform.macOS).keys, 'Option+K, Cmd+K, Ctrl+K'); final linux = openChat(TargetPlatform.linux); expect(linux.keys, 'Alt+K, Ctrl+K'); expect(linux.caveat, contains('kill-line')); diff --git a/app/test/shell/shell_chat_shortcut_test.dart b/app/test/shell/shell_chat_shortcut_test.dart index 69a873d4..0130efbd 100644 --- a/app/test/shell/shell_chat_shortcut_test.dart +++ b/app/test/shell/shell_chat_shortcut_test.dart @@ -13,37 +13,48 @@ const _k = KeyDownEvent( ); void main() { - test('Ctrl+K and Alt+K open Chat; other modifiers and plain K do not', () { - bool chord(Set held, [KeyEvent event = _k]) => - shellChatShortcut(event, _FakeKeyboard(held)); - expect(chord({}), isFalse); - expect(chord({LogicalKeyboardKey.controlLeft}), isTrue); - expect(chord({LogicalKeyboardKey.altLeft}), isTrue); - expect( - chord({LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.altLeft}), - isFalse, - ); - expect( - chord({LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.shiftLeft}), - isFalse, - ); - expect(chord({LogicalKeyboardKey.metaLeft}), isFalse); - expect( - chord({LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.metaLeft}), - isFalse, - ); - expect( - shellChatShortcut( - const KeyUpEvent( - physicalKey: PhysicalKeyboardKey.keyK, - logicalKey: LogicalKeyboardKey.keyK, - timeStamp: Duration.zero, + test( + 'Ctrl+K, Cmd+K and Alt+K open Chat; other modifiers and plain K do not', + () { + bool chord(Set held, [KeyEvent event = _k]) => + shellChatShortcut(event, _FakeKeyboard(held)); + expect(chord({}), isFalse); + expect(chord({LogicalKeyboardKey.controlLeft}), isTrue); + expect(chord({LogicalKeyboardKey.altLeft}), isTrue); + expect( + chord({LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.altLeft}), + isFalse, + ); + expect( + chord({LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.shiftLeft}), + isFalse, + ); + expect(chord({LogicalKeyboardKey.metaLeft}), isTrue); + expect( + chord({LogicalKeyboardKey.metaLeft, LogicalKeyboardKey.shiftLeft}), + isFalse, + ); + expect( + chord({LogicalKeyboardKey.metaLeft, LogicalKeyboardKey.altLeft}), + isFalse, + ); + expect( + chord({LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.metaLeft}), + isFalse, + ); + expect( + shellChatShortcut( + const KeyUpEvent( + physicalKey: PhysicalKeyboardKey.keyK, + logicalKey: LogicalKeyboardKey.keyK, + timeStamp: Duration.zero, + ), + _FakeKeyboard({LogicalKeyboardKey.controlLeft}), ), - _FakeKeyboard({LogicalKeyboardKey.controlLeft}), - ), - isFalse, - ); - }); + isFalse, + ); + }, + ); test('Option+K matches by key position though it types a symbol', () { // On a Mac, Option+K produces ˚: the logical key is not K. @@ -65,11 +76,12 @@ void main() { expect(shellChatShortcutLabels(TargetPlatform.linux), ['Alt+K', 'Ctrl+K']); expect(shellChatShortcutLabels(TargetPlatform.macOS), [ 'Option+K', + 'Cmd+K', 'Ctrl+K', ]); }); - testWidgets('Ctrl+K and Alt+K show Chat from another destination', ( + testWidgets('Ctrl+K, Alt+K and Cmd+K show Chat from another destination', ( tester, ) async { var opened = 0; @@ -104,6 +116,12 @@ void main() { expect(find.text('chat body'), isOnstage); // Already in Chat, the chord still hands the keyboard to the composer. expect(opened, 2); + + await tester.sendKeyDownEvent(LogicalKeyboardKey.metaLeft); + await tester.sendKeyEvent(LogicalKeyboardKey.keyK); + await tester.sendKeyUpEvent(LogicalKeyboardKey.metaLeft); + await tester.pump(); + expect(opened, 3); expect(tester.takeException(), isNull); }); @@ -130,7 +148,7 @@ void main() { }); testWidgets( - 'terminal keeps Ctrl+K for kill-line but Alt+K still opens Chat', + 'terminal keeps Ctrl+K for kill-line but Alt+K and Cmd+K still open Chat', (tester) async { final terminal = FocusNode(); addTearDown(terminal.dispose); @@ -168,6 +186,12 @@ void main() { await tester.pump(); expect(find.text('chat body'), isOnstage); expect(opened, 1); + + await tester.sendKeyDownEvent(LogicalKeyboardKey.metaLeft); + await tester.sendKeyEvent(LogicalKeyboardKey.keyK); + await tester.sendKeyUpEvent(LogicalKeyboardKey.metaLeft); + await tester.pump(); + expect(opened, 2); expect(tester.takeException(), isNull); }, );