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

feat(core): add a new function to set theme dynamically #10210

Merged
merged 31 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d1117c4
Add set_theme
Legend-Master Jul 6, 2024
098e170
Merge branch 'dev' into set-theme
Legend-Master Jul 6, 2024
45c2562
Build
Legend-Master Jul 6, 2024
c0b62c3
Merge branch 'dev' into set-theme
Legend-Master Aug 8, 2024
4771234
Merge remote-tracking branch 'upstream/dev' into set-theme
Legend-Master Aug 8, 2024
0452a46
Merge branch 'dev' into set-theme
Legend-Master Aug 17, 2024
a18c4ea
Fix merge conflict
Legend-Master Aug 17, 2024
81a3666
Merge branch 'dev' into set-theme
Legend-Master Aug 21, 2024
a2f1796
Merge branch 'dev' into set-theme
Legend-Master Aug 26, 2024
427e436
Merge branch 'dev' into set-theme
Legend-Master Aug 26, 2024
ba45eef
Merge branch 'dev' into set-theme
Legend-Master Aug 28, 2024
b8b8db4
Merge branch 'set-theme' of https://github.com/Legend-Master/tauri in…
Legend-Master Aug 28, 2024
8e98855
Migrate bundle.global.js
Legend-Master Aug 28, 2024
3669e8f
Merge branch 'dev' into set-theme
Legend-Master Sep 19, 2024
eff0596
Bump tao
Legend-Master Sep 19, 2024
6ae1c56
setTheme docs
Legend-Master Sep 19, 2024
89a3688
Add set theme to app
Legend-Master Sep 19, 2024
5518bf4
Merge branch 'dev' into set-theme
Legend-Master Sep 19, 2024
74af322
duplicated hide
Legend-Master Sep 19, 2024
ec7f5a8
Add app theme js api
Legend-Master Sep 20, 2024
6e46794
Add basic example
Legend-Master Sep 20, 2024
2faf4ba
comments
Legend-Master Sep 20, 2024
f6a9717
Switch menu theme on set app theme
Legend-Master Sep 20, 2024
649720a
Merge branch 'dev' into set-theme
Legend-Master Sep 21, 2024
ea7eb63
Merge remote-tracking branch 'upstream/dev' into set-theme
Legend-Master Sep 24, 2024
fe03a68
Add unsafe block
Legend-Master Sep 24, 2024
3c95b91
Update tao to 0.30.2
Legend-Master Sep 24, 2024
1b78a07
Apply suggestions from code review
Legend-Master Sep 24, 2024
ff9a460
Add platform specific note to set_app_theme
Legend-Master Sep 24, 2024
404fa9a
Clone instead of lock
Legend-Master Sep 24, 2024
bb57d27
Add indicator for `auto` theme
amrbashir Sep 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/tauri-runtime-wry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ wry = { version = "0.43.1", default-features = false, features = [
"os-webview",
"linux-body",
] }
tao = { version = "0.30", default-features = false, features = ["rwh_06"] }
tao = { version = "0.30.1", default-features = false, features = ["rwh_06"] }
tauri-runtime = { version = "2.0.0-rc.12", path = "../tauri-runtime" }
tauri-utils = { version = "2.0.0-rc.12", path = "../tauri-utils" }
raw-window-handle = "0.6"
Expand Down
15 changes: 15 additions & 0 deletions crates/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,7 @@ pub enum WindowMessage {
SetIgnoreCursorEvents(bool),
SetProgressBar(ProgressBarState),
SetTitleBarStyle(tauri_utils::TitleBarStyle),
SetTheme(Option<Theme>),
DragWindow,
ResizeDragWindow(tauri_runtime::ResizeDirection),
RequestRedraw,
Expand Down Expand Up @@ -2010,6 +2011,13 @@ impl<T: UserEvent> WindowDispatch<T> for WryWindowDispatcher<T> {
Message::Window(self.window_id, WindowMessage::SetTitleBarStyle(style)),
)
}

fn set_theme(&self, theme: Option<Theme>) -> Result<()> {
send_user_message(
&self.context,
Message::Window(self.window_id, WindowMessage::SetTheme(theme)),
)
}
}

#[derive(Clone)]
Expand Down Expand Up @@ -2973,6 +2981,13 @@ fn handle_user_message<T: UserEvent>(
}
};
}
WindowMessage::SetTheme(theme) => {
window.set_theme(match theme {
amrbashir marked this conversation as resolved.
Show resolved Hide resolved
Some(Theme::Light) => Some(TaoTheme::Light),
Some(Theme::Dark) => Some(TaoTheme::Dark),
_ => None,
});
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions crates/tauri-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,4 +799,7 @@ pub trait WindowDispatch<T: UserEvent>: Debug + Clone + Send + Sync + Sized + 's
///
/// - **Linux / Windows / iOS / Android:** Unsupported.
fn set_title_bar_style(&self, style: tauri_utils::TitleBarStyle) -> Result<()>;

/// Set the window theme.
fn set_theme(&self, theme: Option<Theme>) -> Result<()>;
}
1 change: 1 addition & 0 deletions crates/tauri/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ const PLUGINS: &[(&str, &[(&str, bool)])] = &[
("set_progress_bar", false),
("set_icon", false),
("set_title_bar_style", false),
("set_theme", false),
("toggle_maximize", false),
// internal
("internal_toggle_maximize", true),
Expand Down
26 changes: 26 additions & 0 deletions crates/tauri/permissions/window/autogenerated/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,32 @@ Denies the set_skip_taskbar command without any pre-configured scope.
<tr>
<td>

`core:window:allow-set-theme`

</td>
<td>

Enables the set_theme command without any pre-configured scope.

</td>
</tr>

<tr>
<td>

`core:window:deny-set-theme`

</td>
<td>

Denies the set_theme command without any pre-configured scope.

</td>
</tr>

<tr>
<td>

`core:window:allow-set-title`

</td>
Expand Down
2 changes: 1 addition & 1 deletion crates/tauri/scripts/bundle.global.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions crates/tauri/src/test/mock_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,10 @@ impl<T: UserEvent> WindowDispatch<T> for MockWindowDispatcher {
) -> Result<()> {
Ok(())
}

fn set_theme(&self, theme: Option<Theme>) -> Result<()> {
Ok(())
}
}

#[derive(Debug, Clone)]
Expand Down
10 changes: 9 additions & 1 deletion crates/tauri/src/webview/webview_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ use crate::{
},
};
use serde::Serialize;
use tauri_utils::config::{WebviewUrl, WindowConfig};
use tauri_utils::{
config::{WebviewUrl, WindowConfig},
Theme,
};
use url::Url;

use crate::{
Expand Down Expand Up @@ -1570,6 +1573,11 @@ impl<R: Runtime> WebviewWindow<R> {
pub fn set_title_bar_style(&self, style: tauri_utils::TitleBarStyle) -> crate::Result<()> {
self.webview.window().set_title_bar_style(style)
}

/// Set the window theme.
pub fn set_theme(&self, theme: Option<Theme>) -> crate::Result<()> {
self.webview.window().set_theme(theme)
}
}

/// Desktop webview setters and actions.
Expand Down
6 changes: 6 additions & 0 deletions crates/tauri/src/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1981,6 +1981,7 @@ tauri::Builder::default()
})
.map_err(Into::into)
}

/// Sets the title bar style. **macOS only**.
pub fn set_title_bar_style(&self, style: tauri_utils::TitleBarStyle) -> crate::Result<()> {
self
Expand All @@ -1989,6 +1990,11 @@ tauri::Builder::default()
.set_title_bar_style(style)
.map_err(Into::into)
}

/// Set the window theme.
pub fn set_theme(&self, theme: Option<Theme>) -> crate::Result<()> {
self.window.dispatcher.set_theme(theme).map_err(Into::into)
amrbashir marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// Progress bar state.
Expand Down
2 changes: 2 additions & 0 deletions crates/tauri/src/window/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ mod desktop_commands {
setter!(set_visible_on_all_workspaces, bool);
setter!(set_title_bar_style, TitleBarStyle);
setter!(set_size_constraints, WindowSizeConstraints);
setter!(set_theme, Option<Theme>);

#[command(root = "crate")]
pub async fn set_icon<R: Runtime>(
Expand Down Expand Up @@ -287,6 +288,7 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
desktop_commands::set_icon,
desktop_commands::set_visible_on_all_workspaces,
desktop_commands::set_title_bar_style,
desktop_commands::set_theme,
desktop_commands::toggle_maximize,
desktop_commands::internal_toggle_maximize,
]);
Expand Down
10 changes: 10 additions & 0 deletions packages/api/src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1676,6 +1676,16 @@ class Window {
})
}

/**
* Set window theme, pass in undefined or no parameters to follow system theme
Legend-Master marked this conversation as resolved.
Show resolved Hide resolved
*/
async setTheme(theme?: Theme): Promise<void> {
return invoke('plugin:window|set_theme', {
label: this.label,
value: theme
})
}

// Listeners

/**
Expand Down
Loading