diff options
| author | Polesznyák Márk László <116908301+pml68@users.noreply.github.com> | 2025-03-23 02:49:57 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-03-23 02:49:57 +0100 |
| commit | 3076889c00116b22f022792471253e7188c6e93e (patch) | |
| tree | bff0cda7a9152e9f94d3176bbf5acaf879394f5f /src/types | |
| parent | feat: update to Rust 2024 (diff) | |
| parent | feat: finish `ApplyOptions` impls (diff) | |
| download | iced-builder-3076889c00116b22f022792471253e7188c6e93e.tar.gz | |
Merge pull request #7 from pml68/feat/options-backend
Options backend done (for now)
Diffstat (limited to 'src/types')
| -rw-r--r-- | src/types/project.rs | 7 | ||||
| -rwxr-xr-x | src/types/rendered_element.rs | 105 |
2 files changed, 84 insertions, 28 deletions
diff --git a/src/types/project.rs b/src/types/project.rs index e665d5e..721783e 100644 --- a/src/types/project.rs +++ b/src/types/project.rs @@ -1,4 +1,5 @@ use std::path::{Path, PathBuf}; +use std::sync::Arc; extern crate fxhash; use fxhash::FxHashMap; @@ -58,7 +59,7 @@ impl Project { pub async fn from_path( path: PathBuf, - config: Config, + config: Arc<Config>, ) -> Result<(PathBuf, Self), Error> { let contents = tokio::fs::read_to_string(&path).await?; let mut project: Self = serde_json::from_str(&contents)?; @@ -68,7 +69,9 @@ impl Project { Ok((path, project)) } - pub async fn from_file(config: Config) -> Result<(PathBuf, Self), Error> { + pub async fn from_file( + config: Arc<Config>, + ) -> Result<(PathBuf, Self), Error> { let picked_file = rfd::AsyncFileDialog::new() .set_title("Open a JSON file...") .add_filter("*.json, *.JSON", &["json", "JSON"]) diff --git a/src/types/rendered_element.rs b/src/types/rendered_element.rs index e321449..ee3def8 100755 --- a/src/types/rendered_element.rs +++ b/src/types/rendered_element.rs @@ -4,9 +4,9 @@ use iced::advanced::widget::Id; use iced::{Element, Length, widget}; use serde::{Deserialize, Serialize}; -use super::ElementName; use crate::Error; -use crate::types::Message; +use crate::options::ApplyOptions; +use crate::types::{ElementName, Message}; #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct RenderedElement { @@ -36,12 +36,12 @@ impl RenderedElement { } } - pub fn get_id(&self) -> &Id { + pub fn id(&self) -> &Id { &self.id } pub fn find_by_id(&mut self, id: &Id) -> Option<&mut Self> { - if self.get_id() == id { + if self.id() == id { Some(self) } else if let Some(child_elements) = self.child_elements.as_mut() { for element in child_elements { @@ -111,7 +111,7 @@ impl RenderedElement { pub fn insert_after(&mut self, id: &Id, element: &RenderedElement) { if let Some(child_elements) = self.child_elements.as_mut() { if let Some(index) = - child_elements.iter().position(|x| x.get_id() == id) + child_elements.iter().position(|x| x.id() == id) { child_elements.insert(index + 1, element.clone()); } else { @@ -158,25 +158,25 @@ impl RenderedElement { fn preset_options(mut self, options: &[&str]) -> Self { for opt in options { - let _ = self.options.insert(opt.to_string(), None); + let _ = self.options.insert((*opt).to_string(), None); } self } - pub fn option<'a>(mut self, option: &'a str, value: &'a str) -> Self { + pub fn option(mut self, option: String, value: String) -> Self { let _ = self .options - .entry(option.to_owned()) - .and_modify(|opt| *opt = Some(value.to_owned())); + .entry(option) + .and_modify(|opt| *opt = Some(value)); self } - pub fn into_element<'a>(self) -> Element<'a, Message> { + pub fn text_view<'a>(self) -> Element<'a, Message> { let mut children = widget::column![]; if let Some(els) = self.child_elements.clone() { for el in els { - children = children.push(el.clone().into_element()); + children = children.push(el.clone().text_view()); } } iced_drop::droppable( @@ -191,7 +191,7 @@ impl RenderedElement { .padding(10) .style(widget::container::bordered_box), ) - .id(self.get_id().clone()) + .id(self.id().clone()) .drag_hide(true) .on_drop(move |point, rect| { Message::MoveElement(self.clone(), point, rect) @@ -313,28 +313,34 @@ impl std::fmt::Display for RenderedElement { impl<'a> From<RenderedElement> for Element<'a, Message> { fn from(value: RenderedElement) -> Self { - let child_elements = match value.child_elements { - Some(ref elements) => elements.clone(), - None => vec![], - }; + let copy = value.clone(); + let child_elements = copy.child_elements.unwrap_or_default(); - let content: Element<'a, Message> = match value.name.clone() { + let content: Element<'a, Message> = match copy.name { ElementName::Text(s) => { - if s == String::new() { + if s.is_empty() { widget::text("New Text").into() } else { widget::text(s).into() } } ElementName::Button(s) => { - if s == String::new() { - widget::button(widget::text("New Button")).into() + if s.is_empty() { + widget::button(widget::text("New Button")) + .apply_options(copy.options) + .into() } else { - widget::button(widget::text(s)).into() + widget::button(widget::text(s)) + .apply_options(copy.options) + .into() } } - ElementName::Svg(p) => widget::svg(p).into(), - ElementName::Image(p) => widget::image(p).into(), + ElementName::Svg(p) => { + widget::svg(p).apply_options(copy.options).into() + } + ElementName::Image(p) => { + widget::image(p).apply_options(copy.options).into() + } ElementName::Container => { widget::container(if child_elements.len() == 1 { child_elements[0].clone().into() @@ -348,15 +354,17 @@ impl<'a> From<RenderedElement> for Element<'a, Message> { child_elements.into_iter().map(Into::into).collect(), ) .padding(20) + .apply_options(copy.options) .into(), ElementName::Column => widget::Column::from_vec( child_elements.into_iter().map(Into::into).collect(), ) .padding(20) + .apply_options(copy.options) .into(), }; iced_drop::droppable(content) - .id(value.get_id().clone()) + .id(value.id().clone()) .drag_hide(true) .on_drop(move |point, rect| { Message::MoveElement(value.clone(), point, rect) @@ -433,19 +441,35 @@ pub fn text(text: &str) -> RenderedElement { "line_height", "width", "height", + "align_x", + "align_y", ]) } pub fn button(text: &str) -> RenderedElement { RenderedElement::new(ElementName::Button(text.to_owned())) + .preset_options(&["width", "height", "padding", "clip"]) } pub fn svg(path: &str) -> RenderedElement { - RenderedElement::new(ElementName::Svg(path.to_owned())) + RenderedElement::new(ElementName::Svg(path.to_owned())).preset_options(&[ + "width", + "height", + "content_fit", + "rotation", + "opacity", + ]) } pub fn image(path: &str) -> RenderedElement { - RenderedElement::new(ElementName::Image(path.to_owned())) + RenderedElement::new(ElementName::Image(path.to_owned())).preset_options(&[ + "width", + "height", + "content_fit", + "rotation", + "opacity", + "scale", + ]) } pub fn container(content: Option<RenderedElement>) -> RenderedElement { @@ -453,10 +477,30 @@ pub fn container(content: Option<RenderedElement>) -> RenderedElement { Some(el) => RenderedElement::with(ElementName::Container, vec![el]), None => RenderedElement::with(ElementName::Container, vec![]), } + .preset_options(&[ + "padding", + "width", + "height", + "max_width", + "max_height", + "center_x", + "center_y", + "center", + "align_left", + "align_right", + "align_top", + "align_bottom", + "align_x", + "align_y", + "clip", + ]) } pub fn row(child_elements: Option<Vec<RenderedElement>>) -> RenderedElement { RenderedElement::with(ElementName::Row, child_elements.unwrap_or_default()) + .preset_options(&[ + "spacing", "padding", "width", "height", "align_y", "clip", + ]) } pub fn column(child_elements: Option<Vec<RenderedElement>>) -> RenderedElement { @@ -464,4 +508,13 @@ pub fn column(child_elements: Option<Vec<RenderedElement>>) -> RenderedElement { ElementName::Column, child_elements.unwrap_or_default(), ) + .preset_options(&[ + "spacing", + "padding", + "width", + "height", + "max_width", + "align_x", + "clip", + ]) } |
