Copy and paste: the desktop's universal copy key works; Ctrl+C at the terminal login keeps the terminal

Omarchy's Super+C sends Ctrl+C by setting the modifier state with no Ctrl key
event while Super is held. Flutter's web engine only presses a modifier on its
own key event, so the app saw a plain "c": the chat selection was not copied
and the terminal typed the letter. A key modifier sync in front of the
framework's key handler now presses the modifiers the browser event's flags
hold (and outside Apple platforms lets go of a held Meta for Ctrl+C/V/X/A/
Insert), installed by the app's binding.

The terminal copies a selection on Ctrl+C without sending ^C, treats the
injected copy key as copy only, and pastes on Shift+Insert. A refused
clipboard write or read now shows a message instead of failing silently.

Server: before the login completes, Ctrl+C and Ctrl+\ erase the typed
password (Ctrl+U) and Ctrl+Z is dropped, so su is not killed and the terminal
no longer ends.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-29 17:57:28 +00:00
parent 58041e06b7
commit 3d739c3bfc
31 changed files with 2090 additions and 17 deletions
+4
View File
@@ -4,6 +4,8 @@ import '../ui/theme/app_theme.dart';
import '../ui/scale/ui_scale.dart';
import 'package:flutter/material.dart';
import 'clipboard_failures.dart';
import '../shell/adaptive_shell.dart';
import '../shell/destination.dart';
@@ -54,6 +56,7 @@ class MyApp extends StatelessWidget {
if (router != null) {
return MaterialApp.router(
title: 'Pipelines',
scaffoldMessengerKey: appScaffoldMessengerKey,
builder: _scaled,
theme: appLightTheme(fontFamily: fontFamily),
darkTheme: appDarkTheme(fontFamily: fontFamily),
@@ -63,6 +66,7 @@ class MyApp extends StatelessWidget {
}
return MaterialApp(
title: 'Pipelines',
scaffoldMessengerKey: appScaffoldMessengerKey,
builder: _scaled,
theme: appLightTheme(fontFamily: fontFamily),
darkTheme: appDarkTheme(fontFamily: fontFamily),
+2 -1
View File
@@ -5,6 +5,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'app_url_strategy.dart';
import 'pipelines_binding.dart';
import 'pipelines_root.dart';
import 'server_endpoint.dart';
import '../features/settings/settings_store.dart';
@@ -55,7 +56,7 @@ Future<void> runPipelinesApp({
SettingsStore? settingsStore,
}) async {
useAppUrlStrategy();
WidgetsFlutterBinding.ensureInitialized();
PipelinesBinding.ensureInitialized();
registerAppFontLicenses();
// The device's chosen font loads beside the session; a slow download does
// not hold the first frame, and applies when it lands.
+100
View File
@@ -0,0 +1,100 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
/// The app's root messenger, for messages no screen owns.
final GlobalKey<ScaffoldMessengerState> appScaffoldMessengerKey = GlobalKey();
/// The message for a failed clipboard call [method] ([error]), or null for
/// a call whose failure is not the operator's to hear about.
///
/// The web reports a refused clipboard write or read (no permission, an
/// insecure page, a browser without the clipboard API) as an error reply.
String? clipboardFailureMessage(String method, PlatformException error) {
// The web's own reason ("Clipboard is not available in the context." on a
// page that is not secure) helps; its generic "Clipboard.setData failed."
// does not.
final detail = error.message?.trim() ?? '';
final why = detail.isEmpty || detail.startsWith('Clipboard.')
? 'the browser refused access to the clipboard'
: detail;
return switch (method) {
'Clipboard.setData' => 'Could not copy: $why',
'Clipboard.getData' => 'Could not paste: $why',
_ => null,
};
}
/// Shows [message] in the app's root messenger; false when there is none.
bool showAppMessage(String message) {
final messenger = appScaffoldMessengerKey.currentState;
if (messenger == null) return false;
messenger
..hideCurrentSnackBar()
..showSnackBar(SnackBar(content: Text(message)));
return true;
}
/// A [BinaryMessenger] that reports failed clipboard writes and reads.
///
/// Every copy in the app (the selection's copy keys and menu, copy buttons,
/// the terminal) writes through [Clipboard] on the platform channel, and
/// most never look at the result: a refusal would otherwise only reach the
/// console. The reply still reaches the caller unchanged.
class ClipboardReportingMessenger implements BinaryMessenger {
ClipboardReportingMessenger(this._inner, {void Function(String)? report})
: _report = report ?? _show;
final BinaryMessenger _inner;
final void Function(String message) _report;
static void _show(String message) => showAppMessage(message);
static const _codec = JSONMethodCodec();
@override
Future<ByteData?>? send(String channel, ByteData? message) {
final reply = _inner.send(channel, message);
if (reply == null ||
channel != SystemChannels.platform.name ||
message == null) {
return reply;
}
final String method;
try {
method = _codec.decodeMethodCall(message).method;
} on Object {
return reply;
}
if (!method.startsWith('Clipboard.')) return reply;
return reply.then((data) {
_check(method, data);
return data;
});
}
void _check(String method, ByteData? data) {
if (data == null) return;
try {
_codec.decodeEnvelope(data);
} on PlatformException catch (error) {
final message = clipboardFailureMessage(method, error);
if (message != null) _report(message);
} on Object {
// Not a reply this messenger understands; the caller decodes it.
}
}
@override
// ignore: deprecated_member_use
Future<void> handlePlatformMessage(
String channel,
ByteData? data,
PlatformMessageResponseCallback? callback,
) =>
// ignore: deprecated_member_use
_inner.handlePlatformMessage(channel, data, callback);
@override
void setMessageHandler(String channel, MessageHandler? handler) =>
_inner.setMessageHandler(channel, handler);
}
@@ -0,0 +1,38 @@
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import '../core/keyboard/key_modifier_sync.dart';
import 'clipboard_failures.dart';
/// The app's binding: Flutter's, with clipboard failures reported
/// ([ClipboardReportingMessenger]) and, on the web, key events carrying the
/// modifiers their browser events hold ([KeyModifierSync]).
///
/// An entry point calls [ensureInitialized] before anything else creates a
/// binding.
class PipelinesBinding extends WidgetsFlutterBinding {
PipelinesBinding._();
static bool _created = false;
/// The binding, created on the first call.
static WidgetsBinding ensureInitialized() {
if (!_created) {
_created = true;
PipelinesBinding._();
}
return WidgetsBinding.instance;
}
@override
void initInstances() {
super.initInstances();
// The services binding hands key data to the framework only once the
// keyboard state is synced; the modifier sync goes in front of it then.
keyboard.syncKeyboardState().then((_) => installKeyModifierSync());
}
@override
BinaryMessenger createBinaryMessenger() =>
ClipboardReportingMessenger(super.createBinaryMessenger());
}
@@ -0,0 +1,28 @@
import 'key_event_modifiers_stub.dart'
if (dart.library.js_interop) 'key_event_modifiers_web.dart'
as platform;
/// The modifier flags a key event itself carries.
class KeyEventModifiers {
const KeyEventModifiers({
this.control = false,
this.shift = false,
this.alt = false,
this.meta = false,
});
final bool control;
final bool shift;
final bool alt;
final bool meta;
@override
String toString() =>
'KeyEventModifiers(control: $control, shift: $shift, alt: $alt, '
'meta: $meta)';
}
/// The flags of the browser key event being dispatched (null elsewhere, or
/// when no key event is).
KeyEventModifiers? currentKeyEventModifiers() =>
platform.currentKeyEventModifiers();
@@ -0,0 +1,3 @@
import 'key_event_modifiers.dart';
KeyEventModifiers? currentKeyEventModifiers() => null;
@@ -0,0 +1,20 @@
import 'dart:js_interop';
import 'package:web/web.dart' as web;
import 'key_event_modifiers.dart';
/// The browser's current event (`window.event`), set while its listeners
/// run, so the engine's key listener (and the key data it sends the
/// framework synchronously) is inside it.
KeyEventModifiers? currentKeyEventModifiers() {
final event = web.window.event;
if (event == null || !event.isA<web.KeyboardEvent>()) return null;
final key = event as web.KeyboardEvent;
return KeyEventModifiers(
control: key.ctrlKey,
shift: key.shiftKey,
alt: key.altKey,
meta: key.metaKey,
);
}
@@ -0,0 +1,240 @@
import 'dart:ui' as ui;
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'key_event_modifiers.dart';
/// Keeps the framework's modifier state ([HardwareKeyboard]) in step with the
/// modifier flags of the key event being handled.
///
/// A browser reports Ctrl, Shift, Alt and Meta on every key event, but
/// Flutter's web engine only learns that a modifier is down from that
/// modifier's own key event; it releases modifiers the flags say are up, but
/// never presses one the flags say is down. A shortcut a desktop injects
/// (Omarchy's universal copy: Super+C sends Ctrl+C by setting the modifier
/// state, with no Ctrl key event, while Super is still held) therefore
/// reached the app as a plain "c": nothing was copied, and the terminal
/// typed the letter. This sync sits in front of the framework's key data
/// handler and, for each non-modifier key, presses (synthesized) the
/// modifiers the event's flags hold that the framework does not, then
/// releases them with that key.
///
/// Outside Apple platforms Meta is never part of the clipboard keys, so a
/// Meta (Super) that is held, or merged into such a chord, does not stop
/// Ctrl+C, V, X, A or Insert from being the clipboard key: Meta is released
/// for the chord and pressed again after it.
class KeyModifierSync {
KeyModifierSync({
required this.forward,
required this.modifiers,
HardwareKeyboard? keyboard,
TargetPlatform Function()? platform,
}) : _keyboard = keyboard ?? HardwareKeyboard.instance,
_platform = platform ?? (() => defaultTargetPlatform);
/// The framework's key data handler, which this one is placed in front of.
final bool Function(ui.KeyData data) forward;
/// The flags of the key event being handled, or null when unknown.
final KeyEventModifiers? Function() modifiers;
final HardwareKeyboard _keyboard;
final TargetPlatform Function() _platform;
/// The sync installed in front of the app's key events, if any.
static KeyModifierSync? instance;
/// Modifiers pressed by this sync, not by a key.
final Set<PhysicalKeyboardKey> _added = {};
/// Held modifiers this sync released for a chord, to press again after it.
final Map<PhysicalKeyboardKey, LogicalKeyboardKey> _released = {};
/// The key whose release ends the current chord.
PhysicalKeyboardKey? _chordKey;
/// Whether Ctrl is down only because the key event says so: no Ctrl key
/// was pressed, so the chord came from a shortcut the desktop injected
/// (the "copy button"), not from the keyboard's Ctrl key.
bool get controlFromFlags => _added.contains(PhysicalKeyboardKey.controlLeft);
/// Whether Ctrl is down only because the key event says so, for the
/// installed sync (false when none is).
static bool get injectedControl => instance?.controlFromFlags ?? false;
/// Handles one key data from the engine: the framework's handler sees it
/// with the modifiers its event's flags hold.
bool handle(ui.KeyData data) {
final physical = PhysicalKeyboardKey(data.physical);
if (data.physical == 0 && data.logical == 0) return forward(data);
if (_added.contains(physical)) {
// The real key of a modifier this sync pressed: its own events win.
_release(physical, data.timeStamp);
}
if (_released.containsKey(physical)) {
// A modifier released for a chord: the framework already has it up.
// Its repeats are dropped; once it is let go (or the engine notices
// it is up) it is not pressed again after the chord.
switch (data.type) {
case ui.KeyEventType.repeat:
return false;
case ui.KeyEventType.up:
_released.remove(physical);
return false;
case ui.KeyEventType.down:
_released.remove(physical);
}
}
if (data.synthesized || _modifierKeys.contains(physical)) {
return forward(data);
}
final flags = modifiers();
if (flags != null) _reconcile(flags, data);
final handled = forward(data);
if (data.type == ui.KeyEventType.up && physical == _chordKey) {
_endChord(data.timeStamp);
}
return handled;
}
void _reconcile(KeyEventModifiers flags, ui.KeyData data) {
final logical = LogicalKeyboardKey(data.logical);
final clipboard =
flags.control &&
_clipboardKeys.contains(logical) &&
_platform() != TargetPlatform.macOS &&
_platform() != TargetPlatform.iOS;
var changed = false;
final press = <_Modifier>[];
// Releases first, so no chord ever holds both the old and new keys.
for (final modifier in _modifiers) {
final want = modifier.flag(flags) && !(clipboard && modifier.meta);
final held = modifier.keys
.where(_keyboard.physicalKeysPressed.contains)
.toList();
if (want && held.isEmpty) {
press.add(modifier);
} else if (!want) {
for (final key in held) {
if (_added.contains(key)) {
_release(key, data.timeStamp);
changed = true;
} else if (modifier.meta && clipboard) {
// A held Super (Meta) is not part of a clipboard chord here.
final logical = _keyboard.lookUpLayout(key)!;
_released[key] = logical;
_lift(key, logical, data.timeStamp);
changed = true;
}
}
}
}
for (final modifier in press) {
_press(modifier.keys.first, modifier.logical, data.timeStamp);
_added.add(modifier.keys.first);
changed = true;
}
if (changed && data.type == ui.KeyEventType.down) {
_chordKey = PhysicalKeyboardKey(data.physical);
}
}
/// Releases what the chord pressed and presses again what it released.
void _endChord(Duration timeStamp) {
_chordKey = null;
for (final key in _added.toList()) {
_release(key, timeStamp);
}
final released = Map.of(_released);
_released.clear();
released.forEach((key, logical) => _press(key, logical, timeStamp));
}
void _release(PhysicalKeyboardKey key, Duration timeStamp) {
_added.remove(key);
final logical = _keyboard.lookUpLayout(key);
if (logical != null) _lift(key, logical, timeStamp);
}
void _press(
PhysicalKeyboardKey key,
LogicalKeyboardKey logical,
Duration timeStamp,
) => forward(_synthesized(ui.KeyEventType.down, key, logical, timeStamp));
void _lift(
PhysicalKeyboardKey key,
LogicalKeyboardKey logical,
Duration timeStamp,
) => forward(_synthesized(ui.KeyEventType.up, key, logical, timeStamp));
static ui.KeyData _synthesized(
ui.KeyEventType type,
PhysicalKeyboardKey key,
LogicalKeyboardKey logical,
Duration timeStamp,
) => ui.KeyData(
timeStamp: timeStamp,
type: type,
physical: key.usbHidUsage,
logical: logical.keyId,
character: null,
synthesized: true,
);
static final Set<LogicalKeyboardKey> _clipboardKeys = {
LogicalKeyboardKey.keyC,
LogicalKeyboardKey.keyV,
LogicalKeyboardKey.keyX,
LogicalKeyboardKey.keyA,
LogicalKeyboardKey.insert,
};
static final List<_Modifier> _modifiers = [
_Modifier((f) => f.control, LogicalKeyboardKey.controlLeft, const [
PhysicalKeyboardKey.controlLeft,
PhysicalKeyboardKey.controlRight,
]),
_Modifier((f) => f.shift, LogicalKeyboardKey.shiftLeft, const [
PhysicalKeyboardKey.shiftLeft,
PhysicalKeyboardKey.shiftRight,
]),
_Modifier((f) => f.alt, LogicalKeyboardKey.altLeft, const [
PhysicalKeyboardKey.altLeft,
PhysicalKeyboardKey.altRight,
]),
_Modifier((f) => f.meta, LogicalKeyboardKey.metaLeft, const [
PhysicalKeyboardKey.metaLeft,
PhysicalKeyboardKey.metaRight,
], meta: true),
];
static final Set<PhysicalKeyboardKey> _modifierKeys = {
for (final modifier in _modifiers) ...modifier.keys,
};
}
class _Modifier {
const _Modifier(this.flag, this.logical, this.keys, {this.meta = false});
final bool Function(KeyEventModifiers flags) flag;
final LogicalKeyboardKey logical;
final List<PhysicalKeyboardKey> keys;
final bool meta;
}
/// Places a [KeyModifierSync] in front of the app's key events on the web,
/// where the browser's event flags are the modifier truth. Native embedders
/// already derive modifier state from their events' flags.
void installKeyModifierSync() {
if (!kIsWeb || KeyModifierSync.instance != null) return;
final dispatcher = ui.PlatformDispatcher.instance;
final framework = dispatcher.onKeyData;
if (framework == null) return;
final sync = KeyModifierSync(
forward: framework,
modifiers: currentKeyEventModifiers,
);
dispatcher.onKeyData = sync.handle;
KeyModifierSync.instance = sync;
}
@@ -7,11 +7,11 @@ bool _apple() =>
defaultTargetPlatform == TargetPlatform.iOS;
/// The terminal's own key shortcuts. Cmd-C, Cmd-V and Cmd-A on Apple
/// platforms; elsewhere Ctrl-V (and the terminal convention Ctrl-Shift-V)
/// paste and Ctrl-Shift-C or Ctrl-Insert copies, so every other Ctrl key,
/// Ctrl-A included, reaches the process. Ctrl-C copies too while text is
/// selected (see [chatShellCopyKey]); a browser keeps Ctrl-Shift-C for its
/// developer tools, so there it is the key that works.
/// platforms; elsewhere Ctrl-V (and the terminal conventions Ctrl-Shift-V
/// and Shift-Insert) paste and Ctrl-Shift-C or Ctrl-Insert copies, so every
/// other Ctrl key, Ctrl-A included, reaches the process. Ctrl-C copies too
/// while text is selected (see [chatShellCopyKey]); a browser keeps
/// Ctrl-Shift-C for its developer tools, so there it is the key that works.
Map<ShortcutActivator, Intent> chatShellTerminalShortcuts() {
const paste = PasteTextIntent(SelectionChangedCause.keyboard);
if (_apple()) {
@@ -31,6 +31,7 @@ Map<ShortcutActivator, Intent> chatShellTerminalShortcuts() {
CopySelectionTextIntent.copy,
SingleActivator(LogicalKeyboardKey.keyV, control: true): paste,
SingleActivator(LogicalKeyboardKey.keyV, control: true, shift: true): paste,
SingleActivator(LogicalKeyboardKey.insert, shift: true): paste,
};
}
@@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:xterm/xterm.dart';
import '../../../core/keyboard/key_modifier_sync.dart';
import '../conversation/chat_composer_paste_focus.dart';
import '../ui/transcript/chat_link_launcher.dart';
import 'chat_shell_metrics.dart';
@@ -127,14 +128,18 @@ class _ChatShellTerminalViewState extends State<ChatShellTerminalView>
super.dispose();
}
/// Ctrl-C with a selection copies; the view's own keys (find, zoom,
/// scrolling) are answered; the terminal's own shortcuts (copy, paste)
/// are left to it; any other key xterm encodes differently from
/// xterm.dart (Alt as Meta, Ctrl symbols, modified special keys) is sent
/// here with its real character. The rest is the emulator's.
/// Ctrl-C with a selection copies (and sends no ^C); a desktop's copy
/// shortcut (Ctrl+C injected with no Ctrl key held, see
/// [KeyModifierSync.injectedControl]) only ever copies; the view's own
/// keys (find, zoom, scrolling) are answered; the terminal's own
/// shortcuts (copy, paste) are left to it; any other key xterm encodes
/// differently from xterm.dart (Alt as Meta, Ctrl symbols, modified
/// special keys) is sent here with its real character. The rest is the
/// emulator's.
KeyEventResult _key(FocusNode node, KeyEvent event) {
final keyboard = HardwareKeyboard.instance;
if (chatShellCopyKey(event, keyboard) && actions.copySelection()) {
if (chatShellCopyKey(event, keyboard) &&
(actions.copySelection() || KeyModifierSync.injectedControl)) {
return KeyEventResult.handled;
}
if (localKey(event)) return KeyEventResult.handled;
@@ -231,7 +231,7 @@ class KeyboardShortcutRegistry {
KeyboardShortcutEntry(
group: KeyboardShortcutGroup.terminal,
action: 'Paste into the terminal',
keys: _apple ? 'Cmd+V' : 'Ctrl+V, Ctrl+Shift+V',
keys: _apple ? 'Cmd+V' : 'Ctrl+V, Ctrl+Shift+V, Shift+Insert',
),
if (_apple)
const KeyboardShortcutEntry(