Skip to content

Commit

Permalink
Restore window
Browse files Browse the repository at this point in the history
  • Loading branch information
wyhaya committed Sep 13, 2024
1 parent 8b8c43c commit 7e39811
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,14 @@ Add to `src-tauri/capabilities/default.json`
| ----- | ----- | ------- | ------- | ------- |
| ✅ | ✅ | ✅ | ❌ | ❌

## Note
## NOTE

- On `Windows` platform, when calling `set_theme`, the app will restart.
- On `Linux` platform, it has not been extensively tested.
### For Windows

Requires WebView2 Runtime version 101.0.1210.39(May 9, 2022) or higher; otherwise, the app will complete the theme change by `restart`.

See: [https://learn.microsoft.com/en-us/microsoft-edge/webview2/release-notes/archive?tabs=dotnetcsharp#10121039](https://learn.microsoft.com/en-us/microsoft-edge/webview2/release-notes/archive?tabs=dotnetcsharp#10121039)

### For Linux

On Linux platform, it has not been extensively tested.
2 changes: 2 additions & 0 deletions tauri-v2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ serde = { version = "1.0", features = ["derive"] }

[target."cfg(target_os = \"macos\")".dependencies]
cocoa = "0.26"
dirs-next = "2.0"

[target."cfg(target_os = \"linux\")".dependencies]
gtk = "0.18"
Expand All @@ -26,6 +27,7 @@ futures-lite = "2.3"
tokio = { version = "1", features = ["macros"] }

[target."cfg(target_os = \"windows\")".dependencies]
dirs-next = "2.0"
webview2-com = "0.33"
windows = "0.58"
windows-core = "0.58"
Expand Down
34 changes: 29 additions & 5 deletions tauri-v2/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
#![allow(unused_variables)]

mod platform;

use platform::set_theme;
use serde::{Deserialize, Serialize};
use std::fs;
use tauri::plugin::{Builder, TauriPlugin};
use tauri::{command, generate_handler, AppHandle, Manager, RunEvent, Runtime};
use tauri::{command, generate_handler, AppHandle, Config, Manager, Runtime};

const CONFIG_FILENAME: &str = "tauri-plugin-theme";
const ERROR_MESSAGE: &str = "Get app config dir failed";

pub fn init<R: Runtime>() -> TauriPlugin<R> {
pub fn init<R: Runtime>(config: &mut Config) -> TauriPlugin<R> {
#[cfg(any(target_os = "macos", target_os = "windows"))]
{
let theme = saved_theme_value_from_config(&config);
for window in &mut config.app.windows {
match theme {
Theme::Auto => window.theme = None,
Theme::Light => window.theme = Some(tauri::Theme::Light),
Theme::Dark => window.theme = Some(tauri::Theme::Dark),
}
}
}
Builder::new("theme")
.invoke_handler(generate_handler![get_theme, set_theme])
.on_event(|app, e| {
if let RunEvent::Ready = e {
let theme = saved_theme_value(&app);
if let Err(err) = set_theme(app.clone(), theme) {
#[cfg(target_os = "linux")]
if let tauri::RunEvent::Ready = e {
if let Err(err) = set_theme(app.clone(), saved_theme_value(&app)) {
eprintln!("Failed to set theme: {}", err);
}
}
Expand Down Expand Up @@ -57,6 +70,17 @@ fn get_theme<R: Runtime>(app: AppHandle<R>) -> Result<Theme, ()> {
Ok(theme)
}

#[cfg(any(target_os = "macos", target_os = "windows"))]
fn saved_theme_value_from_config(config: &Config) -> Theme {
if let Some(dir) = dirs_next::config_dir() {
let p = dir.join(&config.identifier).join(CONFIG_FILENAME);
return fs::read_to_string(p)
.map(Theme::from)
.unwrap_or(Theme::Auto);
}
Theme::Auto
}

fn saved_theme_value<R: Runtime>(app: &AppHandle<R>) -> Theme {
let config_dir = app.path().app_config_dir().expect(ERROR_MESSAGE);
let p = config_dir.join(CONFIG_FILENAME);
Expand Down
2 changes: 1 addition & 1 deletion tauri-v2/src/platform/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use windows_core::Interface;

#[command]
pub fn set_theme<R: Runtime>(app: AppHandle<R>, theme: Theme) -> Result<(), &'static str> {
save_theme_value(&app, theme);
let err = Arc::new(AtomicU8::new(0));
for (_, window) in app.webview_windows() {
// Window
Expand Down Expand Up @@ -59,7 +60,6 @@ pub fn set_theme<R: Runtime>(app: AppHandle<R>, theme: Theme) -> Result<(), &'st
_ => {}
}
}
save_theme_value(&app, theme);
Ok(())
}

Expand Down

0 comments on commit 7e39811

Please sign in to comment.