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] = [] var urls = (board.readObjects( forClasses: [NSURL.self], options: [.urlReadingFileURLsOnly: true]) ?? []).compactMap { $0 as? URL } // Older apps put a Finder copy only in the legacy filenames list. if urls.isEmpty, let paths = board.propertyList( forType: NSPasteboard.PasteboardType("NSFilenamesPboardType")) as? [String] { urls = paths.map { URL(fileURLWithPath: $0) } } 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)]) } // What the pasteboard held, so an unreadable paste can say so. let types = (board.types ?? []).map { $0.rawValue } return ["files": files, "errors": errors, "types": types] } /// The pasteboard's image as PNG: its own PNG, else its TIFF converted, /// else any image AppKit can read from it (JPEG, HEIC, PDF, ...). private static func image(_ board: NSPasteboard) -> Data? { if let png = board.data(forType: .png) { return png } if let tiff = board.data(forType: .tiff), let rep = NSBitmapImageRep(data: tiff), let png = rep.representation(using: .png, properties: [:]) { return png } guard let image = NSImage(pasteboard: board), let tiff = image.tiffRepresentation, let rep = NSBitmapImageRep(data: tiff) else { return nil } return rep.representation(using: .png, properties: [:]) } }