Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/run-set-theme-on-main-thread.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri-runtime-wry": patch:bug
---

Fix `AppHandle::set_theme` crashes or has no effect on macOS if not ran on main thread
43 changes: 23 additions & 20 deletions crates/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1429,6 +1429,7 @@ pub enum WebviewMessage {

pub enum EventLoopWindowTargetMessage {
CursorPosition(Sender<Result<PhysicalPosition<f64>>>),
SetTheme(Option<Theme>),
}

pub type CreateWindowClosure<T> =
Expand Down Expand Up @@ -2598,15 +2599,10 @@ impl<T: UserEvent> RuntimeHandle<T> for WryHandle<T> {
}

fn set_theme(&self, theme: Option<Theme>) {
self
.context
.main_thread
.window_target
.set_theme(match theme {
Some(Theme::Light) => Some(TaoTheme::Light),
Some(Theme::Dark) => Some(TaoTheme::Dark),
_ => None,
});
let _ = send_user_message(
&self.context,
Message::EventLoopWindowTarget(EventLoopWindowTargetMessage::SetTheme(theme)),
);
}

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

fn cursor_position(&self) -> Result<PhysicalPosition<f64>> {
event_loop_window_getter!(self, EventLoopWindowTargetMessage::CursorPosition)?
self
.context
.main_thread
.window_target
.cursor_position()
Comment on lines +2914 to +2918

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think this is not needed as Wry is !Send and can only be used on main thread so reverted it back

.map(PhysicalPositionWrapper)
.map(Into::into)
.map_err(|_| Error::FailedToGetCursorPosition)
}

fn set_theme(&self, theme: Option<Theme>) {
self.event_loop.set_theme(match theme {
Some(Theme::Light) => Some(TaoTheme::Light),
Some(Theme::Dark) => Some(TaoTheme::Dark),
_ => None,
});
self.event_loop.set_theme(to_tao_theme(theme));
}

#[cfg(target_os = "macos")]
Expand Down Expand Up @@ -3422,11 +3418,7 @@ fn handle_user_message<T: UserEvent>(
window.set_traffic_light_inset(_position);
}
WindowMessage::SetTheme(theme) => {
window.set_theme(match theme {
Some(Theme::Light) => Some(TaoTheme::Light),
Some(Theme::Dark) => Some(TaoTheme::Dark),
_ => None,
});
window.set_theme(to_tao_theme(theme));
}
WindowMessage::SetBackgroundColor(color) => {
window.set_background_color(color.map(Into::into))
Expand Down Expand Up @@ -3875,6 +3867,9 @@ fn handle_user_message<T: UserEvent>(
.map_err(|_| Error::FailedToSendMessage);
sender.send(pos).unwrap();
}
EventLoopWindowTargetMessage::SetTheme(theme) => {
event_loop.set_theme(to_tao_theme(theme));
}
},
}
}
Expand Down Expand Up @@ -4955,3 +4950,11 @@ fn inner_size(
) -> TaoPhysicalSize<u32> {
window.inner_size()
}

fn to_tao_theme(theme: Option<Theme>) -> Option<TaoTheme> {
match theme {
Some(Theme::Light) => Some(TaoTheme::Light),
Some(Theme::Dark) => Some(TaoTheme::Dark),
_ => None,
}
}