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
@@ -0,0 +1,159 @@
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/keyboard/key_event_modifiers.dart';
import 'package:xterm/xterm.dart';
import '../../../core/keyboard/browser_keys.dart';
import 'chat_shell_fixtures.dart';
/// The terminal's clipboard keys as the web app receives them: Ctrl+C
/// copies a selection (and sends no ^C) and interrupts without one; the
/// desktop's universal copy (Omarchy's Super+C, an injected Ctrl+C while
/// Super is held) only ever copies; its paste (Super+V) and Shift+Insert
/// paste.
void main() {
late ShellGateway gateway;
late List<String> copied;
setUp(() => gateway = ShellGateway());
tearDown(() => gateway.close());
String typed() => [
for (final body in gateway.bodies('chat_shell_input')) body['data'],
].join();
Future<BrowserKeys> open(WidgetTester tester) async {
copied = [];
final messenger = tester.binding.defaultBinaryMessenger;
messenger.setMockMethodCallHandler(SystemChannels.platform, (call) async {
switch (call.method) {
case 'Clipboard.setData':
copied.add('${(call.arguments as Map)['text']}');
case 'Clipboard.getData':
return {'text': 'tok'};
}
return null;
});
addTearDown(
() => messenger.setMockMethodCallHandler(SystemChannels.platform, null),
);
gateway.runs = [
shellRecord(
'run_t',
command: 'gh auth login',
terminal: true,
output: 'Paste your token: ',
),
];
final keys = BrowserKeys(tester);
await tester.pumpWidget(shellThread(gateway));
await tester.pump();
await tester.pump(const Duration(milliseconds: 500));
await tester.pump();
await tester.tap(find.byType(TerminalView));
await tester.pump();
return keys;
}
TerminalController controller(WidgetTester tester) =>
tester.widget<TerminalView>(find.byType(TerminalView)).controller!;
void select(WidgetTester tester, int length) {
final view = tester.widget<TerminalView>(find.byType(TerminalView));
final buffer = view.terminal.buffer;
view.controller!.setSelection(
buffer.createAnchor(0, 0),
buffer.createAnchor(length, 0),
);
}
Future<void> settle(WidgetTester tester) async {
for (var i = 0; i < 5; i++) {
await tester.pump(const Duration(milliseconds: 20));
}
}
for (final merged in [false, true]) {
testWidgets(
'Super+C (Ctrl${merged ? ' and Meta' : ''} in the flags) copies the '
'selection and never interrupts',
(tester) async {
final keys = await open(tester);
select(tester, 5);
await keys.superChord(LogicalKeyboardKey.keyC, mergedMeta: merged);
expect(copied, ['Paste']);
expect(controller(tester).selection, isNull);
// Nothing selected: the copy key does nothing, it sends no ^C.
await keys.superChord(LogicalKeyboardKey.keyC, mergedMeta: merged);
expect(copied, ['Paste']);
expect(typed(), isEmpty);
// Super+V pastes into the process.
await keys.superChord(LogicalKeyboardKey.keyV, mergedMeta: merged);
await settle(tester);
expect(typed(), 'tok');
expect(HardwareKeyboard.instance.logicalKeysPressed, isEmpty);
await tester.pump(const Duration(milliseconds: 400));
},
variant: TargetPlatformVariant({
TargetPlatform.linux,
TargetPlatform.windows,
}),
);
}
testWidgets(
'Ctrl+C copies a selection without ^C and interrupts without one; '
'Ctrl+Insert copies, Shift+Insert and Ctrl+V paste',
(tester) async {
final keys = await open(tester);
const ctrl = KeyEventModifiers(control: true);
select(tester, 5);
await keys.chord(LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.keyC);
expect(copied, ['Paste']);
expect(typed(), isEmpty);
expect(controller(tester).selection, isNull);
await keys.chord(LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.keyC);
expect(typed(), '\x03');
select(tester, 10);
await keys.chord(
LogicalKeyboardKey.controlLeft,
LogicalKeyboardKey.insert,
flags: ctrl,
);
expect(copied, ['Paste', 'Paste your']);
await keys.chord(
LogicalKeyboardKey.shiftLeft,
LogicalKeyboardKey.insert,
flags: const KeyEventModifiers(shift: true),
);
await settle(tester);
await keys.chord(LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.keyV);
await settle(tester);
expect(typed(), '\x03toktok');
await tester.pump(const Duration(milliseconds: 400));
},
variant: TargetPlatformVariant({
TargetPlatform.linux,
TargetPlatform.windows,
}),
);
testWidgets(
'on macOS Cmd+C copies and Ctrl+C interrupts',
(tester) async {
final keys = await open(tester);
select(tester, 5);
await keys.chord(
LogicalKeyboardKey.metaLeft,
LogicalKeyboardKey.keyC,
flags: const KeyEventModifiers(meta: true),
);
expect(copied, ['Paste']);
select(tester, 5);
await keys.chord(LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.keyC);
expect(typed(), '\x03');
await tester.pump(const Duration(milliseconds: 400));
},
variant: TargetPlatformVariant.only(TargetPlatform.macOS),
);
}