From d83d679eb9ac5e3b9f5b4b8e9f3390be00e03580 Mon Sep 17 00:00:00 2001 From: nyne Date: Thu, 27 Mar 2025 19:40:51 +0800 Subject: [PATCH] Implement `writeImageToClipboard` on macOS. --- lib/utils/clipboard_image.dart | 5 ++++- macos/Runner/AppDelegate.swift | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/utils/clipboard_image.dart b/lib/utils/clipboard_image.dart index 045276d..4201cec 100644 --- a/lib/utils/clipboard_image.dart +++ b/lib/utils/clipboard_image.dart @@ -15,8 +15,11 @@ Future writeImageToClipboard(Uint8List imageBytes) async { "data": Uint8List.view(data!.buffer) }); image.dispose(); + } else if (Platform.isMacOS) { + await channel.invokeMethod("writeImageToClipboard", { + "data": imageBytes, + }); } else { - // TODO: Implement for other platforms throw UnsupportedError("Clipboard image is not supported on this platform"); } } diff --git a/macos/Runner/AppDelegate.swift b/macos/Runner/AppDelegate.swift index 906f330..d3e795e 100644 --- a/macos/Runner/AppDelegate.swift +++ b/macos/Runner/AppDelegate.swift @@ -38,6 +38,31 @@ class AppDelegate: FlutterAppDelegate { result(FlutterMethodNotImplemented) } } + + let clipboardChannel = FlutterMethodChannel(name: "venera/clipboard", binaryMessenger: controller.engine.binaryMessenger) + + clipboardChannel.setMethodCallHandler { (call, result) in + switch call.method { + case "writeImageToClipboard": + guard let arguments = call.arguments as? [String: Any], + let data = arguments["data"] as? FlutterStandardTypedData else { + result(FlutterError(code: "INVALID_ARGUMENTS", message: "Invalid arguments", details: nil)) + return + } + + guard let image = NSImage(data: data.data) else { + result(FlutterError(code: "INVALID_IMAGE", message: "Could not create image from data", details: nil)) + return + } + + let pasteboard = NSPasteboard.general + pasteboard.clearContents() + pasteboard.writeObjects([image]) + result(true) + default: + result(FlutterMethodNotImplemented) + } + } } func getDirectoryPath() {