Skip to content

Commit 95da1a2

Browse files
fix(core): macos #5122 app.runtime panic in app.set_activation_policy (#8713)
* fix #5122 app.runtime panic in set_activation_policy * allow setting the policy at runtime * add change file --------- Co-authored-by: Lucas Nogueira <lucas@tauri.app>
1 parent a35b416 commit 95da1a2

5 files changed

Lines changed: 56 additions & 15 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"tauri": patch:bug
3+
"tauri-runtime": patch:bug
4+
"tauri-runtime-wry": patch:bug
5+
---
6+
7+
Fix calling `set_activation_policy` when the event loop is running.

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

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ use tauri_runtime::{
2626
};
2727

2828
#[cfg(target_os = "macos")]
29-
use tao::platform::macos::EventLoopWindowTargetExtMacOS;
30-
#[cfg(target_os = "macos")]
31-
use tao::platform::macos::WindowBuilderExtMacOS;
29+
use tao::platform::macos::{EventLoopWindowTargetExtMacOS, WindowBuilderExtMacOS};
3230
#[cfg(target_os = "linux")]
3331
use tao::platform::unix::{WindowBuilderExtUnix, WindowExtUnix};
3432
#[cfg(windows)]
@@ -431,6 +429,16 @@ pub fn map_theme(theme: &TaoTheme) -> Theme {
431429
}
432430
}
433431

432+
#[cfg(target_os = "macos")]
433+
fn tao_activation_policy(activation_policy: ActivationPolicy) -> TaoActivationPolicy {
434+
match activation_policy {
435+
ActivationPolicy::Regular => TaoActivationPolicy::Regular,
436+
ActivationPolicy::Accessory => TaoActivationPolicy::Accessory,
437+
ActivationPolicy::Prohibited => TaoActivationPolicy::Prohibited,
438+
_ => unimplemented!(),
439+
}
440+
}
441+
434442
impl<'a> From<&TaoWindowEvent<'a>> for WindowEventWrapper {
435443
fn from(event: &TaoWindowEvent<'a>) -> Self {
436444
let event = match event {
@@ -1201,6 +1209,8 @@ pub type CreateWebviewClosure = Box<dyn FnOnce(&Window) -> Result<WebviewWrapper
12011209

12021210
pub enum Message<T: 'static> {
12031211
Task(Box<dyn FnOnce() + Send>),
1212+
#[cfg(target_os = "macos")]
1213+
SetActivationPolicy(ActivationPolicy),
12041214
RequestExit(i32),
12051215
#[cfg(target_os = "macos")]
12061216
Application(ApplicationMessage),
@@ -1995,6 +2005,14 @@ impl<T: UserEvent> RuntimeHandle<T> for WryHandle<T> {
19952005
EventProxy(self.context.proxy.clone())
19962006
}
19972007

2008+
#[cfg(target_os = "macos")]
2009+
fn set_activation_policy(&self, activation_policy: ActivationPolicy) -> Result<()> {
2010+
send_user_message(
2011+
&self.context,
2012+
Message::SetActivationPolicy(activation_policy),
2013+
)
2014+
}
2015+
19982016
fn request_exit(&self, code: i32) -> Result<()> {
19992017
// NOTE: request_exit cannot use the `send_user_message` function because it accesses the event loop callback
20002018
self
@@ -2296,12 +2314,7 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
22962314
fn set_activation_policy(&mut self, activation_policy: ActivationPolicy) {
22972315
self
22982316
.event_loop
2299-
.set_activation_policy(match activation_policy {
2300-
ActivationPolicy::Regular => TaoActivationPolicy::Regular,
2301-
ActivationPolicy::Accessory => TaoActivationPolicy::Accessory,
2302-
ActivationPolicy::Prohibited => TaoActivationPolicy::Prohibited,
2303-
_ => unimplemented!(),
2304-
});
2317+
.set_activation_policy(tao_activation_policy(activation_policy));
23052318
}
23062319

23072320
#[cfg(target_os = "macos")]
@@ -2446,6 +2459,10 @@ fn handle_user_message<T: UserEvent>(
24462459
} = context;
24472460
match message {
24482461
Message::Task(task) => task(),
2462+
#[cfg(target_os = "macos")]
2463+
Message::SetActivationPolicy(activation_policy) => {
2464+
event_loop.set_activation_policy_at_runtime(tao_activation_policy(activation_policy))
2465+
}
24492466
Message::RequestExit(_code) => panic!("cannot handle RequestExit on the main thread"),
24502467
#[cfg(target_os = "macos")]
24512468
Message::Application(application_message) => match application_message {

core/tauri-runtime/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,11 @@ pub trait RuntimeHandle<T: UserEvent>: Debug + Clone + Send + Sync + Sized + 'st
208208
/// Creates an `EventLoopProxy` that can be used to dispatch user events to the main event loop.
209209
fn create_proxy(&self) -> <Self::Runtime as Runtime<T>>::EventLoopProxy;
210210

211+
/// Sets the activation policy for the application.
212+
#[cfg(target_os = "macos")]
213+
#[cfg_attr(docsrs, doc(cfg(target_os = "macos")))]
214+
fn set_activation_policy(&self, activation_policy: ActivationPolicy) -> Result<()>;
215+
211216
/// Requests an exit of the event loop.
212217
fn request_exit(&self, code: i32) -> Result<()>;
213218

@@ -313,7 +318,7 @@ pub trait Runtime<T: UserEvent>: Debug + Sized + 'static {
313318
fn primary_monitor(&self) -> Option<Monitor>;
314319
fn available_monitors(&self) -> Vec<Monitor>;
315320

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

core/tauri/src/app.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -876,11 +876,14 @@ impl<R: Runtime> App<R> {
876876
#[cfg(target_os = "macos")]
877877
#[cfg_attr(docsrs, doc(cfg(target_os = "macos")))]
878878
pub fn set_activation_policy(&mut self, activation_policy: ActivationPolicy) {
879-
self
880-
.runtime
881-
.as_mut()
882-
.unwrap()
883-
.set_activation_policy(activation_policy);
879+
if let Some(runtime) = self.runtime.as_mut() {
880+
runtime.set_activation_policy(activation_policy);
881+
} else {
882+
let _ = self
883+
.app_handle()
884+
.runtime_handle
885+
.set_activation_policy(activation_policy);
886+
}
884887
}
885888

886889
/// Change the device event filter mode.

core/tauri/src/test/mock_runtime.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,15 @@ impl<T: UserEvent> RuntimeHandle<T> for MockRuntimeHandle {
120120
EventProxy {}
121121
}
122122

123+
#[cfg(target_os = "macos")]
124+
#[cfg_attr(docsrs, doc(cfg(target_os = "macos")))]
125+
fn set_activation_policy(
126+
&self,
127+
activation_policy: tauri_runtime::ActivationPolicy,
128+
) -> Result<()> {
129+
Ok(())
130+
}
131+
123132
fn request_exit(&self, code: i32) -> Result<()> {
124133
unimplemented!()
125134
}

0 commit comments

Comments
 (0)