Files
pipelines/app/macos/Runner/MainFlutterWindow.swift
T
alan f8ea50fd70 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>
2026-11-21 20:42:05 +00:00

82 lines
2.9 KiB
Swift

import Cocoa
import FlutterMacOS
import UniformTypeIdentifiers
class MainFlutterWindow: NSWindow {
override func awakeFromNib() {
let flutterViewController = FlutterViewController()
let windowFrame = self.frame
self.contentViewController = flutterViewController
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: [:])
}
}