From f9fd39b8c158a9e9f1cae3c60aad45549b45d783 Mon Sep 17 00:00:00 2001 From: Luflosi Date: Sat, 23 Nov 2019 19:18:38 +0100 Subject: [PATCH] Create macOS menu bar where GLFW creates it GLFW creates the menu bar in the applicationWillFinishLaunching method, while kitty creates it in `create_os_window()`. This patch changes the behaviour to match GLFW. In practice, without this change, there can be a short time where the menu bar is not fully populated. --- glfw/cocoa_init.m | 10 ++++++++++ glfw/cocoa_platform.h | 1 + glfw/glfw.py | 2 ++ kitty/cocoa_window.m | 4 ---- kitty/glfw-wrapper.c | 2 ++ kitty/glfw-wrapper.h | 5 +++++ kitty/glfw.c | 4 +--- 7 files changed, 21 insertions(+), 7 deletions(-) diff --git a/glfw/cocoa_init.m b/glfw/cocoa_init.m index 4ca95d6aa..66699dba2 100644 --- a/glfw/cocoa_init.m +++ b/glfw/cocoa_init.m @@ -456,6 +456,8 @@ static GLFWapplicationshouldhandlereopenfun handle_reopen_callback = NULL; _glfwPollMonitorsNS(); } +static GLFWapplicationwillfinishlaunchingfun finish_launching_callback = NULL; + - (void)applicationWillFinishLaunching:(NSNotification *)notification { (void)notification; @@ -478,6 +480,8 @@ static GLFWapplicationshouldhandlereopenfun handle_reopen_callback = NULL; else */ createMenuBar(); } + if (finish_launching_callback) + finish_launching_callback(); } - (void)applicationDidFinishLaunching:(NSNotification *)notification @@ -555,6 +559,12 @@ GLFWAPI GLFWapplicationshouldhandlereopenfun glfwSetApplicationShouldHandleReope return previous; } +GLFWAPI GLFWapplicationwillfinishlaunchingfun glfwSetApplicationWillFinishLaunching(GLFWapplicationwillfinishlaunchingfun callback) { + GLFWapplicationwillfinishlaunchingfun previous = finish_launching_callback; + finish_launching_callback = callback; + return previous; +} + int _glfwPlatformInit(void) { @autoreleasepool { diff --git a/glfw/cocoa_platform.h b/glfw/cocoa_platform.h index 4af0a54ce..93edc74bf 100644 --- a/glfw/cocoa_platform.h +++ b/glfw/cocoa_platform.h @@ -68,6 +68,7 @@ typedef void* CVDisplayLinkRef; typedef VkFlags VkMacOSSurfaceCreateFlagsMVK; typedef int (* GLFWcocoatextinputfilterfun)(int,int,unsigned int, unsigned long); typedef int (* GLFWapplicationshouldhandlereopenfun)(int); +typedef void (* GLFWapplicationwillfinishlaunchingfun)(void); typedef int (* GLFWcocoatogglefullscreenfun)(GLFWwindow*); typedef void (* GLFWcocoarenderframefun)(GLFWwindow*); diff --git a/glfw/glfw.py b/glfw/glfw.py index 90f8a73f0..89daf865e 100755 --- a/glfw/glfw.py +++ b/glfw/glfw.py @@ -165,6 +165,7 @@ def generate_wrappers(glfw_header): GLFWcocoatextinputfilterfun glfwSetCocoaTextInputFilter(GLFWwindow* window, GLFWcocoatextinputfilterfun callback) GLFWcocoatogglefullscreenfun glfwSetCocoaToggleFullscreenIntercept(GLFWwindow *window, GLFWcocoatogglefullscreenfun callback) GLFWapplicationshouldhandlereopenfun glfwSetApplicationShouldHandleReopen(GLFWapplicationshouldhandlereopenfun callback) + GLFWapplicationwillfinishlaunchingfun glfwSetApplicationWillFinishLaunching(GLFWapplicationwillfinishlaunchingfun callback) void glfwGetCocoaKeyEquivalent(int glfw_key, int glfw_mods, void* cocoa_key, void* cocoa_mods) void glfwCocoaRequestRenderFrame(GLFWwindow *w, GLFWcocoarenderframefun callback) void* glfwGetX11Display(void) @@ -200,6 +201,7 @@ const char *action_text, int32_t timeout, GLFWDBusnotificationcreatedfun callbac typedef int (* GLFWcocoatextinputfilterfun)(int,int,unsigned int,unsigned long); typedef int (* GLFWapplicationshouldhandlereopenfun)(int); +typedef void (* GLFWapplicationwillfinishlaunchingfun)(void); typedef int (* GLFWcocoatogglefullscreenfun)(GLFWwindow*); typedef void (* GLFWcocoarenderframefun)(GLFWwindow*); typedef void (*GLFWwaylandframecallbackfunc)(unsigned long long id); diff --git a/kitty/cocoa_window.m b/kitty/cocoa_window.m index f4e0bfa5a..3cb6f414a 100644 --- a/kitty/cocoa_window.m +++ b/kitty/cocoa_window.m @@ -232,8 +232,6 @@ cocoa_send_notification(PyObject *self UNUSED, PyObject *args) { // global menu {{{ void cocoa_create_global_menu(void) { - @autoreleasepool { - NSString* app_name = find_app_name(); NSMenu* bar = [[NSMenu alloc] init]; GlobalMenuTarget *global_menu_target = [GlobalMenuTarget shared_instance]; @@ -325,8 +323,6 @@ cocoa_create_global_menu(void) { [NSApp setServicesProvider:[[[ServiceProvider alloc] init] autorelease]]; - - } // autoreleasepool } void diff --git a/kitty/glfw-wrapper.c b/kitty/glfw-wrapper.c index f3dfafbba..b96823971 100644 --- a/kitty/glfw-wrapper.c +++ b/kitty/glfw-wrapper.c @@ -389,6 +389,8 @@ load_glfw(const char* path) { *(void **) (&glfwSetApplicationShouldHandleReopen_impl) = dlsym(handle, "glfwSetApplicationShouldHandleReopen"); + *(void **) (&glfwSetApplicationWillFinishLaunching_impl) = dlsym(handle, "glfwSetApplicationWillFinishLaunching"); + *(void **) (&glfwGetCocoaKeyEquivalent_impl) = dlsym(handle, "glfwGetCocoaKeyEquivalent"); *(void **) (&glfwCocoaRequestRenderFrame_impl) = dlsym(handle, "glfwCocoaRequestRenderFrame"); diff --git a/kitty/glfw-wrapper.h b/kitty/glfw-wrapper.h index 5c194f906..19a0cadfb 100644 --- a/kitty/glfw-wrapper.h +++ b/kitty/glfw-wrapper.h @@ -1576,6 +1576,7 @@ typedef struct GLFWgamepadstate typedef int (* GLFWcocoatextinputfilterfun)(int,int,unsigned int,unsigned long); typedef int (* GLFWapplicationshouldhandlereopenfun)(int); +typedef void (* GLFWapplicationwillfinishlaunchingfun)(void); typedef int (* GLFWcocoatogglefullscreenfun)(GLFWwindow*); typedef void (* GLFWcocoarenderframefun)(GLFWwindow*); typedef void (*GLFWwaylandframecallbackfunc)(unsigned long long id); @@ -2089,6 +2090,10 @@ typedef GLFWapplicationshouldhandlereopenfun (*glfwSetApplicationShouldHandleReo glfwSetApplicationShouldHandleReopen_func glfwSetApplicationShouldHandleReopen_impl; #define glfwSetApplicationShouldHandleReopen glfwSetApplicationShouldHandleReopen_impl +typedef GLFWapplicationwillfinishlaunchingfun (*glfwSetApplicationWillFinishLaunching_func)(GLFWapplicationwillfinishlaunchingfun); +glfwSetApplicationWillFinishLaunching_func glfwSetApplicationWillFinishLaunching_impl; +#define glfwSetApplicationWillFinishLaunching glfwSetApplicationWillFinishLaunching_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 0873b9271..7aecb4fca 100644 --- a/kitty/glfw.c +++ b/kitty/glfw.c @@ -517,6 +517,7 @@ create_os_window(PyObject UNUSED *self, PyObject *args) { cocoa_set_activation_policy(OPT(macos_hide_from_tasks)); glfwWindowHint(GLFW_COCOA_GRAPHICS_SWITCHING, true); glfwSetApplicationShouldHandleReopen(on_application_reopen); + glfwSetApplicationWillFinishLaunching(cocoa_create_global_menu); if (OPT(hide_window_decorations)) glfwWindowHint(GLFW_DECORATED, false); #endif @@ -595,9 +596,6 @@ create_os_window(PyObject UNUSED *self, PyObject *args) { PyObject *ret = PyObject_CallFunction(load_programs, "O", is_semi_transparent ? Py_True : Py_False); if (ret == NULL) return NULL; Py_DECREF(ret); -#ifdef __APPLE__ - cocoa_create_global_menu(); -#endif #define CC(dest, shape) {\ if (!dest##_cursor) { \ dest##_cursor = glfwCreateStandardCursor(GLFW_##shape##_CURSOR); \