summaryrefslogtreecommitdiff
path: root/iced_builder/src/codegen
diff options
context:
space:
mode:
authorpml68 <contact@pml68.me>2024-10-04 00:44:02 +0200
committerpml68 <contact@pml68.me>2024-10-04 00:44:02 +0200
commit510d68b92972b99868e187dd5340f04780b4c354 (patch)
tree6d0937824d8606423b5afef2a16e182a3a984f8f /iced_builder/src/codegen
parentfeat: implement fmt::Display for RenderedElement, work on props (diff)
downloadiced-builder-510d68b92972b99868e187dd5340f04780b4c354.tar.gz
feat: update to iced 0.13.1, basic project state file, prepare for drag&drop
Diffstat (limited to '')
-rw-r--r--iced_builder/src/codegen/mod.rs122
1 files changed, 59 insertions, 63 deletions
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<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}};");
-
- 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<Self::Message> {{
- {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<String, Error> {
+ 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<Message> {{
+ {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()),
+ }
+ }
+}