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:
@@ -0,0 +1,107 @@
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/application/clipboard_failures.dart';
|
||||
|
||||
/// A platform side that answers every platform-channel call with [reply].
|
||||
class _Platform implements BinaryMessenger {
|
||||
_Platform(this.reply);
|
||||
final ByteData? Function(MethodCall call) reply;
|
||||
static const codec = JSONMethodCodec();
|
||||
|
||||
@override
|
||||
Future<ByteData?>? send(String channel, ByteData? message) async =>
|
||||
reply(codec.decodeMethodCall(message));
|
||||
|
||||
@override
|
||||
Future<void> handlePlatformMessage(
|
||||
String channel,
|
||||
ByteData? data,
|
||||
ui.PlatformMessageResponseCallback? callback,
|
||||
) async {}
|
||||
|
||||
@override
|
||||
void setMessageHandler(String channel, MessageHandler? handler) {}
|
||||
}
|
||||
|
||||
void main() {
|
||||
const codec = JSONMethodCodec();
|
||||
final refused = codec.encodeErrorEnvelope(
|
||||
code: 'copy_fail',
|
||||
message: 'Clipboard.setData failed.',
|
||||
);
|
||||
|
||||
Future<(List<String>, Object?)> call(
|
||||
String method,
|
||||
ByteData? Function(MethodCall) reply,
|
||||
) async {
|
||||
final reports = <String>[];
|
||||
final messenger = ClipboardReportingMessenger(
|
||||
_Platform(reply),
|
||||
report: reports.add,
|
||||
);
|
||||
final channel = MethodChannel(
|
||||
SystemChannels.platform.name,
|
||||
codec,
|
||||
messenger,
|
||||
);
|
||||
Object? error;
|
||||
try {
|
||||
await channel.invokeMethod<void>(method, {'text': 'x'});
|
||||
} on PlatformException catch (e) {
|
||||
error = e;
|
||||
}
|
||||
return (reports, error);
|
||||
}
|
||||
|
||||
test(
|
||||
'a refused clipboard write is reported and still reaches the caller',
|
||||
() async {
|
||||
final (reports, error) = await call('Clipboard.setData', (_) => refused);
|
||||
expect(reports, [
|
||||
'Could not copy: the browser refused access to the clipboard',
|
||||
]);
|
||||
expect(error, isA<PlatformException>());
|
||||
},
|
||||
);
|
||||
|
||||
test('an insecure page says why; a refused read is reported too', () async {
|
||||
final (reports, _) = await call(
|
||||
'Clipboard.getData',
|
||||
(_) => codec.encodeErrorEnvelope(
|
||||
code: 'paste_fail',
|
||||
message: 'Clipboard is not available in the context.',
|
||||
),
|
||||
);
|
||||
expect(reports, [
|
||||
'Could not paste: Clipboard is not available in the context.',
|
||||
]);
|
||||
});
|
||||
|
||||
test('successes and passive checks are not reported', () async {
|
||||
final (ok, _) = await call(
|
||||
'Clipboard.setData',
|
||||
(_) => codec.encodeSuccessEnvelope(null),
|
||||
);
|
||||
expect(ok, isEmpty);
|
||||
final (checks, _) = await call(
|
||||
'Clipboard.hasStrings',
|
||||
(_) => codec.encodeErrorEnvelope(code: 'has_strings_fail'),
|
||||
);
|
||||
expect(checks, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('the message shows in the app', (tester) async {
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
scaffoldMessengerKey: appScaffoldMessengerKey,
|
||||
home: const Scaffold(),
|
||||
),
|
||||
);
|
||||
expect(showAppMessage('Could not copy: denied'), isTrue);
|
||||
await tester.pump();
|
||||
expect(find.text('Could not copy: denied'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
// ignore_for_file: deprecated_member_use
|
||||
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/keyboard/key_event_modifiers.dart';
|
||||
import 'package:pipelines_app/core/keyboard/key_modifier_sync.dart';
|
||||
|
||||
/// Key events as Flutter's web engine delivers browser key events, through
|
||||
/// the app's [KeyModifierSync] into the widget tree under test.
|
||||
///
|
||||
/// Like the engine, it knows a modifier is down only from that modifier's
|
||||
/// own key event, and releases (synthesized) a modifier the next event's
|
||||
/// flags say is up. So a desktop's injected shortcut, which sets the
|
||||
/// modifier flags without any Ctrl key event, arrives exactly as in the
|
||||
/// browser.
|
||||
class BrowserKeys {
|
||||
BrowserKeys(this.tester) {
|
||||
sync = KeyModifierSync(forward: _forward, modifiers: () => _flags);
|
||||
KeyModifierSync.instance = sync;
|
||||
addTearDown(() => KeyModifierSync.instance = null);
|
||||
}
|
||||
|
||||
final WidgetTester tester;
|
||||
late final KeyModifierSync sync;
|
||||
KeyEventModifiers? _flags;
|
||||
final _engineHeld = <PhysicalKeyboardKey>{};
|
||||
|
||||
static final _modifierFlags = {
|
||||
PhysicalKeyboardKey.controlLeft: 'control',
|
||||
PhysicalKeyboardKey.shiftLeft: 'shift',
|
||||
PhysicalKeyboardKey.altLeft: 'alt',
|
||||
PhysicalKeyboardKey.metaLeft: 'meta',
|
||||
};
|
||||
|
||||
static bool _flag(KeyEventModifiers flags, String name) => switch (name) {
|
||||
'control' => flags.control,
|
||||
'shift' => flags.shift,
|
||||
'alt' => flags.alt,
|
||||
_ => flags.meta,
|
||||
};
|
||||
|
||||
/// One browser key event for [key] (down or up) whose flags are [flags].
|
||||
Future<void> event(
|
||||
LogicalKeyboardKey key, {
|
||||
required bool down,
|
||||
KeyEventModifiers flags = const KeyEventModifiers(),
|
||||
String? character,
|
||||
}) async {
|
||||
_flags = flags;
|
||||
final physical = _physical(key);
|
||||
final stamp = Duration.zero;
|
||||
for (final held in _engineHeld.toList()) {
|
||||
if (held == physical || _flag(flags, _modifierFlags[held]!)) continue;
|
||||
_engineHeld.remove(held);
|
||||
sync.handle(_data(ui.KeyEventType.up, held, null, stamp, true));
|
||||
}
|
||||
if (_modifierFlags.containsKey(physical)) {
|
||||
if (down) {
|
||||
_engineHeld.add(physical);
|
||||
} else if (!_engineHeld.remove(physical)) {
|
||||
// The engine drops the release of a key it does not hold.
|
||||
return tester.pump();
|
||||
}
|
||||
}
|
||||
final repeat =
|
||||
down &&
|
||||
HardwareKeyboard.instance.physicalKeysPressed.contains(physical);
|
||||
sync.handle(
|
||||
_data(
|
||||
down
|
||||
? (repeat ? ui.KeyEventType.repeat : ui.KeyEventType.down)
|
||||
: ui.KeyEventType.up,
|
||||
physical,
|
||||
key,
|
||||
stamp,
|
||||
false,
|
||||
character: down ? character : null,
|
||||
raw: (down, key, flags),
|
||||
),
|
||||
);
|
||||
// The engine sends the raw message even for an event whose key data
|
||||
// the sync dropped.
|
||||
if (_pendingRaw.isNotEmpty) _sendRaw();
|
||||
await tester.pump();
|
||||
}
|
||||
|
||||
/// A key pressed and released with [flags] on both events.
|
||||
Future<void> tap(
|
||||
LogicalKeyboardKey key, {
|
||||
KeyEventModifiers flags = const KeyEventModifiers(),
|
||||
String? character,
|
||||
}) async {
|
||||
await event(key, down: true, flags: flags, character: character);
|
||||
await event(key, down: false, flags: flags);
|
||||
}
|
||||
|
||||
/// Omarchy's universal clipboard key: Super held, then [key] with Ctrl in
|
||||
/// its flags (and Meta too when [mergedMeta]), with no Ctrl key event;
|
||||
/// Super is let go afterwards.
|
||||
Future<void> superChord(
|
||||
LogicalKeyboardKey key, {
|
||||
bool mergedMeta = false,
|
||||
String? character,
|
||||
}) async {
|
||||
const meta = KeyEventModifiers(meta: true);
|
||||
final chord = KeyEventModifiers(control: true, meta: mergedMeta);
|
||||
await event(LogicalKeyboardKey.metaLeft, down: true, flags: meta);
|
||||
await event(key, down: true, flags: chord, character: character);
|
||||
await event(key, down: false, flags: chord);
|
||||
await event(LogicalKeyboardKey.metaLeft, down: false);
|
||||
}
|
||||
|
||||
/// [key] with a real modifier key held: [modifier] pressed, [key] tapped,
|
||||
/// [modifier] released.
|
||||
Future<void> chord(
|
||||
LogicalKeyboardKey modifier,
|
||||
LogicalKeyboardKey key, {
|
||||
KeyEventModifiers flags = const KeyEventModifiers(control: true),
|
||||
}) async {
|
||||
await event(modifier, down: true, flags: flags);
|
||||
await tap(key, flags: flags);
|
||||
await event(modifier, down: false);
|
||||
}
|
||||
|
||||
final _pendingRaw = <(bool, LogicalKeyboardKey, KeyEventModifiers)>[];
|
||||
|
||||
ui.KeyData _data(
|
||||
ui.KeyEventType type,
|
||||
PhysicalKeyboardKey physical,
|
||||
LogicalKeyboardKey? logical,
|
||||
Duration stamp,
|
||||
bool synthesized, {
|
||||
String? character,
|
||||
(bool, LogicalKeyboardKey, KeyEventModifiers)? raw,
|
||||
}) {
|
||||
if (raw != null) _pendingRaw.add(raw);
|
||||
return ui.KeyData(
|
||||
timeStamp: stamp,
|
||||
type: type,
|
||||
physical: physical.usbHidUsage,
|
||||
logical:
|
||||
(logical ??
|
||||
HardwareKeyboard.instance.lookUpLayout(physical) ??
|
||||
LogicalKeyboardKey.metaLeft)
|
||||
.keyId,
|
||||
character: character,
|
||||
synthesized: synthesized,
|
||||
);
|
||||
}
|
||||
|
||||
/// The framework's handler, as the engine calls it: key data, then (for
|
||||
/// the event's own key) the raw key message that completes it.
|
||||
bool _forward(ui.KeyData data) {
|
||||
final handled = tester.binding.keyEventManager.handleKeyData(data);
|
||||
if (!data.synthesized && _pendingRaw.isNotEmpty) _sendRaw();
|
||||
return handled;
|
||||
}
|
||||
|
||||
void _sendRaw() {
|
||||
final (down, key, flags) = _pendingRaw.removeLast();
|
||||
tester.binding.keyEventManager.handleRawKeyMessage(
|
||||
_rawMessage(down, key, flags),
|
||||
);
|
||||
}
|
||||
|
||||
static Map<String, Object?> _rawMessage(
|
||||
bool down,
|
||||
LogicalKeyboardKey key,
|
||||
KeyEventModifiers flags,
|
||||
) => {
|
||||
'type': down ? 'keydown' : 'keyup',
|
||||
'keymap': 'web',
|
||||
'code': _physical(key).debugName?.replaceAll(' ', '') ?? '',
|
||||
'key': key.keyLabel,
|
||||
'location': 0,
|
||||
'metaState':
|
||||
(flags.shift ? 0x01 : 0) |
|
||||
(flags.alt ? 0x02 : 0) |
|
||||
(flags.control ? 0x04 : 0) |
|
||||
(flags.meta ? 0x08 : 0),
|
||||
'keyCode': 0,
|
||||
};
|
||||
|
||||
static PhysicalKeyboardKey _physical(LogicalKeyboardKey key) =>
|
||||
_physicalKeys[key] ?? _letterPhysical(key);
|
||||
|
||||
static final _physicalKeys = {
|
||||
LogicalKeyboardKey.controlLeft: PhysicalKeyboardKey.controlLeft,
|
||||
LogicalKeyboardKey.shiftLeft: PhysicalKeyboardKey.shiftLeft,
|
||||
LogicalKeyboardKey.altLeft: PhysicalKeyboardKey.altLeft,
|
||||
LogicalKeyboardKey.metaLeft: PhysicalKeyboardKey.metaLeft,
|
||||
LogicalKeyboardKey.insert: PhysicalKeyboardKey.insert,
|
||||
};
|
||||
|
||||
static PhysicalKeyboardKey _letterPhysical(LogicalKeyboardKey key) {
|
||||
final id = key.keyId;
|
||||
if (id >= 0x61 && id <= 0x7a) {
|
||||
return PhysicalKeyboardKey(0x00070004 + id - 0x61);
|
||||
}
|
||||
throw ArgumentError('no physical key for $key');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,220 @@
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/keyboard/key_event_modifiers.dart';
|
||||
import 'package:pipelines_app/core/keyboard/key_modifier_sync.dart';
|
||||
|
||||
/// A key data as the web engine sends it.
|
||||
ui.KeyData keyData(
|
||||
ui.KeyEventType type,
|
||||
PhysicalKeyboardKey physical,
|
||||
LogicalKeyboardKey logical, {
|
||||
bool synthesized = false,
|
||||
String? character,
|
||||
}) => ui.KeyData(
|
||||
timeStamp: Duration.zero,
|
||||
type: type,
|
||||
physical: physical.usbHidUsage,
|
||||
logical: logical.keyId,
|
||||
character: character,
|
||||
synthesized: synthesized,
|
||||
);
|
||||
|
||||
const _down = ui.KeyEventType.down;
|
||||
const _up = ui.KeyEventType.up;
|
||||
const _repeat = ui.KeyEventType.repeat;
|
||||
|
||||
/// What one key data looks like in a forwarded list: `+key`, `-key`, `=key`
|
||||
/// (repeat), with `*` for synthesized.
|
||||
String describe(ui.KeyData data) {
|
||||
final type = switch (data.type) {
|
||||
ui.KeyEventType.down => '+',
|
||||
ui.KeyEventType.up => '-',
|
||||
ui.KeyEventType.repeat => '=',
|
||||
};
|
||||
final name = PhysicalKeyboardKey(data.physical).debugName ?? '?';
|
||||
return '$type${data.synthesized ? '*' : ''}$name';
|
||||
}
|
||||
|
||||
/// A sync over a record of what it forwards, with a fake keyboard state
|
||||
/// kept as the framework would keep it.
|
||||
class SyncHarness {
|
||||
SyncHarness({TargetPlatform platform = TargetPlatform.linux}) {
|
||||
sync = KeyModifierSync(
|
||||
forward: (data) {
|
||||
if (data.physical != 0) {
|
||||
forwarded.add(describe(data));
|
||||
final key = PhysicalKeyboardKey(data.physical);
|
||||
if (data.type == _up) {
|
||||
pressed.remove(key);
|
||||
} else {
|
||||
pressed[key] = LogicalKeyboardKey(data.logical);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
},
|
||||
modifiers: () => flags,
|
||||
keyboard: _FakeKeyboard(pressed),
|
||||
platform: () => platform,
|
||||
);
|
||||
}
|
||||
|
||||
late final KeyModifierSync sync;
|
||||
final forwarded = <String>[];
|
||||
final pressed = <PhysicalKeyboardKey, LogicalKeyboardKey>{};
|
||||
KeyEventModifiers? flags;
|
||||
|
||||
void send(ui.KeyData data, [KeyEventModifiers? flags]) {
|
||||
this.flags = flags;
|
||||
sync.handle(data);
|
||||
}
|
||||
}
|
||||
|
||||
class _FakeKeyboard extends HardwareKeyboard {
|
||||
_FakeKeyboard(this._pressed);
|
||||
final Map<PhysicalKeyboardKey, LogicalKeyboardKey> _pressed;
|
||||
|
||||
@override
|
||||
Set<PhysicalKeyboardKey> get physicalKeysPressed => _pressed.keys.toSet();
|
||||
|
||||
@override
|
||||
LogicalKeyboardKey? lookUpLayout(PhysicalKeyboardKey physicalKey) =>
|
||||
_pressed[physicalKey];
|
||||
}
|
||||
|
||||
const _ctrl = KeyEventModifiers(control: true);
|
||||
const _ctrlMeta = KeyEventModifiers(control: true, meta: true);
|
||||
const _meta = KeyEventModifiers(meta: true);
|
||||
|
||||
final _c = (PhysicalKeyboardKey.keyC, LogicalKeyboardKey.keyC);
|
||||
final _metaLeft = (PhysicalKeyboardKey.metaLeft, LogicalKeyboardKey.metaLeft);
|
||||
final _ctrlLeft = (
|
||||
PhysicalKeyboardKey.controlLeft,
|
||||
LogicalKeyboardKey.controlLeft,
|
||||
);
|
||||
|
||||
ui.KeyData _key(
|
||||
ui.KeyEventType type,
|
||||
(PhysicalKeyboardKey, LogicalKeyboardKey) key, {
|
||||
bool synthesized = false,
|
||||
}) => keyData(type, key.$1, key.$2, synthesized: synthesized);
|
||||
|
||||
void main() {
|
||||
test('a physical Ctrl+C passes through untouched', () {
|
||||
final h = SyncHarness();
|
||||
h.send(_key(_down, _ctrlLeft), _ctrl);
|
||||
h.send(_key(_down, _c), _ctrl);
|
||||
h.send(_key(_up, _c), _ctrl);
|
||||
h.send(_key(_up, _ctrlLeft));
|
||||
expect(h.forwarded, ['+Control Left', '+Key C', '-Key C', '-Control Left']);
|
||||
expect(h.sync.controlFromFlags, isFalse);
|
||||
});
|
||||
|
||||
test("Omarchy's Super+C (Ctrl in the flags, no Ctrl key) presses Ctrl for "
|
||||
'the chord and releases it with the key', () {
|
||||
final h = SyncHarness();
|
||||
h.send(_key(_down, _metaLeft), _meta);
|
||||
// The engine releases Meta itself: the flags say it is up.
|
||||
h.send(_key(_up, _metaLeft, synthesized: true), _ctrl);
|
||||
h.send(_key(_down, _c), _ctrl);
|
||||
expect(h.sync.controlFromFlags, isTrue);
|
||||
h.send(_key(_repeat, _c), _ctrl);
|
||||
h.send(_key(_up, _c), _ctrl);
|
||||
expect(h.forwarded, [
|
||||
'+Meta Left',
|
||||
'-*Meta Left',
|
||||
'+*Control Left',
|
||||
'+Key C',
|
||||
'=Key C',
|
||||
'-Key C',
|
||||
'-*Control Left',
|
||||
]);
|
||||
expect(h.pressed, isEmpty);
|
||||
expect(h.sync.controlFromFlags, isFalse);
|
||||
});
|
||||
|
||||
test('outside Apple platforms a held Super merged into Ctrl+C is let go '
|
||||
'for the chord and pressed again after it', () {
|
||||
final h = SyncHarness();
|
||||
h.send(_key(_down, _metaLeft), _meta);
|
||||
h.send(_key(_down, _c), _ctrlMeta);
|
||||
expect(h.pressed.keys, {
|
||||
PhysicalKeyboardKey.controlLeft,
|
||||
PhysicalKeyboardKey.keyC,
|
||||
});
|
||||
h.send(_key(_up, _c), _ctrlMeta);
|
||||
expect(h.pressed.keys, {PhysicalKeyboardKey.metaLeft});
|
||||
h.send(_key(_up, _metaLeft));
|
||||
expect(h.forwarded, [
|
||||
'+Meta Left',
|
||||
'-*Meta Left',
|
||||
'+*Control Left',
|
||||
'+Key C',
|
||||
'-Key C',
|
||||
'-*Control Left',
|
||||
'+*Meta Left',
|
||||
'-Meta Left',
|
||||
]);
|
||||
expect(h.pressed, isEmpty);
|
||||
});
|
||||
|
||||
test('Super let go during the chord is not pressed again', () {
|
||||
final h = SyncHarness(platform: TargetPlatform.windows);
|
||||
h.send(_key(_down, _metaLeft), _meta);
|
||||
h.send(_key(_down, _c), _ctrlMeta);
|
||||
h.send(_key(_up, _metaLeft), _ctrl);
|
||||
h.send(_key(_up, _c), _ctrl);
|
||||
expect(h.pressed, isEmpty);
|
||||
expect(h.forwarded.where((e) => e.contains('Meta')), [
|
||||
'+Meta Left',
|
||||
'-*Meta Left',
|
||||
]);
|
||||
});
|
||||
|
||||
test('on macOS Cmd stays part of a Ctrl+Cmd chord', () {
|
||||
final h = SyncHarness(platform: TargetPlatform.macOS);
|
||||
h.send(_key(_down, _metaLeft), _meta);
|
||||
h.send(_key(_down, _c), _ctrlMeta);
|
||||
expect(h.pressed.keys, contains(PhysicalKeyboardKey.metaLeft));
|
||||
expect(h.pressed.keys, contains(PhysicalKeyboardKey.controlLeft));
|
||||
});
|
||||
|
||||
test("a real Ctrl key takes over from the flags' Ctrl", () {
|
||||
final h = SyncHarness();
|
||||
h.send(_key(_down, _c), _ctrl);
|
||||
h.send(_key(_down, _ctrlLeft), _ctrl);
|
||||
expect(h.forwarded, [
|
||||
'+*Control Left',
|
||||
'+Key C',
|
||||
'-*Control Left',
|
||||
'+Control Left',
|
||||
]);
|
||||
expect(h.sync.controlFromFlags, isFalse);
|
||||
h.send(_key(_up, _c), _ctrl);
|
||||
expect(h.pressed.keys, {PhysicalKeyboardKey.controlLeft});
|
||||
});
|
||||
|
||||
test('a key with no Ctrl in its flags releases a stale injected Ctrl', () {
|
||||
final h = SyncHarness();
|
||||
h.send(_key(_down, _c), _ctrl);
|
||||
final v = (PhysicalKeyboardKey.keyV, LogicalKeyboardKey.keyV);
|
||||
h.send(_key(_down, v), const KeyEventModifiers());
|
||||
expect(h.pressed.keys, {
|
||||
PhysicalKeyboardKey.keyC,
|
||||
PhysicalKeyboardKey.keyV,
|
||||
});
|
||||
});
|
||||
|
||||
test('unknown flags leave the event alone', () {
|
||||
final h = SyncHarness();
|
||||
h.send(_key(_down, _c));
|
||||
expect(h.forwarded, ['+Key C']);
|
||||
});
|
||||
|
||||
test('installs only on the web', () {
|
||||
installKeyModifierSync();
|
||||
expect(KeyModifierSync.instance, kIsWeb ? isNotNull : isNull);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_composer_bar.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_composer_controller.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_draft_store.dart';
|
||||
|
||||
import '../../../core/keyboard/browser_keys.dart';
|
||||
import 'chat_composer_bar_fixtures.dart';
|
||||
|
||||
/// The desktop's universal paste key (Omarchy's Super+V sends Ctrl+V while
|
||||
/// Super is held) pastes into the composer like Ctrl+V.
|
||||
void main() {
|
||||
late ComposerBarGateway gateway;
|
||||
late ChatComposerController composer;
|
||||
late int reads;
|
||||
|
||||
setUp(() {
|
||||
gateway = ComposerBarGateway();
|
||||
composer = ChatComposerController(
|
||||
gateway,
|
||||
's',
|
||||
drafts: MemoryChatDraftStore(),
|
||||
);
|
||||
reads = 0;
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||
.setMockMethodCallHandler(SystemChannels.platform, (call) async {
|
||||
if (call.method == 'Clipboard.getData') {
|
||||
reads++;
|
||||
return {'text': 'copied text'};
|
||||
}
|
||||
if (call.method == 'Clipboard.hasStrings') return {'value': true};
|
||||
return null;
|
||||
});
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
composer.dispose();
|
||||
gateway.close();
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||
.setMockMethodCallHandler(SystemChannels.platform, null);
|
||||
});
|
||||
|
||||
for (final merged in [false, true]) {
|
||||
testWidgets(
|
||||
'Super+V (Ctrl${merged ? ' and Meta' : ''} in the flags) pastes into '
|
||||
'the composer',
|
||||
(tester) async {
|
||||
final keys = BrowserKeys(tester);
|
||||
await tester.pumpWidget(
|
||||
materialHost(ChatComposerBar(composer: composer, running: false)),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.showKeyboard(find.byKey(const Key('chat-composer-field')));
|
||||
await keys.superChord(LogicalKeyboardKey.keyV, mergedMeta: merged);
|
||||
await tester.pumpAndSettle();
|
||||
expect(reads, 1);
|
||||
expect(composer.draft, 'copied text');
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
},
|
||||
variant: TargetPlatformVariant({
|
||||
TargetPlatform.linux,
|
||||
TargetPlatform.windows,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
testWidgets(
|
||||
'Super+V, Super+A and Super+X work in any text field (question answers, '
|
||||
'find bars)',
|
||||
(tester) async {
|
||||
final keys = BrowserKeys(tester);
|
||||
final controller = TextEditingController(text: 'answer: ');
|
||||
addTearDown(controller.dispose);
|
||||
await tester.pumpWidget(
|
||||
materialHost(TextField(controller: controller, autofocus: true)),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
controller.selection = const TextSelection.collapsed(offset: 8);
|
||||
await keys.superChord(LogicalKeyboardKey.keyV, mergedMeta: true);
|
||||
await tester.pumpAndSettle();
|
||||
expect(controller.text, 'answer: copied text');
|
||||
await keys.superChord(LogicalKeyboardKey.keyA);
|
||||
expect(controller.selection.textInside(controller.text), controller.text);
|
||||
await keys.superChord(LogicalKeyboardKey.keyX);
|
||||
await tester.pumpAndSettle();
|
||||
expect(controller.text, isEmpty);
|
||||
},
|
||||
variant: TargetPlatformVariant({
|
||||
TargetPlatform.linux,
|
||||
TargetPlatform.windows,
|
||||
}),
|
||||
);
|
||||
}
|
||||
@@ -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),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/keyboard/key_event_modifiers.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/transcript/chat_link_text.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/transcript/chat_markdown_view.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/transcript/chat_transcript_selection.dart';
|
||||
|
||||
import '../../../../core/keyboard/browser_keys.dart';
|
||||
import 'chat_transcript_selection_support.dart';
|
||||
|
||||
/// The desktop's universal copy key (Omarchy's Super+C sends Ctrl+C while
|
||||
/// Super is held) copies the chat's selection like Ctrl+C (Cmd+C on a Mac),
|
||||
/// whether the browser reports Ctrl alone or Ctrl and Meta for it.
|
||||
void main() {
|
||||
late List<String> copied;
|
||||
setUp(() {
|
||||
ChatTranscriptSelectionArea.debugEnabled = true;
|
||||
copied = recordTranscriptClipboard();
|
||||
});
|
||||
tearDown(() => ChatTranscriptSelectionArea.debugEnabled = null);
|
||||
|
||||
Future<void> selectAnswer(WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
const MaterialApp(
|
||||
home: Scaffold(
|
||||
body: ChatTranscriptSelectionArea(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
ChatLinkText('Question 18'),
|
||||
ChatMarkdownView(text: 'Answer 19 with `code`.'),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
final answer = tester.getRect(find.textContaining('Answer 19'));
|
||||
await dragTranscriptSelection(
|
||||
tester,
|
||||
answer.centerLeft + const Offset(1, 0),
|
||||
answer.centerRight - const Offset(1, 0),
|
||||
);
|
||||
}
|
||||
|
||||
bool apple() =>
|
||||
defaultTargetPlatform == TargetPlatform.macOS ||
|
||||
defaultTargetPlatform == TargetPlatform.iOS;
|
||||
|
||||
for (final merged in [false, true]) {
|
||||
testWidgets(
|
||||
'Super+C (Ctrl${merged ? ' and Meta' : ''} in the flags) copies the '
|
||||
'selection',
|
||||
(tester) async {
|
||||
final keys = BrowserKeys(tester);
|
||||
await selectAnswer(tester);
|
||||
await keys.superChord(LogicalKeyboardKey.keyC, mergedMeta: merged);
|
||||
// On a Mac, Ctrl+C (with or without Cmd) is not the copy key.
|
||||
expect(copied, apple() ? isEmpty : ['Answer 19 with code.']);
|
||||
},
|
||||
variant: TargetPlatformVariant.desktop(),
|
||||
);
|
||||
}
|
||||
|
||||
testWidgets(
|
||||
'the keyboard copy keys copy through the browser key path',
|
||||
(tester) async {
|
||||
final keys = BrowserKeys(tester);
|
||||
await selectAnswer(tester);
|
||||
if (apple()) {
|
||||
await keys.chord(
|
||||
LogicalKeyboardKey.metaLeft,
|
||||
LogicalKeyboardKey.keyC,
|
||||
flags: const KeyEventModifiers(meta: true),
|
||||
);
|
||||
expect(copied, ['Answer 19 with code.']);
|
||||
return;
|
||||
}
|
||||
await keys.chord(LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.keyC);
|
||||
await keys.chord(
|
||||
LogicalKeyboardKey.controlLeft,
|
||||
LogicalKeyboardKey.insert,
|
||||
);
|
||||
expect(copied, ['Answer 19 with code.', 'Answer 19 with code.']);
|
||||
// A Ctrl the flags pressed is released with its chord.
|
||||
expect(HardwareKeyboard.instance.isControlPressed, isFalse);
|
||||
},
|
||||
variant: TargetPlatformVariant.desktop(),
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user