Fit chat terminals to their output, keep pastes and keys in the terminal
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.
This commit is contained in:
@@ -1,19 +1,48 @@
|
||||
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 terminal
|
||||
/// takes typing from the key toolbar's field rather than its own hidden one.
|
||||
/// 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).
|
||||
@@ -44,10 +73,18 @@ class ChatShellTerminalView extends StatefulWidget {
|
||||
class _ChatShellTerminalViewState extends State<ChatShellTerminalView> {
|
||||
final TerminalController _controller = TerminalController();
|
||||
final FocusNode _focus = FocusNode(debugLabel: 'chat-shell-terminal');
|
||||
int? _fitRows;
|
||||
|
||||
@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();
|
||||
@@ -58,14 +95,6 @@ class _ChatShellTerminalViewState extends State<ChatShellTerminalView> {
|
||||
if (uri != null) openChatLink(context, uri);
|
||||
}
|
||||
|
||||
/// A finished run's box shrinks to the rows its output uses.
|
||||
void _fit() {
|
||||
if (!mounted || widget.fill || widget.session.live) return;
|
||||
final used = widget.session.terminal.buffer.absoluteCursorY + 1;
|
||||
final rows = used.clamp(1, kChatShellOutputCollapsedLines);
|
||||
if (rows != _fitRows) setState(() => _fitRows = rows);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final session = widget.session;
|
||||
@@ -85,8 +114,11 @@ class _ChatShellTerminalViewState extends State<ChatShellTerminalView> {
|
||||
textStyle: style,
|
||||
padding: padding,
|
||||
readOnly: !live,
|
||||
hardwareKeyboardOnly: !live || chatShellTouchFirst(),
|
||||
keyboardType: TextInputType.visiblePassword,
|
||||
// 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,
|
||||
),
|
||||
@@ -102,12 +134,22 @@ class _ChatShellTerminalViewState extends State<ChatShellTerminalView> {
|
||||
);
|
||||
view = ClipRRect(borderRadius: BorderRadius.circular(8), child: view);
|
||||
if (widget.fill) return view;
|
||||
if (!live) WidgetsBinding.instance.addPostFrameCallback((_) => _fit());
|
||||
final rows = live
|
||||
? kChatShellOutputCollapsedLines
|
||||
: (_fitRows ?? kChatShellOutputCollapsedLines);
|
||||
final line = chatShellTerminalLineHeight(context, style);
|
||||
// A pixel of slack keeps rounding from losing the last row.
|
||||
return SizedBox(height: rows * line + padding.vertical + 1, child: 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,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user