f8ea50fd70
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>
110 lines
3.6 KiB
Dart
110 lines
3.6 KiB
Dart
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;
|