Skip to content

Commit 7a8d570

Browse files
fix: sync webview theme with window theme on Windows, closes #5802 (#5874)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent 0d5835d commit 7a8d570

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

.changes/webview-theme.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"tauri": "patch"
3+
"tauri-runtime": "patch"
4+
"tauri-runtime-wry": "patch"
5+
---
6+
7+
On Windows, change webview theme based on Window theme for more accurate `prefers-color-scheme` support.

core/tauri-runtime-wry/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ exclude = [ "CHANGELOG.md", "/target" ]
1313
readme = "README.md"
1414

1515
[dependencies]
16-
wry = { git = "https://github.com/tauri-apps/wry", branch = "dev", default-features = false, features = [ "file-drop", "protocol" ] }
16+
wry = { git = "https://github.com/tauri-apps/wry", default-features = false, features = [ "file-drop", "protocol" ] }
1717
tauri-runtime = { version = "0.12.1", path = "../tauri-runtime" }
1818
tauri-utils = { version = "1.2.1", path = "../tauri-utils" }
1919
uuid = { version = "1", features = [ "v4" ] }

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2851,6 +2851,19 @@ fn handle_event_loop<T: UserEvent>(
28512851
}
28522852

28532853
match event {
2854+
#[cfg(windows)]
2855+
WryWindowEvent::ThemeChanged(theme) => {
2856+
if let Some(window) = windows.borrow().get(&window_id) {
2857+
if let Some(WindowHandle::Webview { inner, .. }) = &window.inner {
2858+
let theme = match theme {
2859+
WryTheme::Dark => wry::webview::Theme::Dark,
2860+
WryTheme::Light => wry::webview::Theme::Light,
2861+
_ => wry::webview::Theme::Light,
2862+
};
2863+
inner.set_theme(theme);
2864+
}
2865+
}
2866+
}
28542867
WryWindowEvent::CloseRequested => {
28552868
on_close_requested(callback, window_id, windows.clone());
28562869
}
@@ -3025,6 +3038,9 @@ fn create_webview<T: UserEvent>(
30253038
.with_drag_and_drop(webview_attributes.file_drop_handler_enabled);
30263039
}
30273040

3041+
#[cfg(windows)]
3042+
let window_theme = window_builder.inner.window.preferred_theme;
3043+
30283044
#[cfg(target_os = "macos")]
30293045
{
30303046
if window_builder.tabbing_identifier.is_none()
@@ -3075,6 +3091,15 @@ fn create_webview<T: UserEvent>(
30753091
webview_builder = webview_builder.with_additional_browser_args(&additional_browser_args);
30763092
}
30773093

3094+
#[cfg(windows)]
3095+
if let Some(theme) = window_theme {
3096+
webview_builder = webview_builder.with_theme(match theme {
3097+
WryTheme::Dark => wry::webview::Theme::Dark,
3098+
WryTheme::Light => wry::webview::Theme::Light,
3099+
_ => wry::webview::Theme::Light,
3100+
});
3101+
}
3102+
30783103
if let Some(handler) = ipc_handler {
30793104
webview_builder = webview_builder.with_ipc_handler(create_ipc_handler(
30803105
context,

0 commit comments

Comments
 (0)