summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpml68 <contact@pml68.me>2024-09-03 21:55:34 +0200
committerpml68 <contact@pml68.me>2024-09-22 23:55:11 +0200
commite8792ea151e0c26fd3497a09446f1b846df1c87c (patch)
tree3aed2ad399ae78fd6ead35c3d71906f0dbe31b71
parentfeat: format children nodes into code (without props) (diff)
downloadiced-builder-e8792ea151e0c26fd3497a09446f1b846df1c87c.tar.gz
feat: finish codegen for elements **without** props
feat: wow
-rw-r--r--Cargo.lock43
-rw-r--r--Cargo.toml1
-rw-r--r--src/codegen/mod.rs40
-rw-r--r--src/main.rs2
-rw-r--r--src/types/mod.rs34
-rw-r--r--src/types/props.rs44
-rw-r--r--src/types/rendered_element.rs36
7 files changed, 152 insertions, 48 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 2d33a26..b519b92 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -159,6 +159,12 @@ dependencies = [
[[package]]
name = "base64"
+version = "0.13.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
+
+[[package]]
+name = "base64"
version = "0.21.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567"
@@ -212,6 +218,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
[[package]]
+name = "blob-uuid"
+version = "0.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "83fc15853171b33280f5614e77f5fa4debd33f51a86c44daa4ba3d759674c561"
+dependencies = [
+ "base64 0.13.1",
+ "uuid",
+]
+
+[[package]]
name = "block"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1186,6 +1202,7 @@ dependencies = [
"iced_aw",
"rust-format",
"tokio",
+ "unique_id",
]
[[package]]
@@ -1527,6 +1544,12 @@ dependencies = [
]
[[package]]
+name = "lazy_static"
+version = "1.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
+
+[[package]]
name = "lebe"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -3164,6 +3187,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "229730647fbc343e3a80e463c1db7f78f3855d3f3739bee0dda773c9a037c90a"
[[package]]
+name = "unique_id"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ae605c39dfbdec433798d4a8b03ffbac711dc51cdeb1ba5c725bdcaf24e464cc"
+dependencies = [
+ "blob-uuid",
+ "lazy_static",
+ "uuid",
+]
+
+[[package]]
name = "usvg"
version = "0.36.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -3225,6 +3259,15 @@ dependencies = [
]
[[package]]
+name = "uuid"
+version = "1.10.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "81dfa00651efa65069b0b6b651f4aaa31ba9e3c3ce0137aaad053604ee7e0314"
+dependencies = [
+ "getrandom",
+]
+
+[[package]]
name = "version_check"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index 36eabed..301ecfe 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -13,3 +13,4 @@ iced = { version = "0.12.1", features = [ "image","svg","canvas","qr_code","adva
iced_aw = { version = "0.9.3", default-features = false, features = ["menu","color_picker"] }
tokio = { version = "1.40.0", features = ["fs"] }
rust-format = "0.3.4"
+unique_id = "0.1.5"
diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs
index 8e31dc3..5ce8425 100644
--- a/src/codegen/mod.rs
+++ b/src/codegen/mod.rs
@@ -1,38 +1,40 @@
-use crate::types::{ElementName, RenderedElement};
+use crate::types::{props::Props, rendered_element::RenderedElement, ElementName};
impl RenderedElement {
- pub fn codegen(&self) -> Result<(String, String), &str> {
+ pub fn codegen(&self) -> (String, String) {
let mut imports = String::new();
let mut view = String::new();
let mut props = String::new();
let mut elements = String::new();
- match self.children {
- Some(els) => {
- for el in els {
- let mut children = String::new();
+ for element in &self.child_elements {
+ let mut children = String::new();
- match el.codegen() {
- Ok(e) => (children, imports) = e,
- Err(err) => return Err(err),
- }
- elements = format!("{elements},{}", children);
- }
- }
- None => {}
+ (imports, children) = element.codegen();
+ elements = format!("{elements}{},", children);
}
- match self.name {
+ match &self.name {
+ ElementName::Container => {
+ imports = format!("{imports}\nuse iced::widget::container;");
+ view = if self.child_elements.len() < 2 {
+ format!("{view}\ncontainer({elements}){props}")
+ } else {
+ format!("{view}\ncontainer(){props}")
+ };
+ }
ElementName::Row => {
imports = format!("{imports}\nuse iced::widget::row;");
- view = format!("{view}\nrow![{elements}]{props};");
+ view = format!("{view}\nrow![{elements}]{props}");
}
- ElementName::Container => {
- imports = format!("{imports}\nuse iced::widget::container;");
- view = format!("{view}\ncontainer({elements}){props};");
+ ElementName::Text(string) => {
+ imports = format!("{imports}\nuse iced::widget::text;");
+ view = format!("{view}\ntext(\"{string}\"){props}");
}
_ => {}
}
+
+ (imports, view)
}
}
diff --git a/src/main.rs b/src/main.rs
index 16a1daa..b195c27 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -13,7 +13,7 @@ use iced::{
Alignment, Application, Color, Command, Element, Font, Length, Settings,
};
use rust_format::{Formatter, RustFmt};
-use types::{DesignerPage, DesignerState};
+use types::{rendered_element::RenderedElement, DesignerPage, DesignerState};
fn main() -> iced::Result {
App::run(Settings::default())
diff --git a/src/types/mod.rs b/src/types/mod.rs
index 09b0f07..60b28f7 100644
--- a/src/types/mod.rs
+++ b/src/types/mod.rs
@@ -1,24 +1,17 @@
-use std::path::PathBuf;
+pub mod props;
+pub mod rendered_element;
-use iced::{
- alignment::{Horizontal, Vertical},
- widget::{text::Shaping, text_editor},
- Alignment, ContentFit, Font, Length,
-};
+use iced::widget::text_editor;
+use rendered_element::RenderedElement;
+use std::path::PathBuf;
pub struct DesignerState {
pub designer_content: Vec<RenderedElement>,
pub designer_page: DesignerPage,
}
-pub struct RenderedElement {
- pub id: String,
- pub children: Option<Vec<RenderedElement>>,
- pub name: ElementName,
- pub props: Props,
-}
-
pub enum ElementName {
+ App(String, iced::Theme),
Text(String),
Button(String),
TextEditor(text_editor::Content),
@@ -29,21 +22,6 @@ pub enum ElementName {
Column,
}
-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 height: Option<Length>,
- pub width: Option<Length>,
- pub font: Option<Font>,
- pub padding: Option<i32>,
- pub spacing: Option<i32>,
- pub content_fit: Option<ContentFit>,
- pub shaping: Option<Shaping>,
-}
-
pub enum DesignerPage {
Designer,
CodeView,
diff --git a/src/types/props.rs b/src/types/props.rs
new file mode 100644
index 0000000..6dbb0a4
--- /dev/null
+++ b/src/types/props.rs
@@ -0,0 +1,44 @@
+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
new file mode 100644
index 0000000..e909ac5
--- /dev/null
+++ b/src/types/rendered_element.rs
@@ -0,0 +1,36 @@
+use unique_id::{string::StringGenerator, Generator};
+
+use super::{props::Props, ElementName};
+pub struct RenderedElement {
+ pub id: String,
+ pub child_elements: Vec<RenderedElement>,
+ pub name: ElementName,
+ pub props: Props,
+}
+
+impl RenderedElement {
+ pub fn new(name: ElementName) -> Self {
+ let gen = StringGenerator::default();
+ Self {
+ id: gen.next_id(),
+ child_elements: vec![],
+ name,
+ props: Props::default(),
+ }
+ }
+
+ pub fn from_vec(name: ElementName, child_elements: Vec<RenderedElement>) -> Self {
+ let gen = StringGenerator::default();
+ Self {
+ id: gen.next_id(),
+ child_elements,
+ name,
+ props: Props::default(),
+ }
+ }
+
+ pub fn push(mut self, element: RenderedElement) -> Self {
+ self.child_elements.push(element);
+ self
+ }
+}