From cabfcbcb69580cfe79819db9eaebef55259fa337 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 30 May 2022 14:40:45 +0530 Subject: [PATCH] macOS: When pasting text and the clipboard has a filesystem path, paste the full path instead of the text, which is sometimes just the file name Fixes #5142 --- docs/changelog.rst | 3 +++ glfw/cocoa_window.m | 26 +++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index b14448797..16b6c5533 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -46,6 +46,9 @@ Detailed list of changes - ssh kitten: Fix bash not being executed as a login shell since kitty 0.25.0 (:iss:`5130`) +- macOS: When pasting text and the clipboard has a filesystem path, paste the + full path instead of the text, which is sometimes just the file name (:pull:`5142`) + 0.25.1 [2022-05-26] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/glfw/cocoa_window.m b/glfw/cocoa_window.m index 0366af24a..19b9384ba 100644 --- a/glfw/cocoa_window.m +++ b/glfw/cocoa_window.m @@ -2497,10 +2497,30 @@ const char* _glfwPlatformGetClipboardString(void) "Cocoa: Failed to retrieve object from pasteboard"); return NULL; } + free(_glfw.ns.clipboardString); _glfw.ns.clipboardString = NULL; - free(_glfw.ns.clipboardString); - _glfw.ns.clipboardString = _glfw_strdup([object UTF8String]); - + NSDictionary* options = @{NSPasteboardURLReadingFileURLsOnlyKey:@YES}; + NSArray* objs = [pasteboard readObjectsForClasses:@[[NSURL class], [NSString class]] options:options]; + if (!objs) return NULL; + const NSUInteger count = [objs count]; + if (count) { + NSMutableString *path_list = [NSMutableString stringWithCapacity:4096]; // auto-released + const char *text = NULL; + for (NSUInteger i = 0; i < count; i++) { + id obj = objs[i]; + if ([obj isKindOfClass:[NSURL class]]) { + NSURL *url = (NSURL*)obj; + if (url.fileURL && url.fileSystemRepresentation) { + if ([path_list length] > 0) [path_list appendString:@("\n")]; + [path_list appendString:@(url.fileSystemRepresentation)]; + } + } else if ([obj isKindOfClass:[NSString class]]) { + text = [obj UTF8String]; + } + } + if (path_list.length > 0) _glfw.ns.clipboardString = _glfw_strdup([path_list UTF8String]); + else if (text) _glfw.ns.clipboardString = _glfw_strdup([path_list UTF8String]); + } return _glfw.ns.clipboardString; }