From 750071abe111409c1fdcacee05c523c9ee2f640e Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Tue, 10 Nov 2026 00:14:18 +0000 Subject: [PATCH] Keep keys typed in a terminal out of app shortcuts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- app/lib/application/pipelines_app.dart | 6 +-- app/lib/features/chat/chat_screen_keys.dart | 13 ++----- .../chat_composer_paste_focus.dart | 2 + app/lib/features/chat/screen.dart | 1 + .../chat/shell/chat_shell_terminal_view.dart | 38 ++++++++++--------- .../chat/workspace/chat_workspace.dart | 4 +- app/lib/shell/adaptive_shell.dart | 3 ++ app/lib/ui/input/keyboard_owner.dart | 33 ++++++++++++++++ .../terminal/terminal_shortcuts_test.dart | 25 ++++++++++++ 9 files changed, 94 insertions(+), 31 deletions(-) create mode 100644 app/lib/ui/input/keyboard_owner.dart diff --git a/app/lib/application/pipelines_app.dart b/app/lib/application/pipelines_app.dart index 86dee32e..388d7e3d 100644 --- a/app/lib/application/pipelines_app.dart +++ b/app/lib/application/pipelines_app.dart @@ -46,6 +46,7 @@ import 'server_endpoint.dart'; import 'templates_destination.dart'; import 'tab_locations.dart'; import 'tab_locations_store.dart'; +import '../ui/input/keyboard_owner.dart'; /// The application's return-to-foreground policy for the shared socket. /// @@ -482,10 +483,7 @@ class _PipelinesAppState extends State } } - bool get _isEditingText { - final focusContext = FocusManager.instance.primaryFocus?.context; - return focusContext?.findAncestorWidgetOfExactType() != null; - } + bool get _isEditingText => primaryFocusTakesTyping(); /// A replaced socket drops the attention status baseline so the gap cannot /// replay a transition; the Runs list's next read reseeds the rows. diff --git a/app/lib/features/chat/chat_screen_keys.dart b/app/lib/features/chat/chat_screen_keys.dart index 2d4535a9..7a3a51ca 100644 --- a/app/lib/features/chat/chat_screen_keys.dart +++ b/app/lib/features/chat/chat_screen_keys.dart @@ -43,9 +43,7 @@ mixin _ChatScreenKeys on State, _ChatScreenWorkspace { @override bool _toggleRecentChat() { final current = _workspace.activePane?.sessionId; - final previous = _recentSessions - .where((id) => id != current) - .firstOrNull; + final previous = _recentSessions.where((id) => id != current).firstOrNull; if (previous == null) return false; _open(_rowFor(previous) ?? {'id': previous}, focusComposer: true); return true; @@ -87,10 +85,7 @@ mixin _ChatScreenKeys on State, _ChatScreenWorkspace { return true; } - /// Whether an editable text control holds focus, wherever it is. - bool _textFieldHasFocus() { - final context = FocusManager.instance.primaryFocus?.context; - final editable = context?.findAncestorWidgetOfExactType(); - return editable != null && !editable.readOnly; - } + /// Whether something that takes typing holds focus, wherever it is: an + /// editable text field or a terminal. + bool _textFieldHasFocus() => primaryFocusTakesTyping(); } diff --git a/app/lib/features/chat/conversation/chat_composer_paste_focus.dart b/app/lib/features/chat/conversation/chat_composer_paste_focus.dart index b28cb991..ab1128bf 100644 --- a/app/lib/features/chat/conversation/chat_composer_paste_focus.dart +++ b/app/lib/features/chat/conversation/chat_composer_paste_focus.dart @@ -1,4 +1,5 @@ import 'package:flutter/widgets.dart'; +import '../../../ui/input/keyboard_owner.dart'; /// Flutter shares a single web editing element across fields. DOM identity /// alone cannot tell a chat composer from search, rename or another editor. @@ -21,6 +22,7 @@ abstract final class ChatComposerPasteFocus { return false; } if (_inputs.any((focus) => focus.hasFocus)) return true; + if (primaryFocusIsTypingSurface()) return true; final context = primary.context; if (context == null) return false; var editable = context.widget is EditableText; diff --git a/app/lib/features/chat/screen.dart b/app/lib/features/chat/screen.dart index 86658bfc..c7f13579 100644 --- a/app/lib/features/chat/screen.dart +++ b/app/lib/features/chat/screen.dart @@ -46,6 +46,7 @@ import 'ui/sessions/chat_color_palette.dart'; import 'ui/sessions/chat_sessions_body.dart'; import 'ui/sessions/chat_sessions_controller.dart'; import 'ui/sessions/chat_sessions_screen.dart'; +import '../../ui/input/keyboard_owner.dart'; part 'chat_screen_keys.dart'; part 'chat_screen_workspace.dart'; diff --git a/app/lib/features/chat/shell/chat_shell_terminal_view.dart b/app/lib/features/chat/shell/chat_shell_terminal_view.dart index d4e66117..35e48c60 100644 --- a/app/lib/features/chat/shell/chat_shell_terminal_view.dart +++ b/app/lib/features/chat/shell/chat_shell_terminal_view.dart @@ -9,6 +9,7 @@ import 'chat_shell_metrics.dart'; import 'chat_shell_terminal_links.dart'; import 'chat_shell_terminal_session.dart'; import 'chat_shell_terminal_theme.dart'; +import '../../../ui/input/keyboard_owner.dart'; /// Whether the soft keyboard is the main way to type here, so the key /// toolbar offers a text field: the terminal itself takes hardware keys only. @@ -104,23 +105,26 @@ class _ChatShellTerminalViewState extends State { Widget view = Semantics( identifier: 'chat-shell-terminal', label: live ? 'Terminal, type to send input' : 'Terminal output', - child: TerminalView( - session.terminal, - controller: _controller, - focusNode: _focus, - autofocus: widget.autofocus && live, - autoResize: widget.autoResize, - theme: chatShellTerminalTheme(context, widget.cupertino), - textStyle: style, - padding: padding, - readOnly: !live, - // Keys go straight to the process as key events (a focused text - // input would stay closed on the web); touch typing goes through the - // key toolbar's field instead. - hardwareKeyboardOnly: true, - shortcuts: chatShellTerminalShortcuts(), - cursorType: TerminalCursorType.block, - onTapUp: _tapped, + // App-wide shortcuts leave typing in the terminal alone. + child: TypingSurface( + child: TerminalView( + session.terminal, + controller: _controller, + focusNode: _focus, + autofocus: widget.autofocus && live, + autoResize: widget.autoResize, + theme: chatShellTerminalTheme(context, widget.cupertino), + textStyle: style, + padding: padding, + readOnly: !live, + // Keys go straight to the process as key events (a focused text + // input would stay closed on the web); touch typing goes through the + // key toolbar's field instead. + hardwareKeyboardOnly: true, + shortcuts: chatShellTerminalShortcuts(), + cursorType: TerminalCursorType.block, + onTapUp: _tapped, + ), ), ); // The emulator would otherwise inset itself by the window's safe area. diff --git a/app/lib/features/chat/workspace/chat_workspace.dart b/app/lib/features/chat/workspace/chat_workspace.dart index 692ba4e2..a30794af 100644 --- a/app/lib/features/chat/workspace/chat_workspace.dart +++ b/app/lib/features/chat/workspace/chat_workspace.dart @@ -10,6 +10,7 @@ import 'chat_mobile_panes.dart'; import 'chat_pane_switch_motion.dart'; import 'chat_pane_backdrop.dart'; import 'chat_workspace_surface.dart'; +import '../../../ui/input/keyboard_owner.dart'; export 'chat_workspace_controller.dart'; export 'chat_workspace_geometry.dart' show ChatPaneDirection; @@ -122,7 +123,8 @@ class _ChatWorkspaceState extends State if (!widget.shortcutsEnabled || widget.controller.paneCount <= 1 || !_focus.hasFocus || - event is! KeyDownEvent) { + event is! KeyDownEvent || + primaryFocusIsTypingSurface()) { return false; } final keyboard = HardwareKeyboard.instance; diff --git a/app/lib/shell/adaptive_shell.dart b/app/lib/shell/adaptive_shell.dart index 57179e93..dd81fd6f 100644 --- a/app/lib/shell/adaptive_shell.dart +++ b/app/lib/shell/adaptive_shell.dart @@ -13,6 +13,7 @@ import 'list_pane_widths.dart'; import 'shell_bar.dart'; import 'shell_list_shortcuts.dart'; import 'tab_history.dart'; +import '../ui/input/keyboard_owner.dart'; class AdaptiveShell extends StatefulWidget { const AdaptiveShell({ @@ -297,6 +298,8 @@ class _AdaptiveShellState extends State { /// fields. Split-pane navigation belongs to the chat workspace; tab changes /// have no global keyboard chord. bool _handleDestinationKeyEvent(KeyEvent event) { + // A terminal keeps its keys (Escape, Ctrl+/, Ctrl+digits). + if (primaryFocusIsTypingSurface()) return false; final keyboard = HardwareKeyboard.instance; if (_selected == AppDestination.search && event is KeyDownEvent && diff --git a/app/lib/ui/input/keyboard_owner.dart b/app/lib/ui/input/keyboard_owner.dart new file mode 100644 index 00000000..adbe4493 --- /dev/null +++ b/app/lib/ui/input/keyboard_owner.dart @@ -0,0 +1,33 @@ +import 'package:flutter/widgets.dart'; + +/// Marks a widget that takes raw typing without being an [EditableText], such +/// as a terminal emulator. While focus is inside it, app-wide shortcuts that +/// would otherwise act on plain keys (`/` to the composer, Escape, Ctrl+/, +/// Ctrl+1…9, Ctrl+Shift+arrows) leave the keys to it. +class TypingSurface extends StatelessWidget { + const TypingSurface({super.key, required this.child}); + + final Widget child; + + @override + Widget build(BuildContext context) => child; +} + +/// Whether keyboard focus is inside a [TypingSurface]. +bool primaryFocusIsTypingSurface() { + final context = FocusManager.instance.primaryFocus?.context; + if (context == null) return false; + if (context.widget is TypingSurface) return true; + return context.findAncestorWidgetOfExactType() != null; +} + +/// Whether keyboard focus is in something that takes typing: an editable +/// text field or a [TypingSurface]. +bool primaryFocusTakesTyping() { + final context = FocusManager.instance.primaryFocus?.context; + if (context == null) return false; + if (primaryFocusIsTypingSurface()) return true; + if (context.widget case final EditableText field) return !field.readOnly; + final editable = context.findAncestorWidgetOfExactType(); + return editable != null && !editable.readOnly; +} diff --git a/app/test/features/terminal/terminal_shortcuts_test.dart b/app/test/features/terminal/terminal_shortcuts_test.dart index f703ac73..135e5ccf 100644 --- a/app/test/features/terminal/terminal_shortcuts_test.dart +++ b/app/test/features/terminal/terminal_shortcuts_test.dart @@ -5,6 +5,7 @@ import 'package:pipelines_app/features/terminal/terminal_hub.dart'; import 'package:pipelines_app/features/terminal/terminal_layout.dart'; import 'package:pipelines_app/features/terminal/terminal_shortcuts.dart'; import 'package:pipelines_app/features/terminal/terminal_tab.dart'; +import 'package:pipelines_app/ui/input/keyboard_owner.dart'; import 'package:xterm/xterm.dart'; import 'terminal_app.dart'; @@ -239,6 +240,30 @@ void main() { expect(windowsWeb.toggle, ['Alt+T', 'Ctrl+`']); expect(windowsWeb.button, 'Terminal (Alt+T)'); }); + + testWidgets('typing / in the terminal stays in the terminal', (tester) async { + layout.setMode(TerminalMode.panel); + await pumpTerminalApp(tester, hub: hub, layout: layout); + await tester.tap(find.byKey(appField)); + await tester.pump(); + await chord(tester, LogicalKeyboardKey.keyT, [LogicalKeyboardKey.altLeft]); + expect(terminalFocused(key('term_a')), isTrue); + // App-wide shortcuts see a typing surface and leave the key alone. + expect(primaryFocusIsTypingSurface(), isTrue); + expect(primaryFocusTakesTyping(), isTrue); + await tester.sendKeyEvent(LogicalKeyboardKey.slash, character: '/'); + await tester.pump(); + expect(terminalFocused(key('term_a')), isTrue); + expect( + mac.bodies('terminal_input').map((b) => b['data']).join(), + contains('/'), + ); + expect( + find.text('/'), + findsNothing, + reason: 'nothing typed into the field', + ); + }); } /// A keyboard on which only Alt (Option) is held.