import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; /// The app's root messenger, for messages no screen owns. final GlobalKey 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? 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 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); }