diff options
| -rw-r--r-- | iced_builder/src/lib.rs | 1 | ||||
| -rw-r--r-- | iced_builder/src/main.rs | 32 | ||||
| -rw-r--r-- | iced_builder/src/types/element_name.rs | 12 | ||||
| -rwxr-xr-x | iced_builder/src/types/rendered_element.rs | 42 |
4 files changed, 61 insertions, 26 deletions
diff --git a/iced_builder/src/lib.rs b/iced_builder/src/lib.rs index 6de9ba8..420b14c 100644 --- a/iced_builder/src/lib.rs +++ b/iced_builder/src/lib.rs @@ -69,6 +69,7 @@ pub enum Message { CopyCode, SwitchPage(DesignerPage), EditorAction(text_editor::Action), + RefreshEditorContent, DropNewElement(ElementName, iced::Point, iced::Rectangle), HandleNew( ElementName, diff --git a/iced_builder/src/main.rs b/iced_builder/src/main.rs index 424e0e0..fc7f18c 100644 --- a/iced_builder/src/main.rs +++ b/iced_builder/src/main.rs @@ -109,6 +109,14 @@ impl App { self.editor_content.perform(action); } } + Message::RefreshEditorContent => { + let code = self + .project + .clone() + .app_code() + .unwrap_or_else(|err| err.to_string()); + self.editor_content = text_editor::Content::with_text(&code); + } Message::DropNewElement(name, point, _) => { return iced_drop::zones_on_point( move |zones| Message::HandleNew(name.clone(), zones), @@ -128,12 +136,8 @@ impl App { } println!("{:?}", result); } - let code = self - .project - .clone() - .app_code() - .unwrap_or_else(|err| err.to_string()); - self.editor_content = text_editor::Content::with_text(&code); + + return Task::done(Message::RefreshEditorContent); } Message::MoveElement(element, point, _) => { return iced_drop::zones_on_point( @@ -147,15 +151,17 @@ impl App { Message::HandleMove(element, zones) => { let ids: Vec<Id> = zones.into_iter().map(|z| z.0).collect(); if ids.len() > 0 { - println!( - "{:?}", - ActionKind::new( - ids, - &mut self.project.content.clone(), - Some(element.get_id()) - ) + let action = ActionKind::new( + ids, + &mut self.project.content.clone(), + Some(element.get_id()), ); + let result = element.handle_action(self.project.content.as_mut(), action); + + println!("{result:?}"); } + + return Task::done(Message::RefreshEditorContent); } Message::PaneResized(pane_grid::ResizeEvent { split, ratio }) => { self.pane_state.resize(split, ratio); diff --git a/iced_builder/src/types/element_name.rs b/iced_builder/src/types/element_name.rs index c30d6e3..8d00814 100644 --- a/iced_builder/src/types/element_name.rs +++ b/iced_builder/src/types/element_name.rs @@ -47,10 +47,7 @@ impl ElementName { ActionKind::AddNew => Ok(Some(element)), ActionKind::PushFront(id) => { element_tree - .ok_or(Error::String( - "The action was of kind `PushFront`, but no element tree was provided." - .to_owned(), - ))? + .ok_or("The action was of kind `PushFront`, but no element tree was provided.")? .find_by_id(id) .ok_or(Error::NonExistentElement)? .push_front(&element); @@ -58,10 +55,9 @@ impl ElementName { } ActionKind::InsertAfter(parent_id, child_id) => { element_tree - .ok_or(Error::String( - "The action was of kind `InsertAfter`, but no element tree was provided." - .to_owned(), - ))? + .ok_or( + "The action was of kind `InsertAfter`, but no element tree was provided.", + )? .find_by_id(parent_id) .ok_or(Error::NonExistentElement)? .insert_after(child_id, &element); diff --git a/iced_builder/src/types/rendered_element.rs b/iced_builder/src/types/rendered_element.rs index 8b9b222..e2bebfa 100755 --- a/iced_builder/src/types/rendered_element.rs +++ b/iced_builder/src/types/rendered_element.rs @@ -5,7 +5,7 @@ use iced::{widget, Element, Length}; use serde::{Deserialize, Serialize}; use unique_id::{string::StringGenerator, Generator}; -use crate::Message; +use crate::{Error, Message}; use super::element_name::ElementName; @@ -120,6 +120,39 @@ impl RenderedElement { } } + pub fn handle_action( + &self, + element_tree: Option<&mut RenderedElement>, + action: ActionKind, + ) -> Result<(), Error> { + let element_tree = element_tree.unwrap(); + + 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(), + ), + ActionKind::PushFront(id) => { + let old_parent = element_tree.find_parent(self).unwrap(); + old_parent.remove(self); + + let new_parent = element_tree.find_by_id(id).unwrap(); + new_parent.push_front(self); + + Ok(()) + } + ActionKind::InsertAfter(parent_id, target_id) => { + let old_parent = element_tree.find_parent(self).unwrap(); + old_parent.remove(self); + + let new_parent = element_tree.find_by_id(parent_id).unwrap(); + new_parent.insert_after(target_id, self); + + Ok(()) + } + } + } + fn preset_options(mut self, options: Vec<&str>) -> Self { for opt in options { self.options.insert(opt.to_owned(), None); @@ -127,10 +160,11 @@ impl RenderedElement { self } - pub fn option<'a>(&mut self, option: &'a str, value: &'a str) { + pub fn option<'a>(&mut self, option: &'a str, value: &'a str) -> Self { self.options .entry(option.to_owned()) .and_modify(|opt| *opt = Some(value.to_owned())); + self.clone() } pub fn as_element<'a>(self) -> Element<'a, Message> { @@ -231,9 +265,7 @@ impl RenderedElement { } pub fn test() -> RenderedElement { - let mut text1 = text("wow"); - text1.option("height", "120.5"); - text1.option("width", "230"); + let text1 = text("wow").option("height", "120.5").option("width", "230"); let element = container(Some(row(Some(vec