Skip to content

Commit

Permalink
Set activation policy at runtime (#353)
Browse files Browse the repository at this point in the history
* MacOS extension: Set activation policy at runtime

* Add change note

* Apply suggestions from code review

Co-authored-by: Amr Bashir <[email protected]>

* [cherry-pick] Merge in kasperkh's docstrings

* Update .changes/add-runtime-activation-policy-mac.md

Co-authored-by: Amr Bashir <[email protected]>

* rustfmt

Co-authored-by: Amr Bashir <[email protected]>
Co-authored-by: Kasper <[email protected]>
  • Loading branch information
3 people authored Mar 30, 2022
1 parent 34be12d commit ef06c50
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changes/add-runtime-activation-policy-mac.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": minor
---

Add `EventLoopWindowTargetExtMacOS::set_activation_policy_at_runtime`.
38 changes: 35 additions & 3 deletions src/platform/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ use crate::{
#[cfg(feature = "tray")]
use crate::system_tray::{SystemTray, SystemTrayBuilder};

use cocoa::{appkit, base::id};
use cocoa::{
appkit::{
self, NSApplicationActivationPolicy, NSApplicationActivationPolicyAccessory,
NSApplicationActivationPolicyProhibited, NSApplicationActivationPolicyRegular,
},
base::id,
};

/// Additional methods on `Window` that are specific to MacOS.
pub trait WindowExtMacOS {
Expand Down Expand Up @@ -100,6 +106,16 @@ impl Default for ActivationPolicy {
}
}

impl From<ActivationPolicy> for NSApplicationActivationPolicy {
fn from(act_pol: ActivationPolicy) -> Self {
match act_pol {
ActivationPolicy::Regular => NSApplicationActivationPolicyRegular,
ActivationPolicy::Accessory => NSApplicationActivationPolicyAccessory,
ActivationPolicy::Prohibited => NSApplicationActivationPolicyProhibited,
}
}
}

pub trait CustomMenuItemExtMacOS {
fn set_native_image(&mut self, native_image: NativeImage);
}
Expand Down Expand Up @@ -393,8 +409,11 @@ pub trait EventLoopExtMacOS {
/// Sets the activation policy for the application. It is set to
/// `NSApplicationActivationPolicyRegular` by default.
///
/// 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)
/// 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).
/// To set the activation policy after that, use
/// [`EventLoopWindowTargetExtMacOS::set_activation_policy_at_runtime`](crate::platform::macos::EventLoopWindowTargetExtMacOS::set_activation_policy_at_runtime).
fn set_activation_policy(&mut self, activation_policy: ActivationPolicy);

/// Used to prevent a default menubar menu from getting created
Expand Down Expand Up @@ -449,6 +468,12 @@ pub trait EventLoopWindowTargetExtMacOS {
fn show_application(&self);
/// Hide the other applications. In most applications this is typically triggered with Command+Option-H.
fn hide_other_applications(&self);
/// Sets the activation policy for the application. It is set to
/// `NSApplicationActivationPolicyRegular` by default.
///
/// To set the activation policy before the app starts running, see
/// [`EventLoopExtMacOS::set_activation_policy`](crate::platform::macos::EventLoopExtMacOS::set_activation_policy).
fn set_activation_policy_at_runtime(&self, activation_policy: ActivationPolicy);
}

impl<T> EventLoopWindowTargetExtMacOS for EventLoopWindowTarget<T> {
Expand All @@ -469,6 +494,13 @@ impl<T> EventLoopWindowTargetExtMacOS for EventLoopWindowTarget<T> {
let app: cocoa::base::id = unsafe { msg_send![cls, sharedApplication] };
unsafe { msg_send![app, hideOtherApplications: 0] }
}

fn set_activation_policy_at_runtime(&self, activation_policy: ActivationPolicy) {
let cls = objc::runtime::Class::get("NSApplication").unwrap();
let app: cocoa::base::id = unsafe { msg_send![cls, sharedApplication] };
let ns_activation_policy: NSApplicationActivationPolicy = activation_policy.into();
unsafe { msg_send![app, setActivationPolicy: ns_activation_policy] }
}
}

#[cfg(feature = "tray")]
Expand Down

0 comments on commit ef06c50

Please sign in to comment.