From 0dad6dd5b8395d3089bed022a4b8830f7cae7d9f Mon Sep 17 00:00:00 2001 From: pml68 Date: Mon, 30 Dec 2024 02:15:10 +0100 Subject: feat: add config loading, with theming support, limited to Palette for now --- iced_builder/src/types/project.rs | 53 +++++++++++------------------- iced_builder/src/types/rendered_element.rs | 20 +++++------ 2 files changed, 30 insertions(+), 43 deletions(-) (limited to 'iced_builder/src/types') diff --git a/iced_builder/src/types/project.rs b/iced_builder/src/types/project.rs index f4dbcc4..6f3b7ed 100644 --- a/iced_builder/src/types/project.rs +++ b/iced_builder/src/types/project.rs @@ -5,6 +5,7 @@ use rust_format::{Config, Edition, Formatter, RustFmt}; use serde::{Deserialize, Serialize}; use super::rendered_element::RenderedElement; +use crate::theme::theme_from_str; use crate::{Error, Result}; #[derive(Debug, Clone, Serialize, Deserialize)] @@ -14,6 +15,12 @@ pub struct Project { pub element_tree: Option, } +impl Default for Project { + fn default() -> Self { + Self::new() + } +} + impl Project { pub fn new() -> Self { Self { @@ -23,38 +30,21 @@ impl Project { } } - pub fn get_theme(&self) -> Theme { + pub fn get_theme(&self, config: &crate::config::Config) -> Theme { match &self.theme { - Some(theme) => match theme.as_str() { - "Light" => Theme::Light, - "Dark" => Theme::Dark, - "Dracula" => Theme::Dracula, - "Nord" => Theme::Nord, - "Solarized Light" => Theme::SolarizedLight, - "Solarized Dark" => Theme::SolarizedDark, - "Gruvbox Light" => Theme::GruvboxLight, - "Gruvbox Dark" => Theme::GruvboxDark, - "Catppuccin Latte" => Theme::CatppuccinLatte, - "Catppuccin Frappé" => Theme::CatppuccinFrappe, - "Catppuccin Macchiato" => Theme::CatppuccinMacchiato, - "Catppuccin Mocha" => Theme::CatppuccinMocha, - "Tokyo Night" => Theme::TokyoNight, - "Tokyo Night Storm" => Theme::TokyoNightStorm, - "Tokyo Night Light" => Theme::TokyoNightLight, - "Kanagawa Wave" => Theme::KanagawaWave, - "Kanagawa Dragon" => Theme::KanagawaDragon, - "Kanagawa Lotus" => Theme::KanagawaLotus, - "Moonfly" => Theme::Moonfly, - "Nightfly" => Theme::Nightfly, - "Oxocarbon" => Theme::Oxocarbon, - "Ferra" => Theme::Ferra, - _ => Theme::Dark, - }, + Some(theme) => theme_from_str(Some(config), theme), None => Theme::Dark, } } - pub async fn from_path() -> Result<(PathBuf, Self)> { + pub async fn from_path(path: PathBuf) -> Result<(PathBuf, Self)> { + let contents = tokio::fs::read_to_string(&path).await?; + let element: Self = serde_json::from_str(&contents)?; + + Ok((path, element)) + } + + pub async fn from_file() -> Result<(PathBuf, Self)> { let picked_file = rfd::AsyncFileDialog::new() .set_title("Open a JSON file...") .add_filter("*.json, *.JSON", &["json", "JSON"]) @@ -64,10 +54,7 @@ impl Project { let path = picked_file.path().to_owned(); - let contents = tokio::fs::read_to_string(&path).await?; - let element: Self = serde_json::from_str(&contents)?; - - Ok((path, element)) + Self::from_path(path).await } pub async fn write_to_file(self, path: Option) -> Result { @@ -91,7 +78,7 @@ impl Project { Ok(path) } - pub fn app_code(&self) -> Result { + pub fn app_code(&self, config: &crate::config::Config) -> Result { match self.element_tree { Some(ref element_tree) => { let (imports, view) = element_tree.codegen(); @@ -127,7 +114,7 @@ impl Project { Some(ref t) => t, None => "New app", }, - self.get_theme().to_string().replace(" ", "") + self.get_theme(config).to_string().replace(" ", "") ); let config = Config::new_str() .edition(Edition::Rust2021) diff --git a/iced_builder/src/types/rendered_element.rs b/iced_builder/src/types/rendered_element.rs index ccc8668..5270e5a 100755 --- a/iced_builder/src/types/rendered_element.rs +++ b/iced_builder/src/types/rendered_element.rs @@ -43,7 +43,7 @@ impl RenderedElement { pub fn find_by_id(&mut self, id: Id) -> Option<&mut Self> { if self.get_id() == id.clone() { - return Some(self); + Some(self) } else if let Some(child_elements) = self.child_elements.as_mut() { for element in child_elements { let element = element.find_by_id(id.clone()); @@ -51,9 +51,9 @@ impl RenderedElement { return element; } } - return None; + None } else { - return None; + None } } @@ -62,12 +62,12 @@ impl RenderedElement { child_element: &RenderedElement, ) -> Option<&mut Self> { if child_element == self { - return Some(self); + Some(self) } else if self.child_elements.is_some() { if self .child_elements .clone() - .unwrap_or(vec![]) + .unwrap_or_default() .contains(child_element) { return Some(self); @@ -160,7 +160,7 @@ impl RenderedElement { } } - fn preset_options<'a>(mut self, options: &[&'a str]) -> Self { + fn preset_options(mut self, options: &[&str]) -> Self { for opt in options { let _ = self.options.insert(opt.to_string(), None); } @@ -410,10 +410,10 @@ impl Action { .find_by_id(id.clone()) .unwrap(); - // Element IS a parent but ISN'T a non-empty container - match element.is_parent() - && !(element.name == ElementName::Container - && !element.is_empty()) + // Element is a parent and isn't a non-empty container + match (element.is_empty() + || !(element.name == ElementName::Container)) + && element.is_parent() { true => { action = Self::PushFront(id); -- cgit v1.2.3