Skip to content

Commit

Permalink
feat: add support for toggling visibility of toolbars
Browse files Browse the repository at this point in the history
  • Loading branch information
mistgc committed Feb 14, 2024
1 parent 233bf32 commit b9553b8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
29 changes: 26 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,6 +39,8 @@ struct App {
#[derive(Debug)]
enum AppInput {
Realized,
ShowToast(String),
ToggleToolbarsDisplay,
}

#[derive(Debug)]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -173,6 +193,8 @@ impl Component for App {
fn update(&mut self, message: Self::Input, sender: ComponentSender<Self>, 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(),
}
}

Expand Down Expand Up @@ -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();
Expand Down
15 changes: 15 additions & 0 deletions src/sketch_board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub enum SketchBoardInput {
#[derive(Debug, Clone)]
pub enum SketchBoardOutput {
ShowToast(String),
ToggleToolbarsDisplay,
}

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -252,6 +253,14 @@ impl SketchBoard {
}
}

// Toolbars = Tools Toolbar + Style Toolbar
fn handle_toggle_toolbars_display(&mut self, sender: ComponentSender<Self>) -> ToolUpdateResult {
sender
.output_sender()
.emit(SketchBoardOutput::ToggleToolbarsDisplay);
ToolUpdateResult::Unmodified
}

fn handle_toolbar_event(
&mut self,
toolbar_event: ToolbarEvent,
Expand Down Expand Up @@ -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,
}
}
}
Expand Down Expand Up @@ -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() {
Expand Down
18 changes: 17 additions & 1 deletion src/ui/toolbars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,21 @@ pub enum ToolbarEvent {
Undo,
SaveFile,
CopyClipboard,
Show,
Hide,
}

#[derive(Debug, Copy, Clone)]
pub enum ToolsToolbarInput {
ToggleDisplay(bool),
}

#[derive(Debug, Copy, Clone)]
pub enum StyleToolbarInput {
ColorButtonSelected(ColorButtons),
ShowColorDialog,
ColorDialogFinished(Option<Color>),
ToggleDisplay(bool),
}

fn create_icon_pixbuf(color: Color) -> Pixbuf {
Expand All @@ -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! {
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit b9553b8

Please sign in to comment.