diff --git a/docs/changelog.rst b/docs/changelog.rst index 5572d17ac..caebb6680 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -111,6 +111,8 @@ Detailed list of changes - Fix ambiguous width and private use characters not being rendered when used with variable width text-sizing protocol escape codes +- macOS: Quick access terminal: Restore focus to previously active window when hiding the quick access terminal window (:iss:`8627`) + 0.42.0 [2025-05-11] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/glfw/cocoa_platform.h b/glfw/cocoa_platform.h index 727d23d5d..2adf33600 100644 --- a/glfw/cocoa_platform.h +++ b/glfw/cocoa_platform.h @@ -125,6 +125,7 @@ typedef struct _GLFWwindowNS id delegate; id view; id layer; + pid_t previous_front_most_application; bool maximized; bool retina; diff --git a/glfw/cocoa_window.m b/glfw/cocoa_window.m index 720ec5dfa..fe0265d81 100644 --- a/glfw/cocoa_window.m +++ b/glfw/cocoa_window.m @@ -2201,14 +2201,31 @@ void _glfwPlatformMaximizeWindow(_GLFWwindow* window) void _glfwPlatformShowWindow(_GLFWwindow* window) { + NSRunningApplication *app = [[NSWorkspace sharedWorkspace] frontmostApplication]; + window->ns.previous_front_most_application = 0; + if (app && app.processIdentifier != getpid()) window->ns.previous_front_most_application = app.processIdentifier; if (window->ns.layer_shell.is_active && window->ns.layer_shell.config.type == GLFW_LAYER_SHELL_BACKGROUND) { [window->ns.object orderBack:nil]; } else [window->ns.object orderFront:nil]; + debug("Previously active application pid: %d bundle identifier: %s\n", window->ns.previous_front_most_application, + window->ns.previous_front_most_application ? [NSRunningApplication runningApplicationWithProcessIdentifier:window->ns.previous_front_most_application].bundleIdentifier.UTF8String : ""); } void _glfwPlatformHideWindow(_GLFWwindow* window) { [window->ns.object orderOut:nil]; + pid_t prev_app_pid = window->ns.previous_front_most_application; window->ns.previous_front_most_application = 0; + NSRunningApplication *app; + if (window->ns.layer_shell.is_active && prev_app_pid > 0 && (app = [NSRunningApplication runningApplicationWithProcessIdentifier:prev_app_pid])) { + unsigned num_visible = 0; + for (_GLFWwindow *w = _glfw.windowListHead; w; w = w->next) { + if (_glfwPlatformWindowVisible(w)) num_visible++; + } + if (!num_visible) { + [NSApp yieldActivationToApplication: app]; + [app activateWithOptions:0]; + } + } } void _glfwPlatformRequestWindowAttention(_GLFWwindow* window UNUSED)