From e8792ea151e0c26fd3497a09446f1b846df1c87c Mon Sep 17 00:00:00 2001 From: pml68 Date: Tue, 3 Sep 2024 21:55:34 +0200 Subject: feat: finish codegen for elements **without** props feat: wow --- src/codegen/mod.rs | 40 ++++++++++++++++++++------------------- src/main.rs | 2 +- src/types/mod.rs | 34 ++++++--------------------------- src/types/props.rs | 44 +++++++++++++++++++++++++++++++++++++++++++ src/types/rendered_element.rs | 36 +++++++++++++++++++++++++++++++++++ 5 files changed, 108 insertions(+), 48 deletions(-) create mode 100644 src/types/props.rs create mode 100644 src/types/rendered_element.rs (limited to 'src') 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, pub designer_page: DesignerPage, } -pub struct RenderedElement { - pub id: String, - pub children: Option>, - 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, - pub align_x: Option, - pub align_y: Option, - pub horizontal_alignment: Option, - pub vertical_alignment: Option, - pub height: Option, - pub width: Option, - pub font: Option, - pub padding: Option, - pub spacing: Option, - pub content_fit: Option, - pub shaping: Option, -} - 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, + pub align_x: Option, + pub align_y: Option, + pub horizontal_alignment: Option, + pub vertical_alignment: Option, + pub width: Option, + pub height: Option, + pub max_width: Option, + pub max_height: Option, + pub font: Option, + pub padding: Option, + pub spacing: Option, + pub content_fit: Option, + pub shaping: Option, +} + +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, + 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) -> 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 + } +} -- cgit v1.2.3