You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
3.8 KiB
Rust

use std::sync::Mutex;
use tauri::{
plugin::{Builder, TauriPlugin},
Manager, Runtime, api::path::config_dir,
};
use serde::{Serialize, Deserialize};
use std::fs;
fn fls() -> bool {
false
}
#[derive(Serialize, Deserialize, Clone)]
struct AppConfig {
#[serde(skip, default="fls")]
pub write_file: bool,
pub theme: ApplicationTheme,
}
#[derive(Serialize, Deserialize, Clone)]
enum ApplicationTheme {
Light,
Dark,
}
impl AppConfig {
fn default() -> Self {
AppConfig {
theme: ApplicationTheme::Light,
write_file: false,
}
}
fn try_load() -> Option<Self> {
let path = config_dir()?.join("longmont/client.json");
let read_result = fs::read_to_string(path.clone());
if let Err(error) = read_result {
println!("Unable to read file {}", path.to_str()?);
println!("{}", error);
return None
}
let parse_result = serde_json::from_str(read_result.unwrap().as_str());
if let Err(error) = parse_result {
println!("Invalid syntax in config file {}", path.to_str()?);
println!("{}", error);
return None
}
Some(parse_result.unwrap())
}
pub fn load() -> Self {
// Check for config file
let path = config_dir().expect("cannot find config dir").join("longmont/client.json");
if !path.exists() {
println!("No config file found, using defaults");
let mut config = AppConfig::default();
config.write_file = true;
return config
}
// If we can read it, we'll allow writing it back
if let Some(parsed_config) = AppConfig::try_load() {
println!("Loaded config file {}", path.to_str().unwrap());
let mut config = parsed_config.clone();
config.write_file = true;
return config
}
// Otherwise, load defaults and don't allow writing it back
println!("Error encountered, using defaults");
AppConfig::default()
}
pub fn save(&self) {
if !self.write_file {
println!("Did not initially load config from a file, not saving");
return
}
let dir_path = config_dir().expect("cannot find config dir").join("longmont");
let path = dir_path.join("client.json");
if !dir_path.exists() {
fs::create_dir(dir_path).expect("could not create config dir");
}
let config_str = serde_json::to_string(self).expect("Cannot serialize config");
if let Err(err) = fs::write(path.clone(), config_str) {
println!("Could not write config file {}", path.to_str().unwrap());
println!("{}", err);
} else {
println!("Saved config to {}", path.to_str().unwrap())
}
}
}
pub fn init<R: Runtime>() -> TauriPlugin<R> {
let config = AppConfig::load();
Builder::new("config")
.invoke_handler(tauri::generate_handler![set_application_theme, get_application_theme])
.setup(|app| {
app.manage(Mutex::new(config));
Ok(())
})
.build()
}
#[tauri::command]
fn set_application_theme<R: Runtime>(theme: &str, state_lock: tauri::State<Mutex<AppConfig>>, app_handle: tauri::AppHandle<R>) {
let mut config = state_lock.lock().unwrap();
match theme {
"light" => config.theme = ApplicationTheme::Light,
"dark" => config.theme = ApplicationTheme::Dark,
_ => {
panic!("Unknown theme type {}", theme)
}
}
config.save();
app_handle.emit_all("theme-change", config.theme.clone()).expect("Could not emit theme change event");
}
#[tauri::command]
fn get_application_theme(state_lock: tauri::State<Mutex<AppConfig>>) -> ApplicationTheme {
let config = state_lock.lock().unwrap();
config.theme.clone()
}