Skip to content

Commit 96a592b

Browse files
[macos] Adds clipboard string read/write support to macOS (flutter#9313)
1 parent 1cf980f commit 96a592b

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

shell/platform/darwin/macos/framework/Source/FLEViewController.mm

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
namespace {
2020

21+
/// Clipboard plain text format.
22+
constexpr char kTextPlainFormat[] = "text/plain";
23+
2124
/**
2225
* State tracking for mouse events, to adapt between the events coming from the system and the
2326
* events that the embedding API expects.
@@ -156,6 +159,18 @@ - (void)onSettingsChanged:(NSNotification*)notification;
156159
*/
157160
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result;
158161

162+
/**
163+
* Reads the data from the clipboard. |format| specifies the media type of the
164+
* data to obtain.
165+
*/
166+
- (NSDictionary*)getClipboardData:(NSString*)format;
167+
168+
/**
169+
* Clears contents and writes new data into clipboard. |data| is a dictionary where
170+
* the keys are the type of data, and tervalue the data to be stored.
171+
*/
172+
- (void)setClipboardData:(NSDictionary*)data;
173+
159174
@end
160175

161176
#pragma mark - Static methods provided to engine configuration
@@ -612,11 +627,34 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
612627
if ([call.method isEqualToString:@"SystemNavigator.pop"]) {
613628
[NSApp terminate:self];
614629
result(nil);
630+
} else if ([call.method isEqualToString:@"Clipboard.getData"]) {
631+
result([self getClipboardData:call.arguments]);
632+
} else if ([call.method isEqualToString:@"Clipboard.setData"]) {
633+
[self setClipboardData:call.arguments];
634+
result(nil);
615635
} else {
616636
result(FlutterMethodNotImplemented);
617637
}
618638
}
619639

640+
- (NSDictionary*)getClipboardData:(NSString*)format {
641+
NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
642+
if ([format isEqualToString:@(kTextPlainFormat)]) {
643+
NSString* stringInPasteboard = [pasteboard stringForType:NSPasteboardTypeString];
644+
return stringInPasteboard == nil ? nil : @{@"text" : stringInPasteboard};
645+
}
646+
return nil;
647+
}
648+
649+
- (void)setClipboardData:(NSDictionary*)data {
650+
NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
651+
NSString* text = data[@"text"];
652+
if (text && ![text isEqual:[NSNull null]]) {
653+
[pasteboard clearContents];
654+
[pasteboard setString:text forType:NSPasteboardTypeString];
655+
}
656+
}
657+
620658
#pragma mark - FLEReshapeListener
621659

622660
/**

0 commit comments

Comments
 (0)