Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import UIKit

@objc(PasteboardPlugin)
public class PasteboardPlugin: NSObject, FlutterPlugin {
// Pasteboard access can block, e.g. while another app still provides the
// data, so image and writeImage run off the main thread, in call order.
private static let queue = DispatchQueue(label: "one.mixin.pasteboard", qos: .userInitiated)

public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "pasteboard", binaryMessenger: registrar.messenger())
let instance = PasteboardPlugin()
Expand Down Expand Up @@ -32,10 +36,11 @@ public class PasteboardPlugin: NSObject, FlutterPlugin {
}
}

private func image(result: FlutterResult) {
let image = UIPasteboard.general.image
let data = image?.pngData()
result(data)
private func image(result: @escaping FlutterResult) {
Self.queue.async {
let data = UIPasteboard.general.image?.pngData()
DispatchQueue.main.async { result(data) }
}
}

private func files(result: FlutterResult) {
Expand All @@ -46,10 +51,11 @@ public class PasteboardPlugin: NSObject, FlutterPlugin {
result(nil)
}

private func writeImageToPasteboard(_ data: Data, result: FlutterResult) {
let image = UIImage(data: data)
UIPasteboard.general.image = image
result(nil)
private func writeImageToPasteboard(_ data: Data, result: @escaping FlutterResult) {
Self.queue.async {
UIPasteboard.general.image = UIImage(data: data)
DispatchQueue.main.async { result(nil) }
}
}
}

8 changes: 8 additions & 0 deletions packages/pasteboard/lib/pasteboard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class Pasteboard {
/// Returns the image data of the pasteboard.
///
/// available on iOS, Desktop, Web, Android
///
/// iOS: runs off the main thread, in order with [writeImage]. Await it
/// before calling [text], [writeText] or Flutter's `Clipboard` if the
/// order matters.
static Future<Uint8List?> get image => pasteboard.image;

/// only available on Windows and the web.
Expand All @@ -18,6 +22,10 @@ class Pasteboard {
/// available on iOS, Android, Web
///
/// set image data to system pasteboard.
///
/// iOS: runs off the main thread, in order with [image]. Await it before
/// calling [text], [writeText] or Flutter's `Clipboard` if the order
/// matters.
static Future<void> writeImage(Uint8List? image) =>
pasteboard.writeImage(image);

Expand Down