From 7fd4ec50c9ffeefac9d5271a5f490225eab60f21 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 16 Feb 2019 13:21:52 +0530 Subject: [PATCH] macOS: When closing a top-level window only switch focus to the previous kitty window if it is on the same workspace Fixes #1379 Fixes #1383 --- docs/changelog.rst | 3 +++ kitty/cocoa_window.m | 4 ++-- kitty/glfw.c | 15 +++++++++++++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 8f78e9b2e..d0ae6bbc7 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -53,6 +53,9 @@ To update |kitty|, :doc:`follow the instructions `. - Fix scrollback pager history not being cleared when clearing the main scrollback buffer (:iss:`1387`) +- macOS: When closing a top-level window only switch focus to the previous kitty + window if it is on the same workspace (:iss:`1379`) + 0.13.3 [2019-01-19] ------------------------------ diff --git a/kitty/cocoa_window.m b/kitty/cocoa_window.m index 4013edc5b..8f8903c8e 100644 --- a/kitty/cocoa_window.m +++ b/kitty/cocoa_window.m @@ -372,8 +372,8 @@ cocoa_focus_window(void *w) { int cocoa_get_workspace_id(void *w) { NSWindow *window = (NSWindow*)w; - int ans = 0; - CGSGetWindowWorkspace(_CGSDefaultConnection(), [window windowNumber], &ans); + int ans = -1; + if (window) CGSGetWindowWorkspace(_CGSDefaultConnection(), [window windowNumber], &ans); return ans; } diff --git a/kitty/glfw.c b/kitty/glfw.c index 5c73a127f..f5c87c908 100644 --- a/kitty/glfw.c +++ b/kitty/glfw.c @@ -15,6 +15,7 @@ extern void cocoa_create_global_menu(void); extern void cocoa_set_hide_from_tasks(void); extern void cocoa_set_titlebar_color(void *w, color_type color); extern bool cocoa_alt_option_key_pressed(unsigned long); +extern int cocoa_get_workspace_id(void *w); #if GLFW_KEY_LAST >= MAX_KEY_COUNT @@ -594,7 +595,13 @@ create_os_window(PyObject UNUSED *self, PyObject *args) { void destroy_os_window(OSWindow *w) { +#ifdef __APPLE__ + int workspace_id = -1; +#endif if (w->handle) { +#ifdef __APPLE__ + workspace_id = cocoa_get_workspace_id(glfwGetCocoaWindow(w->handle)); +#endif // Ensure mouse cursor is visible and reset to default shape, needed on macOS show_mouse_cursor(w->handle); glfwSetCursor(w->handle, NULL); @@ -608,13 +615,17 @@ destroy_os_window(OSWindow *w) { OSWindow *window_to_focus = NULL; for (size_t i = 0; i < global_state.num_os_windows; i++) { OSWindow *c = global_state.os_windows + i; - if (c->id != w->id && c->handle && c->shown_once && (c->last_focused_counter >= highest_focus_number)) { + if ( + c->id != w->id && c->handle && c->shown_once && + c->last_focused_counter >= highest_focus_number && + (workspace_id == -1 || cocoa_get_workspace_id(glfwGetCocoaWindow(c->handle)) == workspace_id) + ) { highest_focus_number = c->last_focused_counter; window_to_focus = c; } } if (window_to_focus) { - glfwFocusWindow(w->handle); + glfwFocusWindow(window_to_focus->handle); } #endif }