From 7e3981135bdc4d69aa680bc6470c9093f51587b0 Mon Sep 17 00:00:00 2001 From: wyhaya Date: Fri, 13 Sep 2024 13:15:29 +0800 Subject: [PATCH] Restore window --- README.md | 13 +++++++++--- tauri-v2/Cargo.toml | 2 ++ tauri-v2/src/lib.rs | 34 +++++++++++++++++++++++++++----- tauri-v2/src/platform/windows.rs | 2 +- 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 7e6b64d..4a54468 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/tauri-v2/Cargo.toml b/tauri-v2/Cargo.toml index d48e883..a4da883 100644 --- a/tauri-v2/Cargo.toml +++ b/tauri-v2/Cargo.toml @@ -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" @@ -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" diff --git a/tauri-v2/src/lib.rs b/tauri-v2/src/lib.rs index cf7ca49..2fdfdb0 100644 --- a/tauri-v2/src/lib.rs +++ b/tauri-v2/src/lib.rs @@ -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() -> TauriPlugin { +pub fn init(config: &mut Config) -> TauriPlugin { + #[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); } } @@ -57,6 +70,17 @@ fn get_theme(app: AppHandle) -> Result { 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(app: &AppHandle) -> Theme { let config_dir = app.path().app_config_dir().expect(ERROR_MESSAGE); let p = config_dir.join(CONFIG_FILENAME); diff --git a/tauri-v2/src/platform/windows.rs b/tauri-v2/src/platform/windows.rs index 9c2a503..3fba6e9 100644 --- a/tauri-v2/src/platform/windows.rs +++ b/tauri-v2/src/platform/windows.rs @@ -7,6 +7,7 @@ use windows_core::Interface; #[command] pub fn set_theme(app: AppHandle, theme: Theme) -> Result<(), &'static str> { + save_theme_value(&app, theme); let err = Arc::new(AtomicU8::new(0)); for (_, window) in app.webview_windows() { // Window @@ -59,7 +60,6 @@ pub fn set_theme(app: AppHandle, theme: Theme) -> Result<(), &'st _ => {} } } - save_theme_value(&app, theme); Ok(()) }