Skip to content

Commit

Permalink
feat(core): expose set_activation_policy, closes #2258 (#2420)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
lucasfernog and amrbashir committed Aug 13, 2021
1 parent c544cea commit 4a031ad
Show file tree
Hide file tree
Showing 20 changed files with 87 additions and 73 deletions.
5 changes: 5 additions & 0 deletions .changes/app-set-activation-policy.md
@@ -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
@@ -0,0 +1,6 @@
---
"tauri-runtime": patch
"tauri-runtime-wry": patch
---

Adds `set_activation_policy` API to the `Runtime` trait (macOS only).
19 changes: 15 additions & 4 deletions core/tauri-runtime-wry/src/lib.rs
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
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
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
6 changes: 1 addition & 5 deletions core/tauri/src/lib.rs
Expand Up @@ -17,10 +17,6 @@
//! - **system-tray**: Enables application system tray API. Enabled by default if the `systemTray` config is defined on the `tauri.conf.json` file.
//! - **updater**: Enables the application auto updater. Enabled by default if the `updater` config is defined on the `tauri.conf.json` file.

#![allow(
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
clippy::nonstandard_macro_braces,
)]
#![warn(missing_docs, rust_2018_idioms)]
#![cfg_attr(doc_cfg, feature(doc_cfg))]

Expand Down Expand Up @@ -72,7 +68,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
2 changes: 1 addition & 1 deletion core/tauri/src/updater/core.rs
Expand Up @@ -639,7 +639,7 @@ fn copy_files_and_run(
// Example; `/Applications/updater-example.app/Contents/MacOS/updater-example`
// Should return; `updater-example.app`
#[cfg(target_os = "macos")]
fn macos_app_name_in_path(extract_path: &PathBuf) -> String {
fn macos_app_name_in_path(extract_path: &Path) -> String {
let components = extract_path.components();
let app_name = components.last().unwrap();
let app_name = app_name.as_os_str().to_str().unwrap();
Expand Down
27 changes: 14 additions & 13 deletions examples/api/src-tauri/src/main.rs
Expand Up @@ -6,10 +6,6 @@
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
#![allow(
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
clippy::nonstandard_macro_braces,
)]

mod cmd;
mod menu;
Expand All @@ -34,7 +30,8 @@ async fn menu_toggle(window: tauri::Window) {
}

fn main() {
tauri::Builder::default()
#[allow(unused_mut)]
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 +152,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();
}
})
}
4 changes: 0 additions & 4 deletions examples/commands/src-tauri/src/main.rs
Expand Up @@ -6,10 +6,6 @@
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
#![allow(
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
clippy::nonstandard_macro_braces,
)]

// we move some basic commands to a separate module just to show it works
mod commands;
Expand Down
4 changes: 0 additions & 4 deletions examples/helloworld/src-tauri/src/main.rs
Expand Up @@ -6,10 +6,6 @@
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
#![allow(
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
clippy::nonstandard_macro_braces,
)]

fn main() {
tauri::Builder::default()
Expand Down
4 changes: 0 additions & 4 deletions examples/multiwindow/src-tauri/src/main.rs
Expand Up @@ -6,10 +6,6 @@
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
#![allow(
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
clippy::nonstandard_macro_braces,
)]

use tauri::WindowBuilder;

Expand Down
4 changes: 0 additions & 4 deletions examples/navigation/src-tauri/src/main.rs
Expand Up @@ -6,10 +6,6 @@
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
#![allow(
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
clippy::nonstandard_macro_braces,
)]

fn main() {
tauri::Builder::default()
Expand Down
4 changes: 0 additions & 4 deletions examples/resources/src-tauri/src/main.rs
Expand Up @@ -6,10 +6,6 @@
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
#![allow(
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
clippy::nonstandard_macro_braces,
)]

use tauri::{
api::{
Expand Down
4 changes: 0 additions & 4 deletions examples/sidecar/src-tauri/src/main.rs
Expand Up @@ -6,10 +6,6 @@
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
#![allow(
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
clippy::nonstandard_macro_braces,
)]

use tauri::{
api::process::{Command, CommandEvent},
Expand Down
4 changes: 0 additions & 4 deletions examples/splashscreen/src-tauri/src/main.rs
Expand Up @@ -6,10 +6,6 @@
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
#![allow(
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
clippy::nonstandard_macro_braces,
)]

// Application code for a splashscreen system that waits on a Rust initialization script
#[cfg(not(feature = "ui"))]
Expand Down
4 changes: 0 additions & 4 deletions examples/state/src-tauri/src/main.rs
Expand Up @@ -6,10 +6,6 @@
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
#![allow(
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
clippy::nonstandard_macro_braces,
)]

use std::{
collections::HashMap,
Expand Down
4 changes: 0 additions & 4 deletions examples/updater/src-tauri/src/main.rs
Expand Up @@ -6,10 +6,6 @@
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
#![allow(
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
clippy::nonstandard_macro_braces,
)]

#[tauri::command]
fn my_custom_command(argument: String) {
Expand Down
4 changes: 0 additions & 4 deletions tooling/bundler/src/lib.rs
Expand Up @@ -3,10 +3,6 @@
// SPDX-License-Identifier: MIT

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

//! The Tauri bundler is a tool that generates installers or app bundles for executables.
//! It supports auto updating through [tauri](https://docs.rs/tauri).
Expand Down
5 changes: 0 additions & 5 deletions tooling/cli.rs/build.rs
Expand Up @@ -2,11 +2,6 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

#![allow(
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
clippy::nonstandard_macro_braces,
)]

use std::{
env::current_dir,
error::Error,
Expand Down
5 changes: 0 additions & 5 deletions tooling/cli.rs/src/main.rs
Expand Up @@ -2,11 +2,6 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

#![allow(
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
clippy::nonstandard_macro_braces,
)]

pub use anyhow::Result;
use clap::{crate_version, load_yaml, App, AppSettings, ArgMatches};
use dialoguer::Input;
Expand Down

1 comment on commit 4a031ad

@srsholmes
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your hard work on this, much appreciated 👍🏼

Please sign in to comment.