Skip to content

Commit

Permalink
feat(core): add AppHandle::set_activation_policy (#9842)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored May 21, 2024
1 parent d0d974f commit 5d20530
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changes/app-handle-set-activation-policy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch:enhance
---

Added `AppHandle::set_activation_policy` for macOS.
38 changes: 27 additions & 11 deletions core/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,26 @@ impl<R: Runtime> AppHandle<R> {
}
crate::process::restart(&self.env());
}

/// Sets the activation policy for the application. It is set to `NSApplicationActivationPolicyRegular` by default.
///
/// # Examples
/// ```,no_run
/// tauri::Builder::default()
/// .setup(move |app| {
/// #[cfg(target_os = "macos")]
/// app.handle().set_activation_policy(tauri::ActivationPolicy::Accessory);
/// Ok(())
/// });
/// ```
#[cfg(target_os = "macos")]
#[cfg_attr(docsrs, doc(cfg(target_os = "macos")))]
pub fn set_activation_policy(&self, activation_policy: ActivationPolicy) -> crate::Result<()> {
self
.runtime_handle
.set_activation_policy(activation_policy)
.map_err(Into::into)
}
}

impl<R: Runtime> Manager<R> for AppHandle<R> {
Expand Down Expand Up @@ -898,24 +918,20 @@ impl<R: Runtime> App<R> {
///
/// # Examples
/// ```,no_run
/// let mut app = tauri::Builder::default()
/// // on an actual app, remove the string argument
/// .build(tauri::generate_context!("test/fixture/src-tauri/tauri.conf.json"))
/// .expect("error while building tauri application");
/// #[cfg(target_os = "macos")]
/// app.set_activation_policy(tauri::ActivationPolicy::Accessory);
/// app.run(|_app_handle, _event| {});
/// tauri::Builder::default()
/// .setup(move |app| {
/// #[cfg(target_os = "macos")]
/// app.set_activation_policy(tauri::ActivationPolicy::Accessory);
/// Ok(())
/// });
/// ```
#[cfg(target_os = "macos")]
#[cfg_attr(docsrs, doc(cfg(target_os = "macos")))]
pub fn set_activation_policy(&mut self, activation_policy: ActivationPolicy) {
if let Some(runtime) = self.runtime.as_mut() {
runtime.set_activation_policy(activation_policy);
} else {
let _ = self
.app_handle()
.runtime_handle
.set_activation_policy(activation_policy);
let _ = self.app_handle().set_activation_policy(activation_policy);
}
}

Expand Down

0 comments on commit 5d20530

Please sign in to comment.