From 510d68b92972b99868e187dd5340f04780b4c354 Mon Sep 17 00:00:00 2001 From: pml68 Date: Fri, 4 Oct 2024 00:44:02 +0200 Subject: feat: update to iced 0.13.1, basic project state file, prepare for drag&drop --- iced_builder/src/codegen/mod.rs | 122 +++++++++++++++++++--------------------- 1 file changed, 59 insertions(+), 63 deletions(-) (limited to 'iced_builder/src/codegen/mod.rs') diff --git a/iced_builder/src/codegen/mod.rs b/iced_builder/src/codegen/mod.rs index 927b6e4..38e8ec3 100644 --- a/iced_builder/src/codegen/mod.rs +++ b/iced_builder/src/codegen/mod.rs @@ -1,8 +1,12 @@ use rust_format::{Config, Edition, Formatter, RustFmt}; -use crate::types::{ - rendered_element::{container, row, svg, text, RenderedElement}, - ElementName, +use crate::{ + types::{ + project::Project, + rendered_element::{container, row, svg, text, RenderedElement}, + ElementName, + }, + Error, }; impl RenderedElement { @@ -10,8 +14,8 @@ impl RenderedElement { let mut props_string = String::new(); for (k, v) in self.props.clone() { - if let Some(value) = v { - props_string = format!("{props_string}.{k}({value})"); + if let Some(v) = v { + props_string = format!("{props_string}.{k}({v})"); } } @@ -81,70 +85,62 @@ impl RenderedElement { (imports, view) } - pub fn app_code( - &self, - title: &str, - theme: Option, - ) -> Result> { - let (imports, view) = self.codegen(); - let mut app_code = format!("use iced::{{widget::{{{imports}}},Sandbox,Settings,Element}};"); - - app_code = format!( - r#"// Automatically generated by iced Builder - {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 theme(&self) -> iced::Theme {{ - iced::Theme::{} - }} - - fn update(&mut self, message: Self::Message) {{ - - }} - - fn view(&self) -> Element {{ - {view}.into() - }} - }}"#, - if let Some(c) = theme { - c.to_string().replace(' ', "") - } else { - "default()".to_owned() - } - ); - let config = Config::new_str() - .edition(Edition::Rust2021) - .option("trailing_comma", "Never") - .option("imports_granularity", "Crate"); - let rustfmt = RustFmt::from_config(config); - Ok(rustfmt.format_str(app_code)?) - } - pub fn test() -> RenderedElement { - let text1 = text("wow").option("height", "120.5").option("width", "230"); + let mut text1 = text("wow"); + text1.option("height", "120.5"); + text1.option("width", "230"); - let element = container(row(vec![ + let element = container(Some(row(Some(vec![ text1, text("heh"), svg("/mnt/drive_d/git/obs-website/src/lib/assets/bars-solid.svg"), - ])); + ])))); element } } + +impl Project { + pub fn app_code(self) -> Result { + match &self.content { + Some(el) => { + let (imports, view) = el.codegen(); + let mut app_code = format!("use iced::{{widget::{{{imports}}},Element}};"); + + app_code = format!( + r#"// Automatically generated by iced Builder + {app_code} + + fn main() -> iced::Result {{ + iced::run("{}", State::update, State::view) + }} + + #[derive(Default)] + struct State; + + #[derive(Debug, Clone)] + enum Message {{}} + + impl State {{ + fn update(&mut self, _message: Message) {{}} + + fn view(&self) -> Element {{ + {view}.into() + }} + }}"#, + match &self.title { + Some(t) => t, + None => "New app", + } + ); + let config = Config::new_str() + .edition(Edition::Rust2021) + .option("trailing_comma", "Never") + .option("imports_granularity", "Crate"); + let rustfmt = RustFmt::from_config(config); + Ok(rustfmt.format_str(app_code)?) + } + None => Err("No element tree present".into()), + } + } +} -- cgit v1.2.3