wyhaya / tauri-plugin-theme

Dynamically change Tauri App theme
46 stars 6 forks source link

How to get theme on tauri backend? #12

Open gusxodnjs opened 1 week ago

gusxodnjs commented 1 week ago

How to get theme on tauri backend?

My application is creating all webviewWindow on the Tauri backend. when I create webviewWindow on the backend, I want to apply the current theme value. Does this plug-in support that? I think it seems support only javascript api. Is that right?

wyhaya commented 1 week ago

Currently only the JavaScript API is exposed, the Rust API is not. But it is simple, if you want, you can get the theme by writing this code.

fn get_theme_value<R: Runtime>(app: &AppHandle<R>) -> Theme {
    let config_dir = app.path().app_config_dir().unwrap();
    let p = config_dir.join("tauri-plugin-theme");
    fs::read_to_string(p)
        .map(Theme::from)
        .unwrap_or(Theme::Auto)
}

https://github.com/wyhaya/tauri-plugin-theme/blob/df7229ed134d838cf6e64f846b2780a54b21b8db/tauri-plugin-theme/src/lib.rs#L88-L98


There are some issues with this plugin (mostly on Windows), such as setting the theme directly and locking the theme. Until these issues are resolved I would recommend not using this plugin and controlling the theme entirely with CSS and JavaScript.