2332143e5b
Finished terminals are sized synchronously from their rows at the view's width (the post-frame fit made the transcript re-anchor forever); blank rows xterm.dart keeps when rewrapping are dropped; desktop typing uses key events; the composer leaves Cmd/Ctrl-V to a focused terminal; Ctrl-A reaches the process. The probe fixture gains coloured terminal runs and an interactive login. Notes record behaviour, protocol, security and verification.
156 lines
5.5 KiB
Dart
156 lines
5.5 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';
|
|
|
|
/// 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',
|
|
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.
|
|
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,
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|