Files
pipelines/app/lib/application/clipboard_failures.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

101 lines
3.4 KiB
Dart

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