Add global shortcuts for opening Chat
Allow Ctrl+K and Alt+K to switch to Chat from any destination, while preserving terminal Ctrl+K behavior and focusing the active composer. Register the shortcuts in help and cover matching, navigation, terminal handling, and composer focus with regression tests.
This commit is contained in:
@@ -9,6 +9,7 @@ import 'destination.dart';
|
||||
import 'list_detail.dart';
|
||||
import 'list_pane_widths.dart';
|
||||
import 'shell_bar.dart';
|
||||
import 'shell_chat_shortcut.dart';
|
||||
import 'shell_list_shortcuts.dart';
|
||||
import 'shell_search_shortcut_labels.dart';
|
||||
import 'tab_history.dart';
|
||||
@@ -22,6 +23,7 @@ class AdaptiveShell extends StatefulWidget {
|
||||
this.onDestinationSelected,
|
||||
this.onClosePage,
|
||||
this.onSearch,
|
||||
this.onOpenChat,
|
||||
this.attentionCount = 0,
|
||||
this.runningRunCount,
|
||||
this.listPaneWidthStore = const SharedPreferencesListPaneWidthStore(),
|
||||
@@ -47,6 +49,10 @@ class AdaptiveShell extends StatefulWidget {
|
||||
/// shell tests and compositions without routing keep their tab set only.
|
||||
final VoidCallback? onSearch;
|
||||
|
||||
/// Runs after Ctrl+K or Alt+K has shown the Chat destination (also when it
|
||||
/// was already shown), so the open chat's composer can take the keyboard.
|
||||
final VoidCallback? onOpenChat;
|
||||
|
||||
/// Runs needing the operator today, retained in the tab's accessible label.
|
||||
final int attentionCount;
|
||||
|
||||
@@ -271,9 +277,16 @@ class _AdaptiveShellState extends State<AdaptiveShell> {
|
||||
/// fields. Split-pane navigation belongs to the chat workspace; tab changes
|
||||
/// have no global keyboard chord.
|
||||
bool _handleDestinationKeyEvent(KeyEvent event) {
|
||||
final keyboard = HardwareKeyboard.instance;
|
||||
// Ctrl+K is the terminal's kill-line while the terminal has the keyboard;
|
||||
// Alt+K still opens Chat there.
|
||||
if (shellChatShortcut(event, keyboard) &&
|
||||
(keyboard.isAltPressed || !primaryFocusIsTypingSurface())) {
|
||||
if (event is KeyDownEvent) _openChat();
|
||||
return true;
|
||||
}
|
||||
// A terminal keeps its keys (Escape, Ctrl+/, Ctrl+digits).
|
||||
if (primaryFocusIsTypingSurface()) return false;
|
||||
final keyboard = HardwareKeyboard.instance;
|
||||
if (_selected == AppDestination.search &&
|
||||
event is KeyDownEvent &&
|
||||
event.logicalKey == LogicalKeyboardKey.escape &&
|
||||
@@ -299,6 +312,13 @@ class _AdaptiveShellState extends State<AdaptiveShell> {
|
||||
return _bar.lists.open(AppDestination.chat, listIndex);
|
||||
}
|
||||
|
||||
/// Shows Chat (the tab's own selection, so its open chat returns) and then
|
||||
/// lets the app focus that chat's composer.
|
||||
void _openChat() {
|
||||
if (_selected != AppDestination.chat) _select(AppDestination.chat);
|
||||
widget.onOpenChat?.call();
|
||||
}
|
||||
|
||||
/// A popup or nested viewer owns Escape before the underlying Search tab.
|
||||
bool _searchEscape() {
|
||||
final route = ModalRoute.of(context);
|
||||
|
||||
@@ -0,0 +1,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
|
||||
/// 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;
|
||||
final alt = keyboard.isAltPressed;
|
||||
if (control == alt) 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.
|
||||
List<String> shellChatShortcutLabels([TargetPlatform? platform]) {
|
||||
final target = platform ?? defaultTargetPlatform;
|
||||
final apple = target == TargetPlatform.macOS || target == TargetPlatform.iOS;
|
||||
return [apple ? 'Option+K' : 'Alt+K', 'Ctrl+K'];
|
||||
}
|
||||
Reference in New Issue
Block a user