From cb844f573a438ff3a9b2f71fb88e6537edb20f54 Mon Sep 17 00:00:00 2001 From: pml68 Date: Fri, 25 Oct 2024 21:16:54 +0200 Subject: refactor: extract views from `main.rs` file --- iced_builder/src/types/project.rs | 5 +++-- iced_builder/src/types/rendered_element.rs | 8 +++----- 2 files changed, 6 insertions(+), 7 deletions(-) (limited to 'iced_builder/src/types') diff --git a/iced_builder/src/types/project.rs b/iced_builder/src/types/project.rs index 0e0442a..95de9e6 100644 --- a/iced_builder/src/types/project.rs +++ b/iced_builder/src/types/project.rs @@ -103,7 +103,7 @@ impl Project { {app_code} fn main() -> iced::Result {{ - iced::run("{}", State::update, State::view) + iced::application("{}", State::update, State::view).theme(iced::Theme::{}).run() }} #[derive(Default)] @@ -122,7 +122,8 @@ impl Project { match &self.title { Some(t) => t, None => "New app", - } + }, + self.get_theme().to_string().replace(" ", "") ); let config = Config::new_str() .edition(Edition::Rust2021) diff --git a/iced_builder/src/types/rendered_element.rs b/iced_builder/src/types/rendered_element.rs index e2bebfa..3be0d2e 100755 --- a/iced_builder/src/types/rendered_element.rs +++ b/iced_builder/src/types/rendered_element.rs @@ -109,10 +109,7 @@ impl RenderedElement { pub fn insert_after(&mut self, id: Id, element: &RenderedElement) { if let Some(child_elements) = self.child_elements.as_mut() { - if let Some(index) = child_elements - .iter() - .position(|x| Id::new(x.id.clone()) == id) - { + if let Some(index) = child_elements.iter().position(|x| x.get_id() == id) { child_elements.insert(index + 1, element.clone()); } else { child_elements.push(element.clone()); @@ -178,7 +175,8 @@ impl RenderedElement { iced_drop::droppable( widget::container( widget::column![widget::text(self.name.clone().to_string()), children] - .width(Length::Fill), + .width(Length::Fill) + .spacing(10), ) .padding(10) .style(widget::container::bordered_box), -- cgit v1.2.3 From 0e3a2a2f0b9bae9e3871a8bf49f885fd0ef7df1b Mon Sep 17 00:00:00 2001 From: pml68 Date: Fri, 25 Oct 2024 23:37:42 +0200 Subject: refactor: change all leftover mentions of `props` to `options` --- iced_builder/src/types/rendered_element.rs | 36 +++++++++++++----------------- 1 file changed, 15 insertions(+), 21 deletions(-) (limited to 'iced_builder/src/types') diff --git a/iced_builder/src/types/rendered_element.rs b/iced_builder/src/types/rendered_element.rs index 3be0d2e..caf6785 100755 --- a/iced_builder/src/types/rendered_element.rs +++ b/iced_builder/src/types/rendered_element.rs @@ -187,23 +187,17 @@ impl RenderedElement { .into() } - fn props_codegen(&self) -> String { - let mut props_string = String::new(); + pub fn codegen(&self) -> (String, String) { + let mut imports = String::new(); + let mut view = String::new(); + let mut options = String::new(); for (k, v) in self.options.clone() { if let Some(v) = v { - props_string = format!("{props_string}.{k}({v})"); + options = format!("{options}.{k}({v})"); } } - props_string - } - - pub fn codegen(&self) -> (String, String) { - let mut imports = String::new(); - let mut view = String::new(); - let props = self.props_codegen(); - let mut elements = String::new(); if let Some(els) = &self.child_elements { @@ -217,20 +211,20 @@ impl RenderedElement { match &self.name { ElementName::Container => { imports = format!("{imports}container,"); - view = format!("{view}\ncontainer({elements}){props}"); + view = format!("{view}\ncontainer({elements}){options}"); } ElementName::Row => { imports = format!("{imports}row,"); - view = format!("{view}\nrow![{elements}]{props}"); + view = format!("{view}\nrow![{elements}]{options}"); } ElementName::Column => { imports = format!("{imports}column,"); - view = format!("{view}\ncolumn![{elements}]{props}"); + view = format!("{view}\ncolumn![{elements}]{options}"); } ElementName::Text(string) => { imports = format!("{imports}text,"); view = format!( - "{view}\ntext(\"{}\"){props}", + "{view}\ntext(\"{}\"){options}", if *string == String::new() { "New Text" } else { @@ -241,7 +235,7 @@ impl RenderedElement { ElementName::Button(string) => { imports = format!("{imports}button,"); view = format!( - "{view}\nbutton(\"{}\"){props}", + "{view}\nbutton(\"{}\"){options}", if *string == String::new() { "New Button" } else { @@ -251,11 +245,11 @@ impl RenderedElement { } ElementName::Image(path) => { imports = format!("{imports}image,"); - view = format!("{view}\nimage(\"{path}\"){props}"); + view = format!("{view}\nimage(\"{path}\"){options}"); } ElementName::SVG(path) => { imports = format!("{imports}svg,"); - view = format!("{view}\nsvg(\"{path}\"){props}"); + view = format!("{view}\nsvg(\"{path}\"){options}"); } } @@ -277,14 +271,14 @@ impl RenderedElement { impl std::fmt::Display for RenderedElement { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let mut has_props = false; + let mut has_options = false; f.pad("")?; f.write_fmt(format_args!("{:?}\n", self.name))?; f.pad("")?; f.write_str("Options: (")?; for (k, v) in &self.options { if let Some(value) = v { - has_props = true; + has_options = true; f.write_fmt(format_args!( "\n{:width$.precision$}{}: {}", "", @@ -295,7 +289,7 @@ impl std::fmt::Display for RenderedElement { ))?; } } - if has_props { + if has_options { f.write_str("\n")?; f.pad("")?; } -- cgit v1.2.3 From 1d16127d90d93886e3f20b052a12ec705bffa9aa Mon Sep 17 00:00:00 2001 From: pml68 Date: Fri, 25 Oct 2024 23:40:34 +0200 Subject: test(rendered_element): remove unused `test` method --- iced_builder/src/types/rendered_element.rs | 12 ------------ 1 file changed, 12 deletions(-) (limited to 'iced_builder/src/types') diff --git a/iced_builder/src/types/rendered_element.rs b/iced_builder/src/types/rendered_element.rs index caf6785..5b7777a 100755 --- a/iced_builder/src/types/rendered_element.rs +++ b/iced_builder/src/types/rendered_element.rs @@ -255,18 +255,6 @@ impl RenderedElement { (imports, view) } - - pub fn test() -> RenderedElement { - let text1 = text("wow").option("height", "120.5").option("width", "230"); - - 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 std::fmt::Display for RenderedElement { -- cgit v1.2.3 From a2f3e993ac1e0d4f57d7c41d6601f7067190255b Mon Sep 17 00:00:00 2001 From: pml68 Date: Sat, 26 Oct 2024 21:52:37 +0200 Subject: feat: add C-S-s keybind for `Save As` --- iced_builder/src/lib.rs | 1 + iced_builder/src/main.rs | 34 ++++++++++++++++++++++-------- iced_builder/src/types/project.rs | 16 +++++++------- iced_builder/src/types/rendered_element.rs | 24 ++++++++------------- 4 files changed, 43 insertions(+), 32 deletions(-) (limited to 'iced_builder/src/types') diff --git a/iced_builder/src/lib.rs b/iced_builder/src/lib.rs index f18e31d..1c7af06 100644 --- a/iced_builder/src/lib.rs +++ b/iced_builder/src/lib.rs @@ -88,5 +88,6 @@ pub enum Message { OpenFile, FileOpened(Result<(PathBuf, Project), Error>), SaveFile, + SaveFileAs, FileSaved(Result), } diff --git a/iced_builder/src/main.rs b/iced_builder/src/main.rs index 32b52c5..8d8c382 100644 --- a/iced_builder/src/main.rs +++ b/iced_builder/src/main.rs @@ -14,9 +14,9 @@ use iced_builder::{ types::{ element_name::ElementName, project::Project, rendered_element::ActionKind, DesignerPage, }, - views::{designer_view, element_list}, + views::{code_view, designer_view, element_list}, + Message, }; -use iced_builder::{views::code_view, Message}; fn main() -> iced::Result { iced::application(App::title, App::update, App::view) @@ -131,10 +131,10 @@ impl App { Message::HandleNew(name, zones) => { let ids: Vec = zones.into_iter().map(|z| z.0).collect(); if ids.len() > 0 { - let action = ActionKind::new(ids, &mut self.project.content.clone(), None); - let result = name.handle_action(self.project.content.as_mut(), action); + let action = ActionKind::new(ids, &mut self.project.element_tree.clone(), None); + let result = name.handle_action(self.project.element_tree.as_mut(), action); if let Ok(Some(ref element)) = result { - self.project.content = Some(element.clone()); + self.project.element_tree = Some(element.clone()); } println!("{:?}", result); } @@ -155,10 +155,10 @@ impl App { if ids.len() > 0 { let action = ActionKind::new( ids, - &mut self.project.content.clone(), + &mut self.project.element_tree.clone(), Some(element.get_id()), ); - let result = element.handle_action(self.project.content.as_mut(), action); + let result = element.handle_action(self.project.element_tree.as_mut(), action); println!("{result:?}"); } @@ -213,6 +213,16 @@ impl App { ); } } + Message::SaveFileAs => { + if !self.is_loading { + self.is_loading = true; + + return Task::perform( + self.project.clone().write_to_file(None), + Message::FileSaved, + ); + } + } Message::FileSaved(result) => { self.is_loading = false; @@ -229,7 +239,13 @@ impl App { fn subscription(&self) -> iced::Subscription { keyboard::on_key_press(|key, modifiers| match key.as_ref() { keyboard::Key::Character("o") if modifiers.command() => Some(Message::OpenFile), - keyboard::Key::Character("s") if modifiers.command() => Some(Message::SaveFile), + keyboard::Key::Character("s") if modifiers.command() => { + if modifiers.shift() { + Some(Message::SaveFileAs) + } else { + Some(Message::SaveFile) + } + } keyboard::Key::Character("n") if modifiers.command() => Some(Message::NewFile), _ => None, }) @@ -245,7 +261,7 @@ impl App { match pane { Panes::Designer => match &self.designer_page { DesignerPage::Designer => designer_view::view( - &self.project.content, + &self.project.element_tree, self.project.get_theme(), is_focused, ), diff --git a/iced_builder/src/types/project.rs b/iced_builder/src/types/project.rs index 95de9e6..52da41c 100644 --- a/iced_builder/src/types/project.rs +++ b/iced_builder/src/types/project.rs @@ -12,7 +12,7 @@ use super::rendered_element::RenderedElement; pub struct Project { pub title: Option, pub theme: Option, - pub content: Option, + pub element_tree: Option, } impl Project { @@ -20,7 +20,7 @@ impl Project { Self { title: None, theme: None, - content: None, + element_tree: None, } } @@ -92,10 +92,10 @@ impl Project { Ok(path) } - pub fn app_code(self) -> Result { - match &self.content { - Some(el) => { - let (imports, view) = el.codegen(); + pub fn app_code(&self) -> Result { + match self.element_tree { + Some(ref element_tree) => { + let (imports, view) = element_tree.codegen(); let mut app_code = format!("use iced::{{widget::{{{imports}}},Element}};"); app_code = format!( @@ -119,8 +119,8 @@ impl Project { {view}.into() }} }}"#, - match &self.title { - Some(t) => t, + match self.title { + Some(ref t) => t, None => "New app", }, self.get_theme().to_string().replace(" ", "") diff --git a/iced_builder/src/types/rendered_element.rs b/iced_builder/src/types/rendered_element.rs index 5b7777a..50e591a 100755 --- a/iced_builder/src/types/rendered_element.rs +++ b/iced_builder/src/types/rendered_element.rs @@ -127,7 +127,7 @@ impl RenderedElement { match action { ActionKind::Stop => Ok(()), ActionKind::AddNew => Err( - "The action was of kind `AddNew`, but invoking it on an existing element tree is not possible.".into(), + "the action was of kind `AddNew`, but invoking it on an existing element tree is not possible".into(), ), ActionKind::PushFront(id) => { let old_parent = element_tree.find_parent(self).unwrap(); @@ -335,14 +335,14 @@ impl ActionKind { .find_by_id(id.clone()) .unwrap(); - match ( - element.is_parent(), - element.name == ElementName::Container && !element.is_empty(), - ) { - (true, false) => { + // Element IS a parent but ISN'T a non-empty container + match element.is_parent() + && !(element.name == ElementName::Container && !element.is_empty()) + { + true => { action = Self::PushFront(id); } - _ if ids.len() > 2 => { + false if ids.len() > 2 => { let parent = element_tree .as_mut() .unwrap() @@ -396,15 +396,9 @@ pub fn container(content: Option) -> RenderedElement { } pub fn row(child_elements: Option>) -> RenderedElement { - match child_elements { - Some(els) => RenderedElement::with(ElementName::Row, els), - None => RenderedElement::with(ElementName::Row, vec![]), - } + RenderedElement::with(ElementName::Row, child_elements.unwrap_or_default()) } pub fn column(child_elements: Option>) -> RenderedElement { - match child_elements { - Some(els) => RenderedElement::with(ElementName::Column, els), - None => RenderedElement::with(ElementName::Column, vec![]), - } + RenderedElement::with(ElementName::Column, child_elements.unwrap_or_default()) } -- cgit v1.2.3 From 93ac5d1008030ccd587dad72cc3d42fd9eaf2bdf Mon Sep 17 00:00:00 2001 From: pml68 Date: Sat, 26 Oct 2024 21:54:49 +0200 Subject: refactor: rename `ActionKind` to `Action` --- iced_builder/src/main.rs | 8 +++----- iced_builder/src/types/element_name.rs | 12 ++++++------ iced_builder/src/types/rendered_element.rs | 14 +++++++------- 3 files changed, 16 insertions(+), 18 deletions(-) (limited to 'iced_builder/src/types') diff --git a/iced_builder/src/main.rs b/iced_builder/src/main.rs index 8d8c382..195614e 100644 --- a/iced_builder/src/main.rs +++ b/iced_builder/src/main.rs @@ -11,9 +11,7 @@ use iced::{ Alignment, Element, Length, Settings, Task, Theme, }; use iced_builder::{ - types::{ - element_name::ElementName, project::Project, rendered_element::ActionKind, DesignerPage, - }, + types::{element_name::ElementName, project::Project, rendered_element::Action, DesignerPage}, views::{code_view, designer_view, element_list}, Message, }; @@ -131,7 +129,7 @@ impl App { Message::HandleNew(name, zones) => { let ids: Vec = zones.into_iter().map(|z| z.0).collect(); if ids.len() > 0 { - let action = ActionKind::new(ids, &mut self.project.element_tree.clone(), None); + let action = Action::new(ids, &mut self.project.element_tree.clone(), None); let result = name.handle_action(self.project.element_tree.as_mut(), action); if let Ok(Some(ref element)) = result { self.project.element_tree = Some(element.clone()); @@ -153,7 +151,7 @@ impl App { Message::HandleMove(element, zones) => { let ids: Vec = zones.into_iter().map(|z| z.0).collect(); if ids.len() > 0 { - let action = ActionKind::new( + let action = Action::new( ids, &mut self.project.element_tree.clone(), Some(element.get_id()), diff --git a/iced_builder/src/types/element_name.rs b/iced_builder/src/types/element_name.rs index 8d00814..93e12a1 100644 --- a/iced_builder/src/types/element_name.rs +++ b/iced_builder/src/types/element_name.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; use crate::Error; use super::rendered_element::{ - button, column, container, image, row, svg, text, ActionKind, RenderedElement, + button, column, container, image, row, svg, text, Action, RenderedElement, }; #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] @@ -31,7 +31,7 @@ impl ElementName { pub fn handle_action( &self, element_tree: Option<&mut RenderedElement>, - action: ActionKind, + action: Action, ) -> Result, Error> { let element = match self { Self::Text(_) => text(""), @@ -43,9 +43,9 @@ impl ElementName { Self::Column => column(None), }; match action { - ActionKind::Stop => Ok(None), - ActionKind::AddNew => Ok(Some(element)), - ActionKind::PushFront(id) => { + Action::Stop => Ok(None), + Action::AddNew => Ok(Some(element)), + Action::PushFront(id) => { element_tree .ok_or("The action was of kind `PushFront`, but no element tree was provided.")? .find_by_id(id) @@ -53,7 +53,7 @@ impl ElementName { .push_front(&element); Ok(None) } - ActionKind::InsertAfter(parent_id, child_id) => { + Action::InsertAfter(parent_id, child_id) => { element_tree .ok_or( "The action was of kind `InsertAfter`, but no element tree was provided.", diff --git a/iced_builder/src/types/rendered_element.rs b/iced_builder/src/types/rendered_element.rs index 50e591a..08d7ba3 100755 --- a/iced_builder/src/types/rendered_element.rs +++ b/iced_builder/src/types/rendered_element.rs @@ -120,16 +120,16 @@ impl RenderedElement { pub fn handle_action( &self, element_tree: Option<&mut RenderedElement>, - action: ActionKind, + action: Action, ) -> Result<(), Error> { let element_tree = element_tree.unwrap(); match action { - ActionKind::Stop => Ok(()), - ActionKind::AddNew => Err( + Action::Stop => Ok(()), + Action::AddNew => Err( "the action was of kind `AddNew`, but invoking it on an existing element tree is not possible".into(), ), - ActionKind::PushFront(id) => { + Action::PushFront(id) => { let old_parent = element_tree.find_parent(self).unwrap(); old_parent.remove(self); @@ -138,7 +138,7 @@ impl RenderedElement { Ok(()) } - ActionKind::InsertAfter(parent_id, target_id) => { + Action::InsertAfter(parent_id, target_id) => { let old_parent = element_tree.find_parent(self).unwrap(); old_parent.remove(self); @@ -300,14 +300,14 @@ impl std::fmt::Display for RenderedElement { } #[derive(Debug, Clone)] -pub enum ActionKind { +pub enum Action { AddNew, PushFront(Id), InsertAfter(Id, Id), Stop, } -impl ActionKind { +impl Action { pub fn new( ids: Vec, element_tree: &mut Option, -- cgit v1.2.3