Switch from using the key window to the main window mac platform API
Mikayla Maki
and
Max
created 2 years ago
When the help menu is open, the help menu's search field is the key window, and this was causing menu item action resolution to fail
co-authored-by: Max <max@zed.dev>
Change summary
crates/gpui/src/app.rs | 2 +-
crates/gpui/src/app/menu.rs | 6 +++---
crates/gpui/src/platform.rs | 2 +-
crates/gpui/src/platform/mac/platform.rs | 4 ++--
crates/gpui/src/platform/mac/window.rs | 8 ++++----
crates/gpui/src/platform/test.rs | 2 +-
6 files changed, 12 insertions(+), 12 deletions(-)
Detailed changes
@@ -1294,7 +1294,7 @@ impl MutableAppContext {
pub fn is_action_available(&self, action: &dyn Action) -> bool {
let action_type = action.as_any().type_id();
- if let Some(window_id) = self.cx.platform.key_window_id() {
+ if let Some(window_id) = self.cx.platform.main_window_id() {
if let Some(focused_view_id) = self.focused_view_id(window_id) {
for view_id in self.ancestors(window_id, focused_view_id) {
if let Some(view) = self.views.get(&(window_id, view_id)) {
@@ -77,9 +77,9 @@ pub(crate) fn setup_menu_handlers(foreground_platform: &dyn ForegroundPlatform,
let cx = app.0.clone();
move |action| {
let mut cx = cx.borrow_mut();
- if let Some(key_window_id) = cx.cx.platform.key_window_id() {
- if let Some(view_id) = cx.focused_view_id(key_window_id) {
- cx.handle_dispatch_action_from_effect(key_window_id, Some(view_id), action);
+ if let Some(main_window_id) = cx.cx.platform.main_window_id() {
+ if let Some(view_id) = cx.focused_view_id(main_window_id) {
+ cx.handle_dispatch_action_from_effect(main_window_id, Some(view_id), action);
return;
}
}
@@ -58,7 +58,7 @@ pub trait Platform: Send + Sync {
options: WindowOptions,
executor: Rc<executor::Foreground>,
) -> Box<dyn Window>;
- fn key_window_id(&self) -> Option<usize>;
+ fn main_window_id(&self) -> Option<usize>;
fn add_status_item(&self) -> Box<dyn Window>;
@@ -587,8 +587,8 @@ impl platform::Platform for MacPlatform {
Box::new(Window::open(id, options, executor, self.fonts()))
}
- fn key_window_id(&self) -> Option<usize> {
- Window::key_window_id()
+ fn main_window_id(&self) -> Option<usize> {
+ Window::main_window_id()
}
fn add_status_item(&self) -> Box<dyn platform::Window> {
@@ -604,12 +604,12 @@ impl Window {
}
}
- pub fn key_window_id() -> Option<usize> {
+ pub fn main_window_id() -> Option<usize> {
unsafe {
let app = NSApplication::sharedApplication(nil);
- let key_window: id = msg_send![app, keyWindow];
- if msg_send![key_window, isKindOfClass: WINDOW_CLASS] {
- let id = get_window_state(&*key_window).borrow().id;
+ let main_window: id = msg_send![app, mainWindow];
+ if msg_send![main_window, isKindOfClass: WINDOW_CLASS] {
+ let id = get_window_state(&*main_window).borrow().id;
Some(id)
} else {
None
@@ -157,7 +157,7 @@ impl super::Platform for Platform {
}))
}
- fn key_window_id(&self) -> Option<usize> {
+ fn main_window_id(&self) -> Option<usize> {
None
}