Skip to content

Commit 55a2f2c

Browse files
committed
Cocoa: fix quick access terminal hide focus restore
Apple now makes kitty the front most application before sending the service notification. So instead watch for front most application change events from the shared workspace and restore focus to the last non-self application.
1 parent fd5876b commit 55a2f2c

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

docs/changelog.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ Detailed list of changes
115115
- Wayland: Fix incorrect window size calculation when transitioning from
116116
fullscreen to non-fullscreen with client side decorations (:iss:`8826`)
117117

118+
- macOS: Fix hiding quick access terminal window not restoring focus to
119+
previously active application (:disc:`8840`)
120+
118121
0.42.2 [2025-07-16]
119122
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
120123

glfw/cocoa_init.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,14 @@ @interface GLFWApplicationDelegate : NSObject <NSApplicationDelegate>
301301

302302
@implementation GLFWApplicationDelegate
303303

304+
- (void)applicationDidActivate:(NSNotification *)notification {
305+
NSRunningApplication *app = notification.userInfo[NSWorkspaceApplicationKey];
306+
if (app && app.processIdentifier != getpid()) {
307+
_glfw.ns.previous_front_most_application = app.processIdentifier;
308+
debug_rendering("Front most application changed to: %s pid: %d\n", app.bundleIdentifier.UTF8String, app.processIdentifier)
309+
}
310+
}
311+
304312
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
305313
{
306314
(void)sender;
@@ -452,6 +460,7 @@ - (void)render_frame_received:(id)displayIDAsID
452460
CGDirectDisplayID displayID = [(NSNumber*)displayIDAsID unsignedIntValue];
453461
_glfwDispatchRenderFrame(displayID);
454462
}
463+
455464
@end
456465

457466

@@ -816,6 +825,11 @@ int _glfwPlatformInit(bool *supports_window_occlusion)
816825
}
817826

818827
[NSApp setDelegate:_glfw.ns.delegate];
828+
[[[NSWorkspace sharedWorkspace] notificationCenter]
829+
addObserver:_glfw.ns.delegate
830+
selector:@selector(applicationDidActivate:)
831+
name:NSWorkspaceDidActivateApplicationNotification
832+
object:nil];
819833
static struct {
820834
unsigned short virtual_key_code;
821835
NSEventModifierFlags input_source_switch_modifiers;

glfw/cocoa_platform.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ typedef struct _GLFWwindowNS
125125
id delegate;
126126
id view;
127127
id layer;
128-
pid_t previous_front_most_application;
129128

130129
bool maximized;
131130
bool retina;
@@ -189,6 +188,7 @@ typedef struct _GLFWlibraryNS
189188
double restoreCursorPosX, restoreCursorPosY;
190189
// The window whose disabled cursor mode is active
191190
_GLFWwindow* disabledCursorWindow;
191+
pid_t previous_front_most_application;
192192

193193
struct {
194194
CFBundleRef bundle;

glfw/cocoa_window.m

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2228,20 +2228,15 @@ void _glfwPlatformMaximizeWindow(_GLFWwindow* window)
22282228

22292229
void _glfwPlatformShowWindow(_GLFWwindow* window)
22302230
{
2231-
NSRunningApplication *app = [[NSWorkspace sharedWorkspace] frontmostApplication];
2232-
window->ns.previous_front_most_application = 0;
2233-
if (app && app.processIdentifier != getpid()) window->ns.previous_front_most_application = app.processIdentifier;
22342231
if (window->ns.layer_shell.is_active && window->ns.layer_shell.config.type == GLFW_LAYER_SHELL_BACKGROUND) {
22352232
[window->ns.object orderBack:nil];
22362233
} else [window->ns.object orderFront:nil];
2237-
debug("Previously active application pid: %d bundle identifier: %s\n",
2238-
window->ns.previous_front_most_application, app ? app.bundleIdentifier.UTF8String : "");
22392234
}
22402235

22412236
void _glfwPlatformHideWindow(_GLFWwindow* window)
22422237
{
22432238
[window->ns.object orderOut:nil];
2244-
pid_t prev_app_pid = window->ns.previous_front_most_application; window->ns.previous_front_most_application = 0;
2239+
pid_t prev_app_pid = _glfw.ns.previous_front_most_application; _glfw.ns.previous_front_most_application = 0;
22452240
NSRunningApplication *app;
22462241
if (window->ns.layer_shell.is_active && prev_app_pid > 0 && (app = [NSRunningApplication runningApplicationWithProcessIdentifier:prev_app_pid])) {
22472242
unsigned num_visible = 0;

0 commit comments

Comments
 (0)