From aa9cd75fad7da4a853d6ef6f7d7f946571d97467 Mon Sep 17 00:00:00 2001 From: Francisco Magdaleno Arceo Date: Thu, 13 Jun 2019 10:18:17 -0700 Subject: [PATCH 1/3] [macos] Adds clipboard string read/write support to macOS --- .../framework/Source/FLEViewController.mm | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/shell/platform/darwin/macos/framework/Source/FLEViewController.mm b/shell/platform/darwin/macos/framework/Source/FLEViewController.mm index 86f6fc64761ef..bc9ead3ea43ae 100644 --- a/shell/platform/darwin/macos/framework/Source/FLEViewController.mm +++ b/shell/platform/darwin/macos/framework/Source/FLEViewController.mm @@ -18,6 +18,9 @@ namespace { +/// Clipboard plain text format. +constexpr char kTextPlainFormat[] = "text/plain"; + /** * State tracking for mouse events, to adapt between the events coming from the system and the * events that the embedding API expects. @@ -612,11 +615,34 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { if ([call.method isEqualToString:@"SystemNavigator.pop"]) { [NSApp terminate:self]; result(nil); + } else if ([call.method isEqualToString:@"Clipboard.getData"]) { + result([self getClipboardData:call.arguments]); + } else if ([call.method isEqualToString:@"Clipboard.setData"]) { + [self setClipboardData:call.arguments]; + result(nil); } else { result(FlutterMethodNotImplemented); } } +#pragma mark - ClipboardData + +- (NSDictionary*)getClipboardData:(NSString*)format { + NSPasteboard* pasteboard = [NSPasteboard generalPasteboard]; + if (!format || [format isEqualToString:@(kTextPlainFormat)]) { + id stringInPasteboard = [pasteboard stringForType:NSPasteboardTypeString]; + // The pasteboard may contain an item but it may not be a string (an image for instance). + return stringInPasteboard == nil ? nil : @{@"text" : stringInPasteboard}; + } + return nil; +} + +- (void)setClipboardData:(NSDictionary*)data { + NSPasteboard* pasteboard = [NSPasteboard generalPasteboard]; + [pasteboard declareTypes:@[ NSPasteboardTypeString ] owner:self]; + [pasteboard setString:data[@"text"] forType:NSPasteboardTypeString]; +} + #pragma mark - FLEReshapeListener /** From 84d84834b194f7ebacde7e6d2c089b9e3c721c75 Mon Sep 17 00:00:00 2001 From: Francisco Magdaleno Arceo Date: Thu, 13 Jun 2019 12:20:32 -0700 Subject: [PATCH 2/3] Address review comments --- .../framework/Source/FLEViewController.mm | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/shell/platform/darwin/macos/framework/Source/FLEViewController.mm b/shell/platform/darwin/macos/framework/Source/FLEViewController.mm index bc9ead3ea43ae..1379117d66bd5 100644 --- a/shell/platform/darwin/macos/framework/Source/FLEViewController.mm +++ b/shell/platform/darwin/macos/framework/Source/FLEViewController.mm @@ -159,6 +159,16 @@ - (void)onSettingsChanged:(NSNotification*)notification; */ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result; +/** + * Reads the data from the clipboard. + */ +- (NSDictionary*)getClipboardData:(NSString*)format; + +/** + * Clears contents and writes new data into clipboard. + */ +- (void)setClipboardData:(NSDictionary*)data; + @end #pragma mark - Static methods provided to engine configuration @@ -625,13 +635,10 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { } } -#pragma mark - ClipboardData - - (NSDictionary*)getClipboardData:(NSString*)format { NSPasteboard* pasteboard = [NSPasteboard generalPasteboard]; - if (!format || [format isEqualToString:@(kTextPlainFormat)]) { - id stringInPasteboard = [pasteboard stringForType:NSPasteboardTypeString]; - // The pasteboard may contain an item but it may not be a string (an image for instance). + if ([format isEqualToString:@(kTextPlainFormat)]) { + NSString* stringInPasteboard = [pasteboard stringForType:NSPasteboardTypeString]; return stringInPasteboard == nil ? nil : @{@"text" : stringInPasteboard}; } return nil; @@ -639,8 +646,11 @@ - (NSDictionary*)getClipboardData:(NSString*)format { - (void)setClipboardData:(NSDictionary*)data { NSPasteboard* pasteboard = [NSPasteboard generalPasteboard]; - [pasteboard declareTypes:@[ NSPasteboardTypeString ] owner:self]; - [pasteboard setString:data[@"text"] forType:NSPasteboardTypeString]; + NSString* text = data[@"text"]; + if (text && ![text isEqual:[NSNull null]]) { + [pasteboard clearContents]; + [pasteboard setString:data[@"text"] forType:NSPasteboardTypeString]; + } } #pragma mark - FLEReshapeListener From 210ff2391bfb9f8b0c521f8a00ec6b402ebb2180 Mon Sep 17 00:00:00 2001 From: Francisco Magdaleno Arceo Date: Thu, 13 Jun 2019 14:32:30 -0700 Subject: [PATCH 3/3] Add documentation to declarations --- .../darwin/macos/framework/Source/FLEViewController.mm | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/shell/platform/darwin/macos/framework/Source/FLEViewController.mm b/shell/platform/darwin/macos/framework/Source/FLEViewController.mm index 1379117d66bd5..be1b4e50090d8 100644 --- a/shell/platform/darwin/macos/framework/Source/FLEViewController.mm +++ b/shell/platform/darwin/macos/framework/Source/FLEViewController.mm @@ -160,12 +160,14 @@ - (void)onSettingsChanged:(NSNotification*)notification; - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result; /** - * Reads the data from the clipboard. + * Reads the data from the clipboard. |format| specifies the media type of the + * data to obtain. */ - (NSDictionary*)getClipboardData:(NSString*)format; /** - * Clears contents and writes new data into clipboard. + * Clears contents and writes new data into clipboard. |data| is a dictionary where + * the keys are the type of data, and tervalue the data to be stored. */ - (void)setClipboardData:(NSDictionary*)data; @@ -649,7 +651,7 @@ - (void)setClipboardData:(NSDictionary*)data { NSString* text = data[@"text"]; if (text && ![text isEqual:[NSNull null]]) { [pasteboard clearContents]; - [pasteboard setString:data[@"text"] forType:NSPasteboardTypeString]; + [pasteboard setString:text forType:NSPasteboardTypeString]; } }