1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
mod platform;

use platform::set_theme;
use serde::{Deserialize, Serialize};
use std::fs;
use tauri::api::path::app_config_dir;
use tauri::plugin::{Builder, TauriPlugin};
use tauri::utils::config::TauriConfig;
use tauri::{command, generate_handler, Config};
use tauri::{AppHandle, Runtime};

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

#[cfg(target_os = "macos")]
pub fn init<R: Runtime>(_config: &mut Config) -> TauriPlugin<R, TauriConfig> {
    use tauri::RunEvent;
    Builder::new(PLUGIN_NAME)
        .invoke_handler(generate_handler![get_theme, set_theme])
        .on_event(|app, e| {
            if let RunEvent::Ready = e {
                let theme = saved_theme_value(&app.config());
                let _ = set_theme(app.clone(), theme);
            }
        })
        .build()
}

#[cfg(target_os = "linux")]
pub fn init<R: Runtime>(_config: &mut Config) -> TauriPlugin<R, TauriConfig> {
    Builder::new(PLUGIN_NAME)
        .invoke_handler(generate_handler![get_theme, set_theme])
        .setup(|app| {
            let theme = saved_theme_value(&app.config());
            let _ = set_theme(app.clone(), theme);
            Ok(())
        })
        .build()
}

#[cfg(target_os = "windows")]
pub fn init<R: Runtime>(config: &mut Config) -> TauriPlugin<R, TauriConfig> {
    let theme = saved_theme_value(config);
    for window in &mut config.tauri.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(PLUGIN_NAME)
        .invoke_handler(generate_handler![get_theme, set_theme])
        .build()
}

#[command]
fn get_theme<R: Runtime>(app: AppHandle<R>) -> Result<Theme, ()> {
    let theme = saved_theme_value(&app.config());
    Ok(theme)
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Theme {
    Auto,
    Light,
    Dark,
}

impl From<String> for Theme {
    fn from(value: String) -> Self {
        match value.as_str() {
            "light" => Theme::Light,
            "dark" => Theme::Dark,
            _ => Theme::Auto,
        }
    }
}

impl ToString for Theme {
    fn to_string(&self) -> String {
        match self {
            Theme::Auto => "auto".into(),
            Theme::Light => "light".into(),
            Theme::Dark => "dark".into(),
        }
    }
}

pub(crate) fn saved_theme_value(config: &Config) -> Theme {
    let p = app_config_dir(config)
        .expect(ERROR_MESSAGE)
        .join(CONFIG_FILENAME);
    fs::read_to_string(p)
        .map(Theme::from)
        .unwrap_or(Theme::Auto)
}

pub(crate) fn save_theme_value(config: &Config, theme: Theme) {
    let dir = app_config_dir(config).expect(ERROR_MESSAGE);
    if !dir.exists() {
        let _ = std::fs::create_dir_all(&dir);
    }
    let p = dir.join(CONFIG_FILENAME);
    let _ = fs::write(p, theme.to_string());
}