diff options
| author | pml68 <contact@pml68.me> | 2024-09-04 00:37:25 +0200 |
|---|---|---|
| committer | pml68 <contact@pml68.me> | 2024-09-22 23:55:11 +0200 |
| commit | a01402e4c0d226118335a6744ee7cda4f7be552b (patch) | |
| tree | 7cdbcab8eae0593039ea39a99af1a5311240980c /src | |
| parent | feat: finish codegen for elements **without** props (diff) | |
| download | iced-builder-a01402e4c0d226118335a6744ee7cda4f7be552b.tar.gz | |
feat: finish codegen with limited props and elements
Diffstat (limited to '')
| -rw-r--r-- | src/codegen/mod.rs | 71 | ||||
| -rw-r--r-- | src/main.rs | 95 | ||||
| -rw-r--r-- | src/types/mod.rs | 1 |
3 files changed, 113 insertions, 54 deletions
diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index 5ce8425..c8b789b 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -1,10 +1,34 @@ +use rust_format::{Config, Edition, Formatter, RustFmt}; + use crate::types::{props::Props, rendered_element::RenderedElement, ElementName}; +impl Props { + pub fn 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 => {} + } + + props_string + } +} + impl RenderedElement { - pub fn codegen(&self) -> (String, String) { + fn codegen(&self) -> (String, String) { let mut imports = String::new(); let mut view = String::new(); - let mut props = String::new(); + let props = self.props.codegen(); let mut elements = String::new(); @@ -17,7 +41,7 @@ impl RenderedElement { match &self.name { ElementName::Container => { - imports = format!("{imports}\nuse iced::widget::container;"); + imports = format!("{imports}widget::container,"); view = if self.child_elements.len() < 2 { format!("{view}\ncontainer({elements}){props}") } else { @@ -25,11 +49,11 @@ impl RenderedElement { }; } ElementName::Row => { - imports = format!("{imports}\nuse iced::widget::row;"); + imports = format!("{imports}widget::row,"); view = format!("{view}\nrow![{elements}]{props}"); } ElementName::Text(string) => { - imports = format!("{imports}\nuse iced::widget::text;"); + imports = format!("{imports}widget::text,"); view = format!("{view}\ntext(\"{string}\"){props}"); } _ => {} @@ -37,4 +61,41 @@ impl RenderedElement { (imports, view) } + + pub fn app_code(&self, title: String) -> Result<String, Box<dyn std::error::Error>> { + let (imports, view) = self.codegen(); + let mut app_code = format!("use iced::{{{imports}Sandbox,Settings,Element}};"); + + app_code = format!( + r#"{app_code} + + fn main() -> iced::Result {{ + App::run(Settings::default()) + }} + + struct App; + + impl Sandbox for App {{ + type Message = (); + + fn new() -> Self {{ + Self {{}} + }} + + fn title(&self) -> String {{ + "{title}".into() + }} + + fn update(&mut self, message: Message) {{ + + }} + + fn view(&self) -> Element<Message> {{ + {view}.into() + }} + }}"# + ); + + Ok(RustFmt::default().format_str(app_code)?) + } } diff --git a/src/main.rs b/src/main.rs index b195c27..5b691bb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,6 @@ use iced::{ }, Alignment, Application, Color, Command, Element, Font, Length, Settings, }; -use rust_format::{Formatter, RustFmt}; use types::{rendered_element::RenderedElement, DesignerPage, DesignerState}; fn main() -> iced::Result { @@ -65,13 +64,13 @@ impl Application for App { focus: None, designer_state: DesignerState { designer_content: vec![], - designer_page: DesignerPage::Designer, + designer_page: DesignerPage::CodeView, }, element_list: vec!["Column", "Row", "PickList", "PaneGrid", "Button", "Text"] .into_iter() .map(|c| c.to_owned()) .collect(), - editor_content: text_editor::Content::new(), + editor_content: text_editor::Content::with_text(&RenderedElement::test()), }, Command::none(), ) @@ -118,27 +117,56 @@ impl Application for App { let pane_grid = PaneGrid::new(&self.pane_state, |id, pane, _is_maximized| { let is_focused = Some(id) == self.focus; match pane { - Panes::Designer => { - let content = column![text("Designer")] - .align_items(Alignment::Center) - .height(Length::Fill) - .width(Length::Fill); - let title = text("Designer").style(if is_focused { - PANE_ID_COLOR_FOCUSED - } else { - PANE_ID_COLOR_UNFOCUSED - }); - let title_bar = pane_grid::TitleBar::new(title) - .padding(10) - .style(style::title_bar); - pane_grid::Content::new(content) + Panes::Designer => match self.designer_state.designer_page { + DesignerPage::Designer => { + let content = column![text("Designer"),] + .align_items(Alignment::Center) + .height(Length::Fill) + .width(Length::Fill); + let title = text("Designer").style(if is_focused { + PANE_ID_COLOR_FOCUSED + } else { + PANE_ID_COLOR_UNFOCUSED + }); + let title_bar = pane_grid::TitleBar::new(title) + .padding(10) + .style(style::title_bar); + pane_grid::Content::new(content) + .title_bar(title_bar) + .style(if is_focused { + style::pane_focused + } else { + style::pane_active + }) + } + DesignerPage::CodeView => { + let title = text("Generated Code").style(if is_focused { + PANE_ID_COLOR_FOCUSED + } else { + PANE_ID_COLOR_UNFOCUSED + }); + let title_bar = pane_grid::TitleBar::new(title) + .padding(10) + .style(style::title_bar); + pane_grid::Content::new( + text_editor(&self.editor_content) + .highlight::<Highlighter>( + highlighter::Settings { + theme: highlighter::Theme::Base16Mocha, + extension: "rs".to_string(), + }, + |highlight, _theme| highlight.to_format(), + ) + .padding(20), + ) .title_bar(title_bar) .style(if is_focused { style::pane_focused } else { style::pane_active }) - } + } + }, Panes::ElementList => { let items_list = items_list_view(&self.element_list); let content = column![items_list] @@ -160,36 +188,7 @@ impl Application for App { } else { style::pane_active }) - } //Panes::CodeView => { - // let title = text("Generated Code").style(if is_focused { - // PANE_ID_COLOR_FOCUSED - // } else { - // PANE_ID_COLOR_UNFOCUSED - // }); - // let title_bar = - // pane_grid::TitleBar::new(title) - // .padding(10) - // .style(if is_focused { - // style::title_bar_focused - // } else { - // style::title_bar_active - // }); - // pane_grid::Content::new( - // text_editor(&self.editor_content).highlight::<Highlighter>( - // highlighter::Settings { - // theme: highlighter::Theme::Base16Mocha, - // extension: "rs".to_string(), - // }, - // |highlight, _theme| highlight.to_format(), - // ), - // ) - // .title_bar(title_bar) - // .style(if is_focused { - // style::pane_focused - // } else { - // style::pane_active - // }) - //} + } } }) .width(Length::Fill) diff --git a/src/types/mod.rs b/src/types/mod.rs index 60b28f7..1fa23d4 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -11,7 +11,6 @@ pub struct DesignerState { } pub enum ElementName { - App(String, iced::Theme), Text(String), Button(String), TextEditor(text_editor::Content), |
