Skip to content

Commit 4a031ad

Browse files
feat(core): expose set_activation_policy, closes #2258 (#2420)
* feat(core): expose `set_activation_policy`, closes #2258 * fix change file [skip ci] * Update .changes/runtime-set-activation-policy.md [skip ci] Co-authored-by: Amr Bashir <48618675+amrbashir@users.noreply.github.com> * clippy * allow unused mut on example Co-authored-by: Amr Bashir <48618675+amrbashir@users.noreply.github.com>
1 parent c544cea commit 4a031ad

File tree

20 files changed

+87
-73
lines changed

20 files changed

+87
-73
lines changed

.changes/app-set-activation-policy.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Adds `set_activation_policy` API to the `tauri::App` struct (macOS only).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-runtime": patch
3+
"tauri-runtime-wry": patch
4+
---
5+
6+
Adds `set_activation_policy` API to the `Runtime` trait (macOS only).

core/tauri-runtime-wry/src/lib.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ use tauri_runtime::window::MenuEvent;
2323
use tauri_runtime::{SystemTray, SystemTrayEvent};
2424
#[cfg(windows)]
2525
use winapi::shared::windef::HWND;
26-
#[cfg(target_os = "macos")]
27-
use wry::application::platform::macos::WindowExtMacOS;
2826
#[cfg(all(feature = "system-tray", target_os = "macos"))]
2927
use wry::application::platform::macos::{SystemTrayBuilderExtMacOS, SystemTrayExtMacOS};
3028
#[cfg(target_os = "linux")]
@@ -68,10 +66,11 @@ pub use wry::application::window::{Window, WindowBuilder as WryWindowBuilder, Wi
6866
use wry::webview::WebviewExtWindows;
6967

7068
#[cfg(target_os = "macos")]
71-
use tauri_runtime::menu::NativeImage;
69+
use tauri_runtime::{menu::NativeImage, ActivationPolicy};
7270
#[cfg(target_os = "macos")]
7371
pub use wry::application::platform::macos::{
74-
CustomMenuItemExtMacOS, NativeImage as WryNativeImage,
72+
ActivationPolicy as WryActivationPolicy, CustomMenuItemExtMacOS, EventLoopExtMacOS,
73+
NativeImage as WryNativeImage, WindowExtMacOS,
7574
};
7675

7776
use std::{
@@ -1653,6 +1652,18 @@ impl Runtime for Wry {
16531652
id
16541653
}
16551654

1655+
#[cfg(target_os = "macos")]
1656+
fn set_activation_policy(&mut self, activation_policy: ActivationPolicy) {
1657+
self
1658+
.event_loop
1659+
.set_activation_policy(match activation_policy {
1660+
ActivationPolicy::Regular => WryActivationPolicy::Regular,
1661+
ActivationPolicy::Accessory => WryActivationPolicy::Accessory,
1662+
ActivationPolicy::Prohibited => WryActivationPolicy::Prohibited,
1663+
_ => unimplemented!(),
1664+
});
1665+
}
1666+
16561667
#[cfg(any(target_os = "windows", target_os = "macos"))]
16571668
fn run_iteration<F: Fn(RunEvent) + 'static>(&mut self, callback: F) -> RunIteration {
16581669
use wry::application::platform::run_return::EventLoopExtRunReturn;

core/tauri-runtime/src/lib.rs

+18
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,19 @@ pub struct RunIteration {
217217
pub window_count: usize,
218218
}
219219

220+
/// Application's activation policy. Corresponds to NSApplicationActivationPolicy.
221+
#[cfg(target_os = "macos")]
222+
#[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
223+
#[non_exhaustive]
224+
pub enum ActivationPolicy {
225+
/// Corresponds to NSApplicationActivationPolicyRegular.
226+
Regular,
227+
/// Corresponds to NSApplicationActivationPolicyAccessory.
228+
Accessory,
229+
/// Corresponds to NSApplicationActivationPolicyProhibited.
230+
Prohibited,
231+
}
232+
220233
/// A [`Send`] handle to the runtime.
221234
pub trait RuntimeHandle: Debug + Send + Sized + Clone + 'static {
222235
type Runtime: Runtime<Handle = Self>;
@@ -328,6 +341,11 @@ pub trait Runtime: Sized + 'static {
328341
#[cfg_attr(doc_cfg, doc(cfg(feature = "system-tray")))]
329342
fn on_system_tray_event<F: Fn(&SystemTrayEvent) + Send + 'static>(&mut self, f: F) -> Uuid;
330343

344+
/// Sets the activation policy for the application. It is set to `NSApplicationActivationPolicyRegular` by default.
345+
#[cfg(target_os = "macos")]
346+
#[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
347+
fn set_activation_policy(&mut self, activation_policy: ActivationPolicy);
348+
331349
/// Runs the one step of the webview runtime event loop and returns control flow to the caller.
332350
#[cfg(any(target_os = "windows", target_os = "macos"))]
333351
fn run_iteration<F: Fn(RunEvent) + 'static>(&mut self, callback: F) -> RunIteration;

core/tauri/src/app.rs

+27
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ use crate::runtime::{Icon, SystemTrayEvent as RuntimeSystemTrayEvent};
4040
#[cfg(feature = "updater")]
4141
use crate::updater;
4242

43+
#[cfg(target_os = "macos")]
44+
use crate::ActivationPolicy;
45+
4346
pub(crate) type GlobalMenuEventListener<R> = Box<dyn Fn(WindowMenuEvent<R>) + Send + Sync>;
4447
pub(crate) type GlobalWindowEventListener<R> = Box<dyn Fn(GlobalWindowEvent<R>) + Send + Sync>;
4548
#[cfg(feature = "system-tray")]
@@ -388,6 +391,29 @@ impl<R: Runtime> App<R> {
388391
self.handle.clone()
389392
}
390393

394+
/// Sets the activation policy for the application. It is set to `NSApplicationActivationPolicyRegular` by default.
395+
///
396+
/// # Example
397+
/// ```rust,ignore
398+
/// fn main() {
399+
/// let mut app = tauri::Builder::default()
400+
/// .build(tauri::generate_context!())
401+
/// .expect("error while building tauri application");
402+
/// #[cfg(target_os = "macos")]
403+
/// app.set_activation_policy(tauri::ActivationPolicy::Accessory);
404+
/// app.run(|_app_handle, _event| {});
405+
/// }
406+
/// ```
407+
#[cfg(target_os = "macos")]
408+
#[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
409+
pub fn set_activation_policy(&mut self, activation_policy: ActivationPolicy) {
410+
self
411+
.runtime
412+
.as_mut()
413+
.unwrap()
414+
.set_activation_policy(activation_policy);
415+
}
416+
391417
/// Runs the application.
392418
pub fn run<F: Fn(&AppHandle<R>, Event) + 'static>(mut self, callback: F) {
393419
let app_handle = self.handle();
@@ -438,6 +464,7 @@ impl<R: Runtime> App<R> {
438464
/// }
439465
/// }
440466
/// }
467+
/// ```
441468
#[cfg(any(target_os = "windows", target_os = "macos"))]
442469
pub fn run_iteration(&mut self) -> crate::runtime::RunIteration {
443470
let manager = self.manager.clone();

core/tauri/src/lib.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717
//! - **system-tray**: Enables application system tray API. Enabled by default if the `systemTray` config is defined on the `tauri.conf.json` file.
1818
//! - **updater**: Enables the application auto updater. Enabled by default if the `updater` config is defined on the `tauri.conf.json` file.
1919
20-
#![allow(
21-
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
22-
clippy::nonstandard_macro_braces,
23-
)]
2420
#![warn(missing_docs, rust_2018_idioms)]
2521
#![cfg_attr(doc_cfg, feature(doc_cfg))]
2622

@@ -72,7 +68,7 @@ pub use runtime::menu::CustomMenuItem;
7268

7369
#[cfg(target_os = "macos")]
7470
#[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
75-
pub use runtime::menu::NativeImage;
71+
pub use runtime::{menu::NativeImage, ActivationPolicy};
7672

7773
pub use {
7874
self::api::assets::Assets,

core/tauri/src/updater/core.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ fn copy_files_and_run(
639639
// Example; `/Applications/updater-example.app/Contents/MacOS/updater-example`
640640
// Should return; `updater-example.app`
641641
#[cfg(target_os = "macos")]
642-
fn macos_app_name_in_path(extract_path: &PathBuf) -> String {
642+
fn macos_app_name_in_path(extract_path: &Path) -> String {
643643
let components = extract_path.components();
644644
let app_name = components.last().unwrap();
645645
let app_name = app_name.as_os_str().to_str().unwrap();

examples/api/src-tauri/src/main.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
all(not(debug_assertions), target_os = "windows"),
77
windows_subsystem = "windows"
88
)]
9-
#![allow(
10-
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
11-
clippy::nonstandard_macro_braces,
12-
)]
139

1410
mod cmd;
1511
mod menu;
@@ -34,7 +30,8 @@ async fn menu_toggle(window: tauri::Window) {
3430
}
3531

3632
fn main() {
37-
tauri::Builder::default()
33+
#[allow(unused_mut)]
34+
let mut app = tauri::Builder::default()
3835
.on_page_load(|window, _| {
3936
let window_ = window.clone();
4037
window.listen("js-event", move |event| {
@@ -155,12 +152,16 @@ fn main() {
155152
menu_toggle,
156153
])
157154
.build(tauri::generate_context!())
158-
.expect("error while building tauri application")
159-
.run(|app_handle, e| {
160-
if let Event::CloseRequested { label, api, .. } = e {
161-
api.prevent_close();
162-
let window = app_handle.get_window(&label).unwrap();
163-
window.emit("close-requested", ()).unwrap();
164-
}
165-
})
155+
.expect("error while building tauri application");
156+
157+
#[cfg(target_os = "macos")]
158+
app.set_activation_policy(tauri::ActivationPolicy::Regular);
159+
160+
app.run(|app_handle, e| {
161+
if let Event::CloseRequested { label, api, .. } = e {
162+
api.prevent_close();
163+
let window = app_handle.get_window(&label).unwrap();
164+
window.emit("close-requested", ()).unwrap();
165+
}
166+
})
166167
}

examples/commands/src-tauri/src/main.rs

-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
all(not(debug_assertions), target_os = "windows"),
77
windows_subsystem = "windows"
88
)]
9-
#![allow(
10-
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
11-
clippy::nonstandard_macro_braces,
12-
)]
139

1410
// we move some basic commands to a separate module just to show it works
1511
mod commands;

examples/helloworld/src-tauri/src/main.rs

-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
all(not(debug_assertions), target_os = "windows"),
77
windows_subsystem = "windows"
88
)]
9-
#![allow(
10-
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
11-
clippy::nonstandard_macro_braces,
12-
)]
139

1410
fn main() {
1511
tauri::Builder::default()

examples/multiwindow/src-tauri/src/main.rs

-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
all(not(debug_assertions), target_os = "windows"),
77
windows_subsystem = "windows"
88
)]
9-
#![allow(
10-
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
11-
clippy::nonstandard_macro_braces,
12-
)]
139

1410
use tauri::WindowBuilder;
1511

examples/navigation/src-tauri/src/main.rs

-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
all(not(debug_assertions), target_os = "windows"),
77
windows_subsystem = "windows"
88
)]
9-
#![allow(
10-
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
11-
clippy::nonstandard_macro_braces,
12-
)]
139

1410
fn main() {
1511
tauri::Builder::default()

examples/resources/src-tauri/src/main.rs

-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
all(not(debug_assertions), target_os = "windows"),
77
windows_subsystem = "windows"
88
)]
9-
#![allow(
10-
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
11-
clippy::nonstandard_macro_braces,
12-
)]
139

1410
use tauri::{
1511
api::{

examples/sidecar/src-tauri/src/main.rs

-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
all(not(debug_assertions), target_os = "windows"),
77
windows_subsystem = "windows"
88
)]
9-
#![allow(
10-
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
11-
clippy::nonstandard_macro_braces,
12-
)]
139

1410
use tauri::{
1511
api::process::{Command, CommandEvent},

examples/splashscreen/src-tauri/src/main.rs

-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
all(not(debug_assertions), target_os = "windows"),
77
windows_subsystem = "windows"
88
)]
9-
#![allow(
10-
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
11-
clippy::nonstandard_macro_braces,
12-
)]
139

1410
// Application code for a splashscreen system that waits on a Rust initialization script
1511
#[cfg(not(feature = "ui"))]

examples/state/src-tauri/src/main.rs

-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
all(not(debug_assertions), target_os = "windows"),
77
windows_subsystem = "windows"
88
)]
9-
#![allow(
10-
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
11-
clippy::nonstandard_macro_braces,
12-
)]
139

1410
use std::{
1511
collections::HashMap,

examples/updater/src-tauri/src/main.rs

-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
all(not(debug_assertions), target_os = "windows"),
77
windows_subsystem = "windows"
88
)]
9-
#![allow(
10-
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
11-
clippy::nonstandard_macro_braces,
12-
)]
139

1410
#[tauri::command]
1511
fn my_custom_command(argument: String) {

tooling/bundler/src/lib.rs

-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
// SPDX-License-Identifier: MIT
44

55
#![warn(missing_docs, rust_2018_idioms)]
6-
#![allow(
7-
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
8-
clippy::nonstandard_macro_braces,
9-
)]
106

117
//! The Tauri bundler is a tool that generates installers or app bundles for executables.
128
//! It supports auto updating through [tauri](https://docs.rs/tauri).

tooling/cli.rs/build.rs

-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
#![allow(
6-
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
7-
clippy::nonstandard_macro_braces,
8-
)]
9-
105
use std::{
116
env::current_dir,
127
error::Error,

tooling/cli.rs/src/main.rs

-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
#![allow(
6-
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
7-
clippy::nonstandard_macro_braces,
8-
)]
9-
105
pub use anyhow::Result;
116
use clap::{crate_version, load_yaml, App, AppSettings, ArgMatches};
127
use dialoguer::Input;

0 commit comments

Comments
 (0)