Files
pipelines/app/test/core/keyboard/browser_keys.dart
T
alan 3d739c3bfc 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>
2026-11-29 17:57:28 +00:00

205 lines
6.6 KiB
Dart

// 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');
}
}