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.
This commit is contained in:
2026-11-10 00:14:18 +00:00
parent 339cce0424
commit 750071abe1
9 changed files with 94 additions and 31 deletions
+2 -4
View File
@@ -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<PipelinesApp>
}
}
bool get _isEditingText {
final focusContext = FocusManager.instance.primaryFocus?.context;
return focusContext?.findAncestorWidgetOfExactType<EditableText>() != 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.
+4 -9
View File
@@ -43,9 +43,7 @@ mixin _ChatScreenKeys on State<ChatScreen>, _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<ChatScreen>, _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<EditableText>();
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();
}
@@ -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;
+1
View File
@@ -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';
@@ -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<ChatShellTerminalView> {
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.
@@ -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<ChatWorkspace>
if (!widget.shortcutsEnabled ||
widget.controller.paneCount <= 1 ||
!_focus.hasFocus ||
event is! KeyDownEvent) {
event is! KeyDownEvent ||
primaryFocusIsTypingSurface()) {
return false;
}
final keyboard = HardwareKeyboard.instance;
+3
View File
@@ -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<AdaptiveShell> {
/// 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 &&
+33
View File
@@ -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<TypingSurface>() != 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<EditableText>();
return editable != null && !editable.readOnly;
}
@@ -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.