From eea6c89ce603d452985581ebea3d42543142ab85 Mon Sep 17 00:00:00 2001 From: pml68 Date: Sun, 16 Feb 2025 15:32:11 +0100 Subject: feat!: add name to `Theme`, `is_dark` -> `dark` --- assets/themes/Rose Pine.toml | 33 --------------------------------- assets/themes/rose_pine.toml | 35 +++++++++++++++++++++++++++++++++++ src/config.rs | 21 +++++++-------------- src/theme.rs | 42 +++++++++++++++++++++++++++++------------- 4 files changed, 71 insertions(+), 60 deletions(-) delete mode 100644 assets/themes/Rose Pine.toml create mode 100644 assets/themes/rose_pine.toml diff --git a/assets/themes/Rose Pine.toml b/assets/themes/Rose Pine.toml deleted file mode 100644 index df7342b..0000000 --- a/assets/themes/Rose Pine.toml +++ /dev/null @@ -1,33 +0,0 @@ -is_dark = true - -[palette] -background = "#26233a" -text = "#e0def4" -primary = "#9ccfd8" -success = "#f6c177" -danger = "#eb6f92" - -[background] -base = { color = "#191724", text = "#e0def4" } -weak = { color = "#1f1d2e", text = "#e0def4" } -strong = { color = "#26233a", text = "#f4ebd3" } - -[primary] -base = { color = "#eb6f92", text = "#000000" } -weak = { color = "#f6c177", text = "#000000" } -strong = { color = "#ebbcba", text = "#000000" } - -[secondary] -base = { color = "#31748f", text = "#ffffff" } -weak = { color = "#9ccfd8", text = "#000000" } -strong = { color = "#c4a7e7", text = "#000000" } - -[success] -base = { color = "#9ccfd8", text = "#000000" } -weak = { color = "#6e6a86", text = "#ffffff" } -strong = { color = "#908caa", text = "#000000" } - -[danger] -base = { color = "#eb6f92", text = "#ffffff" } -weak = { color = "#f6c177", text = "#ffffff" } -strong = { color = "#524f67", text = "#ffffff" } diff --git a/assets/themes/rose_pine.toml b/assets/themes/rose_pine.toml new file mode 100644 index 0000000..5a89d91 --- /dev/null +++ b/assets/themes/rose_pine.toml @@ -0,0 +1,35 @@ +name = "Rosé Pine" + +dark = true + +[palette] +background = "#26233a" +text = "#e0def4" +primary = "#9ccfd8" +success = "#f6c177" +danger = "#eb6f92" + +[background] +base = { color = "#191724", text = "#e0def4" } +weak = { color = "#1f1d2e", text = "#e0def4" } +strong = { color = "#26233a", text = "#f4ebd3" } + +[primary] +base = { color = "#eb6f92", text = "#000000" } +weak = { color = "#f6c177", text = "#000000" } +strong = { color = "#ebbcba", text = "#000000" } + +[secondary] +base = { color = "#31748f", text = "#ffffff" } +weak = { color = "#9ccfd8", text = "#000000" } +strong = { color = "#c4a7e7", text = "#000000" } + +[success] +base = { color = "#9ccfd8", text = "#000000" } +weak = { color = "#6e6a86", text = "#ffffff" } +strong = { color = "#908caa", text = "#000000" } + +[danger] +base = { color = "#eb6f92", text = "#ffffff" } +weak = { color = "#f6c177", text = "#ffffff" } +strong = { color = "#524f67", text = "#ffffff" } diff --git a/src/config.rs b/src/config.rs index 9d29af7..5569aae 100644 --- a/src/config.rs +++ b/src/config.rs @@ -79,13 +79,13 @@ impl Config { let content = fs::read_to_string(entry.path()).await.ok()?; let theme: Theme = toml::from_str(content.as_ref()).ok()?; - let name = entry.path().file_stem()?.to_string_lossy().to_string(); - Some(theme.into_iced_theme(name)) + Some(iced::Theme::from(theme)) }; + let mut selected = Theme::default().into(); let mut all = iced::Theme::ALL.to_owned(); - let mut selected = iced::Theme::default(); + all.push(Theme::default().into()); if theme_index(&theme_name, iced::Theme::ALL).is_some() { selected = theme_from_str(None, &theme_name); @@ -98,18 +98,11 @@ impl Config { continue; }; - let Some(file_name) = entry.file_name().to_str().map(String::from) - else { - continue; - }; - - if let Some(file_name) = file_name.strip_suffix(".toml") { - if let Some(theme) = read_entry(entry).await { - if file_name == theme_name { - selected = theme.clone(); - } - all.push(theme); + if let Some(theme) = read_entry(entry).await { + if theme.to_string() == theme_name { + selected = theme.clone(); } + all.push(theme); } } diff --git a/src/theme.rs b/src/theme.rs index db16372..7cb51c1 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -5,6 +5,9 @@ use iced::Color; use crate::config::Config; +const DEFAULT_THEME_CONTENT: &str = + include_str!("../assets/themes/rose_pine.toml"); + pub fn theme_index(theme_name: &str, slice: &[iced::Theme]) -> Option { slice .iter() @@ -166,20 +169,41 @@ pub struct Appearance { impl Default for Appearance { fn default() -> Self { Self { - selected: iced::Theme::default(), - all: iced::Theme::ALL.into(), + selected: Theme::default().into(), + all: { + let mut themes = iced::Theme::ALL.to_owned(); + themes.push(Theme::default().into()); + themes.into() + }, } } } -#[derive(Debug, Default, serde::Deserialize)] +#[derive(Debug, serde::Deserialize)] pub struct Theme { + name: String, palette: ThemePalette, - is_dark: Option, + dark: Option, #[serde(flatten)] extended: Option, } +impl From for iced::Theme { + fn from(value: Theme) -> Self { + iced::Theme::custom_with_fn( + value.name.clone(), + value.palette.clone().into(), + |_| value.into(), + ) + } +} + +impl Default for Theme { + fn default() -> Self { + toml::from_str(DEFAULT_THEME_CONTENT).expect("parse default theme") + } +} + #[derive(Debug, Clone, serde::Deserialize)] pub struct ThemePalette { #[serde(with = "color_serde")] @@ -194,14 +218,6 @@ pub struct ThemePalette { danger: Color, } -impl Theme { - pub fn into_iced_theme(self, name: String) -> iced::Theme { - iced::Theme::custom_with_fn(name, self.palette.clone().into(), |_| { - self.into() - }) - } -} - impl Default for ThemePalette { fn default() -> Self { let palette = iced::Theme::default().palette(); @@ -231,7 +247,7 @@ impl From for Extended { fn from(theme: Theme) -> Self { let mut extended = Extended::generate(theme.palette.into()); - if let Some(is_dark) = theme.is_dark { + if let Some(is_dark) = theme.dark { extended.is_dark = is_dark; } -- cgit v1.2.3