Skip to content

Commit

Permalink
fix: sync webview theme with window theme on Windows, closes tauri-ap…
Browse files Browse the repository at this point in the history
…ps#5802 (tauri-apps#5874)

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
2 people authored and luoffei committed Dec 29, 2022
1 parent 48cf433 commit f05880e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .changes/webview-theme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"tauri": "patch"
"tauri-runtime": "patch"
"tauri-runtime-wry": "patch"
---

On Windows, change webview theme based on Window theme for more accurate `prefers-color-scheme` support.
25 changes: 25 additions & 0 deletions core/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2851,6 +2851,19 @@ fn handle_event_loop<T: UserEvent>(
}

match event {
#[cfg(windows)]
WryWindowEvent::ThemeChanged(theme) => {
if let Some(window) = windows.borrow().get(&window_id) {
if let Some(WindowHandle::Webview { inner, .. }) = &window.inner {
let theme = match theme {
WryTheme::Dark => wry::webview::Theme::Dark,
WryTheme::Light => wry::webview::Theme::Light,
_ => wry::webview::Theme::Light,
};
inner.set_theme(theme);
}
}
}
WryWindowEvent::CloseRequested => {
on_close_requested(callback, window_id, windows.clone());
}
Expand Down Expand Up @@ -3027,6 +3040,9 @@ fn create_webview<T: UserEvent>(
.with_drag_and_drop(webview_attributes.file_drop_handler_enabled);
}

#[cfg(windows)]
let window_theme = window_builder.inner.window.preferred_theme;

#[cfg(target_os = "macos")]
{
if window_builder.tabbing_identifier.is_none()
Expand Down Expand Up @@ -3077,6 +3093,15 @@ fn create_webview<T: UserEvent>(
webview_builder = webview_builder.with_additional_browser_args(&additional_browser_args);
}

#[cfg(windows)]
if let Some(theme) = window_theme {
webview_builder = webview_builder.with_theme(match theme {
WryTheme::Dark => wry::webview::Theme::Dark,
WryTheme::Light => wry::webview::Theme::Light,
_ => wry::webview::Theme::Light,
});
}

if let Some(handler) = ipc_handler {
webview_builder = webview_builder.with_ipc_handler(create_ipc_handler(
context,
Expand Down

0 comments on commit f05880e

Please sign in to comment.