Skip to content

Commit

Permalink
Switch menu theme on set app theme
Browse files Browse the repository at this point in the history
  • Loading branch information
Legend-Master committed Sep 20, 2024
1 parent 2faf4ba commit f6a9717
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
14 changes: 5 additions & 9 deletions crates/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2562,15 +2562,11 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
}

fn set_theme(&self, theme: Option<Theme>) {
self
.context
.main_thread
.window_target
.set_theme(match theme {
Some(Theme::Light) => Some(TaoTheme::Light),
Some(Theme::Dark) => Some(TaoTheme::Dark),
_ => None,
});
self.event_loop.set_theme(match theme {
Some(Theme::Light) => Some(TaoTheme::Light),
Some(Theme::Dark) => Some(TaoTheme::Dark),
_ => None,
});
}

#[cfg(target_os = "macos")]
Expand Down
15 changes: 15 additions & 0 deletions crates/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,21 @@ macro_rules! shared_app_impl {

/// Set the app theme.
pub fn set_theme(&self, theme: Option<Theme>) {
#[cfg(windows)]
for window in self.manager.windows().values() {
if let (Some(window_menu), Ok(hwnd)) = (&*window.menu_lock(), window.hwnd()) {
let raw_hwnd = hwnd.0 as isize;
let menu = window_menu.menu.clone();
let _ = self.run_on_main_thread(move || {
let _ = menu.inner().set_theme_for_hwnd(
raw_hwnd,
theme
.map(crate::menu::map_to_menu_theme)
.unwrap_or(muda::MenuTheme::Auto),
);
});
};
}
match self.runtime() {
RuntimeOrDispatch::Runtime(h) => h.set_theme(theme),
RuntimeOrDispatch::RuntimeHandle(h) => h.set_theme(theme),
Expand Down
2 changes: 1 addition & 1 deletion crates/tauri/src/app/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn default_window_icon<R: Runtime>(
}

#[command(root = "crate")]
pub fn set_app_theme<R: Runtime>(app: AppHandle<R>, theme: Option<Theme>) {
pub async fn set_app_theme<R: Runtime>(app: AppHandle<R>, theme: Option<Theme>) {
app.set_theme(theme);
}

Expand Down
20 changes: 19 additions & 1 deletion examples/api/src/views/App.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<script>
import { show, hide } from '@tauri-apps/api/app'
import { show, hide, setTheme } from '@tauri-apps/api/app'
export let onMessage
/** @type {import('@tauri-apps/api/window').Theme | undefined} */
let theme
function showApp() {
hideApp()
Expand All @@ -20,6 +22,21 @@
.then(() => onMessage('Hide app'))
.catch(onMessage)
}
async function switchTheme() {
switch (theme) {
case 'dark':
theme = 'light'
break
case 'light':
theme = undefined
break
case undefined:
theme = 'dark'
break
}
setTheme(theme)
}
</script>

<div>
Expand All @@ -30,4 +47,5 @@
on:click={showApp}>Show</button
>
<button class="btn" id="hide" on:click={hideApp}>Hide</button>
<button class="btn" id="hide" on:click={switchTheme}>Switch Theme</button>
</div>

0 comments on commit f6a9717

Please sign in to comment.