Skip to content

Commit

Permalink
feat: add EventLoopExtMacOS::set_activate_ignoring_other_apps (#612)
Browse files Browse the repository at this point in the history
  • Loading branch information
grant0417 committed Nov 1, 2022
1 parent 38fef10 commit d2c6a91
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changes/macos-activate-ignoring-other-apps.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": patch
---

Add `EventLoopExtMacOS::set_activate_ignoring_other_apps` on macOS.
17 changes: 17 additions & 0 deletions src/platform/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,16 @@ pub trait EventLoopExtMacOS {
/// [`run`](crate::event_loop::EventLoop::run) or
/// [`run_return`](crate::platform::run_return::EventLoopExtRunReturn::run_return)
fn enable_default_menu_creation(&mut self, enable: bool);

/// Used to prevent the application from automatically activating when launched if
/// another application is already active
///
/// The default behavior is to ignore other applications and activate when launched.
///
/// This function only takes effect if it's called before calling
/// [`run`](crate::event_loop::EventLoop::run) or
/// [`run_return`](crate::platform::run_return::EventLoopExtRunReturn::run_return)
fn set_activate_ignoring_other_apps(&mut self, ignore: bool);
}
impl<T> EventLoopExtMacOS for EventLoop<T> {
#[inline]
Expand All @@ -516,6 +526,13 @@ impl<T> EventLoopExtMacOS for EventLoop<T> {
get_aux_state_mut(&**self.event_loop.delegate).create_default_menu = enable;
}
}

#[inline]
fn set_activate_ignoring_other_apps(&mut self, ignore: bool) {
unsafe {
get_aux_state_mut(&**self.event_loop.delegate).activate_ignoring_other_apps = ignore;
}
}
}

/// Additional methods on `MonitorHandle` that are specific to MacOS.
Expand Down
3 changes: 3 additions & 0 deletions src/platform_impl/macos/app_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub struct AuxDelegateState {
pub activation_policy: ActivationPolicy,

pub create_default_menu: bool,

pub activate_ignoring_other_apps: bool,
}

pub struct AppDelegateClass(pub *const Class);
Expand Down Expand Up @@ -67,6 +69,7 @@ extern "C" fn new(class: &Class, _: Sel) -> id {
Box::into_raw(Box::new(RefCell::new(AuxDelegateState {
activation_policy: ActivationPolicy::Regular,
create_default_menu: true,
activate_ignoring_other_apps: true,
}))) as *mut c_void,
);
this
Expand Down
8 changes: 6 additions & 2 deletions src/platform_impl/macos/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,12 @@ impl AppState {
unsafe {
let ns_app = NSApp();
window_activation_hack(ns_app);
// TODO: Consider allowing the user to specify they don't want their application activated
ns_app.activateIgnoringOtherApps_(YES);
let ignore = if get_aux_state_mut(app_delegate).activate_ignoring_other_apps {
YES
} else {
NO
};
ns_app.activateIgnoringOtherApps_(ignore);
};
HANDLER.set_ready();
HANDLER.waker().start();
Expand Down

0 comments on commit d2c6a91

Please sign in to comment.