From 5dff4b1bfbaf91143f8b9c267de013e85d321705 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Mon, 6 Feb 2023 05:37:07 -0700 Subject: [PATCH] macos: fixup open_untitled_file handling This event is not triggered unless invoking the bundle via open, and we need to gate it until after we've finished launching, otherwise we will open two windows on startup. closes: https://github.com/wez/wezterm/issues/3064 refs: https://github.com/wez/wezterm/issues/3057 --- window/src/os/macos/app.rs | 39 +++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/window/src/os/macos/app.rs b/window/src/os/macos/app.rs index 7082a9d5c..150e90dc5 100644 --- a/window/src/os/macos/app.rs +++ b/window/src/os/macos/app.rs @@ -9,7 +9,7 @@ use cocoa::foundation::NSInteger; use config::keyassignment::KeyAssignment; use objc::declare::ClassDecl; use objc::rc::StrongPtr; -use objc::runtime::{Class, Object, Sel}; +use objc::runtime::{Class, Object, Sel, BOOL, NO, YES}; use objc::*; const CLS_NAME: &str = "WezTermAppDelegate"; @@ -68,14 +68,31 @@ extern "C" fn application_will_finish_launching( log::debug!("application_will_finish_launching"); } -extern "C" fn application_open_untitled_file(_self: &mut Object, _sel: Sel, _app: *mut Object) { - if let Some(conn) = Connection::get() { - conn.dispatch_app_event(ApplicationEvent::PerformKeyAssignment( - KeyAssignment::SpawnWindow, - )); +extern "C" fn application_did_finish_launching(this: &mut Object, _sel: Sel, _notif: *mut Object) { + log::debug!("application_did_finish_launching"); + unsafe { + (*this).set_ivar("launched", YES); } } +extern "C" fn application_open_untitled_file( + this: &mut Object, + _sel: Sel, + _app: *mut Object, +) -> BOOL { + let launched: BOOL = unsafe { *this.get_ivar("launched") }; + log::debug!("application_open_file launched={launched}"); + if let Some(conn) = Connection::get() { + if launched == YES { + conn.dispatch_app_event(ApplicationEvent::PerformKeyAssignment( + KeyAssignment::SpawnWindow, + )); + } + return YES; + } + NO +} + extern "C" fn wezterm_perform_key_assignment( _self: &mut Object, _sel: Sel, @@ -149,6 +166,8 @@ fn get_class() -> &'static Class { let mut cls = ClassDecl::new(CLS_NAME, class!(NSWindow)) .expect("Unable to register application class"); + cls.add_ivar::("launched"); + unsafe { cls.add_method( sel!(applicationShouldTerminate:), @@ -158,6 +177,10 @@ fn get_class() -> &'static Class { sel!(applicationWillFinishLaunching:), application_will_finish_launching as extern "C" fn(&mut Object, Sel, *mut Object), ); + cls.add_method( + sel!(applicationDidFinishLaunching:), + application_did_finish_launching as extern "C" fn(&mut Object, Sel, *mut Object), + ); cls.add_method( sel!(application:openFile:), application_open_file as extern "C" fn(&mut Object, Sel, *mut Object, *mut Object), @@ -177,7 +200,8 @@ fn get_class() -> &'static Class { ); cls.add_method( sel!(applicationOpenUntitledFile:), - application_open_untitled_file as extern "C" fn(&mut Object, Sel, *mut Object), + application_open_untitled_file + as extern "C" fn(&mut Object, Sel, *mut Object) -> BOOL, ); } @@ -190,6 +214,7 @@ pub fn create_app_delegate() -> StrongPtr { unsafe { let delegate: *mut Object = msg_send![cls, alloc]; let delegate: *mut Object = msg_send![delegate, init]; + (*delegate).set_ivar("launched", NO); StrongPtr::new(delegate) } }