From b9553b8d6e5cc74f7dbc3b1b47673aa6d5e65f9e Mon Sep 17 00:00:00 2001 From: zaiic Date: Wed, 14 Feb 2024 19:31:17 +0800 Subject: [PATCH] feat: add support for toggling visibility of toolbars --- src/main.rs | 29 ++++++++++++++++++++++++++--- src/sketch_board.rs | 15 +++++++++++++++ src/ui/toolbars.rs | 18 +++++++++++++++++- 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index ea805ca..fd3d2ee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,7 @@ use anyhow::{anyhow, Context, Result}; use sketch_board::SketchBoardOutput; use ui::toast::Toast; -use ui::toolbars::{StyleToolbar, ToolsToolbar}; +use ui::toolbars::{StyleToolbar, StyleToolbarInput, ToolsToolbar, ToolsToolbarInput}; mod command_line; mod configuration; @@ -39,6 +39,8 @@ struct App { #[derive(Debug)] enum AppInput { Realized, + ShowToast(String), + ToggleToolbarsDisplay, } #[derive(Debug)] @@ -132,6 +134,24 @@ impl App { None => println!("Cannot apply style"), } } + + fn toggle_toolbars_display(&self) { + let is_showed = + self.tools_toolbar.widget().get_visible() && self.style_toolbar.widget().get_visible(); + if is_showed { + self.tools_toolbar.widget().hide(); + self.style_toolbar.widget().hide(); + } else { + self.tools_toolbar.widget().show(); + self.style_toolbar.widget().show(); + } + self.tools_toolbar + .sender() + .emit(ToolsToolbarInput::ToggleDisplay(is_showed)); + self.style_toolbar + .sender() + .emit(StyleToolbarInput::ToggleDisplay(is_showed)); + } } #[relm4::component] @@ -173,6 +193,8 @@ impl Component for App { fn update(&mut self, message: Self::Input, sender: ComponentSender, root: &Self::Root) { match message { AppInput::Realized => self.resize_window_initial(root, sender), + AppInput::ShowToast(msg) => self.toast.emit(ui::toast::ToastMessage::Show(msg)), + AppInput::ToggleToolbarsDisplay => self.toggle_toolbars_display(), } } @@ -203,8 +225,9 @@ impl Component for App { let sketch_board = SketchBoard::builder() .launch(image) - .forward(toast.sender(), |t| match t { - SketchBoardOutput::ShowToast(msg) => ui::toast::ToastMessage::Show(msg), + .forward(sender.input_sender(), |t| match t { + SketchBoardOutput::ShowToast(msg) => AppInput::ShowToast(msg), + SketchBoardOutput::ToggleToolbarsDisplay => AppInput::ToggleToolbarsDisplay, }); let sketch_board_sender = sketch_board.sender().clone(); diff --git a/src/sketch_board.rs b/src/sketch_board.rs index f15676c..29e74e2 100644 --- a/src/sketch_board.rs +++ b/src/sketch_board.rs @@ -30,6 +30,7 @@ pub enum SketchBoardInput { #[derive(Debug, Clone)] pub enum SketchBoardOutput { ShowToast(String), + ToggleToolbarsDisplay, } #[derive(Debug, Clone, Copy)] @@ -252,6 +253,14 @@ impl SketchBoard { } } + // Toolbars = Tools Toolbar + Style Toolbar + fn handle_toggle_toolbars_display(&mut self, sender: ComponentSender) -> ToolUpdateResult { + sender + .output_sender() + .emit(SketchBoardOutput::ToggleToolbarsDisplay); + ToolUpdateResult::Unmodified + } + fn handle_toolbar_event( &mut self, toolbar_event: ToolbarEvent, @@ -318,6 +327,10 @@ impl SketchBoard { } ToolbarEvent::Undo => self.handle_undo(), ToolbarEvent::Redo => self.handle_redo(), + + ToolbarEvent::Show => ToolUpdateResult::Unmodified, + // TODO: disable all action for sketch board when hidden toolbars + ToolbarEvent::Hide => ToolUpdateResult::Unmodified, } } } @@ -395,6 +408,8 @@ impl Component for SketchBoard { self.handle_undo() } else if ke.key == Key::y && ke.modifier == ModifierType::CONTROL_MASK { self.handle_redo() + } else if ke.key == Key::t && ke.modifier == ModifierType::CONTROL_MASK { + self.handle_toggle_toolbars_display(sender) } else if ke.key == Key::s && ke.modifier == ModifierType::CONTROL_MASK { self.handle_save(sender); if APP_CONFIG.read().early_exit() { diff --git a/src/ui/toolbars.rs b/src/ui/toolbars.rs index 10afa9d..f8209a6 100644 --- a/src/ui/toolbars.rs +++ b/src/ui/toolbars.rs @@ -34,6 +34,13 @@ pub enum ToolbarEvent { Undo, SaveFile, CopyClipboard, + Show, + Hide, +} + +#[derive(Debug, Copy, Clone)] +pub enum ToolsToolbarInput { + ToggleDisplay(bool), } #[derive(Debug, Copy, Clone)] @@ -41,6 +48,7 @@ pub enum StyleToolbarInput { ColorButtonSelected(ColorButtons), ShowColorDialog, ColorDialogFinished(Option), + ToggleDisplay(bool), } fn create_icon_pixbuf(color: Color) -> Pixbuf { @@ -55,7 +63,7 @@ fn create_icon(color: Color) -> gtk::Image { #[relm4::component(pub)] impl SimpleComponent for ToolsToolbar { type Init = (); - type Input = (); + type Input = ToolsToolbarInput; type Output = ToolbarEvent; view! { @@ -398,6 +406,14 @@ impl Component for StyleToolbar { .output_sender() .emit(ToolbarEvent::ColorSelected(color)); } + + StyleToolbarInput::ToggleDisplay(is_showed) => { + if is_showed { + sender.output_sender().emit(ToolbarEvent::Hide); + } else { + sender.output_sender().emit(ToolbarEvent::Show); + } + } } } fn init(