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/main.rs | 174 +++++------------------------------------------ 1 file changed, 17 insertions(+), 157 deletions(-) (limited to 'iced_builder/src/main.rs') diff --git a/iced_builder/src/main.rs b/iced_builder/src/main.rs index fc7f18c..32b52c5 100644 --- a/iced_builder/src/main.rs +++ b/iced_builder/src/main.rs @@ -2,19 +2,21 @@ use std::path::PathBuf; use iced::{ advanced::widget::Id, - clipboard, highlighter, keyboard, + clipboard, keyboard, widget::{ - button, column, container, + button, container, pane_grid::{self, Pane, PaneGrid}, - row, text, text_editor, themer, tooltip, Column, Space, + row, text_editor, Column, }, - Alignment, Element, Font, Length, Settings, Task, Theme, + Alignment, Element, Length, Settings, Task, Theme, }; -use iced_builder::types::{ - element_name::ElementName, project::Project, rendered_element::ActionKind, DesignerPage, +use iced_builder::{ + types::{ + element_name::ElementName, project::Project, rendered_element::ActionKind, DesignerPage, + }, + views::{designer_view, element_list}, }; -use iced_builder::Message; -use iced_drop::droppable; +use iced_builder::{views::code_view, Message}; fn main() -> iced::Result { iced::application(App::title, App::update, App::view) @@ -242,96 +244,16 @@ impl App { let is_focused = Some(id) == self.focus; match pane { Panes::Designer => match &self.designer_page { - DesignerPage::Designer => { - let el_tree = match self.project.content.clone() { - Some(tree) => tree.as_element(), - None => text("Open a project or begin creating one").into(), - }; - let content = container(themer(self.project.get_theme(), el_tree)) - .id(iced::widget::container::Id::new("drop_zone")) - .height(Length::Fill) - .width(Length::Fill); - let title = row![ - text("Designer"), - Space::with_width(Length::Fill), - button("Switch to Code view") - .on_press(Message::SwitchPage(DesignerPage::CodeView)), - ] - .align_y(Alignment::Center); - let title_bar = pane_grid::TitleBar::new(title) - .padding(10) - .style(style::title_bar); - pane_grid::Content::new(content) - .title_bar(title_bar) - .style(if is_focused { - style::pane_focused - } else { - style::pane_active - }) - } + DesignerPage::Designer => designer_view::view( + &self.project.content, + self.project.get_theme(), + is_focused, + ), DesignerPage::CodeView => { - let title = row![ - text("Generated Code"), - Space::with_width(Length::Fill), - tooltip( - button( - container( - text('\u{0e801}').font(Font::with_name("editor-icons")) - ) - .center_x(30) - ) - .on_press(Message::CopyCode), - "Copy code to clipboard", - tooltip::Position::FollowCursor - ), - Space::with_width(20), - button("Switch to Designer view") - .on_press(Message::SwitchPage(DesignerPage::Designer)) - ] - .align_y(Alignment::Center); - let title_bar = pane_grid::TitleBar::new(title) - .padding(10) - .style(style::title_bar); - pane_grid::Content::new( - text_editor(&self.editor_content) - .on_action(Message::EditorAction) - .highlight( - "rs", - if self.dark_theme { - highlighter::Theme::SolarizedDark - } else { - highlighter::Theme::InspiredGitHub - }, - ) - .height(Length::Fill) - .padding(20), - ) - .title_bar(title_bar) - .style(if is_focused { - style::pane_focused - } else { - style::pane_active - }) + code_view::view(&self.editor_content, self.dark_theme, is_focused) } }, - Panes::ElementList => { - let items_list = items_list_view(self.element_list.clone()); - let content = column![items_list] - .align_x(Alignment::Center) - .height(Length::Fill) - .width(Length::Fill); - let title = text("Element List"); - let title_bar = pane_grid::TitleBar::new(title) - .padding(10) - .style(style::title_bar); - pane_grid::Content::new(content) - .title_bar(title_bar) - .style(if is_focused { - style::pane_focused - } else { - style::pane_active - }) - } + Panes::ElementList => element_list::view(&self.element_list, is_focused), } }) .width(Length::Fill) @@ -351,65 +273,3 @@ impl App { container(content).height(Length::Fill).into() } } - -fn items_list_view<'a>(items: Vec) -> Element<'a, Message> { - let mut column = Column::new() - .spacing(20) - .align_x(Alignment::Center) - .width(Length::Fill); - - for item in items { - column = column.push( - droppable(text(item.clone().to_string())) - .on_drop(move |point, rect| Message::DropNewElement(item.clone(), point, rect)), - ); - } - - container(column) - .width(Length::Fill) - .height(Length::Fill) - .into() -} - -mod style { - use iced::widget::container::Style; - use iced::{Border, Theme}; - - pub fn title_bar(theme: &Theme) -> Style { - let palette = theme.extended_palette(); - - Style { - text_color: Some(palette.background.strong.text), - background: Some(palette.background.strong.color.into()), - ..Default::default() - } - } - - pub fn pane_active(theme: &Theme) -> Style { - let palette = theme.extended_palette(); - - Style { - background: Some(palette.background.weak.color.into()), - border: Border { - width: 1.0, - color: palette.background.strong.color, - ..Border::default() - }, - ..Default::default() - } - } - - pub fn pane_focused(theme: &Theme) -> Style { - let palette = theme.extended_palette(); - - Style { - background: Some(palette.background.weak.color.into()), - border: Border { - width: 4.0, - color: palette.background.strong.color, - ..Border::default() - }, - ..Default::default() - } - } -} -- 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/main.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 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/main.rs') 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 From 564cf74dfaa2eb6ae574c4cb26318958822ee746 Mon Sep 17 00:00:00 2001 From: pml68 Date: Sat, 26 Oct 2024 21:56:19 +0200 Subject: test: remove all leftover println calls c --- iced_builder/src/main.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'iced_builder/src/main.rs') diff --git a/iced_builder/src/main.rs b/iced_builder/src/main.rs index 195614e..ed3f264 100644 --- a/iced_builder/src/main.rs +++ b/iced_builder/src/main.rs @@ -134,7 +134,6 @@ impl App { if let Ok(Some(ref element)) = result { self.project.element_tree = Some(element.clone()); } - println!("{:?}", result); } return Task::done(Message::RefreshEditorContent); @@ -156,9 +155,7 @@ impl App { &mut self.project.element_tree.clone(), Some(element.get_id()), ); - let result = element.handle_action(self.project.element_tree.as_mut(), action); - - println!("{result:?}"); + let _ = element.handle_action(self.project.element_tree.as_mut(), action); } return Task::done(Message::RefreshEditorContent); -- cgit v1.2.3