Skip to content

Commit 4acae1e

Browse files
fix(macos): run app.set_theme on main thread (#13443)
* fix(macos): run `app.set_theme` on main thread * Change file
1 parent d38d90b commit 4acae1e

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-runtime-wry": patch:bug
3+
---
4+
5+
Fix `AppHandle::set_theme` crashes or has no effect on macOS if not ran on main thread

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

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,6 +1429,7 @@ pub enum WebviewMessage {
14291429

14301430
pub enum EventLoopWindowTargetMessage {
14311431
CursorPosition(Sender<Result<PhysicalPosition<f64>>>),
1432+
SetTheme(Option<Theme>),
14321433
}
14331434

14341435
pub type CreateWindowClosure<T> =
@@ -2598,15 +2599,10 @@ impl<T: UserEvent> RuntimeHandle<T> for WryHandle<T> {
25982599
}
25992600

26002601
fn set_theme(&self, theme: Option<Theme>) {
2601-
self
2602-
.context
2603-
.main_thread
2604-
.window_target
2605-
.set_theme(match theme {
2606-
Some(Theme::Light) => Some(TaoTheme::Light),
2607-
Some(Theme::Dark) => Some(TaoTheme::Dark),
2608-
_ => None,
2609-
});
2602+
let _ = send_user_message(
2603+
&self.context,
2604+
Message::EventLoopWindowTarget(EventLoopWindowTargetMessage::SetTheme(theme)),
2605+
);
26102606
}
26112607

26122608
#[cfg(target_os = "macos")]
@@ -2915,18 +2911,18 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
29152911
}
29162912

29172913
fn cursor_position(&self) -> Result<PhysicalPosition<f64>> {
2918-
event_loop_window_getter!(self, EventLoopWindowTargetMessage::CursorPosition)?
2914+
self
2915+
.context
2916+
.main_thread
2917+
.window_target
2918+
.cursor_position()
29192919
.map(PhysicalPositionWrapper)
29202920
.map(Into::into)
29212921
.map_err(|_| Error::FailedToGetCursorPosition)
29222922
}
29232923

29242924
fn set_theme(&self, theme: Option<Theme>) {
2925-
self.event_loop.set_theme(match theme {
2926-
Some(Theme::Light) => Some(TaoTheme::Light),
2927-
Some(Theme::Dark) => Some(TaoTheme::Dark),
2928-
_ => None,
2929-
});
2925+
self.event_loop.set_theme(to_tao_theme(theme));
29302926
}
29312927

29322928
#[cfg(target_os = "macos")]
@@ -3422,11 +3418,7 @@ fn handle_user_message<T: UserEvent>(
34223418
window.set_traffic_light_inset(_position);
34233419
}
34243420
WindowMessage::SetTheme(theme) => {
3425-
window.set_theme(match theme {
3426-
Some(Theme::Light) => Some(TaoTheme::Light),
3427-
Some(Theme::Dark) => Some(TaoTheme::Dark),
3428-
_ => None,
3429-
});
3421+
window.set_theme(to_tao_theme(theme));
34303422
}
34313423
WindowMessage::SetBackgroundColor(color) => {
34323424
window.set_background_color(color.map(Into::into))
@@ -3875,6 +3867,9 @@ fn handle_user_message<T: UserEvent>(
38753867
.map_err(|_| Error::FailedToSendMessage);
38763868
sender.send(pos).unwrap();
38773869
}
3870+
EventLoopWindowTargetMessage::SetTheme(theme) => {
3871+
event_loop.set_theme(to_tao_theme(theme));
3872+
}
38783873
},
38793874
}
38803875
}
@@ -4955,3 +4950,11 @@ fn inner_size(
49554950
) -> TaoPhysicalSize<u32> {
49564951
window.inner_size()
49574952
}
4953+
4954+
fn to_tao_theme(theme: Option<Theme>) -> Option<TaoTheme> {
4955+
match theme {
4956+
Some(Theme::Light) => Some(TaoTheme::Light),
4957+
Some(Theme::Dark) => Some(TaoTheme::Dark),
4958+
_ => None,
4959+
}
4960+
}

0 commit comments

Comments
 (0)