Start work on new clipboard api support for macos

This commit is contained in:
Kovid Goyal 2022-09-08 21:45:13 +05:30
parent 91c00fb5ac
commit bcfa2a64e4
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
5 changed files with 40 additions and 44 deletions

View File

@ -992,8 +992,6 @@ void _glfwPlatformTerminate(void)
_glfw.ns.appleSettings = nil; _glfw.ns.appleSettings = nil;
} }
free(_glfw.ns.clipboardString);
_glfwTerminateNSGL(); _glfwTerminateNSGL();
if (global_shortcuts != nil) { [global_shortcuts release]; global_shortcuts = nil; } if (global_shortcuts != nil) { [global_shortcuts release]; global_shortcuts = nil; }

View File

@ -183,7 +183,6 @@ typedef struct _GLFWlibraryNS
char keyName[64]; char keyName[64];
char text[256]; char text[256];
char* clipboardString;
CGPoint cascadePoint; CGPoint cascadePoint;
// Where to place the cursor when re-enabled // Where to place the cursor when re-enabled
double restoreCursorPosX, restoreCursorPosY; double restoreCursorPosX, restoreCursorPosY;

View File

@ -30,6 +30,8 @@
#include "../kitty/monotonic.h" #include "../kitty/monotonic.h"
#include <Availability.h> #include <Availability.h>
#import <CoreServices/CoreServices.h>
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
#include <float.h> #include <float.h>
#include <string.h> #include <string.h>
@ -2506,55 +2508,51 @@ bool _glfwPlatformToggleFullscreen(_GLFWwindow* w, unsigned int flags) {
return made_fullscreen; return made_fullscreen;
} }
void _glfwPlatformSetClipboardString(const char* string) // Clipboard {{{
{
static void
list_clipboard_mimetypes(GLFWclipboardwritedatafun write_data, void *object) {
#define w(x) { if (ok) ok = write_data(object, x, strlen(x)); }
NSPasteboard* pasteboard = [NSPasteboard generalPasteboard]; NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
[pasteboard declareTypes:@[NSPasteboardTypeString] owner:nil];
[pasteboard setString:@(string) forType:NSPasteboardTypeString];
}
void _glfwPlatformSetPrimarySelectionString(const char* string) {
(void)string;
// Apple doesnt have a primary selection
}
const char* _glfwPlatformGetPrimarySelectionString(void) { return ""; }
const char* _glfwPlatformGetClipboardString(void)
{
NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
free(_glfw.ns.clipboardString); _glfw.ns.clipboardString = NULL;
NSDictionary* options = @{NSPasteboardURLReadingFileURLsOnlyKey:@YES}; NSDictionary* options = @{NSPasteboardURLReadingFileURLsOnlyKey:@YES};
NSArray* objs = [pasteboard readObjectsForClasses:@[[NSURL class], [NSString class]] options:options]; BOOL has_file_urls = [pasteboard canReadObjectForClasses:@[[NSURL class]] options:options];
if (objs) { BOOL has_strings = [pasteboard canReadObjectForClasses:@[[NSString class]] options:nil];
const NSUInteger count = [objs count]; /* NSLog(@"has_file_urls: %d has_strings: %d", has_file_urls, has_strings); */
if (count) { bool ok = true;
NSMutableString *path_list = [NSMutableString stringWithCapacity:4096]; // auto-released if (has_strings) w("text/plain");
NSMutableString *text_list = [NSMutableString stringWithCapacity:4096]; // auto-released if (has_file_urls) w("text/local-path-list");
for (NSUInteger i = 0; i < count; i++) { if (@available(macOS 11.0, *)) {
id obj = objs[i]; for (NSPasteboardItem * item in pasteboard.pasteboardItems) {
if ([obj isKindOfClass:[NSURL class]]) { for (NSPasteboardType type in item.types) {
NSURL *url = (NSURL*)obj; /* NSLog(@"%@", type); */
if (url.fileURL && url.fileSystemRepresentation) { UTType *ut = [UTType typeWithIdentifier:type];
if ([path_list length] > 0) [path_list appendString:@("\n")]; if (ut != nil) {
[path_list appendString:@(url.fileSystemRepresentation)]; /* NSLog(@"ut: %@ mt: %@ tags: %@", ut, ut.preferredMIMEType, ut.tags); */
if (ut.preferredMIMEType != nil && ![ut.preferredMIMEType hasPrefix:@"text/plain"]) {
w([ut.preferredMIMEType UTF8String]);
} }
} else if ([obj isKindOfClass:[NSString class]]) {
if ([text_list length] > 0) [text_list appendString:@("\n")];
[text_list appendString:obj];
} }
} }
const char *text = NULL;
if (path_list.length > 0) text = [path_list UTF8String];
else if (text_list.length > 0) text = [text_list UTF8String];
if (text) _glfw.ns.clipboardString = _glfw_strdup(text);
} }
} }
if (!_glfw.ns.clipboardString) _glfwInputError(GLFW_PLATFORM_ERROR, "Cocoa: Failed to retrieve object from pasteboard"); #undef w
return _glfw.ns.clipboardString;
} }
void
_glfwPlatformGetClipboard(GLFWClipboardType clipboard_type, const char* mime_type, GLFWclipboardwritedatafun write_data, void *object) {
if (clipboard_type != GLFW_CLIPBOARD) return;
(void)mime_type; (void) write_data; (void) object;
if (mime_type == NULL) {
list_clipboard_mimetypes(write_data, object);
return;
}
}
void _glfwPlatformSetClipboard(GLFWClipboardType t) {
if (t != GLFW_CLIPBOARD) return;
}
// }}}
EGLenum _glfwPlatformGetEGLPlatform(EGLint** attribs) EGLenum _glfwPlatformGetEGLPlatform(EGLint** attribs)
{ {
if (_glfw.egl.ANGLE_platform_angle) if (_glfw.egl.ANGLE_platform_angle)

View File

@ -111,7 +111,7 @@ def init_env(
elif module == 'cocoa': elif module == 'cocoa':
ans.cppflags.append('-DGL_SILENCE_DEPRECATION') ans.cppflags.append('-DGL_SILENCE_DEPRECATION')
for f_ in 'Cocoa IOKit CoreFoundation CoreVideo'.split(): for f_ in 'Cocoa IOKit CoreFoundation CoreVideo UniformTypeIdentifiers'.split():
ans.ldpaths.extend(('-framework', f_)) ans.ldpaths.extend(('-framework', f_))
elif module == 'wayland': elif module == 'wayland':

View File

@ -288,6 +288,7 @@ class Boss:
cocoa_set_notification_activated_callback cocoa_set_notification_activated_callback
) )
cocoa_set_notification_activated_callback(notification_activated) cocoa_set_notification_activated_callback(notification_activated)
print(111111111, self.clipboard.get_available_mime_types_for_paste())
def update_keymap(self) -> None: def update_keymap(self) -> None:
self.keymap = get_options().keymap.copy() self.keymap = get_options().keymap.copy()