Merge main into the terminal flow-control branch
Conflicts resolved keeping both sides: main's active-client rules, input lane, reply marking and key input, and this branch's flow control, acknowledgements, subscriptions and sliced emulator feed. terminal_ack joins the terminal lane; the feed marks each live slice it writes as drawing, so the emulator's answers still go to onReply. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -26,7 +26,9 @@ class TerminalTab extends ChangeNotifier {
|
||||
session = ChatShellTerminalSession(
|
||||
_run(),
|
||||
onInput: _input,
|
||||
onReply: (data) => _input(data, reply: true),
|
||||
onResize: _resized,
|
||||
onActivate: _activated,
|
||||
);
|
||||
session.terminal.onTitleChange = _titled;
|
||||
session.onDrawn = _drawn;
|
||||
@@ -93,6 +95,10 @@ class TerminalTab extends ChangeNotifier {
|
||||
_window = 0;
|
||||
_update(info);
|
||||
unawaited(resume());
|
||||
// The new connection reports the size it draws at; the server applies
|
||||
// it while no other client is the active one.
|
||||
final size = session.viewSize;
|
||||
if (size != null) _resized(size.$1, size.$2);
|
||||
}
|
||||
|
||||
void disconnected() {
|
||||
@@ -217,19 +223,25 @@ class TerminalTab extends ChangeNotifier {
|
||||
_notify();
|
||||
}
|
||||
|
||||
void _input(String data) {
|
||||
/// Sends typed text, or with [reply] the emulator's automatic answer to a
|
||||
/// query, which the server passes on only from the active client. Typing
|
||||
/// carries the size this client draws at: it becomes the active client.
|
||||
void _input(String data, {bool reply = false}) {
|
||||
if (_disposed || !connected) return;
|
||||
final id = _info.id;
|
||||
final gateway = _gateway;
|
||||
(_pipe ??= ChatShellInputPipe(
|
||||
(data) => gateway.call('terminal_input', {
|
||||
'terminal_request': {'terminal_id': id, 'data': data},
|
||||
}),
|
||||
(failure) {
|
||||
error = '$failure';
|
||||
_notify();
|
||||
},
|
||||
)).add(data);
|
||||
(_pipe ??= ChatShellInputPipe((data, reply) {
|
||||
final size = reply ? null : session.viewSize;
|
||||
return gateway.call('terminal_input', {
|
||||
'terminal_request': {
|
||||
'terminal_id': id,
|
||||
'data': data,
|
||||
if (reply) 'reply': true,
|
||||
'cols': ?size?.$1,
|
||||
'rows': ?size?.$2,
|
||||
},
|
||||
});
|
||||
}, _failed)).add(data, reply: reply);
|
||||
}
|
||||
|
||||
void _resized(int cols, int rows) {
|
||||
@@ -237,23 +249,40 @@ class TerminalTab extends ChangeNotifier {
|
||||
_resize?.cancel();
|
||||
_resize = Timer(chatShellResizeDelay, () {
|
||||
if (_sentSize == (cols, rows) || !connected) return;
|
||||
_sentSize = (cols, rows);
|
||||
_gateway
|
||||
.call('terminal_resize', {
|
||||
'terminal_request': {
|
||||
'terminal_id': _info.id,
|
||||
'cols': cols,
|
||||
'rows': rows,
|
||||
},
|
||||
})
|
||||
.catchError((Object failure) {
|
||||
error = '$failure';
|
||||
_notify();
|
||||
return <String, dynamic>{};
|
||||
});
|
||||
_sendSize(cols, rows, activate: false);
|
||||
});
|
||||
}
|
||||
|
||||
/// A view gained focus: this client becomes the active one and the
|
||||
/// terminal takes its size at once.
|
||||
void _activated(int cols, int rows) {
|
||||
if (_disposed || !_info.running || !connected) return;
|
||||
_resize?.cancel();
|
||||
_sendSize(cols, rows, activate: true);
|
||||
}
|
||||
|
||||
void _sendSize(int cols, int rows, {required bool activate}) {
|
||||
_sentSize = (cols, rows);
|
||||
_gateway
|
||||
.call('terminal_resize', {
|
||||
'terminal_request': {
|
||||
'terminal_id': _info.id,
|
||||
'cols': cols,
|
||||
'rows': rows,
|
||||
if (activate) 'activate': true,
|
||||
},
|
||||
})
|
||||
.catchError((Object failure) {
|
||||
_failed(failure);
|
||||
return <String, dynamic>{};
|
||||
});
|
||||
}
|
||||
|
||||
void _failed(Object failure) {
|
||||
error = '$failure';
|
||||
_notify();
|
||||
}
|
||||
|
||||
void _notify() {
|
||||
if (!_disposed) notifyListeners();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user