Skip to content

Commit

Permalink
refactor state out
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Gabriel committed Feb 23, 2024
1 parent 6da77fb commit 778c658
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 40 deletions.
32 changes: 8 additions & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,24 +134,6 @@ 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 @@ -194,7 +176,14 @@ impl Component for App {
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(),
AppInput::ToggleToolbarsDisplay => {
self.tools_toolbar
.sender()
.emit(ToolsToolbarInput::ToggleVisibility);
self.style_toolbar
.sender()
.emit(StyleToolbarInput::ToggleVisibility);
}
}
}

Expand Down Expand Up @@ -241,11 +230,6 @@ impl Component for App {
.launch(())
.forward(sketch_board.sender(), SketchBoardInput::ToolbarEvent);

if APP_CONFIG.read().default_hide_toolbars() {
tools_toolbar.widget().hide();
style_toolbar.widget().hide();
}

// Model
let model = App {
sketch_board,
Expand Down
4 changes: 0 additions & 4 deletions src/sketch_board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,6 @@ 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
38 changes: 26 additions & 12 deletions src/ui/toolbars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ use relm4::{
prelude::*,
};

pub struct ToolsToolbar {}
pub struct ToolsToolbar {
visible: bool,
}

pub struct StyleToolbar {
custom_color: Color,
custom_color_pixbuf: Pixbuf,
color_action: SimpleAction,
visible: bool,
}

#[derive(Debug, Copy, Clone)]
Expand All @@ -34,21 +37,19 @@ pub enum ToolbarEvent {
Undo,
SaveFile,
CopyClipboard,
Show,
Hide,
}

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

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

fn create_icon_pixbuf(color: Color) -> Pixbuf {
Expand All @@ -75,6 +76,9 @@ impl SimpleComponent for ToolsToolbar {
add_css_class: "toolbar",
add_css_class: "toolbar-top",

#[watch]
set_visible: model.visible,


gtk::Button {
set_focusable: false,
Expand Down Expand Up @@ -191,12 +195,22 @@ impl SimpleComponent for ToolsToolbar {
},
}

fn update(&mut self, message: Self::Input, _sender: ComponentSender<Self>) {
match message {
ToolsToolbarInput::ToggleVisibility => {
self.visible = !self.visible;
}
}
}

fn init(
_: Self::Init,
root: &Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
let model = ToolsToolbar {};
let model = ToolsToolbar {
visible: !APP_CONFIG.read().default_hide_toolbars(),
};
let widgets = view_output!();

// Tools Action for selecting tools
Expand Down Expand Up @@ -290,6 +304,9 @@ impl Component for StyleToolbar {
add_css_class: "toolbar",
add_css_class: "toolbar-bottom",

#[watch]
set_visible: model.visible,

gtk::ToggleButton {
set_focusable: false,
set_hexpand: false,
Expand Down Expand Up @@ -407,12 +424,8 @@ impl Component for StyleToolbar {
.emit(ToolbarEvent::ColorSelected(color));
}

StyleToolbarInput::ToggleDisplay(is_showed) => {
if is_showed {
sender.output_sender().emit(ToolbarEvent::Hide);
} else {
sender.output_sender().emit(ToolbarEvent::Show);
}
StyleToolbarInput::ToggleVisibility => {
self.visible = !self.visible;
}
}
}
Expand Down Expand Up @@ -450,6 +463,7 @@ impl Component for StyleToolbar {
custom_color,
custom_color_pixbuf,
color_action: SimpleAction::from(color_action.clone()),
visible: !APP_CONFIG.read().default_hide_toolbars(),
};

// create widgets
Expand Down

0 comments on commit 778c658

Please sign in to comment.