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.
@@ -188,6 +188,7 @@ class _SettingsFontSectionState extends State<SettingsFontSection> {
/// The platform's own interface family (Roboto), independent of the font the
/// theme currently applies.
String? _systemFamily(BuildContext context) => Typography.material2021(
platform: Theme.of(context).platform,
).black.bodyLarge?.fontFamily;
String? _systemFamily(BuildContext context) =>
Typography.material2021(
platform: Theme.of(context).platform,
).black.bodyLarge?.fontFamily;
+12 -31
View File
@@ -10,6 +10,9 @@ import '../chat/shell/chat_shell_terminals.dart';
import 'terminal_info.dart';
import 'terminal_layout.dart';
import 'terminal_stream.dart';
import 'terminal_tab_events.dart';
export 'terminal_tab_events.dart';
/// One open machine terminal in the app: its record, the output held for
/// redraws, and the emulator session the panel, window and page draw (the
@@ -17,7 +20,7 @@ import 'terminal_stream.dart';
///
/// It outlives every view: hiding the panel keeps it, and reconnecting
/// resumes its output from the last byte it holds.
class TerminalTab extends ChangeNotifier {
class TerminalTab extends ChangeNotifier with TerminalTabEvents {
TerminalTab({
required this.machineId,
required this.machineName,
@@ -35,15 +38,14 @@ class TerminalTab extends ChangeNotifier {
);
session.terminal.onTitleChange = _titled;
session.onDrawn = _drawn;
session.events.bells.addListener(_rang);
session.events.viewers.addListener(_viewed);
session.events.workingDirectory.addListener(_moved);
followEvents();
}
final String machineId;
String machineName;
ApiGateway _gateway;
TerminalInfo _info;
@override
late final ChatShellTerminalSession session;
final TerminalStream _stream = TerminalStream();
ChatShellInputPipe? _pipe;
@@ -66,10 +68,6 @@ class TerminalTab extends ChangeNotifier {
/// The last refusal of input, a resize or a read, in the server's words.
String? error;
/// Whether the bell rang while no view showed this terminal: its tab
/// shows a dot until it is shown again.
bool bellUnseen = false;
String get key => terminalKey(machineId, _info.id);
TerminalInfo get info => _info;
ApiGateway get gateway => _gateway;
@@ -85,13 +83,8 @@ class TerminalTab extends ChangeNotifier {
/// The shell's current folder as it reported it (OSC 7), else the one it
/// started in.
String get cwd => _reportedCwd ?? _info.cwd;
String? _reportedCwd;
void _moved() {
_reportedCwd = session.events.workingDirectory.value;
_notify();
}
@override
String get cwd => super.cwd ?? _info.cwd;
ChatShellRun _run() => ChatShellRun(
id: _info.id,
@@ -238,19 +231,6 @@ class TerminalTab extends ChangeNotifier {
);
}
void _rang() {
if (bellUnseen || !session.layout.bell) return;
if (session.events.viewers.value > 0) return;
bellUnseen = true;
_notify();
}
void _viewed() {
if (!bellUnseen || session.events.viewers.value == 0) return;
bellUnseen = false;
_notify();
}
void _titled(String title) {
if (title == _oscTitle) return;
_oscTitle = title.trim();
@@ -317,6 +297,9 @@ class TerminalTab extends ChangeNotifier {
_notify();
}
@override
void notifyEvent() => _notify();
void _notify() {
if (!_disposed) notifyListeners();
}
@@ -328,9 +311,7 @@ class TerminalTab extends ChangeNotifier {
_pipe = null;
session.terminal.onTitleChange = null;
session.onDrawn = null;
session.events.bells.removeListener(_rang);
session.events.viewers.removeListener(_viewed);
session.events.workingDirectory.removeListener(_moved);
unfollowEvents();
session.dispose();
super.dispose();
}
@@ -0,0 +1,52 @@
import 'package:flutter/foundation.dart';
import '../chat/shell/chat_shell_terminal_session.dart';
/// What a terminal tab shows of its program's reports: a dot for a bell
/// rung while no view showed the terminal (until it is shown again), and
/// the folder it last reported (OSC 7) for an untitled tab and its tooltip.
mixin TerminalTabEvents on ChangeNotifier {
ChatShellTerminalSession get session;
/// Notifies the tab's listeners unless it is disposed.
void notifyEvent();
/// Whether the bell rang while no view showed this terminal.
bool bellUnseen = false;
/// The folder the shell last reported, if it did.
String? get cwd => _reportedCwd;
String? _reportedCwd;
void followEvents() {
final events = session.events;
events.bells.addListener(_rang);
events.viewers.addListener(_viewed);
events.workingDirectory.addListener(_moved);
}
void unfollowEvents() {
final events = session.events;
events.bells.removeListener(_rang);
events.viewers.removeListener(_viewed);
events.workingDirectory.removeListener(_moved);
}
void _rang() {
if (bellUnseen || !session.layout.bell) return;
if (session.events.viewers.value > 0) return;
bellUnseen = true;
notifyEvent();
}
void _viewed() {
if (!bellUnseen || session.events.viewers.value == 0) return;
bellUnseen = false;
notifyEvent();
}
void _moved() {
_reportedCwd = session.events.workingDirectory.value;
notifyEvent();
}
}