Terminal: text input for IMEs, Capture browser keys, touch key row

- Desktop and web terminals use the text-input path too, so IMEs, dead
  keys, the emoji picker and dictation work; the key handler still
  encodes special keys and chords first (handled keys are never typed
  again), composing text is drawn at the cursor and sent on commit, the
  platform editable sits at the cursor, and a closed input reopens on
  the next key. The editable terminal is a semantics text field so web
  typing also works with semantics on.
- Web: "Capture browser keys" (terminal menu and right-click menu) goes
  full screen and calls navigator.keyboard.lock(); hold Esc to leave.
  No-op where unsupported; cheatsheet and menu note give alternatives.
- Touch: scrollable key row with sticky Ctrl/Alt (tap once, double-tap
  lock), Esc, Tab, arrows, Home/End, PgUp/PgDn and symbols through the
  hardware encoder; soft keyboard types per key (delete detection,
  Return as Enter); the line field moves behind the row's Line button.
- Capture kit: compose=, --semantics false, --user-agent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-26 02:43:57 +00:00
parent e526150b1e
commit c5af000453
36 changed files with 1730 additions and 173 deletions
@@ -5,6 +5,7 @@ import 'package:pipelines_app/features/chat/shell/chat_shell_terminal_search.dar
import 'package:xterm/xterm.dart';
import 'terminal_view_harness.dart';
import 'terminal_typing.dart';
const _field = ValueKey('terminal-find-field');
@@ -108,7 +109,7 @@ void main() {
expect(find.byKey(_field), findsNothing);
expect(harness.controller(tester).highlights, isEmpty);
expect(harness.view(tester).focusNode!.hasFocus, isTrue);
await tester.sendKeyEvent(LogicalKeyboardKey.keyX);
await typeKey(tester, LogicalKeyboardKey.keyX);
await tester.pump();
expect(harness.typed, 'x');
}, variant: desktops);
@@ -0,0 +1,203 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/shell/chat_shell_terminal_keys.dart';
import 'package:pipelines_app/features/chat/shell/chat_shell_terminal_sticky.dart';
import 'package:pipelines_app/features/chat/shell/chat_shell_terminal_view.dart';
import 'terminal_typing.dart';
import 'terminal_view_harness.dart';
final _touch = TargetPlatformVariant({
TargetPlatform.android,
TargetPlatform.iOS,
});
/// A phone-sized live terminal with the touch key row under it.
Future<TerminalViewHarness> _pumpRow(WidgetTester tester) async {
final harness = TerminalViewHarness();
tester.view.physicalSize = const Size(400, 700);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
addTearDown(harness.session.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Column(
children: [
Expanded(
child: ChatShellTerminalView(
session: harness.session,
fill: true,
autofocus: true,
),
),
ChatShellTerminalKeys(session: harness.session, touch: true),
],
),
),
),
);
await tester.pump();
return harness;
}
Widget _key(WidgetTester tester, String id) =>
tester.widget(find.byKey(Key('chat-shell-key-$id')));
void main() {
group('sticky modifiers', () {
var now = DateTime(2026);
ChatShellStickyModifiers sticky() =>
ChatShellStickyModifiers(clock: () => now);
test('a tap holds one for one key; a quick second tap locks it', () {
final mods = sticky();
mods.tapCtrl();
expect(mods.ctrl, ChatShellStickyState.once);
expect(mods.take(), (ctrl: true, alt: false));
expect(mods.ctrl, ChatShellStickyState.off);
expect(mods.take(), (ctrl: false, alt: false));
mods.tapAlt();
now = now.add(const Duration(milliseconds: 200));
mods.tapAlt();
expect(mods.alt, ChatShellStickyState.locked);
expect(mods.take(), (ctrl: false, alt: true));
expect(mods.take(), (ctrl: false, alt: true));
mods.tapAlt();
expect(mods.alt, ChatShellStickyState.off);
});
test('a slow second tap releases a held one instead of locking it', () {
final mods = sticky();
mods.tapCtrl();
now = now.add(const Duration(seconds: 2));
mods.tapCtrl();
expect(mods.ctrl, ChatShellStickyState.off);
});
test('held text: Ctrl maps the first character, Alt prefixes ESC', () {
expect(chatShellStickyText('c', ctrl: true, alt: false), '\x03');
expect(chatShellStickyText('b', ctrl: false, alt: true), '\x1bb');
expect(chatShellStickyText('cd', ctrl: true, alt: true), '\x1b\x03d');
expect(chatShellStickyText('é', ctrl: true, alt: false), 'é');
});
});
testWidgets('the row sends the keys a phone lacks, as hardware keys do', (
tester,
) async {
final harness = await _pumpRow(tester);
for (final id in [
'esc',
'tab',
'left',
'up',
'home',
'end',
'pgup',
'pgdn',
'sym-124',
'sym-126',
'sym-45',
'sym-47',
'sym-92',
'enter',
]) {
await tapKey(tester, id);
}
expect(
harness.typed,
'\x1b\t\x1b[D\x1b[A\x1b[H\x1b[F\x1b[5~\x1b[6~|~-/\\\r',
);
// Application cursor keys (DECCKM) from the program's own mode.
harness.sent.clear();
await harness.write(tester, '\x1b[?1h');
await tapKey(tester, 'up');
await tapKey(tester, 'home');
expect(harness.typed, '\x1bOA\x1bOH');
}, variant: _touch);
testWidgets('a tapped Ctrl or Alt applies to the next key or typed '
'character once; a double tap locks it until tapped again', (
tester,
) async {
final harness = await _pumpRow(tester);
final sticky = harness.session.modifiers;
expect(_key(tester, 'ctrl'), isA<OutlinedButton>());
await tapKey(tester, 'ctrl');
expect(sticky.ctrl, ChatShellStickyState.once);
expect(_key(tester, 'ctrl'), isA<FilledButton>());
// The soft keyboard's next character takes it.
await commitText(tester, 'c');
expect(harness.typed, '\x03');
expect(sticky.ctrl, ChatShellStickyState.off);
await commitText(tester, 'c');
expect(harness.typed, '\x03c');
// A row key takes it too, with xterm's modifier parameter.
harness.sent.clear();
await tapKey(tester, 'ctrl');
await tapKey(tester, 'right');
expect(harness.typed, '\x1b[1;5C');
await tapKey(tester, 'ctrl');
await tapKey(tester, 'sym-47');
expect(harness.typed, '\x1b[1;5C\x1f');
// Double tap: locked for every key until released.
harness.sent.clear();
await tester.ensureVisible(find.byKey(const Key('chat-shell-key-alt')));
await tester.pump();
await tester.tap(find.byKey(const Key('chat-shell-key-alt')));
await tester.pump(const Duration(milliseconds: 100));
await tester.tap(find.byKey(const Key('chat-shell-key-alt')));
await tester.pump();
expect(sticky.alt, ChatShellStickyState.locked);
expect(
find.descendant(
of: find.byKey(const Key('chat-shell-key-alt')),
matching: find.byIcon(Icons.lock),
),
findsOneWidget,
);
await commitText(tester, 'b');
await commitText(tester, 'f');
await tapKey(tester, 'left');
expect(harness.typed, '\x1bb\x1bf\x1b[1;3D');
expect(sticky.alt, ChatShellStickyState.locked);
await tapKey(tester, 'alt');
expect(sticky.alt, ChatShellStickyState.off);
await commitText(tester, 'b');
expect(harness.typed, '\x1bb\x1bf\x1b[1;3Db');
}, variant: _touch);
testWidgets('a held modifier applies to a soft keyboard key event', (
tester,
) async {
final harness = await _pumpRow(tester);
await tapKey(tester, 'ctrl');
await tester.sendKeyEvent(LogicalKeyboardKey.backspace);
expect(harness.typed, '\b');
await tester.sendKeyEvent(LogicalKeyboardKey.backspace);
expect(harness.typed, '\b\x7f');
}, variant: _touch);
testWidgets(
'Line opens a field for a whole line, sent with Enter',
(tester) async {
final harness = await _pumpRow(tester);
final field = find.byKey(const Key('chat-shell-terminal-field'));
expect(field, findsNothing);
await tapKey(tester, 'line');
expect(field, findsOneWidget);
await tester.enterText(field, 'echo hi');
await tester.testTextInput.receiveAction(TextInputAction.send);
await tester.pump();
expect(harness.typed, 'echo hi\r');
await tapKey(tester, 'line');
expect(field, findsNothing);
},
variant: TargetPlatformVariant.only(TargetPlatform.android),
);
}
@@ -5,6 +5,7 @@ import 'package:pipelines_app/features/chat/shell/chat_shell_runs_controller.dar
import 'package:xterm/xterm.dart';
import 'chat_shell_fixtures.dart';
import 'terminal_typing.dart';
void main() {
late ShellGateway gateway;
@@ -43,8 +44,8 @@ void main() {
expect(find.byKey(const Key('chat-shell-terminal-field')), findsNothing);
await tester.tap(find.byType(TerminalView));
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.keyG);
await tester.sendKeyEvent(LogicalKeyboardKey.digit7);
await typeKey(tester, LogicalKeyboardKey.keyG);
await typeKey(tester, LogicalKeyboardKey.digit7);
await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp);
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.keyC);
@@ -156,7 +157,7 @@ void main() {
await press(tester, LogicalKeyboardKey.arrowLeft, modifiers: alt);
await press(tester, LogicalKeyboardKey.arrowRight, modifiers: ctrl);
await press(tester, LogicalKeyboardKey.keyT, modifiers: ctrl);
await press(tester, LogicalKeyboardKey.keyX);
await typeKey(tester, LogicalKeyboardKey.keyX);
expect(
typed(),
'\x1bb\x1bf\x1b.\x1bB\x1b\x7f\x1f\x1d'
@@ -289,10 +290,8 @@ void main() {
),
);
await openRunning(tester);
await tester.tap(find.byKey(const Key('chat-shell-key-paste')));
await tester.pump();
await tester.tap(find.byKey(const Key('chat-shell-key-enter')));
await tester.pump();
await tapKey(tester, 'paste');
await tapKey(tester, 'enter');
expect(typed(), 'gho_pasted-token\r');
});
@@ -301,13 +300,16 @@ void main() {
) async {
await openRunning(tester, output: 'Password: ');
for (final key in ['ctrl-c', 'tab', 'esc', 'left', 'down', 'right']) {
await tester.tap(find.byKey(Key('chat-shell-key-$key')));
await tester.pump();
await tapKey(tester, key);
}
expect(typed(), '\x03\t\x1b\x1b[D\x1b[B\x1b[C');
gateway.calls.clear();
// Typing goes to the terminal key by key; Line opens a field for a
// whole line.
final field = find.byKey(const Key('chat-shell-terminal-field'));
expect(field, findsNothing);
await tapKey(tester, 'line');
expect(field, findsOneWidget);
await tester.tap(find.byKey(const Key('chat-shell-terminal-hide')));
await tester.pump();
@@ -331,8 +333,7 @@ void main() {
expect(tester.testTextInput.isVisible, isTrue);
expect(tester.testTextInput.hasAnyClients, isTrue);
gateway.calls.clear();
tester.testTextInput.enterText('tok');
await tester.pump();
await commitText(tester, 'tok');
await tester.testTextInput.receiveAction(TextInputAction.newline);
await tester.pump();
expect(typed(), startsWith('tok'));
@@ -390,7 +391,7 @@ void main() {
expect(claim['activate'], isTrue);
expect((claim['cols'], claim['rows']), (laidOut['cols'], laidOut['rows']));
// Typing carries this client's size.
await tester.sendKeyEvent(LogicalKeyboardKey.keyG);
await typeKey(tester, LogicalKeyboardKey.keyG);
await tester.pump();
var inputs = gateway.bodies('chat_shell_input');
expect(inputs.single['data'], 'g');
@@ -0,0 +1,166 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:xterm/xterm.dart';
import 'terminal_typing.dart';
import 'terminal_view_harness.dart';
/// Desktops whose IMEs, dead keys, emoji pickers and dictation commit text
/// through the text input (the web runs on these too).
final _desktops = TargetPlatformVariant({
TargetPlatform.macOS,
TargetPlatform.linux,
TargetPlatform.windows,
});
void main() {
testWidgets('a desktop terminal takes committed text from the text input: '
'IME, dead keys, emoji and dictation', (tester) async {
final harness = TerminalViewHarness();
await harness.pump(tester);
expect(tester.testTextInput.hasAnyClients, isTrue);
// A dead key's composed character, a CJK commit, an emoji, a phrase.
await commitText(tester, 'é');
await commitText(tester, '日本');
await commitText(tester, '\u{1F44D}');
await commitText(tester, ':q!');
expect(harness.typed, 'é日本\u{1F44D}:q!');
}, variant: _desktops);
testWidgets('composing text is drawn at the cursor and sent only when '
'committed; the IME keeps its keys meanwhile', (tester) async {
final harness = TerminalViewHarness();
await harness.pump(tester);
final render = harness.viewState(tester).renderTerminal;
await composeText(tester, 'にほ');
expect(render.composingText, 'にほ');
// Enter picks the IME's candidate, not the shell's line.
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.sendKeyEvent(LogicalKeyboardKey.backspace);
expect(harness.typed, isEmpty);
await commitText(tester, '日本');
expect(render.composingText, isNull);
expect(harness.typed, '日本');
// A dead key: ´ is shown, é is sent.
await composeText(tester, '´');
expect(harness.typed, '日本');
await commitText(tester, 'é');
expect(harness.typed, '日本é');
// A cancelled composition sends nothing.
await composeText(tester, 'x');
await commitText(tester, '');
expect(harness.typed, '日本é');
}, variant: _desktops);
testWidgets('a key the terminal encodes is taken, so the platform never '
'types it again; a printing key is left to the text input', (
tester,
) async {
final harness = TerminalViewHarness();
await harness.pump(tester);
// Handled: the browser's default (inserting text) is prevented.
expect(await tester.sendKeyEvent(LogicalKeyboardKey.enter), isTrue);
expect(await tester.sendKeyEvent(LogicalKeyboardKey.backspace), isTrue);
expect(await tester.sendKeyEvent(LogicalKeyboardKey.arrowLeft), isTrue);
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
expect(await tester.sendKeyEvent(LogicalKeyboardKey.keyC), isTrue);
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
expect(harness.typed, '\r\x7f\x1b[D\x03');
// Not handled: the platform types it through the text input, once.
expect(await tester.sendKeyEvent(LogicalKeyboardKey.keyA), isFalse);
expect(harness.typed, '\r\x7f\x1b[D\x03');
await typeKey(tester, LogicalKeyboardKey.keyA);
expect(harness.typed, '\r\x7f\x1b[D\x03a');
}, variant: _desktops);
testWidgets('after the platform closes the text input (a browser window '
'losing focus), a printing key is typed and the input reopened', (
tester,
) async {
final harness = TerminalViewHarness();
await harness.pump(tester);
tester.testTextInput.closeConnection();
await tester.pump();
tester.testTextInput.log.clear();
expect(await tester.sendKeyEvent(LogicalKeyboardKey.keyQ), isTrue);
expect(harness.typed, 'q');
expect(
tester.testTextInput.log.map((call) => call.method),
contains('TextInput.setClient'),
);
await commitText(tester, 'w');
expect(harness.typed, 'qw');
}, variant: _desktops);
testWidgets('the text input sits at the cursor, so an IME opens its '
'candidates there', (tester) async {
final harness = TerminalViewHarness();
await harness.pump(tester);
await harness.write(tester, r'$ echo ');
tester.testTextInput.log.clear();
await tester.pump();
// The editable follows the cursor once painted.
harness.terminal.write('x');
await tester.pump();
final calls = tester.testTextInput.log.where(
(call) => call.method == 'TextInput.setEditableSizeAndTransform',
);
expect(calls, isNotEmpty);
final transform = List<num>.from(
(calls.last.arguments as Map)['transform'] as List,
);
final cursor = harness.viewState(tester).globalCursorRect;
expect(transform[12], moreOrLessEquals(cursor.left));
expect(transform[13], moreOrLessEquals(cursor.top));
}, variant: TargetPlatformVariant.only(TargetPlatform.macOS));
testWidgets('a live terminal is a focused text field to assistive '
'technology (the web text input under semantics)', (tester) async {
final semantics = tester.ensureSemantics();
final harness = TerminalViewHarness();
await harness.pump(tester);
expect(
tester.getSemantics(find.byType(TerminalView)),
isSemantics(isTextField: true, isFocused: true, isEnabled: true),
);
semantics.dispose();
}, variant: TargetPlatformVariant.only(TargetPlatform.linux));
testWidgets('a finished run attaches no text input', (tester) async {
final harness = TerminalViewHarness(live: false);
await harness.pump(tester);
await tester.tap(find.byType(TerminalView));
await tester.pump(const Duration(seconds: 1));
expect(tester.testTextInput.hasAnyClients, isFalse);
}, variant: _desktops);
testWidgets('a soft keyboard types key by key: characters, Backspace and '
'Return reach the process without submitting a line', (tester) async {
final harness = TerminalViewHarness();
await harness.pump(tester);
expect(defaultTargetPlatform, TargetPlatform.android);
await commitText(tester, 'l');
expect(harness.typed, 'l');
await commitText(tester, 's');
expect(harness.typed, 'ls');
// Backspace on the (otherwise empty) field deletes one of its two
// placeholder spaces, which is read as Backspace.
final before = '${tester.testTextInput.editingState!['text']}';
expect(before, ' ');
tester.testTextInput.updateEditingValue(
const TextEditingValue(
text: ' ',
selection: TextSelection.collapsed(offset: 1),
),
);
await tester.pump();
expect(harness.typed, 'ls\x7f');
// Return types a line break, which is Enter.
await commitText(tester, '\n');
expect(harness.typed, 'ls\x7f\r');
// Close the keyboard connection before the tree goes away.
await tester.pumpWidget(const SizedBox.shrink());
}, variant: TargetPlatformVariant.only(TargetPlatform.android));
}
@@ -0,0 +1,59 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
/// Commits [text] through the platform text input, as an IME, dead key,
/// the emoji picker, dictation or a soft keyboard does: appended to what
/// the focused client last told the platform, the caret at its end.
Future<void> commitText(WidgetTester tester, String text) async {
final before = '${tester.testTextInput.editingState?['text'] ?? ''}';
final value = '$before$text';
tester.testTextInput.updateEditingValue(
TextEditingValue(
text: value,
selection: TextSelection.collapsed(offset: value.length),
),
);
await tester.pump();
}
/// Sends [text] as composing (marked) text: drawn, not yet committed.
Future<void> composeText(WidgetTester tester, String text) async {
final before = '${tester.testTextInput.editingState?['text'] ?? ''}';
final value = '$before$text';
tester.testTextInput.updateEditingValue(
TextEditingValue(
text: value,
selection: TextSelection.collapsed(offset: value.length),
composing: TextRange(start: before.length, end: value.length),
),
);
await tester.pump();
}
/// Presses [key] as a desktop platform does: the key event first, and only
/// if no handler took it, its [character] (default: the key's label in
/// lower case) through the text input. A key the terminal encoded is never
/// typed a second time.
Future<void> typeKey(
WidgetTester tester,
LogicalKeyboardKey key, {
String? character,
}) async {
final handled = await tester.sendKeyEvent(key, character: character);
if (!handled) {
final text = character ?? key.keyLabel.toLowerCase();
if (text.isNotEmpty) await commitText(tester, text);
}
await tester.pump();
}
/// Taps the key toolbar's key [id] (`chat-shell-key-<id>`), scrolling the
/// touch key row to it first.
Future<void> tapKey(WidgetTester tester, String id) async {
final key = find.byKey(Key('chat-shell-key-$id'));
await tester.ensureVisible(key);
await tester.pump();
await tester.tap(key);
await tester.pump();
}