From faa5443d437daad15c4013f116330dd21568e171 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 7 Jun 2018 07:17:37 +0530 Subject: [PATCH] macOS: When clicking dock icon with no windows, create a new window --- glfw/cocoa_platform.h | 1 + glfw/cocoa_window.m | 15 +++++++++++++++ glfw/glfw.py | 3 +++ kitty/glfw-wrapper.c | 2 ++ kitty/glfw-wrapper.h | 6 ++++++ kitty/glfw.c | 10 ++++++++++ 6 files changed, 37 insertions(+) diff --git a/glfw/cocoa_platform.h b/glfw/cocoa_platform.h index e33fbb652..a95c2a658 100644 --- a/glfw/cocoa_platform.h +++ b/glfw/cocoa_platform.h @@ -38,6 +38,7 @@ typedef void* id; typedef VkFlags VkMacOSSurfaceCreateFlagsMVK; typedef int (* GLFWcocoatextinputfilterfun)(int,int,int); +typedef int (* GLFWapplicationshouldhandlereopenfun)(int); typedef struct VkMacOSSurfaceCreateInfoMVK { diff --git a/glfw/cocoa_window.m b/glfw/cocoa_window.m index d821bfe94..4f9d112ec 100644 --- a/glfw/cocoa_window.m +++ b/glfw/cocoa_window.m @@ -482,6 +482,15 @@ static const NSRange kEmptyRange = { NSNotFound, 0 }; return NSTerminateCancel; } +static GLFWapplicationshouldhandlereopenfun handle_reopen_callback = NULL; + +- (BOOL)applicationShouldHandleReopen:(NSApplication *)sender hasVisibleWindows:(BOOL)flag +{ + if (!handle_reopen_callback) return YES; + if (handle_reopen_callback(flag)) return YES; + return NO; +} + - (void)applicationDidChangeScreenParameters:(NSNotification *) notification { _GLFWwindow* window; @@ -2089,6 +2098,12 @@ GLFWAPI GLFWcocoatextinputfilterfun glfwSetCocoaTextInputFilter(GLFWwindow *hand return previous; } +GLFWAPI GLFWapplicationshouldhandlereopenfun glfwSetApplicationShouldHandleReopen(GLFWapplicationshouldhandlereopenfun callback) { + GLFWapplicationshouldhandlereopenfun previous = handle_reopen_callback; + handle_reopen_callback = callback; + return previous; +} + GLFWAPI void glfwGetCocoaKeyEquivalent(int glfw_key, int glfw_mods, unsigned short *cocoa_key, int *cocoa_mods) { *cocoa_key = 0; *cocoa_mods = 0; diff --git a/glfw/glfw.py b/glfw/glfw.py index a17177c74..051603ba1 100755 --- a/glfw/glfw.py +++ b/glfw/glfw.py @@ -202,6 +202,7 @@ def generate_wrappers(glfw_header, glfw_native_header): void* glfwGetCocoaWindow(GLFWwindow* window) uint32_t glfwGetCocoaMonitor(GLFWmonitor* monitor) GLFWcocoatextinputfilterfun glfwSetCocoaTextInputFilter(GLFWwindow* window, GLFWcocoatextinputfilterfun callback) + GLFWapplicationshouldhandlereopenfun glfwSetApplicationShouldHandleReopen(GLFWapplicationshouldhandlereopenfun callback) void glfwGetCocoaKeyEquivalent(int glfw_key, int glfw_mods, void* cocoa_key, void* cocoa_mods) void* glfwGetX11Display(void) int32_t glfwGetX11Window(GLFWwindow* window) @@ -220,6 +221,8 @@ def generate_wrappers(glfw_header, glfw_native_header): #include #include typedef int (* GLFWcocoatextinputfilterfun)(int,int,unsigned int); +typedef int (* GLFWapplicationshouldhandlereopenfun)(int); + {} {} diff --git a/kitty/glfw-wrapper.c b/kitty/glfw-wrapper.c index 80c82b371..f34fae43f 100644 --- a/kitty/glfw-wrapper.c +++ b/kitty/glfw-wrapper.c @@ -359,6 +359,8 @@ load_glfw(const char* path) { *(void **) (&glfwSetCocoaTextInputFilter_impl) = dlsym(handle, "glfwSetCocoaTextInputFilter"); + *(void **) (&glfwSetApplicationShouldHandleReopen_impl) = dlsym(handle, "glfwSetApplicationShouldHandleReopen"); + *(void **) (&glfwGetCocoaKeyEquivalent_impl) = dlsym(handle, "glfwGetCocoaKeyEquivalent"); *(void **) (&glfwGetX11Display_impl) = dlsym(handle, "glfwGetX11Display"); diff --git a/kitty/glfw-wrapper.h b/kitty/glfw-wrapper.h index 0d1cd175e..234426774 100644 --- a/kitty/glfw-wrapper.h +++ b/kitty/glfw-wrapper.h @@ -2,6 +2,8 @@ #include #include typedef int (* GLFWcocoatextinputfilterfun)(int,int,unsigned int); +typedef int (* GLFWapplicationshouldhandlereopenfun)(int); + /*! @name GLFW version macros @@ -1836,6 +1838,10 @@ typedef GLFWcocoatextinputfilterfun (*glfwSetCocoaTextInputFilter_func)(GLFWwind glfwSetCocoaTextInputFilter_func glfwSetCocoaTextInputFilter_impl; #define glfwSetCocoaTextInputFilter glfwSetCocoaTextInputFilter_impl +typedef GLFWapplicationshouldhandlereopenfun (*glfwSetApplicationShouldHandleReopen_func)(GLFWapplicationshouldhandlereopenfun); +glfwSetApplicationShouldHandleReopen_func glfwSetApplicationShouldHandleReopen_impl; +#define glfwSetApplicationShouldHandleReopen glfwSetApplicationShouldHandleReopen_impl + typedef void (*glfwGetCocoaKeyEquivalent_func)(int, int, void*, void*); glfwGetCocoaKeyEquivalent_func glfwGetCocoaKeyEquivalent_impl; #define glfwGetCocoaKeyEquivalent glfwGetCocoaKeyEquivalent_impl diff --git a/kitty/glfw.c b/kitty/glfw.c index 6b22513c0..9de40bc8c 100644 --- a/kitty/glfw.c +++ b/kitty/glfw.c @@ -331,6 +331,15 @@ filter_option(int key UNUSED, int mods, unsigned int scancode UNUSED) { return ((mods == GLFW_MOD_ALT) || (mods == (GLFW_MOD_ALT | GLFW_MOD_SHIFT))) ? 1 : 0; } static GLFWwindow *application_quit_canary = NULL; + +static int +on_application_reopen(int has_visible_windows) { + if (has_visible_windows) return true; + set_cocoa_pending_action(NEW_OS_WINDOW); + // Without unjam wait_for_events() blocks until the next event + unjam_event_loop(); + return false; +} #endif void @@ -372,6 +381,7 @@ create_os_window(PyObject UNUSED *self, PyObject *args) { #ifdef __APPLE__ if (OPT(macos_hide_titlebar)) glfwWindowHint(GLFW_DECORATED, false); glfwWindowHint(GLFW_COCOA_GRAPHICS_SWITCHING, true); + glfwSetApplicationShouldHandleReopen(on_application_reopen); #endif }