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: [:]) } }