From f8ea50fd700cfa36ffce5d1fd7d377f7ecdb7dfd Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sat, 21 Nov 2026 20:42:05 +0000 Subject: [PATCH] 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 --- .../chat/conversation/chat_composer_bar.dart | 8 +- .../domain/chat_clipboard_paste_native.dart | 109 +++++++++++ .../domain/chat_clipboard_paste_stub.dart | 20 +- app/macos/Runner/MainFlutterWindow.swift | 66 +++++++ .../chat_clipboard_paste_native_test.dart | 178 ++++++++++++++++++ 5 files changed, 373 insertions(+), 8 deletions(-) create mode 100644 app/lib/features/chat/domain/chat_clipboard_paste_native.dart create mode 100644 app/test/features/chat/domain/chat_clipboard_paste_native_test.dart diff --git a/app/lib/features/chat/conversation/chat_composer_bar.dart b/app/lib/features/chat/conversation/chat_composer_bar.dart index 35899d97..9747608d 100644 --- a/app/lib/features/chat/conversation/chat_composer_bar.dart +++ b/app/lib/features/chat/conversation/chat_composer_bar.dart @@ -328,8 +328,12 @@ class _ChatComposerBarState extends State { 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; diff --git a/app/lib/features/chat/domain/chat_clipboard_paste_native.dart b/app/lib/features/chat/domain/chat_clipboard_paste_native.dart new file mode 100644 index 00000000..f155c15e --- /dev/null +++ b/app/lib/features/chat/domain/chat_clipboard_paste_native.dart @@ -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 _readPasteboard( + MethodChannel channel, + ChatPasteFiles onFiles, + ChatPasteFailed onError, + ChatPasteText onText, + int Function() nextSequence, +) async { + Map? reply; + try { + reply = await channel.invokeMapMethod('read'); + } catch (error) { + onError('Could not read pasted files: $error'); + } + final files = []; + for (final raw in (reply?['files'] as List?) ?? 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?) ?? 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; diff --git a/app/lib/features/chat/domain/chat_clipboard_paste_stub.dart b/app/lib/features/chat/domain/chat_clipboard_paste_stub.dart index 3b390d00..e93ec3ec 100644 --- a/app/lib/features/chat/domain/chat_clipboard_paste_stub.dart +++ b/app/lib/features/chat/domain/chat_clipboard_paste_stub.dart @@ -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, + ); diff --git a/app/macos/Runner/MainFlutterWindow.swift b/app/macos/Runner/MainFlutterWindow.swift index 3cc05eb2..d6c20729 100644 --- a/app/macos/Runner/MainFlutterWindow.swift +++ b/app/macos/Runner/MainFlutterWindow.swift @@ -1,5 +1,6 @@ import Cocoa import FlutterMacOS +import UniformTypeIdentifiers class MainFlutterWindow: NSWindow { override func awakeFromNib() { @@ -9,7 +10,72 @@ class MainFlutterWindow: NSWindow { self.setFrame(windowFrame, display: true) RegisterGeneratedPlugins(registry: flutterViewController) + ClipboardFiles.register(with: flutterViewController.engine.binaryMessenger) super.awakeFromNib() } } + +/// Reads the files on the general pasteboard for the chat composer's Cmd+V +/// (lib/features/chat/domain/chat_clipboard_paste_native.dart). Flutter's own +/// clipboard carries text only. +/// +/// `read` replies `{files: [{name, mime, bytes}], errors: [String]}`: the +/// pasteboard's file URLs (a Finder copy) when it has any, else one copied +/// image (a screenshot or an image copied from another app) as PNG with no +/// name, else no files. +enum ClipboardFiles { + static func register(with messenger: FlutterBinaryMessenger) { + let channel = FlutterMethodChannel( + name: "pipelines/clipboard_files", binaryMessenger: messenger) + channel.setMethodCallHandler { call, result in + guard call.method == "read" else { + result(FlutterMethodNotImplemented) + return + } + result(read(NSPasteboard.general)) + } + } + + static func read(_ board: NSPasteboard) -> [String: Any] { + var files: [[String: Any]] = [] + var errors: [String] = [] + let urls = + board.readObjects( + forClasses: [NSURL.self], + options: [.urlReadingFileURLsOnly: true]) as? [URL] ?? [] + for url in urls { + var isDirectory: ObjCBool = false + if FileManager.default.fileExists(atPath: url.path, isDirectory: &isDirectory), + isDirectory.boolValue + { + errors.append("\(url.lastPathComponent) is a folder; paste its files instead.") + continue + } + do { + let data = try Data(contentsOf: url) + let mime = UTType(filenameExtension: url.pathExtension)?.preferredMIMEType ?? "" + files.append([ + "name": url.lastPathComponent, + "mime": mime, + "bytes": FlutterStandardTypedData(bytes: data), + ]) + } catch { + errors.append("Could not read \(url.lastPathComponent): \(error.localizedDescription)") + } + } + if urls.isEmpty, let png = image(board) { + files.append(["name": "", "mime": "image/png", "bytes": FlutterStandardTypedData(bytes: png)]) + } + return ["files": files, "errors": errors] + } + + /// The pasteboard's image as PNG: its own PNG, else its TIFF converted. + private static func image(_ board: NSPasteboard) -> Data? { + if let png = board.data(forType: .png) { return png } + guard let tiff = board.data(forType: .tiff), + let rep = NSBitmapImageRep(data: tiff) + else { return nil } + return rep.representation(using: .png, properties: [:]) + } +} diff --git a/app/test/features/chat/domain/chat_clipboard_paste_native_test.dart b/app/test/features/chat/domain/chat_clipboard_paste_native_test.dart new file mode 100644 index 00000000..262b3c1c --- /dev/null +++ b/app/test/features/chat/domain/chat_clipboard_paste_native_test.dart @@ -0,0 +1,178 @@ + +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/chat/conversation/chat_composer_bar.dart'; +import 'package:pipelines_app/features/chat/conversation/chat_composer_controller.dart'; +import 'package:pipelines_app/features/chat/conversation/chat_draft_store.dart'; +import 'package:pipelines_app/features/chat/domain/chat_clipboard_paste.dart'; +import 'package:pipelines_app/features/chat/domain/chat_clipboard_paste_native.dart'; + +import '../conversation/chat_composer_bar_fixtures.dart'; + +final _macOS = TargetPlatformVariant.only(TargetPlatform.macOS); + +Finder get _field => find.byKey(const Key('chat-composer-field')); + +void main() { + late ComposerBarGateway gateway; + late ChatComposerController composer; + late Map pasteboard; + late int fileReads, textReads; + + setUp(() { + gateway = ComposerBarGateway(); + composer = ChatComposerController( + gateway, + 's', + drafts: MemoryChatDraftStore(), + ); + pasteboard = {'files': const [], 'errors': const []}; + fileReads = 0; + textReads = 0; + final messenger = + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger; + messenger.setMockMethodCallHandler(chatClipboardFilesChannel, (call) async { + expect(call.method, 'read'); + fileReads++; + return pasteboard; + }); + messenger.setMockMethodCallHandler(SystemChannels.platform, (call) async { + if (call.method == 'Clipboard.getData') { + textReads++; + return {'text': 'copied text'}; + } + if (call.method == 'Clipboard.hasStrings') return {'value': true}; + return null; + }); + }); + + tearDown(() { + composer.dispose(); + gateway.close(); + final messenger = + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger; + messenger.setMockMethodCallHandler(chatClipboardFilesChannel, null); + messenger.setMockMethodCallHandler(SystemChannels.platform, null); + }); + + // Production wiring: no injected paste, so the composer takes the host's. + Future mount(WidgetTester tester) async { + await tester.pumpWidget( + materialHost(ChatComposerBar(composer: composer, running: false)), + ); + await tester.pumpAndSettle(); + } + + Future paste( + WidgetTester tester, { + LogicalKeyboardKey modifier = LogicalKeyboardKey.metaLeft, + }) async { + await tester.sendKeyDownEvent(modifier); + await tester.sendKeyEvent(LogicalKeyboardKey.keyV); + await tester.sendKeyUpEvent(modifier); + await tester.pumpAndSettle(); + } + + Future done(WidgetTester tester) => tester.pumpWidget(const SizedBox()); + + test('only the macOS host reads clipboard files natively', () { + expect(chatHostReadsClipboardFiles(TargetPlatform.macOS), isTrue); + for (final platform in [ + TargetPlatform.linux, + TargetPlatform.windows, + TargetPlatform.android, + TargetPlatform.iOS, + ]) { + expect(chatHostReadsClipboardFiles(platform), isFalse); + } + }); + + testWidgets('the macOS host supports clipboard-file paste', (tester) async { + expect(hostChatClipboardPaste.isSupported, isTrue); + }, variant: _macOS); + + for (final focused in [true, false]) { + testWidgets( + 'Cmd+V attaches a Finder file and a screenshot ' + '(${focused ? 'focused' : 'unfocused'} composer)', + (tester) async { + pasteboard = { + 'files': [ + { + 'name': 'report.pdf', + 'mime': 'application/pdf', + 'bytes': Uint8List.fromList([1, 2]), + }, + { + 'name': '', + 'mime': 'image/png', + 'bytes': Uint8List.fromList([3]), + }, + ], + 'errors': const [], + }; + await mount(tester); + if (focused) { + await tester.showKeyboard(_field); + } else { + FocusManager.instance.primaryFocus?.unfocus(); + } + await tester.pump(); + await paste(tester); + + expect(fileReads, 1); + expect(find.text('report.pdf'), findsOneWidget); + expect(find.text('pasted-1.png'), findsOneWidget); + // The pasteboard's file-name text is not pasted alongside the files. + expect(textReads, 0); + expect(composer.draft, isEmpty); + await done(tester); + }, + variant: _macOS, + ); + } + + testWidgets('Cmd+V of plain text pastes it once into the focused composer', ( + tester, + ) async { + await mount(tester); + await tester.showKeyboard(_field); + await paste(tester); + + expect(fileReads, 1); + expect(textReads, 1); + expect(composer.draft, 'copied text'); + await done(tester); + }, variant: _macOS); + + testWidgets('an unreadable pasted file is reported, not dropped silently', ( + tester, + ) async { + pasteboard = { + 'files': const [], + 'errors': ['Downloads is a folder; paste its files instead.'], + }; + await mount(tester); + await tester.showKeyboard(_field); + await paste(tester); + + expect( + find.text('Downloads is a folder; paste its files instead.'), + findsOneWidget, + ); + expect(composer.draft, isEmpty); + await done(tester); + }, variant: _macOS); + + testWidgets('Ctrl+V is not the macOS paste shortcut', (tester) async { + await mount(tester); + FocusManager.instance.primaryFocus?.unfocus(); + await tester.pump(); + await paste(tester, modifier: LogicalKeyboardKey.controlLeft); + + expect(fileReads, 0); + expect(composer.draft, isEmpty); + await done(tester); + }, variant: _macOS); +}