Skip to content

Commit

Permalink
feat(core): expose set_activation_policy, closes #2258
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Aug 13, 2021
1 parent c544cea commit 036a573
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changes/app-set-activation-policy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch
---

Adds `set_activation_policy` API to the `tauri::App` struct (macOS only).
6 changes: 6 additions & 0 deletions .changes/runtime-set-activation-policy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri-runtime": patch
"tauri-runtime-wry" patch
---

Adds `set_activation_policy` API to the `Runtime` trait.
19 changes: 15 additions & 4 deletions core/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ use tauri_runtime::window::MenuEvent;
use tauri_runtime::{SystemTray, SystemTrayEvent};
#[cfg(windows)]
use winapi::shared::windef::HWND;
#[cfg(target_os = "macos")]
use wry::application::platform::macos::WindowExtMacOS;
#[cfg(all(feature = "system-tray", target_os = "macos"))]
use wry::application::platform::macos::{SystemTrayBuilderExtMacOS, SystemTrayExtMacOS};
#[cfg(target_os = "linux")]
Expand Down Expand Up @@ -68,10 +66,11 @@ pub use wry::application::window::{Window, WindowBuilder as WryWindowBuilder, Wi
use wry::webview::WebviewExtWindows;

#[cfg(target_os = "macos")]
use tauri_runtime::menu::NativeImage;
use tauri_runtime::{menu::NativeImage, ActivationPolicy};
#[cfg(target_os = "macos")]
pub use wry::application::platform::macos::{
CustomMenuItemExtMacOS, NativeImage as WryNativeImage,
ActivationPolicy as WryActivationPolicy, CustomMenuItemExtMacOS, EventLoopExtMacOS,
NativeImage as WryNativeImage, WindowExtMacOS,
};

use std::{
Expand Down Expand Up @@ -1653,6 +1652,18 @@ impl Runtime for Wry {
id
}

#[cfg(target_os = "macos")]
fn set_activation_policy(&mut self, activation_policy: ActivationPolicy) {
self
.event_loop
.set_activation_policy(match activation_policy {
ActivationPolicy::Regular => WryActivationPolicy::Regular,
ActivationPolicy::Accessory => WryActivationPolicy::Accessory,
ActivationPolicy::Prohibited => WryActivationPolicy::Prohibited,
_ => unimplemented!(),
});
}

#[cfg(any(target_os = "windows", target_os = "macos"))]
fn run_iteration<F: Fn(RunEvent) + 'static>(&mut self, callback: F) -> RunIteration {
use wry::application::platform::run_return::EventLoopExtRunReturn;
Expand Down
18 changes: 18 additions & 0 deletions core/tauri-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,19 @@ pub struct RunIteration {
pub window_count: usize,
}

/// Application's activation policy. Corresponds to NSApplicationActivationPolicy.
#[cfg(target_os = "macos")]
#[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
#[non_exhaustive]
pub enum ActivationPolicy {
/// Corresponds to NSApplicationActivationPolicyRegular.
Regular,
/// Corresponds to NSApplicationActivationPolicyAccessory.
Accessory,
/// Corresponds to NSApplicationActivationPolicyProhibited.
Prohibited,
}

/// A [`Send`] handle to the runtime.
pub trait RuntimeHandle: Debug + Send + Sized + Clone + 'static {
type Runtime: Runtime<Handle = Self>;
Expand Down Expand Up @@ -328,6 +341,11 @@ pub trait Runtime: Sized + 'static {
#[cfg_attr(doc_cfg, doc(cfg(feature = "system-tray")))]
fn on_system_tray_event<F: Fn(&SystemTrayEvent) + Send + 'static>(&mut self, f: F) -> Uuid;

/// Sets the activation policy for the application. It is set to `NSApplicationActivationPolicyRegular` by default.
#[cfg(target_os = "macos")]
#[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
fn set_activation_policy(&mut self, activation_policy: ActivationPolicy);

/// Runs the one step of the webview runtime event loop and returns control flow to the caller.
#[cfg(any(target_os = "windows", target_os = "macos"))]
fn run_iteration<F: Fn(RunEvent) + 'static>(&mut self, callback: F) -> RunIteration;
Expand Down
27 changes: 27 additions & 0 deletions core/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ use crate::runtime::{Icon, SystemTrayEvent as RuntimeSystemTrayEvent};
#[cfg(feature = "updater")]
use crate::updater;

#[cfg(target_os = "macos")]
use crate::ActivationPolicy;

pub(crate) type GlobalMenuEventListener<R> = Box<dyn Fn(WindowMenuEvent<R>) + Send + Sync>;
pub(crate) type GlobalWindowEventListener<R> = Box<dyn Fn(GlobalWindowEvent<R>) + Send + Sync>;
#[cfg(feature = "system-tray")]
Expand Down Expand Up @@ -388,6 +391,29 @@ impl<R: Runtime> App<R> {
self.handle.clone()
}

/// Sets the activation policy for the application. It is set to `NSApplicationActivationPolicyRegular` by default.
///
/// # Example
/// ```rust,ignore
/// fn main() {
/// let mut app = tauri::Builder::default()
/// .build(tauri::generate_context!())
/// .expect("error while building tauri application");
/// #[cfg(target_os = "macos")]
/// app.set_activation_policy(tauri::ActivationPolicy::Accessory);
/// app.run(|_app_handle, _event| {});
/// }
/// ```
#[cfg(target_os = "macos")]
#[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
pub fn set_activation_policy(&mut self, activation_policy: ActivationPolicy) {
self
.runtime
.as_mut()
.unwrap()
.set_activation_policy(activation_policy);
}

/// Runs the application.
pub fn run<F: Fn(&AppHandle<R>, Event) + 'static>(mut self, callback: F) {
let app_handle = self.handle();
Expand Down Expand Up @@ -438,6 +464,7 @@ impl<R: Runtime> App<R> {
/// }
/// }
/// }
/// ```
#[cfg(any(target_os = "windows", target_os = "macos"))]
pub fn run_iteration(&mut self) -> crate::runtime::RunIteration {
let manager = self.manager.clone();
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub use runtime::menu::CustomMenuItem;

#[cfg(target_os = "macos")]
#[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
pub use runtime::menu::NativeImage;
pub use runtime::{menu::NativeImage, ActivationPolicy};

pub use {
self::api::assets::Assets,
Expand Down
22 changes: 13 additions & 9 deletions examples/api/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async fn menu_toggle(window: tauri::Window) {
}

fn main() {
tauri::Builder::default()
let mut app = tauri::Builder::default()
.on_page_load(|window, _| {
let window_ = window.clone();
window.listen("js-event", move |event| {
Expand Down Expand Up @@ -155,12 +155,16 @@ fn main() {
menu_toggle,
])
.build(tauri::generate_context!())
.expect("error while building tauri application")
.run(|app_handle, e| {
if let Event::CloseRequested { label, api, .. } = e {
api.prevent_close();
let window = app_handle.get_window(&label).unwrap();
window.emit("close-requested", ()).unwrap();
}
})
.expect("error while building tauri application");

#[cfg(target_os = "macos")]
app.set_activation_policy(tauri::ActivationPolicy::Regular);

app.run(|app_handle, e| {
if let Event::CloseRequested { label, api, .. } = e {
api.prevent_close();
let window = app_handle.get_window(&label).unwrap();
window.emit("close-requested", ()).unwrap();
}
})
}

0 comments on commit 036a573

Please sign in to comment.