Skip to content

Commit

Permalink
Merge pull request #45 from mistgc/toggle-toolbars
Browse files Browse the repository at this point in the history
feat: add support for toggling visibility of toolbars
  • Loading branch information
gabm authored Feb 23, 2024
2 parents 06061c2 + 778c658 commit 2283b12
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 7 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ annotation-size-factor = 2
output-filename = "/tmp/test-%Y-%m-%d_%H:%M:%S.png"
# After copying the screenshot, save it to a file as well
save-after-copy = false
# Hide toolbars by default
default-hide-toolbars = false

# custom colours for the colour palette
[color-palette]
Expand Down Expand Up @@ -98,7 +100,7 @@ Options:
--fullscreen
Start Satty in fullscreen mode
--output-filename <OUTPUT_FILENAME>
Filename to use for saving action. Omit to disable saving to file. Might contain format specifiers: https://docs.rs/chrono/latest/chrono/format/strftime/index.html
Filename to use for saving action. Omit to disable saving to file. Might contain format specifiers: <https://docs.rs/chrono/latest/chrono/format/strftime/index.html>
--early-exit
Exit directly after copy/save action
--initial-tool <TOOL>
Expand All @@ -109,6 +111,8 @@ Options:
Increase or decrease the size of the annotations
--save-after-copy
After copying the screenshot, save it to a file as well
-d, --default-hide-toolbars
Hide toolbars by default
-h, --help
Print help
-V, --version
Expand Down
2 changes: 2 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ annotation-size-factor = 2
output-filename = "/tmp/test-%Y-%m-%d_%H:%M:%S.png"
# After copying the screenshot, save it to a file as well
save-after-copy = false
# Hide toolbars by default
dafault-hide-toolbars = false

# custom colours for the colour palette
[color-palette]
Expand Down
4 changes: 4 additions & 0 deletions src/command_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ pub struct CommandLine {
/// After copying the screenshot, save it to a file as well
#[arg(long)]
pub save_after_copy: bool,

/// Hide toolbars by default
#[arg(short, long)]
pub default_hide_toolbars: bool,
}

#[derive(Debug, Clone, Copy, Default, ValueEnum)]
Expand Down
13 changes: 13 additions & 0 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub struct Configuration {
annotation_size_factor: f64,
save_after_copy: bool,
color_palette: ColorPalette,
default_hide_toolbars: bool,
}

pub struct ColorPalette {
Expand Down Expand Up @@ -142,6 +143,9 @@ impl Configuration {
if let Some(v) = general.save_after_copy {
self.save_after_copy = v;
}
if let Some(v) = general.default_hide_toolbars {
self.default_hide_toolbars = v;
}
}
fn merge(&mut self, file: Option<ConfigurationFile>, command_line: CommandLine) {
// input_filename is required and needs to be overwritten
Expand All @@ -164,6 +168,9 @@ impl Configuration {
if command_line.early_exit {
self.early_exit = command_line.early_exit;
}
if command_line.default_hide_toolbars {
self.default_hide_toolbars = command_line.default_hide_toolbars;
}
if let Some(v) = command_line.initial_tool {
self.initial_tool = v.into();
}
Expand Down Expand Up @@ -216,6 +223,10 @@ impl Configuration {
pub fn color_palette(&self) -> &ColorPalette {
&self.color_palette
}

pub fn default_hide_toolbars(&self) -> bool {
self.default_hide_toolbars
}
}

impl Default for Configuration {
Expand All @@ -230,6 +241,7 @@ impl Default for Configuration {
annotation_size_factor: 1.0f64,
save_after_copy: false,
color_palette: ColorPalette::default(),
default_hide_toolbars: false,
}
}
}
Expand Down Expand Up @@ -264,6 +276,7 @@ struct ConfiguationFileGeneral {
annotation_size_factor: Option<f64>,
output_filename: Option<String>,
save_after_copy: Option<bool>,
default_hide_toolbars: Option<bool>,
}

#[derive(Deserialize)]
Expand Down
18 changes: 15 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 @@ -173,6 +175,15 @@ 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.tools_toolbar
.sender()
.emit(ToolsToolbarInput::ToggleVisibility);
self.style_toolbar
.sender()
.emit(StyleToolbarInput::ToggleVisibility);
}
}
}

Expand Down Expand Up @@ -203,8 +214,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
14 changes: 14 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 @@ -262,6 +263,17 @@ 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 @@ -405,6 +417,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
36 changes: 33 additions & 3 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 @@ -36,11 +39,17 @@ pub enum ToolbarEvent {
CopyClipboard,
}

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

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

fn create_icon_pixbuf(color: Color) -> Pixbuf {
Expand All @@ -55,7 +64,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 All @@ -67,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 @@ -183,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 @@ -282,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 @@ -398,6 +423,10 @@ impl Component for StyleToolbar {
.output_sender()
.emit(ToolbarEvent::ColorSelected(color));
}

StyleToolbarInput::ToggleVisibility => {
self.visible = !self.visible;
}
}
}
fn init(
Expand Down Expand Up @@ -434,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 2283b12

Please sign in to comment.