Modernized copy/paste APIs on macOS #7185
This commit is contained in:
parent
1aa1398012
commit
77fddfc7ea
1 changed files with 12 additions and 10 deletions
|
@ -1864,28 +1864,30 @@ bool OS_OSX::can_draw() const {
|
||||||
|
|
||||||
void OS_OSX::set_clipboard(const String &p_text) {
|
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];
|
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
|
||||||
[pasteboard declareTypes:types owner:nil];
|
[pasteboard clearContents];
|
||||||
[pasteboard setString:[NSString stringWithUTF8String:p_text.utf8().get_data()]
|
[pasteboard writeObjects:copiedStringArray];
|
||||||
forType:NSStringPboardType];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String OS_OSX::get_clipboard() const {
|
String OS_OSX::get_clipboard() const {
|
||||||
|
|
||||||
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
|
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 "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
NSString *object = [pasteboard stringForType:NSStringPboardType];
|
NSArray *objectsToPaste = [pasteboard readObjectsForClasses:classArray options:options];
|
||||||
if (!object) {
|
NSString *string = [objectsToPaste objectAtIndex:0];
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
char *utfs = strdup([object UTF8String]);
|
char *utfs = strdup([string UTF8String]);
|
||||||
String ret;
|
String ret;
|
||||||
ret.parse_utf8(utfs);
|
ret.parse_utf8(utfs);
|
||||||
free(utfs);
|
free(utfs);
|
||||||
|
|
Loading…
Reference in a new issue