Skip to content

Commit

Permalink
fix(Windows): sync menubar theme with the window theme (#9832)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
amrbashir and lucasfernog committed May 24, 2024
1 parent 9ac9303 commit 276c4b1
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .changes/core-windows-menubar-theme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri": "patch:bug"
---

On Windows, fix wrong menubar theme when window is using an explicit theme.

6 changes: 6 additions & 0 deletions .changes/runtime-window-builder-get-theme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri-runtime": patch:enhance
"tauri-runtime-wry": patch:enhance
---

Added `WindowBuilder::get_theme`.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions core/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,13 @@ impl WindowBuilder for WindowBuilderWrapper {
fn has_icon(&self) -> bool {
self.inner.window.window_icon.is_some()
}

fn get_theme(&self) -> Option<Theme> {
self.inner.window.preferred_theme.map(|theme| match theme {
TaoTheme::Dark => Theme::Dark,
_ => Theme::Light,
})
}
}

#[cfg(any(
Expand Down
2 changes: 2 additions & 0 deletions core/tauri-runtime/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,8 @@ pub trait WindowBuilder: WindowBuilderBase {

/// Whether the icon was set or not.
fn has_icon(&self) -> bool;

fn get_theme(&self) -> Option<Theme>;
}

/// A window that has yet to be built.
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ dunce = "1"
specta = { version = "^2.0.0-rc.9", optional = true, default-features = false, features = [ "function" ] }

[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]
muda = { version = "0.13", default-features = false, features = [ "serde" ] }
muda = { version = "0.13.4", default-features = false, features = [ "serde" ] }
tray-icon = { version = "0.13", default-features = false, features = [ "serde" ], optional = true }

[target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies]
Expand Down
8 changes: 7 additions & 1 deletion core/tauri/src/manager/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ impl<R: Runtime> MenuManager<R> {
pub(crate) fn prepare_window_menu_creation_handler(
&self,
window_menu: Option<&crate::window::WindowMenu<R>>,
#[allow(unused)] theme: Option<tauri_utils::Theme>,
) -> Option<impl Fn(tauri_runtime::window::RawWindow<'_>)> {
{
if let Some(menu) = window_menu {
Expand All @@ -71,7 +72,12 @@ impl<R: Runtime> MenuManager<R> {
let menu = menu.menu.clone();
Some(move |raw: tauri_runtime::window::RawWindow<'_>| {
#[cfg(target_os = "windows")]
let _ = menu.inner().init_for_hwnd(raw.hwnd as _);
{
let theme = theme
.map(crate::menu::map_to_menu_theme)
.unwrap_or(muda::MenuTheme::Auto);
let _ = menu.inner().init_for_hwnd_with_theme(raw.hwnd as _, theme);
}
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
Expand Down
9 changes: 9 additions & 0 deletions core/tauri/src/menu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -763,3 +763,12 @@ pub(crate) mod sealed {
) -> crate::Result<()>;
}
}

#[cfg(windows)]
pub(crate) fn map_to_menu_theme(theme: tauri_utils::Theme) -> muda::MenuTheme {
match theme {
tauri_utils::Theme::Light => muda::MenuTheme::Light,
tauri_utils::Theme::Dark => muda::MenuTheme::Dark,
_ => muda::MenuTheme::Auto,
}
}
4 changes: 4 additions & 0 deletions core/tauri/src/test/mock_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,10 @@ impl WindowBuilder for MockWindowBuilder {
fn has_icon(&self) -> bool {
false
}

fn get_theme(&self) -> Option<Theme> {
None
}
}

impl<T: UserEvent> WebviewDispatch<T> for MockWebviewDispatcher {
Expand Down
9 changes: 7 additions & 2 deletions core/tauri/src/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ tauri::Builder::default()
#[cfg(desktop)]
let handler = app_manager
.menu
.prepare_window_menu_creation_handler(window_menu.as_ref());
.prepare_window_menu_creation_handler(window_menu.as_ref(), self.window_builder.get_theme());
#[cfg(not(desktop))]
#[allow(clippy::type_complexity)]
let handler: Option<Box<dyn Fn(tauri_runtime::window::RawWindow<'_>) + Send>> = None;
Expand Down Expand Up @@ -1160,7 +1160,12 @@ tauri::Builder::default()
self.run_on_main_thread(move || {
#[cfg(windows)]
if let Ok(hwnd) = window.hwnd() {
let _ = menu_.inner().init_for_hwnd(hwnd.0);
let theme = window
.theme()
.map(crate::menu::map_to_menu_theme)
.unwrap_or(muda::MenuTheme::Auto);

let _ = menu_.inner().init_for_hwnd_with_theme(hwnd.0, theme);
}
#[cfg(any(
target_os = "linux",
Expand Down
4 changes: 2 additions & 2 deletions examples/api/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 276c4b1

Please sign in to comment.