Skip to content

Commit 276c4b1

Browse files
fix(Windows): sync menubar theme with the window theme (#9832)
* fix(Windows): sync menubar theme with the window theme ref: tauri-apps/muda#170 * fix moc runtime * muda 0.13.4 * missing change file --------- Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent 9ac9303 commit 276c4b1

File tree

11 files changed

+54
-9
lines changed

11 files changed

+54
-9
lines changed
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri": "patch:bug"
3+
---
4+
5+
On Windows, fix wrong menubar theme when window is using an explicit theme.
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-runtime": patch:enhance
3+
"tauri-runtime-wry": patch:enhance
4+
---
5+
6+
Added `WindowBuilder::get_theme`.

Cargo.lock

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

+7
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,13 @@ impl WindowBuilder for WindowBuilderWrapper {
10231023
fn has_icon(&self) -> bool {
10241024
self.inner.window.window_icon.is_some()
10251025
}
1026+
1027+
fn get_theme(&self) -> Option<Theme> {
1028+
self.inner.window.preferred_theme.map(|theme| match theme {
1029+
TaoTheme::Dark => Theme::Dark,
1030+
_ => Theme::Light,
1031+
})
1032+
}
10261033
}
10271034

10281035
#[cfg(any(

core/tauri-runtime/src/window.rs

+2
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,8 @@ pub trait WindowBuilder: WindowBuilderBase {
409409

410410
/// Whether the icon was set or not.
411411
fn has_icon(&self) -> bool;
412+
413+
fn get_theme(&self) -> Option<Theme>;
412414
}
413415

414416
/// A window that has yet to be built.

core/tauri/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ dunce = "1"
7878
specta = { version = "^2.0.0-rc.9", optional = true, default-features = false, features = [ "function" ] }
7979

8080
[target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\", target_os = \"windows\", target_os = \"macos\"))".dependencies]
81-
muda = { version = "0.13", default-features = false, features = [ "serde" ] }
81+
muda = { version = "0.13.4", default-features = false, features = [ "serde" ] }
8282
tray-icon = { version = "0.13", default-features = false, features = [ "serde" ], optional = true }
8383

8484
[target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies]

core/tauri/src/manager/menu.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ impl<R: Runtime> MenuManager<R> {
5454
pub(crate) fn prepare_window_menu_creation_handler(
5555
&self,
5656
window_menu: Option<&crate::window::WindowMenu<R>>,
57+
#[allow(unused)] theme: Option<tauri_utils::Theme>,
5758
) -> Option<impl Fn(tauri_runtime::window::RawWindow<'_>)> {
5859
{
5960
if let Some(menu) = window_menu {
@@ -71,7 +72,12 @@ impl<R: Runtime> MenuManager<R> {
7172
let menu = menu.menu.clone();
7273
Some(move |raw: tauri_runtime::window::RawWindow<'_>| {
7374
#[cfg(target_os = "windows")]
74-
let _ = menu.inner().init_for_hwnd(raw.hwnd as _);
75+
{
76+
let theme = theme
77+
.map(crate::menu::map_to_menu_theme)
78+
.unwrap_or(muda::MenuTheme::Auto);
79+
let _ = menu.inner().init_for_hwnd_with_theme(raw.hwnd as _, theme);
80+
}
7581
#[cfg(any(
7682
target_os = "linux",
7783
target_os = "dragonfly",

core/tauri/src/menu/mod.rs

+9
Original file line numberDiff line numberDiff line change
@@ -763,3 +763,12 @@ pub(crate) mod sealed {
763763
) -> crate::Result<()>;
764764
}
765765
}
766+
767+
#[cfg(windows)]
768+
pub(crate) fn map_to_menu_theme(theme: tauri_utils::Theme) -> muda::MenuTheme {
769+
match theme {
770+
tauri_utils::Theme::Light => muda::MenuTheme::Light,
771+
tauri_utils::Theme::Dark => muda::MenuTheme::Dark,
772+
_ => muda::MenuTheme::Auto,
773+
}
774+
}

core/tauri/src/test/mock_runtime.rs

+4
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,10 @@ impl WindowBuilder for MockWindowBuilder {
462462
fn has_icon(&self) -> bool {
463463
false
464464
}
465+
466+
fn get_theme(&self) -> Option<Theme> {
467+
None
468+
}
465469
}
466470

467471
impl<T: UserEvent> WebviewDispatch<T> for MockWebviewDispatcher {

core/tauri/src/window/mod.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ tauri::Builder::default()
390390
#[cfg(desktop)]
391391
let handler = app_manager
392392
.menu
393-
.prepare_window_menu_creation_handler(window_menu.as_ref());
393+
.prepare_window_menu_creation_handler(window_menu.as_ref(), self.window_builder.get_theme());
394394
#[cfg(not(desktop))]
395395
#[allow(clippy::type_complexity)]
396396
let handler: Option<Box<dyn Fn(tauri_runtime::window::RawWindow<'_>) + Send>> = None;
@@ -1160,7 +1160,12 @@ tauri::Builder::default()
11601160
self.run_on_main_thread(move || {
11611161
#[cfg(windows)]
11621162
if let Ok(hwnd) = window.hwnd() {
1163-
let _ = menu_.inner().init_for_hwnd(hwnd.0);
1163+
let theme = window
1164+
.theme()
1165+
.map(crate::menu::map_to_menu_theme)
1166+
.unwrap_or(muda::MenuTheme::Auto);
1167+
1168+
let _ = menu_.inner().init_for_hwnd_with_theme(hwnd.0, theme);
11641169
}
11651170
#[cfg(any(
11661171
target_os = "linux",

examples/api/src-tauri/Cargo.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)