Merge branch 'worktree-agent-ad4dacaa0da379451'

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-26 02:44:31 +00:00
36 changed files with 1730 additions and 173 deletions
+1 -1
View File
@@ -65,7 +65,7 @@ class TerminalBody extends StatelessWidget {
padding: const EdgeInsets.fromLTRB(8, 0, 8, 8),
child: ChatShellTerminalKeys(
session: tab.session,
field: chatShellTouchFirst(),
touch: chatShellTouchFirst(),
),
),
if (!tab.info.running && tab.connected) _closeEnded(tab),
@@ -0,0 +1,99 @@
import 'package:flutter/material.dart';
import 'terminal_browser_keys_stub.dart'
if (dart.library.js_interop) 'terminal_browser_keys_web.dart'
as impl;
/// What captures the browser's own keys: the Keyboard Lock API in full
/// screen on the web ([impl]); nothing elsewhere.
abstract class TerminalBrowserKeysBackend {
/// Whether this browser can lock its keys (Chrome and Edge can; Firefox,
/// Safari and native builds cannot).
bool get supported;
/// Enters full screen and locks the keys; false if the browser refused.
Future<bool> lock();
/// Unlocks the keys and leaves full screen.
Future<void> unlock();
/// Called when the browser ends the capture itself (Esc held, full screen
/// left another way).
set onReleased(void Function() callback);
}
/// "Capture browser keys": while on, the page is full screen with the
/// keyboard locked, so Ctrl+W, Ctrl+T, Ctrl+N, Ctrl+Tab and the like reach
/// the terminal instead of closing or opening browser tabs. Holding Esc
/// ends it, as the browser requires. A no-op where [supported] is false.
class TerminalBrowserKeys extends ChangeNotifier {
TerminalBrowserKeys({TerminalBrowserKeysBackend? backend})
: _backend = backend ?? impl.TerminalBrowserKeysPlatform() {
_backend.onReleased = _released;
}
/// The app's one capture (the page is full screen once at most).
static TerminalBrowserKeys instance = TerminalBrowserKeys();
final TerminalBrowserKeysBackend _backend;
bool _active = false;
bool get supported => _backend.supported;
bool get active => _active;
/// Starts the capture; false where unsupported or refused.
Future<bool> capture() async {
if (!supported) return false;
if (_active) return true;
final locked = await _backend.lock();
if (locked != _active) {
_active = locked;
notifyListeners();
}
return locked;
}
/// Ends the capture.
Future<void> release() async {
if (!_active) return;
_active = false;
notifyListeners();
await _backend.unlock();
}
void _released() {
if (!_active) return;
_active = false;
notifyListeners();
}
}
/// The menu label for [keys]' toggle.
String terminalBrowserKeysLabel(TerminalBrowserKeys keys) =>
keys.active ? 'Release browser keys' : 'Capture browser keys';
/// Turns the capture on or off from a menu, saying what happened.
Future<void> toggleTerminalBrowserKeys(
BuildContext context, [
TerminalBrowserKeys? keys,
]) async {
final capture = keys ?? TerminalBrowserKeys.instance;
final messenger = ScaffoldMessenger.maybeOf(context);
if (capture.active) {
await capture.release();
return;
}
final captured = await capture.capture();
messenger
?..hideCurrentSnackBar()
..showSnackBar(
SnackBar(
content: Text(
captured
? 'Browser keys such as Ctrl+W go to the terminal. '
'Hold Esc to leave full screen.'
: 'This browser did not let the page capture its keys.',
),
),
);
}
@@ -0,0 +1,16 @@
import 'terminal_browser_keys.dart';
/// Native builds get every key already; there is nothing to capture.
class TerminalBrowserKeysPlatform implements TerminalBrowserKeysBackend {
@override
bool get supported => false;
@override
Future<bool> lock() async => false;
@override
Future<void> unlock() async {}
@override
set onReleased(void Function() callback) {}
}
@@ -0,0 +1,73 @@
import 'dart:js_interop';
import 'dart:js_interop_unsafe';
import 'package:web/web.dart' as web;
import 'terminal_browser_keys.dart';
/// The Keyboard Lock API (`navigator.keyboard.lock()`), which Chrome and
/// Edge honour only in full screen: the page enters full screen, then locks
/// every key. The browser keeps holding Esc as the way out; leaving full
/// screen ends the lock.
class TerminalBrowserKeysPlatform implements TerminalBrowserKeysBackend {
TerminalBrowserKeysPlatform() {
web.document.addEventListener(
'fullscreenchange',
((web.Event _) {
if (web.document.fullscreenElement == null) _onReleased?.call();
}).toJS,
);
}
void Function()? _onReleased;
JSObject? get _keyboard {
final keyboard = (web.window.navigator as JSObject)['keyboard'];
return keyboard.isUndefinedOrNull ? null : keyboard as JSObject;
}
@override
bool get supported {
final keyboard = _keyboard;
return keyboard != null &&
keyboard.has('lock') &&
web.document.fullscreenEnabled;
}
@override
Future<bool> lock() async {
final keyboard = _keyboard;
final root = web.document.documentElement;
if (keyboard == null || root == null) return false;
try {
if (web.document.fullscreenElement == null) {
await root.requestFullscreen().toDart;
}
await keyboard.callMethod<JSPromise<JSAny?>>('lock'.toJS).toDart;
return true;
} catch (_) {
// Refused (no user gesture, a policy, an iframe): back out of full
// screen so the page is as it was.
await _exitFullscreen();
return false;
}
}
@override
Future<void> unlock() async {
_keyboard?.callMethod<JSAny?>('unlock'.toJS);
await _exitFullscreen();
}
Future<void> _exitFullscreen() async {
if (web.document.fullscreenElement == null) return;
try {
await web.document.exitFullscreen().toDart;
} catch (_) {
// Already leaving full screen.
}
}
@override
set onReleased(void Function() callback) => _onReleased = callback;
}
@@ -94,8 +94,19 @@ class TerminalShortcutLabels {
inTerminalNote,
if (tabs.isNotEmpty) 'Next or previous tab: ${tabs.join(' / ')}',
'Copy selected text: $copy',
?browserKeysNote,
].join('\n');
/// A browser outside macOS keeps Ctrl+W, Ctrl+T and Ctrl+N (close and
/// open tabs and windows) from the page: how to send them anyway, or
/// what to press instead. Null where the terminal receives them (native
/// builds; Safari and Chrome on a Mac use Cmd for those).
String? get browserKeysNote => web && !_apple
? 'The browser keeps Ctrl+W, Ctrl+T and Ctrl+N: choose Capture '
'browser keys (Chrome, Edge; hold Esc to leave), or press '
'Alt+Backspace instead of Ctrl+W to delete a word.'
: null;
/// The keys that copy the terminal's selected text. A browser keeps
/// Ctrl+Shift+C for its developer tools.
String get copy => _apple
@@ -2,6 +2,7 @@ import 'dart:async';
import 'package:flutter/material.dart';
import 'terminal_browser_keys.dart';
import 'terminal_hub.dart';
import 'terminal_layout.dart';
import 'terminal_menu.dart';
@@ -29,6 +30,9 @@ class TerminalTabStrip extends StatelessWidget {
/// False on the narrow page, which has no panel or window to choose.
final bool showModes;
/// The browser keys capture the menu offers (tests replace the instance).
TerminalBrowserKeys get browserKeys => TerminalBrowserKeys.instance;
@override
Widget build(BuildContext context) {
final selected = hub.selected;
@@ -54,33 +58,53 @@ class TerminalTabStrip extends StatelessWidget {
),
_newTerminal(),
if (showModes)
TerminalMenuButton<_Option>(
label: 'Terminal options',
info: TerminalShortcutLabels.current().summary,
icon: Icons.more_vert,
choices: [
for (final mode in TerminalMode.values)
TerminalChoice(
_Option(mode),
mode.label,
checked: layout.mode == mode,
key: ValueKey('terminal-mode-${mode.name}'),
),
const TerminalChoice(
_Option(null),
'Hide terminal',
key: ValueKey('terminal-hide'),
),
],
onSelected: (option) => option.mode == null
? layout.hide()
: layout.setMode(option.mode!),
ListenableBuilder(
listenable: browserKeys,
builder: (context, _) => _options(context),
),
...trailing,
],
);
}
/// The options menu: the modes, the browser keys capture (on a browser
/// that can) and Hide terminal, with the shortcuts under them.
Widget _options(BuildContext context) => TerminalMenuButton<_Option>(
label: 'Terminal options',
info: TerminalShortcutLabels.current().summary,
icon: Icons.more_vert,
choices: [
for (final mode in TerminalMode.values)
TerminalChoice(
_Option(mode),
mode.label,
checked: layout.mode == mode,
key: ValueKey('terminal-mode-${mode.name}'),
),
if (browserKeys.supported)
TerminalChoice(
const _Option(null, browserKeys: true),
'Capture browser keys',
checked: browserKeys.active,
key: const ValueKey('terminal-browser-keys'),
),
const TerminalChoice(
_Option(null),
'Hide terminal',
key: ValueKey('terminal-hide'),
),
],
onSelected: (option) {
if (option.browserKeys) {
unawaited(toggleTerminalBrowserKeys(context, browserKeys));
} else if (option.mode == null) {
layout.hide();
} else {
layout.setMode(option.mode!);
}
},
);
String _label(TerminalTab tab) =>
hub.multiMachine ? '${tab.title} · ${tab.machineName}' : tab.title;
@@ -125,8 +149,10 @@ class TerminalTabStrip extends StatelessWidget {
}
}
/// A mode to switch to, or (null) hiding the terminal.
/// A mode to switch to, (null) hiding the terminal, or the browser keys
/// capture.
class _Option {
const _Option(this.mode);
const _Option(this.mode, {this.browserKeys = false});
final TerminalMode? mode;
final bool browserKeys;
}