From b6ff8e77499edf36f7ef378fa6ebeb2b9b47ad8b Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Fri, 16 Sep 2022 10:55:15 +0200 Subject: [PATCH] Introduce workaround for spurious `windowDidBecomeKey` event --- crates/gpui/src/platform/mac/window.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/crates/gpui/src/platform/mac/window.rs b/crates/gpui/src/platform/mac/window.rs index 5ecebc6f39..375a271eac 100644 --- a/crates/gpui/src/platform/mac/window.rs +++ b/crates/gpui/src/platform/mac/window.rs @@ -1074,10 +1074,29 @@ fn window_fullscreen_changed(this: &Object, is_fullscreen: bool) { } } -extern "C" fn window_did_change_key_status(this: &Object, _: Sel, _: id) { +extern "C" fn window_did_change_key_status(this: &Object, selector: Sel, _: id) { let window_state = unsafe { get_window_state(this) }; let window_state_borrow = window_state.borrow(); let is_active = unsafe { window_state_borrow.native_window.isKeyWindow() }; + + // When opening a pop-up while the application isn't active, Cocoa sends a spurious + // `windowDidBecomeKey` message to the previous key window even though that window + // isn't actually key. This causes a bug if the application is later activated while + // the pop-up is still open, making it impossible to activate the previous key window + // even if the pop-up gets closed. The only way to activate it again is to de-activate + // the app and re-activate it, which is a pretty bad UX. + // The following code detects the spurious event and invokes `resignKeyWindow`: + // in theory, we're not supposed to invoke this method manually but it balances out + // the spurious `becomeKeyWindow` event and helps us work around that bug. + if selector == sel!(windowDidBecomeKey:) { + if !is_active { + unsafe { + let _: () = msg_send![window_state_borrow.native_window, resignKeyWindow]; + return; + } + } + } + let executor = window_state_borrow.executor.clone(); drop(window_state_borrow); executor