summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpml68 <contact@pml68.me>2024-09-22 19:42:08 +0200
committerpml68 <contact@pml68.me>2024-09-22 23:55:11 +0200
commit7a97a64faa983b1de64dedb49bdbf34582f1f4c8 (patch)
treef8378d4382153148df19f64402ffbdfa66820770
parentfeat: rework props (diff)
downloadiced-builder-7a97a64faa983b1de64dedb49bdbf34582f1f4c8.tar.gz
feat: finish codegen
-rw-r--r--src/codegen/mod.rs59
-rw-r--r--src/types/mod.rs2
-rw-r--r--src/types/rendered_element.rs8
3 files changed, 55 insertions, 14 deletions
diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs
index d0d8ee8..bc3c379 100644
--- a/src/codegen/mod.rs
+++ b/src/codegen/mod.rs
@@ -20,11 +20,16 @@ impl RenderedElement {
let mut elements = String::new();
- for element in &self.child_elements {
- let mut children = String::new();
-
- (imports, children) = element.codegen();
- elements = format!("{elements}{},", children);
+ match self.name {
+ ElementName::Column | ElementName::Row | ElementName::Container => {
+ for element in &self.child_elements {
+ let mut children = String::new();
+
+ (imports, children) = element.codegen();
+ elements = format!("{elements}{},", children);
+ }
+ }
+ _ => {}
}
match &self.name {
@@ -40,6 +45,10 @@ impl RenderedElement {
imports = format!("{imports}row,");
view = format!("{view}\nrow![{elements}]{props}");
}
+ ElementName::Column => {
+ imports = format!("{imports}column,");
+ view = format!("{view}\ncolumn![{elements}]{props}");
+ }
ElementName::Text(string) => {
imports = format!("{imports}text,");
view = format!(
@@ -51,13 +60,35 @@ impl RenderedElement {
}
);
}
- _ => {}
+ ElementName::Button(string) => {
+ imports = format!("{imports}button,");
+ view = format!(
+ "{view}\nbutton(\"{}\"){props}",
+ if *string == String::new() {
+ "New Button"
+ } else {
+ string
+ }
+ );
+ }
+ ElementName::Image(path) => {
+ imports = format!("{imports}image,");
+ view = format!("{view}\nimage({}){props}", path.display().to_string());
+ }
+ ElementName::SVG(path) => {
+ imports = format!("{imports}svg,");
+ view = format!("{view}\nsvg({}){props}", path.display().to_string());
+ }
}
(imports, view)
}
- pub fn app_code(&self, title: &str) -> Result<String, Box<dyn std::error::Error>> {
+ pub fn app_code(
+ &self,
+ title: &str,
+ theme: Option<iced::Theme>,
+ ) -> Result<String, Box<dyn std::error::Error>> {
let (imports, view) = self.codegen();
let mut app_code = format!("use iced::{{widget::{{{imports}}},Sandbox,Settings,Element}};");
@@ -81,6 +112,10 @@ impl RenderedElement {
"{title}".into()
}}
+ fn theme(&self) -> iced::Theme {{
+ iced::Theme::{}
+ }}
+
fn update(&mut self, message: Message) {{
}}
@@ -88,7 +123,12 @@ impl RenderedElement {
fn view(&self) -> Element<Message> {{
{view}.into()
}}
- }}"#
+ }}"#,
+ if let Some(c) = theme {
+ c.to_string().replace(' ', "")
+ } else {
+ "default()".to_owned()
+ }
);
Ok(RustFmt::default().format_str(app_code)?)
@@ -99,12 +139,13 @@ impl RenderedElement {
text1.set_property("padding", "[10, 20]");
text1.set_property("spacing", "20.0");
text1.set_property("max_height", "120.5");
+ text1.set_property("max_width", "230");
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()
+ element.app_code("new app", None).unwrap()
}
}
diff --git a/src/types/mod.rs b/src/types/mod.rs
index c837ee1..7a04d79 100644
--- a/src/types/mod.rs
+++ b/src/types/mod.rs
@@ -1,6 +1,5 @@
pub mod rendered_element;
-use iced::widget::text_editor;
use rendered_element::RenderedElement;
use std::path::PathBuf;
@@ -13,7 +12,6 @@ pub struct DesignerState {
pub enum ElementName {
Text(&'static str),
Button(&'static str),
- TextEditor(text_editor::Content),
SVG(PathBuf),
Image(PathBuf),
Container,
diff --git a/src/types/rendered_element.rs b/src/types/rendered_element.rs
index cbc923d..f05594d 100644
--- a/src/types/rendered_element.rs
+++ b/src/types/rendered_element.rs
@@ -2,11 +2,13 @@ use std::collections::HashMap;
use unique_id::{string::StringGenerator, Generator};
+use iced::advanced::widget::Id;
+
use super::ElementName;
#[derive(Debug)]
pub struct RenderedElement {
- pub id: String,
+ pub id: Id,
pub child_elements: Vec<RenderedElement>,
pub name: ElementName,
pub props: HashMap<&'static str, &'static str>,
@@ -16,7 +18,7 @@ impl RenderedElement {
pub fn new(name: ElementName) -> Self {
let gen = StringGenerator::default();
Self {
- id: gen.next_id(),
+ id: Id::new(gen.next_id()),
child_elements: vec![],
name,
props: HashMap::new(),
@@ -26,7 +28,7 @@ impl RenderedElement {
pub fn from_vec(name: ElementName, child_elements: Vec<RenderedElement>) -> Self {
let gen = StringGenerator::default();
Self {
- id: gen.next_id(),
+ id: Id::new(gen.next_id()),
child_elements,
name,
props: HashMap::new(),