Merge pull request #22685 from remmah/patch-1

Modernized copy/paste APIs on macOS #7185
This commit is contained in:
Rémi Verschelde 2018-10-05 12:20:57 +02:00 committed by GitHub
commit da6646dd5e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1859,28 +1859,30 @@ bool OS_OSX::can_draw() const {
void OS_OSX::set_clipboard(const String &p_text) {
NSArray *types = [NSArray arrayWithObjects:NSStringPboardType, nil];
NSString *copiedString = [NSString stringWithUTF8String:p_text.utf8().get_data()];
NSArray *copiedStringArray = [NSArray arrayWithObject:copiedString];
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
[pasteboard declareTypes:types owner:nil];
[pasteboard setString:[NSString stringWithUTF8String:p_text.utf8().get_data()]
forType:NSStringPboardType];
[pasteboard clearContents];
[pasteboard writeObjects:copiedStringArray];
}
String OS_OSX::get_clipboard() const {
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
NSArray *classArray = [NSArray arrayWithObject:[NSString class]];
NSDictionary *options = [NSDictionary dictionary];
if (![[pasteboard types] containsObject:NSStringPboardType]) {
BOOL ok = [pasteboard canReadObjectForClasses:classArray options:options];
if (!ok) {
return "";
}
NSString *object = [pasteboard stringForType:NSStringPboardType];
if (!object) {
return "";
}
NSArray *objectsToPaste = [pasteboard readObjectsForClasses:classArray options:options];
NSString *string = [objectsToPaste objectAtIndex:0];
char *utfs = strdup([object UTF8String]);
char *utfs = strdup([string UTF8String]);
String ret;
ret.parse_utf8(utfs);
free(utfs);