Open the on-screen keyboard for terminals on phones

On Android and iOS a tap on a terminal (a chat command card or the terminal
panel) now opens the soft keyboard and typing goes to the process, with no
autocorrect. Desktop keeps raw key events. Desktop keyboard tests now run
as macOS, since tests default to Android.
This commit is contained in:
2026-11-10 00:19:46 +00:00
parent 750071abe1
commit b44938d028
4 changed files with 287 additions and 217 deletions
@@ -117,10 +117,13 @@ class _ChatShellTerminalViewState extends State<ChatShellTerminalView> {
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,
// Desktop keys go straight to the process as key events (a focused
// text input stays closed in desktop browsers). On phones and tablets
// a tap opens the on-screen keyboard and typing goes to the terminal;
// the key toolbar's field stays as an alternative.
hardwareKeyboardOnly: !chatShellTouchFirst(),
// No autocorrect or suggestions rewriting what is sent to a shell.
keyboardType: TextInputType.visiblePassword,
shortcuts: chatShellTerminalShortcuts(),
cursorType: TerminalCursorType.block,
onTapUp: _tapped,
@@ -122,6 +122,29 @@ void main() {
expect(tester.widget<TextField>(field).controller!.text, isEmpty);
});
testWidgets(
'tapping the terminal on a phone opens the keyboard and typing reaches '
'the process',
(tester) async {
await openRunning(tester, output: 'Paste your token: ');
expect(tester.testTextInput.isVisible, isFalse);
await tester.tap(find.byType(TerminalView));
await tester.pump();
expect(tester.testTextInput.isVisible, isTrue);
expect(tester.testTextInput.hasAnyClients, isTrue);
gateway.calls.clear();
tester.testTextInput.enterText('tok');
await tester.pump();
await tester.testTextInput.receiveAction(TextInputAction.newline);
await tester.pump();
expect(typed(), startsWith('tok'));
// Close the keyboard connection before the tree goes away.
await tester.pumpWidget(const SizedBox.shrink());
await tester.pump(const Duration(seconds: 1));
},
variant: TargetPlatformVariant.only(TargetPlatform.android),
);
testWidgets('a running terminal is resized to its view; a finished one is '
'read-only', (tester) async {
await openRunning(tester, output: 'working\r\n');
@@ -41,28 +41,33 @@ void main() {
for (final mode in TerminalMode.values) {
for (final MapEntry(key: name, value: (letter, modifier))
in toggles.entries) {
testWidgets('$name toggles the ${mode.label.toLowerCase()} from a text '
'field and from inside the terminal', (tester) async {
layout.setMode(mode);
final surface = mode == TerminalMode.panel ? panelFinder : floatFinder;
await pumpTerminalApp(tester, hub: hub, layout: layout);
await tester.tap(find.byKey(appField));
await tester.pump();
await chord(tester, letter, [modifier]);
expect(surface, findsOneWidget);
expect(layout.visible, isTrue);
// The shortcut never types into the field it was pressed in.
expect(find.text('t'), findsNothing);
expect(terminalFocused(key('term_a')), isTrue);
// Inside the terminal the same keys hide it instead of reaching the
// shell (Ctrl+T would be transpose-chars there).
final typed = mac.bodies('terminal_input').length;
await chord(tester, letter, [modifier]);
expect(surface, findsNothing);
expect(layout.visible, isFalse);
expect(mac.bodies('terminal_input'), hasLength(typed));
expect(hub.tabs, hasLength(3));
});
desktopTestWidgets(
'$name toggles the ${mode.label.toLowerCase()} from a text '
'field and from inside the terminal',
(tester) async {
layout.setMode(mode);
final surface = mode == TerminalMode.panel
? panelFinder
: floatFinder;
await pumpTerminalApp(tester, hub: hub, layout: layout);
await tester.tap(find.byKey(appField));
await tester.pump();
await chord(tester, letter, [modifier]);
expect(surface, findsOneWidget);
expect(layout.visible, isTrue);
// The shortcut never types into the field it was pressed in.
expect(find.text('t'), findsNothing);
expect(terminalFocused(key('term_a')), isTrue);
// Inside the terminal the same keys hide it instead of reaching the
// shell (Ctrl+T would be transpose-chars there).
final typed = mac.bodies('terminal_input').length;
await chord(tester, letter, [modifier]);
expect(surface, findsNothing);
expect(layout.visible, isFalse);
expect(mac.bodies('terminal_input'), hasLength(typed));
expect(hub.tabs, hasLength(3));
},
);
}
}
@@ -76,7 +81,7 @@ void main() {
expect(terminalToggleShortcut(event, _AltHeld()), isTrue);
});
testWidgets('holding the chord toggles once and types nothing', (
desktopTestWidgets('holding the chord toggles once and types nothing', (
tester,
) async {
await pumpTerminalApp(tester, hub: hub, layout: layout);
@@ -93,7 +98,9 @@ void main() {
expect(mac.bodies('terminal_input'), isEmpty);
});
testWidgets('Shift or a second modifier leaves T alone', (tester) async {
desktopTestWidgets('Shift or a second modifier leaves T alone', (
tester,
) async {
await pumpTerminalApp(tester, hub: hub, layout: layout);
for (final modifiers in [
[LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.shiftLeft],
@@ -105,28 +112,31 @@ void main() {
}
});
testWidgets('showing focuses the terminal; hiding gives the keyboard back '
'to the field that had it', (tester) async {
await pumpTerminalApp(tester, hub: hub, layout: layout);
await tester.tap(find.byKey(appField));
await tester.pump();
expect(appFieldFocused(tester), isTrue);
await altT(tester);
expect(terminalFocused(key('term_a')), isTrue);
expect(appFieldFocused(tester), isFalse);
// Typing goes straight to the shell.
await tester.sendKeyEvent(LogicalKeyboardKey.keyX);
await tester.pump();
expect(mac.bodies('terminal_input').last['data'], 'x');
await altT(tester);
expect(appFieldFocused(tester), isTrue);
// The bar button focuses it as well.
await tester.tap(find.byIcon(Icons.terminal));
await tester.pumpAndSettle();
expect(terminalFocused(key('term_a')), isTrue);
});
desktopTestWidgets(
'showing focuses the terminal; hiding gives the keyboard back '
'to the field that had it',
(tester) async {
await pumpTerminalApp(tester, hub: hub, layout: layout);
await tester.tap(find.byKey(appField));
await tester.pump();
expect(appFieldFocused(tester), isTrue);
await altT(tester);
expect(terminalFocused(key('term_a')), isTrue);
expect(appFieldFocused(tester), isFalse);
// Typing goes straight to the shell.
await tester.sendKeyEvent(LogicalKeyboardKey.keyX);
await tester.pump();
expect(mac.bodies('terminal_input').last['data'], 'x');
await altT(tester);
expect(appFieldFocused(tester), isTrue);
// The bar button focuses it as well.
await tester.tap(find.byIcon(Icons.terminal));
await tester.pumpAndSettle();
expect(terminalFocused(key('term_a')), isTrue);
},
);
testWidgets('a terminal left shown before a reload is focused once it '
desktopTestWidgets('a terminal left shown before a reload is focused once it '
'lists', (tester) async {
layout.show();
await pumpTerminalApp(tester, hub: hub, layout: layout);
@@ -137,7 +147,7 @@ void main() {
await tester.pump(const Duration(milliseconds: 300));
});
testWidgets('Ctrl+Tab and Ctrl+Shift+Tab move between terminal tabs, '
desktopTestWidgets('Ctrl+Tab and Ctrl+Shift+Tab move between terminal tabs, '
'wrapping, and focus each; hidden, they do nothing here', (tester) async {
await pumpTerminalApp(tester, hub: hub, layout: layout);
await ctrlTab(tester);
@@ -165,7 +175,9 @@ void main() {
expect(hub.selected!.info.id, 'term_a');
});
testWidgets('a tab picked by pointer takes the keyboard', (tester) async {
desktopTestWidgets('a tab picked by pointer takes the keyboard', (
tester,
) async {
layout.show();
await pumpTerminalApp(tester, hub: hub, layout: layout);
await tester.pumpAndSettle();
@@ -174,7 +186,7 @@ void main() {
expect(terminalFocused(key('term_c')), isTrue);
});
testWidgets('opening with no terminal starts one', (tester) async {
desktopTestWidgets('opening with no terminal starts one', (tester) async {
final empty = TerminalGateway();
final bare = TerminalHub(TestMachines([machine('mac', empty)]));
addTearDown(bare.dispose);
@@ -188,31 +200,34 @@ void main() {
expect(empty.bodies('terminal_open'), hasLength(1));
});
testWidgets('on a narrow window the keys open and close the full-screen '
'page, focused, and Ctrl+Tab moves between its tabs', (tester) async {
await pumpTerminalApp(
tester,
hub: hub,
layout: layout,
size: const Size(390, 800),
);
await tester.tap(find.byKey(appField));
await tester.pump();
await chord(tester, LogicalKeyboardKey.keyT, [
LogicalKeyboardKey.controlLeft,
]);
expect(find.widgetWithText(AppBar, 'Terminal'), findsOneWidget);
expect(terminalFocused(key('term_a')), isTrue);
await ctrlTab(tester, reverse: true);
expect(hub.selected!.info.id, 'term_c');
expect(terminalFocused(key('term_c')), isTrue);
await chord(tester, LogicalKeyboardKey.keyT, [
LogicalKeyboardKey.controlLeft,
]);
expect(find.widgetWithText(AppBar, 'Terminal'), findsNothing);
expect(find.byType(TerminalView), findsNothing);
expect(appFieldFocused(tester), isTrue);
});
desktopTestWidgets(
'on a narrow window the keys open and close the full-screen '
'page, focused, and Ctrl+Tab moves between its tabs',
(tester) async {
await pumpTerminalApp(
tester,
hub: hub,
layout: layout,
size: const Size(390, 800),
);
await tester.tap(find.byKey(appField));
await tester.pump();
await chord(tester, LogicalKeyboardKey.keyT, [
LogicalKeyboardKey.controlLeft,
]);
expect(find.widgetWithText(AppBar, 'Terminal'), findsOneWidget);
expect(terminalFocused(key('term_a')), isTrue);
await ctrlTab(tester, reverse: true);
expect(hub.selected!.info.id, 'term_c');
expect(terminalFocused(key('term_c')), isTrue);
await chord(tester, LogicalKeyboardKey.keyT, [
LogicalKeyboardKey.controlLeft,
]);
expect(find.widgetWithText(AppBar, 'Terminal'), findsNothing);
expect(find.byType(TerminalView), findsNothing);
expect(appFieldFocused(tester), isTrue);
},
);
test('the listed shortcuts are the ones the platform delivers', () {
const mac = TerminalShortcutLabels(
@@ -241,7 +256,9 @@ void main() {
expect(windowsWeb.button, 'Terminal (Alt+T)');
});
testWidgets('typing / in the terminal stays in the terminal', (tester) async {
desktopTestWidgets('typing / in the terminal stays in the terminal', (
tester,
) async {
layout.setMode(TerminalMode.panel);
await pumpTerminalApp(tester, hub: hub, layout: layout);
await tester.tap(find.byKey(appField));
@@ -277,3 +294,12 @@ class _AltHeld extends HardwareKeyboard {
@override
bool get isShiftPressed => false;
}
/// Hardware keys reach the terminal on desktop; phones type through the
/// on-screen keyboard instead. These tests drive desktop key events.
void desktopTestWidgets(String description, WidgetTesterCallback callback) =>
testWidgets(
description,
callback,
variant: TargetPlatformVariant.only(TargetPlatform.macOS),
);
@@ -69,129 +69,135 @@ void main() {
Map<String, dynamic> saved() =>
jsonDecode(store.saved!) as Map<String, dynamic>;
testWidgets('Ctrl+` and the bar button toggle the bottom panel; hiding '
'keeps every shell', (tester) async {
await pumpApp(tester);
expect(find.byKey(const ValueKey('terminal-panel')), findsNothing);
await ctrlBacktick(tester);
expect(find.byKey(const ValueKey('terminal-panel')), findsOneWidget);
expect(find.text('src'), findsOneWidget);
expect(find.text('docs'), findsOneWidget);
expect(saved()['visible'], isTrue);
// The terminal has the keyboard, yet the shortcut still hides it.
await tester.tap(find.byType(TerminalView));
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.keyX);
await tester.pump();
expect(mac.bodies('terminal_input').last['data'], 'x');
await ctrlBacktick(tester);
expect(find.byKey(const ValueKey('terminal-panel')), findsNothing);
expect(hub.tabs, hasLength(2));
expect(mac.bodies('terminal_close'), isEmpty);
await tester.tap(find.byTooltip(TerminalToggleButton.label));
await tester.pumpAndSettle();
expect(find.byKey(const ValueKey('terminal-panel')), findsOneWidget);
// Reopening draws the same emulator, output and all.
expect(find.byType(TerminalView), findsOneWidget);
});
desktopTestWidgets(
'Ctrl+` and the bar button toggle the bottom panel; hiding '
'keeps every shell',
(tester) async {
await pumpApp(tester);
expect(find.byKey(const ValueKey('terminal-panel')), findsNothing);
await ctrlBacktick(tester);
expect(find.byKey(const ValueKey('terminal-panel')), findsOneWidget);
expect(find.text('src'), findsOneWidget);
expect(find.text('docs'), findsOneWidget);
expect(saved()['visible'], isTrue);
// The terminal has the keyboard, yet the shortcut still hides it.
await tester.tap(find.byType(TerminalView));
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.keyX);
await tester.pump();
expect(mac.bodies('terminal_input').last['data'], 'x');
await ctrlBacktick(tester);
expect(find.byKey(const ValueKey('terminal-panel')), findsNothing);
expect(hub.tabs, hasLength(2));
expect(mac.bodies('terminal_close'), isEmpty);
await tester.tap(find.byTooltip(TerminalToggleButton.label));
await tester.pumpAndSettle();
expect(find.byKey(const ValueKey('terminal-panel')), findsOneWidget);
// Reopening draws the same emulator, output and all.
expect(find.byType(TerminalView), findsOneWidget);
},
);
testWidgets('the panel resizes from its top edge and remembers its height', (
tester,
) async {
await pumpApp(tester);
await ctrlBacktick(tester);
final before = tester
.getSize(find.byKey(const ValueKey('terminal-panel')))
.height;
expect(before, terminalPanelDefaultHeight);
await tester.drag(
find.byKey(const ValueKey('terminal-panel-resize')),
const Offset(0, -120),
);
await tester.pumpAndSettle();
final after = tester
.getSize(find.byKey(const ValueKey('terminal-panel')))
.height;
expect(after, closeTo(before + 120, 1));
expect(saved()['panel_height'], closeTo(after, 1));
// The app above shrinks rather than hiding under it.
final field = tester.getRect(find.byType(Scaffold).first);
expect(field.bottom, closeTo(800 - after, 1));
final restored = TerminalLayout(store: store);
await restored.restore();
expect(restored.panelHeight, closeTo(after, 1));
expect(restored.visible, isTrue);
// The resized terminal tells the server its new size.
await tester.pump(const Duration(milliseconds: 200));
expect(mac.bodies('terminal_resize'), isNotEmpty);
});
desktopTestWidgets(
'the panel resizes from its top edge and remembers its height',
(tester) async {
await pumpApp(tester);
await ctrlBacktick(tester);
final before = tester
.getSize(find.byKey(const ValueKey('terminal-panel')))
.height;
expect(before, terminalPanelDefaultHeight);
await tester.drag(
find.byKey(const ValueKey('terminal-panel-resize')),
const Offset(0, -120),
);
await tester.pumpAndSettle();
final after = tester
.getSize(find.byKey(const ValueKey('terminal-panel')))
.height;
expect(after, closeTo(before + 120, 1));
expect(saved()['panel_height'], closeTo(after, 1));
// The app above shrinks rather than hiding under it.
final field = tester.getRect(find.byType(Scaffold).first);
expect(field.bottom, closeTo(800 - after, 1));
final restored = TerminalLayout(store: store);
await restored.restore();
expect(restored.panelHeight, closeTo(after, 1));
expect(restored.visible, isTrue);
// The resized terminal tells the server its new size.
await tester.pump(const Duration(milliseconds: 200));
expect(mac.bodies('terminal_resize'), isNotEmpty);
},
);
testWidgets('the floating window moves, resizes, minimises and maximises', (
tester,
) async {
layout
..setMode(TerminalMode.float)
..show();
await pumpApp(tester);
await tester.pump();
final window = find.byKey(const ValueKey('terminal-float'));
expect(window, findsOneWidget);
final start = layout.floatRect;
await tester.drag(
find.byKey(const ValueKey('terminal-float-title')),
const Offset(60, 40),
);
await tester.pumpAndSettle();
expect(layout.floatRect.topLeft, start.topLeft + const Offset(60, 40));
await tester.drag(
find.byKey(const ValueKey('terminal-float-resize')),
const Offset(80, 50),
);
await tester.pumpAndSettle();
expect(layout.floatRect.size, start.size + const Offset(80, 50));
expect(saved()['float'][2], closeTo(start.width + 80, 1));
await tester.tap(find.byTooltip('Minimise'));
await tester.pumpAndSettle();
expect(find.byType(TerminalView), findsNothing);
expect(hub.tabs, hasLength(2));
await tester.tap(find.byTooltip('Restore'));
await tester.pumpAndSettle();
expect(find.byType(TerminalView), findsOneWidget);
await tester.tap(find.byTooltip('Maximise'));
await tester.pumpAndSettle();
expect(
tester.getRect(
find.descendant(of: window, matching: find.byType(Material)).first,
),
const Rect.fromLTWH(0, 0, 1200, 800).deflate(terminalFloatMargin),
);
await tester.tap(find.byTooltip('Restore size'));
await tester.pumpAndSettle();
expect(layout.maximized, isFalse);
});
desktopTestWidgets(
'the floating window moves, resizes, minimises and maximises',
(tester) async {
layout
..setMode(TerminalMode.float)
..show();
await pumpApp(tester);
await tester.pump();
final window = find.byKey(const ValueKey('terminal-float'));
expect(window, findsOneWidget);
final start = layout.floatRect;
await tester.drag(
find.byKey(const ValueKey('terminal-float-title')),
const Offset(60, 40),
);
await tester.pumpAndSettle();
expect(layout.floatRect.topLeft, start.topLeft + const Offset(60, 40));
await tester.drag(
find.byKey(const ValueKey('terminal-float-resize')),
const Offset(80, 50),
);
await tester.pumpAndSettle();
expect(layout.floatRect.size, start.size + const Offset(80, 50));
expect(saved()['float'][2], closeTo(start.width + 80, 1));
await tester.tap(find.byTooltip('Minimise'));
await tester.pumpAndSettle();
expect(find.byType(TerminalView), findsNothing);
expect(hub.tabs, hasLength(2));
await tester.tap(find.byTooltip('Restore'));
await tester.pumpAndSettle();
expect(find.byType(TerminalView), findsOneWidget);
await tester.tap(find.byTooltip('Maximise'));
await tester.pumpAndSettle();
expect(
tester.getRect(
find.descendant(of: window, matching: find.byType(Material)).first,
),
const Rect.fromLTWH(0, 0, 1200, 800).deflate(terminalFloatMargin),
);
await tester.tap(find.byTooltip('Restore size'));
await tester.pumpAndSettle();
expect(layout.maximized, isFalse);
},
);
testWidgets('tabs select and close their terminals; New terminal opens one', (
tester,
) async {
layout.show();
await pumpApp(tester);
await tester.pumpAndSettle();
expect(hub.selected!.info.id, 'term_a');
await tester.tap(find.text('docs'));
await tester.pumpAndSettle();
expect(hub.selected!.info.id, 'term_b');
await tester.tap(find.byTooltip('New terminal'));
await tester.pumpAndSettle();
expect(mac.bodies('terminal_open'), hasLength(1));
expect(hub.tabs, hasLength(3));
expect(hub.selected!.info.id, 'term_1');
await tester.tap(find.byTooltip('Close terminal').first);
await tester.pumpAndSettle();
expect(mac.bodies('terminal_close').single['terminal_id'], 'term_a');
expect(find.text('src'), findsNothing);
});
desktopTestWidgets(
'tabs select and close their terminals; New terminal opens one',
(tester) async {
layout.show();
await pumpApp(tester);
await tester.pumpAndSettle();
expect(hub.selected!.info.id, 'term_a');
await tester.tap(find.text('docs'));
await tester.pumpAndSettle();
expect(hub.selected!.info.id, 'term_b');
await tester.tap(find.byTooltip('New terminal'));
await tester.pumpAndSettle();
expect(mac.bodies('terminal_open'), hasLength(1));
expect(hub.tabs, hasLength(3));
expect(hub.selected!.info.id, 'term_1');
await tester.tap(find.byTooltip('Close terminal').first);
await tester.pumpAndSettle();
expect(mac.bodies('terminal_close').single['terminal_id'], 'term_a');
expect(find.text('src'), findsNothing);
},
);
testWidgets('the options menu switches between panel and window', (
desktopTestWidgets('the options menu switches between panel and window', (
tester,
) async {
layout.show();
@@ -211,7 +217,7 @@ void main() {
expect(find.byKey(const ValueKey('terminal-float')), findsNothing);
});
testWidgets('Settings chooses the mode', (tester) async {
desktopTestWidgets('Settings chooses the mode', (tester) async {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
@@ -230,23 +236,35 @@ void main() {
expect(layout.mode, TerminalMode.panel);
});
testWidgets('a narrow window opens the terminal full screen with the key '
'toolbar', (tester) async {
layout.show();
await pumpApp(tester, size: const Size(390, 800));
// The panel is not drawn on a phone even when left shown.
expect(find.byKey(const ValueKey('terminal-panel')), findsNothing);
await tester.tap(find.byTooltip(TerminalToggleButton.label));
await tester.pumpAndSettle();
expect(find.widgetWithText(AppBar, 'Terminal'), findsOneWidget);
expect(find.byType(TerminalView), findsOneWidget);
expect(find.byType(ChatShellTerminalKeys), findsOneWidget);
await tester.tap(find.byKey(const Key('chat-shell-key-ctrl-c')));
await tester.pump();
expect(mac.bodies('terminal_input').last['data'], '\x03');
await tester.pageBack();
await tester.pumpAndSettle();
expect(find.byType(TerminalView), findsNothing);
expect(hub.tabs, hasLength(2));
});
desktopTestWidgets(
'a narrow window opens the terminal full screen with the key '
'toolbar',
(tester) async {
layout.show();
await pumpApp(tester, size: const Size(390, 800));
// The panel is not drawn on a phone even when left shown.
expect(find.byKey(const ValueKey('terminal-panel')), findsNothing);
await tester.tap(find.byTooltip(TerminalToggleButton.label));
await tester.pumpAndSettle();
expect(find.widgetWithText(AppBar, 'Terminal'), findsOneWidget);
expect(find.byType(TerminalView), findsOneWidget);
expect(find.byType(ChatShellTerminalKeys), findsOneWidget);
await tester.tap(find.byKey(const Key('chat-shell-key-ctrl-c')));
await tester.pump();
expect(mac.bodies('terminal_input').last['data'], '\x03');
await tester.pageBack();
await tester.pumpAndSettle();
expect(find.byType(TerminalView), findsNothing);
expect(hub.tabs, hasLength(2));
},
);
}
/// Hardware keys reach the terminal on desktop; phones type through the
/// on-screen keyboard instead. These tests drive desktop key events.
void desktopTestWidgets(String description, WidgetTesterCallback callback) =>
testWidgets(
description,
callback,
variant: TargetPlatformVariant.only(TargetPlatform.macOS),
);