summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpml68 <contact@pml68.me>2024-09-19 23:26:22 +0200
committerpml68 <contact@pml68.me>2024-09-22 23:55:11 +0200
commit71743cb18fbb69430d092df59a722f5995be31c3 (patch)
tree707e4a8076085623b2bb3bcf2ec8ef1054f1bdf2
parentfeat: finish codegen with limited props and elements (diff)
downloadiced-builder-71743cb18fbb69430d092df59a722f5995be31c3.tar.gz
feat: rework props
w
Diffstat (limited to '')
-rw-r--r--src/codegen/mod.rs57
-rw-r--r--src/types/mod.rs6
-rw-r--r--src/types/props.rs44
-rw-r--r--src/types/rendered_element.rs17
4 files changed, 49 insertions, 75 deletions
diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs
index c8b789b..d0d8ee8 100644
--- a/src/codegen/mod.rs
+++ b/src/codegen/mod.rs
@@ -1,34 +1,22 @@
use rust_format::{Config, Edition, Formatter, RustFmt};
-use crate::types::{props::Props, rendered_element::RenderedElement, ElementName};
+use crate::types::{rendered_element::RenderedElement, ElementName};
-impl Props {
- pub fn codegen(&self) -> String {
+impl RenderedElement {
+ fn props_codegen(&self) -> String {
let mut props_string = String::new();
- match self.spacing {
- Some(s) => {
- props_string = format!("{props_string}.spacing({s})");
- }
- None => {}
- }
-
- match self.max_height {
- Some(h) => {
- props_string = format!("{props_string}.max_height({h})");
- }
- None => {}
+ for (k, v) in self.props.clone() {
+ props_string = format!("{props_string}.{k}({v})");
}
props_string
}
-}
-impl RenderedElement {
fn codegen(&self) -> (String, String) {
let mut imports = String::new();
let mut view = String::new();
- let props = self.props.codegen();
+ let props = self.props_codegen();
let mut elements = String::new();
@@ -41,7 +29,7 @@ impl RenderedElement {
match &self.name {
ElementName::Container => {
- imports = format!("{imports}widget::container,");
+ imports = format!("{imports}container,");
view = if self.child_elements.len() < 2 {
format!("{view}\ncontainer({elements}){props}")
} else {
@@ -49,12 +37,19 @@ impl RenderedElement {
};
}
ElementName::Row => {
- imports = format!("{imports}widget::row,");
+ imports = format!("{imports}row,");
view = format!("{view}\nrow![{elements}]{props}");
}
ElementName::Text(string) => {
- imports = format!("{imports}widget::text,");
- view = format!("{view}\ntext(\"{string}\"){props}");
+ imports = format!("{imports}text,");
+ view = format!(
+ "{view}\ntext(\"{}\"){props}",
+ if *string == String::new() {
+ "New Text"
+ } else {
+ string
+ }
+ );
}
_ => {}
}
@@ -62,9 +57,9 @@ impl RenderedElement {
(imports, view)
}
- pub fn app_code(&self, title: String) -> Result<String, Box<dyn std::error::Error>> {
+ pub fn app_code(&self, title: &str) -> Result<String, Box<dyn std::error::Error>> {
let (imports, view) = self.codegen();
- let mut app_code = format!("use iced::{{{imports}Sandbox,Settings,Element}};");
+ let mut app_code = format!("use iced::{{widget::{{{imports}}},Sandbox,Settings,Element}};");
app_code = format!(
r#"{app_code}
@@ -98,4 +93,18 @@ impl RenderedElement {
Ok(RustFmt::default().format_str(app_code)?)
}
+
+ pub fn test() -> String {
+ let mut text1 = RenderedElement::new(ElementName::Text("wow"));
+ text1.set_property("padding", "[10, 20]");
+ text1.set_property("spacing", "20.0");
+ text1.set_property("max_height", "120.5");
+
+ let element = RenderedElement::new(ElementName::Container).push(RenderedElement::from_vec(
+ ElementName::Row,
+ vec![text1, RenderedElement::new(ElementName::Text("heh"))],
+ ));
+
+ element.app_code("new app").unwrap()
+ }
}
diff --git a/src/types/mod.rs b/src/types/mod.rs
index 1fa23d4..c837ee1 100644
--- a/src/types/mod.rs
+++ b/src/types/mod.rs
@@ -1,4 +1,3 @@
-pub mod props;
pub mod rendered_element;
use iced::widget::text_editor;
@@ -10,9 +9,10 @@ pub struct DesignerState {
pub designer_page: DesignerPage,
}
+#[derive(Debug)]
pub enum ElementName {
- Text(String),
- Button(String),
+ Text(&'static str),
+ Button(&'static str),
TextEditor(text_editor::Content),
SVG(PathBuf),
Image(PathBuf),
diff --git a/src/types/props.rs b/src/types/props.rs
deleted file mode 100644
index 6dbb0a4..0000000
--- a/src/types/props.rs
+++ /dev/null
@@ -1,44 +0,0 @@
-use core::f32;
-
-use iced::{
- alignment::{Horizontal, Vertical},
- widget::text::Shaping,
- Alignment, ContentFit, Font, Length, Padding,
-};
-pub struct Props {
- pub align_items: Option<Alignment>,
- pub align_x: Option<Horizontal>,
- pub align_y: Option<Vertical>,
- pub horizontal_alignment: Option<Horizontal>,
- pub vertical_alignment: Option<Vertical>,
- pub width: Option<Length>,
- pub height: Option<Length>,
- pub max_width: Option<f32>,
- pub max_height: Option<f32>,
- pub font: Option<Font>,
- pub padding: Option<Padding>,
- pub spacing: Option<f32>,
- pub content_fit: Option<ContentFit>,
- pub shaping: Option<Shaping>,
-}
-
-impl Default for Props {
- fn default() -> Self {
- Self {
- align_items: None,
- align_x: None,
- align_y: None,
- horizontal_alignment: None,
- vertical_alignment: None,
- width: None,
- height: None,
- max_width: None,
- max_height: None,
- font: None,
- padding: None,
- spacing: None,
- content_fit: None,
- shaping: None,
- }
- }
-}
diff --git a/src/types/rendered_element.rs b/src/types/rendered_element.rs
index e909ac5..cbc923d 100644
--- a/src/types/rendered_element.rs
+++ b/src/types/rendered_element.rs
@@ -1,11 +1,15 @@
+use std::collections::HashMap;
+
use unique_id::{string::StringGenerator, Generator};
-use super::{props::Props, ElementName};
+use super::ElementName;
+
+#[derive(Debug)]
pub struct RenderedElement {
pub id: String,
pub child_elements: Vec<RenderedElement>,
pub name: ElementName,
- pub props: Props,
+ pub props: HashMap<&'static str, &'static str>,
}
impl RenderedElement {
@@ -15,7 +19,7 @@ impl RenderedElement {
id: gen.next_id(),
child_elements: vec![],
name,
- props: Props::default(),
+ props: HashMap::new(),
}
}
@@ -25,7 +29,7 @@ impl RenderedElement {
id: gen.next_id(),
child_elements,
name,
- props: Props::default(),
+ props: HashMap::new(),
}
}
@@ -33,4 +37,9 @@ impl RenderedElement {
self.child_elements.push(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;
+ }
}