From 1afa91bbb4ec91f0e3b6a8767d75de46d426e5dc Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 7 Jun 2018 06:19:36 +0530 Subject: [PATCH] macOS: Keep kitty running when the last window is closed This is how applications are supposed to behave on macOS. Fixes #543 --- docs/changelog.rst | 3 +++ kitty/child-monitor.c | 3 +++ kitty/glfw.c | 19 ++++++++++++++++++- kitty/state.h | 1 + 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 6700c7ccc..96b2daad9 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -47,6 +47,9 @@ Changelog - macOS: Fix the new OS window keyboard shortcut (:sc:`new_os_window`) not working if no kitty window currently has focus. (:iss:`524`) +- macOS: Keep kitty running even when the last window is closed. This is in + line with how applications are supposed to behave on macOS (:iss:`543`) + - Add a config option (:opt:`editor`) to set the EDITOR kitty uses (:iss:`580`) - Add a config option (:opt:`x11_hide_window_decorations`) to hide window diff --git a/kitty/child-monitor.c b/kitty/child-monitor.c index 788c846f4..877a5fe77 100644 --- a/kitty/child-monitor.c +++ b/kitty/child-monitor.c @@ -748,6 +748,9 @@ process_pending_closes(ChildMonitor *self) { remove_os_window(os_window->id); } else has_open_windows = true; } +#ifdef __APPLE__ + if (!has_open_windows && !application_quit_requested()) has_open_windows = true; +#endif return has_open_windows; } diff --git a/kitty/glfw.c b/kitty/glfw.c index 26c45eb57..6b22513c0 100644 --- a/kitty/glfw.c +++ b/kitty/glfw.c @@ -330,6 +330,7 @@ static int 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; #endif void @@ -393,8 +394,17 @@ create_os_window(PyObject UNUSED *self, PyObject *args) { // creation, which causes a resize event and all the associated processing. // The temp window is used to get the DPI. glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); - GLFWwindow *temp_window = glfwCreateWindow(640, 480, "temp", NULL, global_state.num_os_windows ? global_state.os_windows[0].handle : NULL); + GLFWwindow *common_context = global_state.num_os_windows ? global_state.os_windows[0].handle : NULL; +#ifdef __APPLE__ + if (is_first_window && !application_quit_canary) { + application_quit_canary = glfwCreateWindow(100, 200, "quit_canary", NULL, NULL); + } + if (!common_context) common_context = application_quit_canary; +#endif + + GLFWwindow *temp_window = glfwCreateWindow(640, 480, "temp", NULL, common_context); if (temp_window == NULL) { fatal("Failed to create GLFW temp window!"); } + double dpi_x, dpi_y; get_window_dpi(temp_window, &dpi_x, &dpi_y); FONTS_DATA_HANDLE fonts_data = load_fonts_data(global_state.font_sz_in_pts, dpi_x, dpi_y); @@ -513,6 +523,13 @@ destroy_os_window(OSWindow *w) { #endif } +#ifdef __APPLE__ +bool +application_quit_requested() { + return !application_quit_canary || glfwWindowShouldClose(application_quit_canary); +} +#endif + // Global functions {{{ static void error_callback(int error, const char* description) { diff --git a/kitty/state.h b/kitty/state.h index 663c5885b..c91cdb137 100644 --- a/kitty/state.h +++ b/kitty/state.h @@ -194,4 +194,5 @@ typedef enum { PREFERENCES_WINDOW = 1, NEW_OS_WINDOW = 2 } CocoaPendingAction; void set_cocoa_pending_action(CocoaPendingAction action); +bool application_quit_requested(); #endif