Files
pipelines/app/lib/features/chat/shell/chat_shell_terminal_view.dart
T
alan b44938d028 Open the on-screen keyboard for terminals on phones
On Android and iOS a tap on a terminal (a chat command card or the terminal
panel) now opens the soft keyboard and typing goes to the process, with no
autocorrect. Desktop keeps raw key events. Desktop keyboard tests now run
as macOS, since tests default to Android.
2026-11-10 00:19:46 +00:00

163 lines
5.9 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:xterm/xterm.dart';
import '../conversation/chat_composer_paste_focus.dart';
import '../ui/transcript/chat_link_launcher.dart';
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.
bool chatShellTouchFirst() =>
defaultTargetPlatform == TargetPlatform.android ||
defaultTargetPlatform == TargetPlatform.iOS;
/// The terminal's own key shortcuts. Cmd-C, Cmd-V and Cmd-A on Apple
/// platforms; elsewhere Ctrl-V (and the terminal convention Ctrl-Shift-V)
/// paste and Ctrl-Shift-C copies, so every other Ctrl key, Ctrl-A and Ctrl-C
/// included, reaches the process.
Map<ShortcutActivator, Intent> chatShellTerminalShortcuts() {
const paste = PasteTextIntent(SelectionChangedCause.keyboard);
final apple =
defaultTargetPlatform == TargetPlatform.macOS ||
defaultTargetPlatform == TargetPlatform.iOS;
if (apple) {
return const {
SingleActivator(LogicalKeyboardKey.keyC, meta: true):
CopySelectionTextIntent.copy,
SingleActivator(LogicalKeyboardKey.keyV, meta: true): paste,
SingleActivator(LogicalKeyboardKey.keyA, meta: true): SelectAllTextIntent(
SelectionChangedCause.keyboard,
),
};
}
return const {
SingleActivator(LogicalKeyboardKey.keyC, control: true, shift: true):
CopySelectionTextIntent.copy,
SingleActivator(LogicalKeyboardKey.keyV, control: true): paste,
SingleActivator(LogicalKeyboardKey.keyV, control: true, shift: true): paste,
};
}
/// One run's terminal emulator view. In a card it is bounded to
/// [kChatShellOutputCollapsedLines] rows (a finished run shrinks to its
/// output); with [fill] it takes the space its parent gives (full screen).
/// It sizes the terminal to its own layout; a running process is resized to
/// match. Tapping a printed web link opens it in the browser.
class ChatShellTerminalView extends StatefulWidget {
const ChatShellTerminalView({
super.key,
required this.session,
required this.cupertino,
this.fill = false,
this.autofocus = false,
this.autoResize = true,
});
final ChatShellTerminalSession session;
final bool cupertino;
final bool fill;
final bool autofocus;
/// False while another view of the same terminal (full screen) sizes it.
final bool autoResize;
@override
State<ChatShellTerminalView> createState() => _ChatShellTerminalViewState();
}
class _ChatShellTerminalViewState extends State<ChatShellTerminalView> {
final TerminalController _controller = TerminalController();
final FocusNode _focus = FocusNode(debugLabel: 'chat-shell-terminal');
@override
void initState() {
super.initState();
// A paste while the terminal has focus goes to the process, not to the
// chat composer.
ChatComposerPasteFocus.registerInput(_focus);
}
@override
void dispose() {
ChatComposerPasteFocus.unregisterInput(_focus);
_controller.dispose();
_focus.dispose();
super.dispose();
}
void _tapped(TapUpDetails details, CellOffset offset) {
final uri = chatShellLinkAt(widget.session.terminal.buffer, offset);
if (uri != null) openChatLink(context, uri);
}
@override
Widget build(BuildContext context) {
final session = widget.session;
final live = session.live;
final style = chatShellTerminalStyle(context, widget.cupertino);
const padding = EdgeInsets.all(8);
Widget view = Semantics(
identifier: 'chat-shell-terminal',
label: live ? 'Terminal, type to send input' : 'Terminal output',
// 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,
// Desktop keys go straight to the process as key events (a focused
// text input stays closed in desktop browsers). On phones and tablets
// a tap opens the on-screen keyboard and typing goes to the terminal;
// the key toolbar's field stays as an alternative.
hardwareKeyboardOnly: !chatShellTouchFirst(),
// No autocorrect or suggestions rewriting what is sent to a shell.
keyboardType: TextInputType.visiblePassword,
shortcuts: chatShellTerminalShortcuts(),
cursorType: TerminalCursorType.block,
onTapUp: _tapped,
),
),
);
// The emulator would otherwise inset itself by the window's safe area.
view = MediaQuery.removePadding(
context: context,
removeTop: true,
removeBottom: true,
removeLeft: true,
removeRight: true,
child: view,
);
view = ClipRRect(borderRadius: BorderRadius.circular(8), child: view);
if (widget.fill) return view;
final cell = chatShellTerminalCellSize(context, style);
return LayoutBuilder(
builder: (context, constraints) {
// A finished run's box fits the rows its output takes at this width.
final cols = ((constraints.maxWidth - padding.horizontal) / cell.width)
.floor()
.clamp(1, 1000);
final rows = live
? kChatShellOutputCollapsedLines
: session.rowsAt(cols).clamp(1, kChatShellOutputCollapsedLines);
// A pixel of slack keeps rounding from losing the last row.
return SizedBox(
height: rows * cell.height + padding.vertical + 1,
child: view,
);
},
);
}
}