summaryrefslogtreecommitdiff
path: root/iced_builder/src/types
diff options
context:
space:
mode:
Diffstat (limited to 'iced_builder/src/types')
-rw-r--r--iced_builder/src/types/mod.rs19
-rw-r--r--iced_builder/src/types/rendered_element.rs63
2 files changed, 57 insertions, 25 deletions
diff --git a/iced_builder/src/types/mod.rs b/iced_builder/src/types/mod.rs
index eaf1686..def04cf 100644
--- a/iced_builder/src/types/mod.rs
+++ b/iced_builder/src/types/mod.rs
@@ -1,18 +1,19 @@
pub mod rendered_element;
use rendered_element::RenderedElement;
+use serde::{Deserialize, Serialize};
pub struct DesignerState {
pub designer_content: Option<RenderedElement>,
pub designer_page: DesignerPage,
}
-#[derive(Debug, Clone, Copy)]
+#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ElementName {
- Text(&'static str),
- Button(&'static str),
- SVG(&'static str),
- Image(&'static str),
+ Text(String),
+ Button(String),
+ SVG(String),
+ Image(String),
Container,
Row,
Column,
@@ -20,10 +21,10 @@ pub enum ElementName {
impl ElementName {
pub const ALL: [Self; 7] = [
- Self::Text(""),
- Self::Button(""),
- Self::SVG(""),
- Self::Image(""),
+ Self::Text(String::new()),
+ Self::Button(String::new()),
+ Self::SVG(String::new()),
+ Self::Image(String::new()),
Self::Container,
Self::Row,
Self::Column,
diff --git a/iced_builder/src/types/rendered_element.rs b/iced_builder/src/types/rendered_element.rs
index f05594d..1938aa0 100644
--- a/iced_builder/src/types/rendered_element.rs
+++ b/iced_builder/src/types/rendered_element.rs
@@ -1,47 +1,78 @@
use std::collections::HashMap;
+use serde::{Deserialize, Serialize};
use unique_id::{string::StringGenerator, Generator};
-use iced::advanced::widget::Id;
-
use super::ElementName;
-#[derive(Debug)]
+#[derive(Debug, Serialize, Deserialize)]
pub struct RenderedElement {
- pub id: Id,
- pub child_elements: Vec<RenderedElement>,
+ pub id: String,
+ pub child_elements: Option<Vec<RenderedElement>>,
pub name: ElementName,
- pub props: HashMap<&'static str, &'static str>,
+ pub props: HashMap<String, Option<String>>,
}
impl RenderedElement {
- pub fn new(name: ElementName) -> Self {
+ fn new(name: ElementName) -> Self {
let gen = StringGenerator::default();
Self {
- id: Id::new(gen.next_id()),
- child_elements: vec![],
+ id: gen.next_id(),
+ child_elements: None,
name,
props: HashMap::new(),
}
}
- pub fn from_vec(name: ElementName, child_elements: Vec<RenderedElement>) -> Self {
+ fn from_vec(name: ElementName, child_elements: Vec<RenderedElement>) -> Self {
let gen = StringGenerator::default();
Self {
- id: Id::new(gen.next_id()),
- child_elements,
+ id: gen.next_id(),
+ child_elements: Some(child_elements),
name,
props: HashMap::new(),
}
}
pub fn push(mut self, element: RenderedElement) -> Self {
- self.child_elements.push(element);
+ if let Some(els) = self.child_elements.as_mut() {
+ els.push(element);
+ } else {
+ self.child_elements = Some(vec![element]);
+ }
self
}
- pub fn set_property(&mut self, prop: &'static str, value: &'static str) {
- let prop_ref = self.props.entry(prop).or_insert(value);
- *prop_ref = value;
+ pub fn option(mut self, prop: &'static str, value: &'static str) -> Self {
+ let prop_ref = self
+ .props
+ .entry(prop.to_owned())
+ .or_insert(Some(value.to_owned()));
+ *prop_ref = Some(value.to_owned());
+ self
}
}
+
+pub fn text(text: &str) -> RenderedElement {
+ RenderedElement::new(ElementName::Text(text.to_owned()))
+}
+
+pub fn button(text: &str) -> RenderedElement {
+ RenderedElement::new(ElementName::Button(text.to_owned()))
+}
+
+pub fn svg(path: &str) -> RenderedElement {
+ RenderedElement::new(ElementName::SVG(path.to_owned()))
+}
+
+pub fn image(path: &str) -> RenderedElement {
+ RenderedElement::new(ElementName::Image(path.to_owned()))
+}
+
+pub fn container(content: RenderedElement) -> RenderedElement {
+ RenderedElement::from_vec(ElementName::Container, vec![content])
+}
+
+pub fn row(child_elements: Vec<RenderedElement>) -> RenderedElement {
+ RenderedElement::from_vec(ElementName::Row, child_elements)
+}