summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpml68 <contact@pml68.dev>2025-02-16 15:32:11 +0100
committerpml68 <contact@pml68.dev>2025-02-16 15:32:11 +0100
commiteea6c89ce603d452985581ebea3d42543142ab85 (patch)
tree3c71bd07cf2b99a5542ff19ce3207791a27810da
parentrefactor: random stuff (diff)
downloadiced-builder-eea6c89ce603d452985581ebea3d42543142ab85.tar.gz
feat!: add name to `Theme`, `is_dark` -> `dark`
-rw-r--r--assets/themes/rose_pine.toml (renamed from assets/themes/Rose Pine.toml)4
-rw-r--r--src/config.rs21
-rw-r--r--src/theme.rs42
3 files changed, 39 insertions, 28 deletions
diff --git a/assets/themes/Rose Pine.toml b/assets/themes/rose_pine.toml
index df7342b..5a89d91 100644
--- a/assets/themes/Rose Pine.toml
+++ b/assets/themes/rose_pine.toml
@@ -1,4 +1,6 @@
-is_dark = true
+name = "Rosé Pine"
+
+dark = true
[palette]
background = "#26233a"
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<usize> {
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<bool>,
+ dark: Option<bool>,
#[serde(flatten)]
extended: Option<ExtendedThemePalette>,
}
+impl From<Theme> 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<Theme> 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;
}