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/lib.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'iced_builder/src/lib.rs') diff --git a/iced_builder/src/lib.rs b/iced_builder/src/lib.rs index 420b14c..f18e31d 100644 --- a/iced_builder/src/lib.rs +++ b/iced_builder/src/lib.rs @@ -1,4 +1,5 @@ pub mod types; +pub mod views; use std::path::PathBuf; -- 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/lib.rs') 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 27fcc7a6a3dee7777449c0517a11838310aa164d Mon Sep 17 00:00:00 2001 From: pml68 Date: Sat, 26 Oct 2024 22:22:45 +0200 Subject: feat: implement `std::error::Error` for custom `Error` enum --- Cargo.lock | 9 +++++---- iced_builder/Cargo.toml | 1 + iced_builder/src/lib.rs | 36 +++++++++++------------------------- iced_drop/README.md | 2 -- 4 files changed, 17 insertions(+), 31 deletions(-) (limited to 'iced_builder/src/lib.rs') diff --git a/Cargo.lock b/Cargo.lock index d65a742..867bd8b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1593,6 +1593,7 @@ dependencies = [ "rust-format", "serde", "serde_json", + "thiserror", "tokio", "unique_id", ] @@ -3563,18 +3564,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.64" +version = "1.0.65" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d50af8abc119fb8bb6dbabcfa89656f46f84aa0ac7688088608076ad2b459a84" +checksum = "5d11abd9594d9b38965ef50805c5e469ca9cc6f197f883f717e0269a3057b3d5" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.64" +version = "1.0.65" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08904e7672f5eb876eaaf87e0ce17857500934f4981c4a0ab2b4aa98baac7fc3" +checksum = "ae71770322cbd277e69d762a16c444af02aa0575ac0d174f0b9562d3b37f8602" dependencies = [ "proc-macro2", "quote", diff --git a/iced_builder/Cargo.toml b/iced_builder/Cargo.toml index d788bc2..d08d485 100644 --- a/iced_builder/Cargo.toml +++ b/iced_builder/Cargo.toml @@ -19,6 +19,7 @@ rfd = "0.15.0" rust-format = "0.3.4" unique_id = "0.1.5" indexmap = { version = "2.6.0", features = ["serde"] } +thiserror = "1.0.65" [[bin]] name = "iced-builder" diff --git a/iced_builder/src/lib.rs b/iced_builder/src/lib.rs index 1c7af06..14a044e 100644 --- a/iced_builder/src/lib.rs +++ b/iced_builder/src/lib.rs @@ -8,41 +8,27 @@ use types::{ element_name::ElementName, project::Project, rendered_element::RenderedElement, DesignerPage, }; -#[derive(Debug, Clone)] +use thiserror::Error; + +#[derive(Debug, Clone, Error)] pub enum Error { - IOError(std::io::ErrorKind), + #[error("an IO error accured: {0}")] + IOError(String), + #[error("a Serde error accured: {0}")] SerdeError(String), + #[error("an RustFmt error accured: {0}")] FormatError(String), + #[error("the element tree contains no matching element")] NonExistentElement, + #[error("the file dialog has been closed without selecting a valid option")] DialogClosed, + #[error("{0}")] String(String), } -impl std::fmt::Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Self::SerdeError(string) | Self::FormatError(string) | Self::String(string) => { - write!(f, "{}", string) - } - Self::IOError(kind) => { - write!(f, "{}", kind) - } - Self::NonExistentElement => { - write!(f, "The element tree contains no matching element.") - } - Self::DialogClosed => { - write!( - f, - "The file dialog has been closed without selecting a valid option." - ) - } - } - } -} - impl From for Error { fn from(value: std::io::Error) -> Self { - Self::IOError(value.kind()) + Self::IOError(value.to_string()) } } diff --git a/iced_drop/README.md b/iced_drop/README.md index 3768d54..41b637b 100644 --- a/iced_drop/README.md +++ b/iced_drop/README.md @@ -1,7 +1,5 @@ # iced_drop -# Modified to use iced 0.13.1 for compatibility - A small library which provides a custom widget and operation to make drag and drop easier to implement in [iced](https://github.com/iced-rs/iced/tree/master) ## Usage -- cgit v1.2.3