Terminal: xterm key encoding, no live reflow on resize, Alt+T/Ctrl+T reach the shell

- Live terminals no longer reflow (xterm.dart 4.0.0 left the cursor on the
  wrong row); narrowing cuts and erases cells past the width, rows scroll
  into scrollback keeping the cursor row; finished output still reflows.
- App-side key encoding before xterm.dart's defaults: Alt as Meta (ESC +
  the typed character), Ctrl symbol table, xterm modifier parameter for
  arrows/Home/End/PgUp/PgDn/Ins/Del/F-keys, DECCKM and DECKPAM, Alt+BS.
- Settings > Appearance > Terminal: Use Option as Meta key (Macs, on).
- Inside a focused terminal only Cmd+T and Ctrl+` toggle it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 23:32:29 +00:00
parent 5ab1ec9e00
commit 7d1ffe8ad3
22 changed files with 1408 additions and 61 deletions
@@ -75,6 +75,11 @@ class TerminalLayout extends ChangeNotifier {
Rect floatRect = terminalFloatDefaultRect;
bool maximized = false;
/// Whether a Mac's Option key is Meta in the terminal (Option+B sends
/// ESC b, readline's word back) rather than typing its symbol (∫). Other
/// platforms' Alt is always Meta.
bool optionAsMeta = true;
/// Whether the last change could not be saved.
bool saveFailed = false;
@@ -100,6 +105,7 @@ class TerminalLayout extends ChangeNotifier {
}
}
maximized = json['maximized'] == true;
optionAsMeta = json['option_as_meta'] != false;
} catch (_) {
return;
}
@@ -107,6 +113,7 @@ class TerminalLayout extends ChangeNotifier {
}
void setMode(TerminalMode value) => _change(() => mode = value);
void setOptionAsMeta(bool value) => _change(() => optionAsMeta = value);
void toggle() => _change(() => visible = !visible);
void show() => _change(() => visible = true);
void hide() => _change(() => visible = false);
@@ -128,6 +135,7 @@ class TerminalLayout extends ChangeNotifier {
floatRect.height,
],
'maximized': maximized,
'option_as_meta': optionAsMeta,
}),
);
if (revision != _revision || saved != saveFailed) return;
@@ -3,13 +3,18 @@ import 'package:flutter/services.dart';
/// Whether [event] shows or hides the terminal: Alt+T, Ctrl+T, Cmd+T
/// (Meta+T) or Ctrl+` (Shift allowed only with Ctrl+`). They are global key
/// handlers, so inside the terminal they toggle rather than reaching the
/// shell (whose Ctrl+T, transpose-chars, is then not available) and inside
/// text fields they toggle rather than type.
/// handlers, so inside text fields they toggle rather than type. While a
/// terminal has the keyboard ([inTerminal]) only Cmd+T and Ctrl+` toggle:
/// Ctrl+T (transpose-chars) and Alt+T (ESC t, transpose-words) reach the
/// shell.
///
/// A held chord's repeats match too (and are swallowed); only its first
/// press toggles.
bool terminalToggleShortcut(KeyEvent event, HardwareKeyboard keyboard) {
bool terminalToggleShortcut(
KeyEvent event,
HardwareKeyboard keyboard, {
bool inTerminal = false,
}) {
if (event is KeyUpEvent) return false;
final control = keyboard.isControlPressed;
final meta = keyboard.isMetaPressed;
@@ -24,6 +29,7 @@ bool terminalToggleShortcut(KeyEvent event, HardwareKeyboard keyboard) {
event.logicalKey == LogicalKeyboardKey.keyT ||
alt && event.physicalKey == PhysicalKeyboardKey.keyT;
if (!t || keyboard.isShiftPressed) return false;
if (inTerminal) return meta && !control && !alt;
return [control, meta, alt].where((pressed) => pressed).length == 1;
}
@@ -66,6 +72,15 @@ class TerminalShortcutLabels {
'Ctrl+`',
];
/// The keys that still hide the terminal while it has the keyboard: its
/// Ctrl+T and Alt+T are the shell's.
List<String> get toggleInTerminal => [if (!web && _apple) 'Cmd+T', 'Ctrl+`'];
/// How the in-terminal exception reads in the menu and the cheatsheet.
String get inTerminalNote =>
'Inside the terminal, ${_apple ? 'Option+T' : 'Alt+T'} and Ctrl+T '
'reach the shell; ${toggleInTerminal.join(' or ')} hides it.';
/// The keys that move between tabs, or none where the browser keeps them.
List<String> get tabs =>
web ? const [] : const ['Ctrl+Tab', 'Ctrl+Shift+Tab'];
@@ -76,6 +91,7 @@ class TerminalShortcutLabels {
/// Every shortcut, one per line, for the terminal's menu.
String get summary => [
'Show or hide: ${toggle.join(', ')}',
inTerminalNote,
if (tabs.isNotEmpty) 'Next or previous tab: ${tabs.join(' / ')}',
'Copy selected text: $copy',
].join('\n');
@@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../../core/api_gateway.dart';
import '../../ui/input/keyboard_owner.dart';
import '../../ui/motion/motion.dart';
import 'terminal_focus.dart';
import 'terminal_hub.dart';
@@ -81,6 +82,9 @@ class _TerminalSurfaceState extends State<TerminalSurface>
bool _panelShown = false;
String? _selected;
/// The key of the toggle chord held down, if any.
PhysicalKeyboardKey? _toggleKey;
@override
void initState() {
super.initState();
@@ -150,8 +154,21 @@ class _TerminalSurfaceState extends State<TerminalSurface>
KeyEventResult _key(KeyEvent event) {
if (!mounted) return KeyEventResult.ignored;
final keyboard = HardwareKeyboard.instance;
if (terminalToggleShortcut(event, keyboard)) {
if (event is KeyDownEvent) toggle();
// A held toggle chord's repeats stay swallowed after the terminal it
// showed takes the keyboard (where Alt+T and Ctrl+T are the shell's).
if (event.physicalKey == _toggleKey) {
if (event is KeyUpEvent) _toggleKey = null;
if (event is KeyRepeatEvent) return KeyEventResult.handled;
}
if (terminalToggleShortcut(
event,
keyboard,
inTerminal: primaryFocusIsTypingSurface(),
)) {
if (event is KeyDownEvent) {
_toggleKey = event.physicalKey;
toggle();
}
return KeyEventResult.handled;
}
final step = terminalTabShortcut(event, keyboard);