Settings, took care of warnings
This commit is contained in:
49
xmpd-settings/src/lib.rs
Normal file
49
xmpd-settings/src/lib.rs
Normal file
@@ -0,0 +1,49 @@
|
||||
use std::{path::PathBuf, sync::{Arc, Mutex, MutexGuard}};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use theme::Theme;
|
||||
|
||||
mod theme;
|
||||
|
||||
lazy_static::lazy_static!(
|
||||
static ref SETTINGS: Arc<Mutex<Settings>> = Arc::new(Mutex::new(Settings::default()));
|
||||
);
|
||||
|
||||
pub type Result<T> = anyhow::Result<T>;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||
pub struct Settings {
|
||||
#[serde(skip)]
|
||||
settings_path: PathBuf,
|
||||
pub theme: Theme,
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
pub fn get() -> crate::Result<MutexGuard<'static, Self>> {
|
||||
match SETTINGS.lock() {
|
||||
Ok(l) => Ok(l),
|
||||
Err(e) => Err(anyhow::anyhow!(format!("{e:?}"))),
|
||||
}
|
||||
}
|
||||
pub fn load(&mut self, path: Option<PathBuf>) -> Result<()> {
|
||||
let path = path.unwrap_or(self.settings_path.clone());
|
||||
if !path.exists() {
|
||||
std::fs::write(&path, "[theme]")?;
|
||||
self.save(Some(path.clone()))?;
|
||||
}
|
||||
let data = std::fs::read_to_string(&path)?;
|
||||
let data: Self = toml::from_str(&data)?;
|
||||
*self = data;
|
||||
self.settings_path = path;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn save(&mut self, path: Option<PathBuf>) -> Result<()> {
|
||||
let path = path.unwrap_or(self.settings_path.clone());
|
||||
let data = toml::to_string_pretty(&self)?;
|
||||
std::fs::write(&path, data)?;
|
||||
self.settings_path = path;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
47
xmpd-settings/src/theme.rs
Normal file
47
xmpd-settings/src/theme.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Theme {
|
||||
#[serde(default="Theme::default_accent_color")]
|
||||
pub accent_color: egui::Color32,
|
||||
#[serde(default="Theme::default_primary_bg_color")]
|
||||
pub primary_bg_color: egui::Color32,
|
||||
#[serde(default="Theme::default_secondary_bg_color")]
|
||||
pub secondary_bg_color: egui::Color32,
|
||||
#[serde(default="Theme::default_text_color")]
|
||||
pub text_color: egui::Color32,
|
||||
#[serde(default="Theme::default_dim_text_color")]
|
||||
pub dim_text_color: egui::Color32,
|
||||
}
|
||||
|
||||
impl Default for Theme {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
accent_color: Self::default_accent_color(),
|
||||
primary_bg_color: Self::default_primary_bg_color(),
|
||||
secondary_bg_color: Self::default_secondary_bg_color(),
|
||||
text_color: Self::default_text_color(),
|
||||
dim_text_color: Self::default_dim_text_color(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Theme {
|
||||
fn default_accent_color() -> egui::Color32 {
|
||||
egui::Color32::from_rgb(5, 102, 146) // #0566F6
|
||||
}
|
||||
fn default_primary_bg_color() -> egui::Color32 {
|
||||
egui::Color32::from_rgb(31, 34, 40) // #1F2228
|
||||
}
|
||||
fn default_secondary_bg_color() -> egui::Color32 {
|
||||
egui::Color32::from_rgb(47, 53, 61) // #2f353d
|
||||
}
|
||||
fn default_text_color() -> egui::Color32 {
|
||||
egui::Color32::from_rgb(223, 223, 223) // #dfdfdf
|
||||
}
|
||||
fn default_dim_text_color() -> egui::Color32 {
|
||||
egui::Color32::from_rgb(175, 175, 175 ) // #afafaf
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user