Paste files and images into the chat composer on macOS
Native hosts reported clipboard-file paste as unsupported, and Flutter's Clipboard carries text only, so Cmd+V of a Finder file or a screenshot in the macOS app pasted nothing (or the file name). The macOS Runner now answers a pipelines/clipboard_files channel with the pasteboard's file URLs, else its image as PNG; the composer's Cmd+V reads it and attaches the files, falling back to the pasteboard text when there are none. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -328,8 +328,12 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
|
||||
bool _canAcceptTextPaste() => _canAcceptPaste() && !_focus.hasFocus;
|
||||
|
||||
KeyEventResult _nativePasteKey(KeyEvent event) {
|
||||
// Web uses the browser's clipboard event, never a second Clipboard read.
|
||||
if (kIsWeb || event is! KeyDownEvent || !_canAcceptTextPaste()) {
|
||||
// Web uses the browser's clipboard event, never a second Clipboard read; a
|
||||
// native host that reads clipboard files (macOS) owns its paste key too.
|
||||
if (kIsWeb ||
|
||||
_paste.isSupported ||
|
||||
event is! KeyDownEvent ||
|
||||
!_canAcceptTextPaste()) {
|
||||
return KeyEventResult.ignored;
|
||||
}
|
||||
final keys = HardwareKeyboard.instance;
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
import 'chat_attachment_picker.dart';
|
||||
import 'chat_clipboard_paste.dart';
|
||||
|
||||
/// The macOS Runner's pasteboard reader (macos/Runner/MainFlutterWindow.swift).
|
||||
const MethodChannel chatClipboardFilesChannel = MethodChannel(
|
||||
'pipelines/clipboard_files',
|
||||
);
|
||||
|
||||
/// Clipboard-file paste for a desktop host whose Runner answers
|
||||
/// [chatClipboardFilesChannel].
|
||||
///
|
||||
/// Flutter's own [Clipboard] carries text only, so a native host reads the
|
||||
/// pasteboard through the Runner. Each subscription watches for the platform
|
||||
/// paste shortcut (Cmd+V). When its composer may take the paste it consumes
|
||||
/// the key, asks the Runner for the pasteboard's files (Finder file URLs, else
|
||||
/// a copied or screenshot image) and hands them over; a pasteboard without
|
||||
/// files pastes its text into the composer instead, so the consumed key never
|
||||
/// loses a text paste.
|
||||
ChatClipboardPaste nativeChatClipboardPaste({
|
||||
MethodChannel channel = chatClipboardFilesChannel,
|
||||
}) => ChatClipboardPaste(
|
||||
subscribe: (onFiles, canAccept, onError, onText, canAcceptText) {
|
||||
var sequence = 0;
|
||||
KeyEventResult handle(KeyEvent event) {
|
||||
if (event is! KeyDownEvent ||
|
||||
event.logicalKey != LogicalKeyboardKey.keyV ||
|
||||
!chatIsNativePasteShortcut() ||
|
||||
!canAccept()) {
|
||||
return KeyEventResult.ignored;
|
||||
}
|
||||
unawaited(
|
||||
_readPasteboard(channel, onFiles, onError, onText, () => ++sequence),
|
||||
);
|
||||
return KeyEventResult.handled;
|
||||
}
|
||||
|
||||
FocusManager.instance.addEarlyKeyEventHandler(handle);
|
||||
return () => FocusManager.instance.removeEarlyKeyEventHandler(handle);
|
||||
},
|
||||
);
|
||||
|
||||
/// Whether the held modifiers make V the host's paste shortcut: Cmd alone on
|
||||
/// macOS.
|
||||
bool chatIsNativePasteShortcut() {
|
||||
final keys = HardwareKeyboard.instance;
|
||||
return keys.isMetaPressed &&
|
||||
!keys.isControlPressed &&
|
||||
!keys.isAltPressed &&
|
||||
!keys.isShiftPressed;
|
||||
}
|
||||
|
||||
Future<void> _readPasteboard(
|
||||
MethodChannel channel,
|
||||
ChatPasteFiles onFiles,
|
||||
ChatPasteFailed onError,
|
||||
ChatPasteText onText,
|
||||
int Function() nextSequence,
|
||||
) async {
|
||||
Map<Object?, Object?>? reply;
|
||||
try {
|
||||
reply = await channel.invokeMapMethod<Object?, Object?>('read');
|
||||
} catch (error) {
|
||||
onError('Could not read pasted files: $error');
|
||||
}
|
||||
final files = <ChatPickedFile>[];
|
||||
for (final raw in (reply?['files'] as List<Object?>?) ?? const []) {
|
||||
if (raw is! Map) continue;
|
||||
final mime = (raw['mime'] as String?) ?? '';
|
||||
final recorded = ((raw['name'] as String?) ?? '').trim();
|
||||
final bytes = raw['bytes'];
|
||||
files.add(
|
||||
ChatPickedFile(
|
||||
name: recorded.isEmpty
|
||||
? chatPastedAttachmentName(mime, nextSequence())
|
||||
: recorded,
|
||||
mime: mime,
|
||||
bytes: bytes is Uint8List ? bytes : Uint8List(0),
|
||||
),
|
||||
);
|
||||
}
|
||||
final failures = [
|
||||
for (final failure in (reply?['errors'] as List<Object?>?) ?? const [])
|
||||
'$failure',
|
||||
];
|
||||
if (files.isNotEmpty) {
|
||||
// As on web: kept files first, since adding clears an older error.
|
||||
onFiles(files);
|
||||
failures.forEach(onError);
|
||||
return;
|
||||
}
|
||||
failures.forEach(onError);
|
||||
if (failures.isNotEmpty) return;
|
||||
try {
|
||||
final text = (await Clipboard.getData(Clipboard.kTextPlain))?.text;
|
||||
if (text != null && text.isNotEmpty) onText(text);
|
||||
} catch (_) {
|
||||
onError('Could not read clipboard text.');
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether [platform] has a Runner that answers [chatClipboardFilesChannel].
|
||||
bool chatHostReadsClipboardFiles(TargetPlatform platform) =>
|
||||
!kIsWeb && platform == TargetPlatform.macOS;
|
||||
@@ -1,12 +1,20 @@
|
||||
import 'chat_clipboard_paste.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
/// Native fallback for clipboard-file paste.
|
||||
import 'chat_clipboard_paste.dart';
|
||||
import 'chat_clipboard_paste_native.dart';
|
||||
|
||||
/// Native clipboard-file paste.
|
||||
///
|
||||
/// The composer keeps the host field's own text paste on every platform; this
|
||||
/// slice observes clipboard files on web only. A non-web host reports that
|
||||
/// missing capability rather than pretending no paste happened.
|
||||
/// macOS reads the pasteboard through its Runner
|
||||
/// ([nativeChatClipboardPaste]). Other native hosts have no pasteboard reader
|
||||
/// yet: they report that missing capability rather than pretending no paste
|
||||
/// happened, and the composer keeps the host field's own text paste.
|
||||
const String chatClipboardPasteUnsupportedMessage =
|
||||
'Pasting files is not supported on this platform yet.';
|
||||
|
||||
ChatClipboardPaste get hostChatClipboardPaste =>
|
||||
const ChatClipboardPaste.unsupported(chatClipboardPasteUnsupportedMessage);
|
||||
chatHostReadsClipboardFiles(defaultTargetPlatform)
|
||||
? nativeChatClipboardPaste()
|
||||
: const ChatClipboardPaste.unsupported(
|
||||
chatClipboardPasteUnsupportedMessage,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user