Terminal round 2: tests for every feature; split find host and tab events

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-26 00:48:11 +00:00
parent 6832ec789e
commit e65354496a
14 changed files with 932 additions and 156 deletions
@@ -0,0 +1,83 @@
import 'package:flutter/material.dart';
import 'package:xterm/xterm.dart';
import 'chat_shell_terminal_actions.dart';
import 'chat_shell_terminal_find.dart';
import 'chat_shell_terminal_find_bar.dart';
import 'chat_shell_terminal_session.dart';
/// A terminal view's find in the scrollback: opening the bar (or focusing
/// it), stepping between matches and scrolling each into view, and closing
/// it back to the terminal.
mixin ChatShellTerminalFindHost<T extends StatefulWidget> on State<T> {
ChatShellTerminalSession get session;
TerminalController get controller;
FocusNode get focus;
ChatShellTerminalActions get actions;
final FocusNode _findFocus = FocusNode(debugLabel: 'terminal-find');
ChatShellTerminalFind? _find;
void disposeFind() {
_find?.dispose();
_findFocus.dispose();
}
/// Opens the find bar, or focuses its field when it is open.
void openFind() {
if (_find != null) {
_findFocus.requestFocus();
return;
}
final scheme = Theme.of(context).colorScheme;
setState(() {
_find = ChatShellTerminalFind(
terminal: session.terminal,
controller: controller,
hit: scheme.tertiary.withValues(alpha: 0.3),
current: scheme.tertiary.withValues(alpha: 0.6),
);
});
}
void _closeFind() {
final find = _find;
if (find == null) return;
setState(() => _find = null);
find.dispose();
focus.requestFocus();
}
void _stepFind(int step) {
final find = _find;
if (find == null) return;
if (step != 0) find.step(step);
final match = find.selected;
if (match != null) actions.reveal(match.begin.y);
}
/// [child] with the find bar over its top end while it is open.
Widget overlayFindBar(Widget child) {
final find = _find;
return Stack(
children: [
child,
if (find != null)
PositionedDirectional(
top: 8,
start: 8,
end: 8,
child: Align(
alignment: AlignmentDirectional.topEnd,
child: ChatShellTerminalFindBar(
find: find,
focus: _findFocus,
onStep: _stepFind,
onClose: _closeFind,
),
),
),
],
);
}
}
@@ -12,6 +12,7 @@ import 'chat_shell_terminal_links.dart';
import 'chat_shell_terminal_session.dart';
import 'chat_shell_terminal_shortcuts.dart';
import 'chat_shell_terminal_theme.dart';
import 'chat_shell_terminal_find_host.dart';
import 'chat_shell_terminal_view_features.dart';
import '../../../ui/input/keyboard_owner.dart';
@@ -57,6 +58,7 @@ class ChatShellTerminalView extends StatefulWidget {
class _ChatShellTerminalViewState extends State<ChatShellTerminalView>
with
SingleTickerProviderStateMixin,
ChatShellTerminalFindHost<ChatShellTerminalView>,
ChatShellTerminalFeatures<ChatShellTerminalView> {
@override
final TerminalController controller = TerminalController();
@@ -132,24 +134,6 @@ class _ChatShellTerminalViewState extends State<ChatShellTerminalView>
return KeyEventResult.handled;
}
/// A finished run's output takes no keys of its own, so the copy keys
/// (and the view's own) are answered here while it has the keyboard.
KeyEventResult _readOnlyKey(FocusNode node, KeyEvent event) {
if (localKey(event)) return KeyEventResult.handled;
if (event is! KeyDownEvent ||
event.logicalKey != LogicalKeyboardKey.keyC &&
event.logicalKey != LogicalKeyboardKey.insert) {
return KeyEventResult.ignored;
}
final keyboard = HardwareKeyboard.instance;
if (!keyboard.isControlPressed && !keyboard.isMetaPressed) {
return KeyEventResult.ignored;
}
return actions.copySelection()
? KeyEventResult.handled
: KeyEventResult.ignored;
}
void _tapped(TapUpDetails details, CellOffset offset) {
final link = chatShellTerminalLinkAt(widget.session.terminal, offset);
if (link != null) openChatLink(context, link.uri);
@@ -215,7 +199,7 @@ class _ChatShellTerminalViewState extends State<ChatShellTerminalView>
onPointerDown: (_) => _outputFocus.requestFocus(),
child: Focus(
focusNode: _outputFocus,
onKeyEvent: _readOnlyKey,
onKeyEvent: readOnlyKey,
child: view,
),
);
@@ -6,34 +6,29 @@ import 'package:xterm/xterm.dart';
import 'chat_shell_terminal_actions.dart';
import 'chat_shell_terminal_context_menu.dart';
import 'chat_shell_terminal_cursor_blink.dart';
import 'chat_shell_terminal_find.dart';
import 'chat_shell_terminal_find_bar.dart';
import 'chat_shell_terminal_find_host.dart';
import 'chat_shell_terminal_link_hover.dart';
import 'chat_shell_terminal_selection_guard.dart';
import 'chat_shell_terminal_session.dart';
import 'chat_shell_terminal_shortcuts.dart';
/// The terminal view's own features around the emulator: find in the
/// scrollback, text size, scrolling keys, the context menu, link hover,
/// the cursor blink, stale-selection clearing and the OSC 52 toast. The
/// view supplies its emulator's parts and calls [initFeatures] and
/// [disposeFeatures].
/// The terminal view's own features around the emulator: text size,
/// scrolling keys, the context menu, link hover, the cursor blink,
/// stale-selection clearing and the OSC 52 toast (find in the scrollback is
/// [ChatShellTerminalFindHost]). The view supplies its emulator's parts and
/// calls [initFeatures] and [disposeFeatures].
mixin ChatShellTerminalFeatures<T extends StatefulWidget>
on State<T>, TickerProvider {
ChatShellTerminalSession get session;
TerminalController get controller;
FocusNode get focus;
on State<T>, TickerProvider, ChatShellTerminalFindHost<T> {
ScrollController get scroll;
GlobalKey<TerminalViewState> get viewKey;
@override
late final ChatShellTerminalActions actions;
late final ChatShellLinkHover hover;
late final ChatShellCursorBlink blink;
late final ChatShellSelectionGuard _guard;
final FocusNode _findFocus = FocusNode(debugLabel: 'terminal-find');
ChatShellTerminalFind? _find;
bool _reduceMotion = false;
bool _hoverQueued = false;
bool _attached = false;
/// The text size the view draws at now, which zooming steps from.
double currentFontSize = 14;
@@ -52,18 +47,23 @@ mixin ChatShellTerminalFeatures<T extends StatefulWidget>
_guard = ChatShellSelectionGuard(_terminal, controller);
scroll.addListener(hover.refresh);
_terminal.addListener(_terminalChanged);
session.events.attach(_copied);
// Counted as shown once drawn: the tab it clears a bell dot on is
// being built now.
SchedulerBinding.instance.addPostFrameCallback((_) {
if (!mounted || _attached) return;
_attached = true;
session.events.attach(_copied);
});
}
void disposeFeatures() {
session.events.detach(_copied);
if (_attached) session.events.detach(_copied);
_terminal.removeListener(_terminalChanged);
scroll.removeListener(hover.refresh);
_guard.dispose();
blink.dispose();
hover.dispose();
_find?.dispose();
_findFocus.dispose();
disposeFind();
}
@override
@@ -136,62 +136,22 @@ mixin ChatShellTerminalFeatures<T extends StatefulWidget>
return true;
}
/// Opens the find bar, or focuses its field when it is open.
void openFind() {
if (_find != null) {
_findFocus.requestFocus();
return;
/// A finished run's output takes no keys of its own, so the copy keys
/// (and the view's own) are answered here while it has the keyboard.
KeyEventResult readOnlyKey(FocusNode node, KeyEvent event) {
if (localKey(event)) return KeyEventResult.handled;
if (event is! KeyDownEvent ||
event.logicalKey != LogicalKeyboardKey.keyC &&
event.logicalKey != LogicalKeyboardKey.insert) {
return KeyEventResult.ignored;
}
final scheme = Theme.of(context).colorScheme;
setState(() {
_find = ChatShellTerminalFind(
terminal: _terminal,
controller: controller,
hit: scheme.tertiary.withValues(alpha: 0.3),
current: scheme.tertiary.withValues(alpha: 0.6),
);
});
}
void _closeFind() {
final find = _find;
if (find == null) return;
setState(() => _find = null);
find.dispose();
focus.requestFocus();
}
void _stepFind(int step) {
final find = _find;
if (find == null) return;
if (step != 0) find.step(step);
final match = find.selected;
if (match != null) actions.reveal(match.begin.y);
}
/// [child] with the find bar over its top end while it is open.
Widget overlayFindBar(Widget child) {
final find = _find;
return Stack(
children: [
child,
if (find != null)
PositionedDirectional(
top: 8,
start: 8,
end: 8,
child: Align(
alignment: AlignmentDirectional.topEnd,
child: ChatShellTerminalFindBar(
find: find,
focus: _findFocus,
onStep: _stepFind,
onClose: _closeFind,
),
),
),
],
);
final keyboard = HardwareKeyboard.instance;
if (!keyboard.isControlPressed && !keyboard.isMetaPressed) {
return KeyEventResult.ignored;
}
return actions.copySelection()
? KeyEventResult.handled
: KeyEventResult.ignored;
}
/// [view] with link hover feedback and the web's browser menu kept off.