From 66ffb6895cb9df4475f1e35ff490553168e24ea4 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 15 Mar 2026 08:59:44 +0000 Subject: [PATCH] Fix macOS keyboard focus not restored when switching back from another space Fixes #9665 Fixes #9666 --- docs/changelog.rst | 2 ++ glfw/cocoa_init.m | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index a79b2b873..c778ba9ee 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -188,6 +188,8 @@ Detailed list of changes - Don't use neighboring tab colors for tab bar margins in translucent windows (:iss:`9663`) +- macOS: Fix OS window focus not restored when switching spaces (:iss:`9665`) + 0.46.0 [2026-03-11] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/glfw/cocoa_init.m b/glfw/cocoa_init.m index 8bac2837d..4ea90333f 100644 --- a/glfw/cocoa_init.m +++ b/glfw/cocoa_init.m @@ -311,6 +311,24 @@ static NSDictionary *global_shortcuts = nil; } } +- (void)applicationDidBecomeActive:(NSNotification *)notification { + (void)notification; + // When the application becomes active after switching spaces (e.g., swiping + // back from a fullscreen app on another space), macOS may not send + // windowDidBecomeKey: for the already-key window. This leaves GLFW thinking + // no window has focus (since windowDidResignKey: was sent when leaving). + // Ensure GLFW's focus state is updated to match the actual key window. + NSWindow *keyWindow = [NSApp keyWindow]; + if (keyWindow && !_glfw.focusedWindowId) { + for (_GLFWwindow *window = _glfw.windowListHead; window; window = window->next) { + if (window->ns.object == keyWindow) { + if (_glfw.focusedWindowId != window->id) _glfwInputWindowFocus(window, true); + break; + } + } + } +} + - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender { (void)sender;