Skip to content

Commit

Permalink
feat: add writeImage support for MacOS. (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
plateaukao authored Jun 1, 2023
1 parent b665d4f commit 8a00c5e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/pasteboard/lib/src/pasteboard_platform_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class PasteboardPlatformIO implements PasteboardPlatform {
if (image == null) {
return;
}
if (Platform.isIOS) {
if (Platform.isIOS || Platform.isMacOS) {
await _channel.invokeMethod<void>('writeImage', image);
}
}
Expand Down
14 changes: 14 additions & 0 deletions packages/pasteboard/macos/Classes/PasteboardPlugin.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Cocoa
import FlutterMacOS
import AppKit

public class PasteboardPlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterPluginRegistrar) {
Expand All @@ -20,11 +21,24 @@ public class PasteboardPlugin: NSObject, FlutterPlugin {
} else {
result(FlutterError(code: "0", message: "arguments is not String list.", details: nil))
}
case "writeImage":
if let data = call.arguments as? FlutterStandardTypedData {
writeImageToPasteboard(data.data, result: result)
} else {
result(FlutterError(code: "0", message: "arguments is not data", details: nil))
}
default:
result(FlutterMethodNotImplemented)
}
}

private func writeImageToPasteboard(_ data: Data, result: FlutterResult) {
let image = NSImage(data: data) ?? NSImage()
NSPasteboard.general.clearContents()
NSPasteboard.general.writeObjects([image as NSImage])
result(nil)
}

private func image(result: FlutterResult) {
guard let image = NSPasteboard.general.readObjects(forClasses: [NSImage.self], options: nil)?.first as? NSImage else {
result(nil)
Expand Down

0 comments on commit 8a00c5e

Please sign in to comment.