|
18 | 18 |
|
19 | 19 | namespace { |
20 | 20 |
|
| 21 | +/// Clipboard plain text format. |
| 22 | +constexpr char kTextPlainFormat[] = "text/plain"; |
| 23 | + |
21 | 24 | /** |
22 | 25 | * State tracking for mouse events, to adapt between the events coming from the system and the |
23 | 26 | * events that the embedding API expects. |
@@ -156,6 +159,18 @@ - (void)onSettingsChanged:(NSNotification*)notification; |
156 | 159 | */ |
157 | 160 | - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result; |
158 | 161 |
|
| 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 | + |
159 | 174 | @end |
160 | 175 |
|
161 | 176 | #pragma mark - Static methods provided to engine configuration |
@@ -612,11 +627,34 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { |
612 | 627 | if ([call.method isEqualToString:@"SystemNavigator.pop"]) { |
613 | 628 | [NSApp terminate:self]; |
614 | 629 | 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); |
615 | 635 | } else { |
616 | 636 | result(FlutterMethodNotImplemented); |
617 | 637 | } |
618 | 638 | } |
619 | 639 |
|
| 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 | + |
620 | 658 | #pragma mark - FLEReshapeListener |
621 | 659 |
|
622 | 660 | /** |
|
0 commit comments