Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sync webview theme with window theme on Windows, closes #5802 #5874

Merged
merged 5 commits into from
Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/webview-theme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": "patch"
---

On Windows, change webview theme based on Window theme for more accurate `prefers-color-scheme` support.
2 changes: 1 addition & 1 deletion core/tauri-runtime-wry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exclude = [ "CHANGELOG.md", "/target" ]
readme = "README.md"

[dependencies]
wry = { version = "0.23", default-features = false, features = [ "file-drop", "protocol" ] }
wry = { git = "https://github.com/tauri-apps/wry", default-features = false, features = [ "file-drop", "protocol" ] }
Copy link
Member Author

Choose a reason for hiding this comment

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

TODO: update to wry@0.24 when it is released and before this is merged.

tauri-runtime = { version = "0.12.1", path = "../tauri-runtime" }
tauri-utils = { version = "1.2.1", path = "../tauri-utils" }
uuid = { version = "1", features = [ "v4" ] }
Expand Down
25 changes: 23 additions & 2 deletions core/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ pub use wry::application::platform::macos::{
};

use std::{
borrow::Cow,
cell::RefCell,
collections::{
hash_map::Entry::{Occupied, Vacant},
Expand Down Expand Up @@ -286,7 +287,7 @@ impl From<&WryRequest<Vec<u8>>> for HttpRequestWrapper {
}

// response
struct HttpResponseWrapper(WryResponse<Vec<u8>>);
struct HttpResponseWrapper(WryResponse<Cow<'static, [u8]>>);
impl From<HttpResponse> for HttpResponseWrapper {
fn from(response: HttpResponse) -> Self {
let (parts, body) = response.into_parts();
Expand All @@ -300,7 +301,7 @@ impl From<HttpResponse> for HttpResponseWrapper {
res_builder = res_builder.header(name, val);
}

let res = res_builder.body(body).unwrap();
let res = res_builder.body(body.into()).unwrap();
Self(res)
}
}
Expand Down Expand Up @@ -2840,6 +2841,18 @@ 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,
_ => wry::webview::Theme::Light,
};
inner.set_theme(theme);
}
}
}
WryWindowEvent::CloseRequested => {
on_close_requested(callback, window_id, windows.clone());
}
Expand Down Expand Up @@ -3059,6 +3072,14 @@ fn create_webview<T: UserEvent>(
webview_builder = webview_builder.with_additional_browser_args(&additional_browser_args);
}

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

if let Some(handler) = ipc_handler {
webview_builder = webview_builder.with_ipc_handler(create_ipc_handler(
context,
Expand Down
2 changes: 2 additions & 0 deletions core/tauri-runtime/src/webview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct WebviewAttributes {
pub clipboard: bool,
pub accept_first_mouse: bool,
pub additional_browser_args: Option<String>,
pub theme: Option<Theme>,
}

impl WebviewAttributes {
Expand All @@ -43,6 +44,7 @@ impl WebviewAttributes {
clipboard: false,
accept_first_mouse: false,
additional_browser_args: None,
theme: None,
}
}

Expand Down
1 change: 1 addition & 0 deletions core/tauri/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ impl<'a, R: Runtime> WindowBuilder<'a, R> {
/// - **macOS**: Only supported on macOS 10.14+.
#[must_use]
pub fn theme(mut self, theme: Option<Theme>) -> Self {
self.webview_attributes.theme = theme;
self.window_builder = self.window_builder.theme(theme);
self
}
Expand Down