Files
pipelines/app/lib/features/chat/chat_screen_keys.dart
T
alan 750071abe1 Keep keys typed in a terminal out of app shortcuts
Terminals are marked as typing surfaces. While one has focus, the chat's
"/ jumps to the composer", the Search tab's Escape, Ctrl+/, Ctrl+1…9 and
Ctrl+Shift+arrow pane moves leave the key to the terminal, like they do for
text fields.
2026-11-10 00:14:18 +00:00

92 lines
3.7 KiB
Dart

part of 'screen.dart';
/// How many chats the visit remembers for Ctrl+Tab over a single pane.
const chatRecentSessionLimit = 8;
/// Keys the Chat destination answers from anywhere in it, also while the
/// sidebar or a toolbar owns focus: a bare `/` puts the keyboard in the open
/// chat's composer, and Ctrl+Tab over a single pane returns to the chat shown
/// before it (and back again). Both are handled before the focus tree, like
/// the workspace's pane cycling, and leave other destinations, popups,
/// dialogs and any focused text field alone.
mixin _ChatScreenKeys on State<ChatScreen>, _ChatScreenWorkspace {
void _open(JsonMap session, {bool focusComposer});
/// The chats this visit has shown in the workspace, most recent first; the
/// active one leads it. In memory only: a reopened app starts afresh.
final List<String> _recentSessions = [];
/// Moves the active pane's chat to the front. Called on every workspace
/// change, including a restored layout, so the chat opened before the
/// current one is always the second entry.
@override
void _noteRecentSession() {
final id = _workspace.activePane?.sessionId;
if (id == null || id.isEmpty || _recentSessions.firstOrNull == id) return;
_recentSessions
..remove(id)
..insert(0, id);
if (_recentSessions.length > chatRecentSessionLimit) {
_recentSessions.removeLast();
}
}
/// Drops [id]: a closed chat, or one `/new` moved to a fresh session.
void _forgetRecentSession(String id) => _recentSessions.remove(id);
/// Drops every remembered chat: ids belong to one server.
@override
void _forgetRecentSessions() => _recentSessions.clear();
/// Over a single pane, opens the chat shown before the active one with its
/// composer focused; false when this visit has shown no other chat.
@override
bool _toggleRecentChat() {
final current = _workspace.activePane?.sessionId;
final previous = _recentSessions.where((id) => id != current).firstOrNull;
if (previous == null) return false;
_open(_rowFor(previous) ?? {'id': previous}, focusComposer: true);
return true;
}
/// Whether the shown destination is Chat with no popup, dialog or other
/// route over it, so a key pressed now is meant for the open chat.
@override
bool _chatOwnsKeyboard() {
final destination = ShellBarScope.maybeOf(context)?.registry.focus;
if (destination != null && destination != AppDestination.chat) return false;
final focusContext = FocusManager.instance.primaryFocus?.context;
final route = focusContext == null
? ModalRoute.of(context)
: ModalRoute.of(focusContext);
return route is! PopupRoute && (route == null || route.isCurrent);
}
/// A bare `/` typed outside any text field focuses the active chat's
/// composer. The composer takes focus after this frame (see
/// [ChatComposerFocusRequests]), once the host has finished delivering the
/// keystroke, so the slash itself is not typed into it. Inside a text field
/// (the composer, a dialog's field) `/` stays an ordinary character.
bool _slashFocusKey(KeyEvent event) {
final keyboard = HardwareKeyboard.instance;
if (event is! KeyDownEvent ||
event.character != '/' ||
keyboard.isControlPressed ||
keyboard.isMetaPressed ||
keyboard.isAltPressed ||
!_workspaceVisible) {
return false;
}
final id = _workspace.activePane?.sessionId;
if (id == null || _textFieldHasFocus() || !_chatOwnsKeyboard()) {
return false;
}
_composerFocus.request(id);
return true;
}
/// Whether something that takes typing holds focus, wherever it is: an
/// editable text field or a terminal.
bool _textFieldHasFocus() => primaryFocusTakesTyping();
}