tauri_plugin_theme_v1/
lib.rs

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
108
109
110
111
112
113
114
115
116
117
118
mod platform;

use platform::cmd_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";

pub struct ThemePlugin;

impl ThemePlugin {
    #[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![cmd_get_theme, cmd_set_theme])
            .on_event(|app, e| {
                if let RunEvent::Ready = e {
                    let theme = saved_theme_value(&app.config());
                    let _ = cmd_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![cmd_get_theme, cmd_set_theme])
            .setup(|app| {
                let theme = saved_theme_value(&app.config());
                let _ = cmd_set_theme(app.clone(), theme);
                Ok(())
            })
            .build()
    }

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

#[command]
fn cmd_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());
}

pub fn set_theme<R: Runtime>(app: AppHandle<R>, theme: Theme) -> Result<(), &'static str>{
    cmd_set_theme(app, theme)
}

pub fn get_theme(config: &Config) -> Theme {
    saved_theme_value(config)
}