From ba002a13cd13ee77bb386610c19395ebc75aac99 Mon Sep 17 00:00:00 2001 From: Pol Rivero <65060696+pol-rivero@users.noreply.github.com> Date: Sat, 28 Dec 2024 18:34:22 +0100 Subject: [PATCH 1/4] Implement Action on Enter --- src/command_line.rs | 11 +++++++++++ src/configuration.rs | 31 ++++++++++++++++++++++++++++++- src/femtovg_area/imp.rs | 3 ++- src/femtovg_area/mod.rs | 3 ++- src/sketch_board.rs | 15 ++++++++------- 5 files changed, 53 insertions(+), 10 deletions(-) diff --git a/src/command_line.rs b/src/command_line.rs index 7ad36a1..3fafa11 100644 --- a/src/command_line.rs +++ b/src/command_line.rs @@ -41,6 +41,10 @@ pub struct CommandLine { #[arg(long)] pub annotation_size_factor: Option, + /// Action to perform when pressing Enter + #[arg(long)] + pub action_on_enter: Option, + /// After copying the screenshot, save it to a file as well #[arg(long)] pub save_after_copy: bool, @@ -82,6 +86,13 @@ pub enum Tools { Brush, } +#[derive(Debug, Clone, Copy, Default, ValueEnum)] +pub enum Action { + #[default] + SaveToClipboard, + SaveToFile, +} + #[derive(Debug, Clone, Copy, Default, ValueEnum)] pub enum Highlighters { #[default] diff --git a/src/configuration.rs b/src/configuration.rs index 682f5f1..adc206a 100644 --- a/src/configuration.rs +++ b/src/configuration.rs @@ -12,7 +12,7 @@ use thiserror::Error; use xdg::{BaseDirectories, BaseDirectoriesError}; use crate::{ - command_line::CommandLine, + command_line::{Action as CommandLineAction, CommandLine}, style::Color, tools::{Highlighters, Tools}, }; @@ -40,6 +40,7 @@ pub struct Configuration { initial_tool: Tools, copy_command: Option, annotation_size_factor: f32, + action_on_enter: Action, save_after_copy: bool, color_palette: ColorPalette, default_hide_toolbars: bool, @@ -95,6 +96,22 @@ impl ColorPalette { } } +#[derive(Debug, Clone, Copy, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum Action { + SaveToClipboard, + SaveToFile, +} + +impl From for Action { + fn from(action: CommandLineAction) -> Self { + match action { + CommandLineAction::SaveToClipboard => Self::SaveToClipboard, + CommandLineAction::SaveToFile => Self::SaveToFile, + } + } +} + impl Configuration { pub fn load() { // parse commandline options and exit if error @@ -142,6 +159,9 @@ impl Configuration { if let Some(v) = general.annotation_size_factor { self.annotation_size_factor = v; } + if let Some(v) = general.action_on_enter { + self.action_on_enter = v; + } if let Some(v) = general.save_after_copy { self.save_after_copy = v; } @@ -197,6 +217,9 @@ impl Configuration { if let Some(v) = command_line.annotation_size_factor { self.annotation_size_factor = v; } + if let Some(v) = command_line.action_on_enter { + self.action_on_enter = v.into(); + } if command_line.save_after_copy { self.save_after_copy = command_line.save_after_copy; } @@ -247,6 +270,10 @@ impl Configuration { self.annotation_size_factor } + pub fn action_on_enter(&self) -> Action { + self.action_on_enter + } + pub fn save_after_copy(&self) -> bool { self.save_after_copy } @@ -281,6 +308,7 @@ impl Default for Configuration { initial_tool: Tools::Pointer, copy_command: None, annotation_size_factor: 1.0, + action_on_enter: Action::SaveToClipboard, save_after_copy: false, color_palette: ColorPalette::default(), default_hide_toolbars: false, @@ -331,6 +359,7 @@ struct ConfigurationFileGeneral { copy_command: Option, annotation_size_factor: Option, output_filename: Option, + action_on_enter: Option, save_after_copy: Option, default_hide_toolbars: Option, primary_highlighter: Option, diff --git a/src/femtovg_area/imp.rs b/src/femtovg_area/imp.rs index 7ca45d6..f37ffd2 100644 --- a/src/femtovg_area/imp.rs +++ b/src/femtovg_area/imp.rs @@ -19,8 +19,9 @@ use relm4::{gtk, Sender}; use resource::resource; use crate::{ + configuration::Action, math::Vec2D, - sketch_board::{Action, SketchBoardInput}, + sketch_board::SketchBoardInput, tools::{CropTool, Drawable, Tool}, APP_CONFIG, }; diff --git a/src/femtovg_area/mod.rs b/src/femtovg_area/mod.rs index e9b543a..a6d8533 100644 --- a/src/femtovg_area/mod.rs +++ b/src/femtovg_area/mod.rs @@ -10,8 +10,9 @@ use relm4::{ }; use crate::{ + configuration::Action, math::Vec2D, - sketch_board::{Action, SketchBoardInput}, + sketch_board::SketchBoardInput, tools::{CropTool, Drawable, Tool}, }; diff --git a/src/sketch_board.rs b/src/sketch_board.rs index 9ab8e9c..21aa6a9 100644 --- a/src/sketch_board.rs +++ b/src/sketch_board.rs @@ -15,7 +15,7 @@ use gtk::prelude::*; use relm4::gtk::gdk::{DisplayManager, Key, ModifierType, Texture}; use relm4::{gtk, Component, ComponentParts, ComponentSender}; -use crate::configuration::APP_CONFIG; +use crate::configuration::{Action, APP_CONFIG}; use crate::femtovg_area::FemtoVGArea; use crate::math::Vec2D; use crate::notification::log_result; @@ -32,12 +32,6 @@ pub enum SketchBoardInput { RenderResult(RenderedImage, Action), } -#[derive(Debug, Clone, Copy)] -pub enum Action { - SaveToClipboard, - SaveToFile, -} - #[derive(Debug, Clone)] pub enum SketchBoardOutput { ToggleToolbarsDisplay, @@ -448,6 +442,13 @@ impl Component for SketchBoard { relm4::main_application().quit(); // this is only here to make rust happy. The application should exit with the previous call ToolUpdateResult::Unmodified + } else if ke.key == Key::Return || ke.key == Key::KP_Enter { + // First, let the tool handle the event. If the tool does nothing, we can do our thing (otherwise require a second Enter) + let result: ToolUpdateResult = self.active_tool.borrow_mut().handle_event(ToolEvent::Input(ie)); + if let ToolUpdateResult::Unmodified = result { + self.renderer.request_render(APP_CONFIG.read().action_on_enter()); + } + result } else { self.active_tool .borrow_mut() From 3ae3f3f5c593cc056e98066d1409fccf8915f90c Mon Sep 17 00:00:00 2001 From: Pol Rivero <65060696+pol-rivero@users.noreply.github.com> Date: Sun, 29 Dec 2024 09:43:08 +0100 Subject: [PATCH 2/4] Update documentation (action-on-enter) --- README.md | 4 ++++ config.toml | 2 ++ 2 files changed, 6 insertions(+) diff --git a/README.md b/README.md index 35abb62..841e9b8 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,8 @@ copy-command = "wl-copy" annotation-size-factor = 2 # 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 output-filename = "/tmp/test-%Y-%m-%d_%H:%M:%S.png" +# Action to perform when the Enter key is pressed [possible values: save-to-clipboard, save-to-file] +action-on-enter = "save-to-clipboard" # After copying the screenshot, save it to a file as well save-after-copy = false # Hide toolbars by default @@ -139,6 +141,8 @@ Options: Configure the command to be called on copy, for example `wl-copy` --annotation-size-factor Increase or decrease the size of the annotations + --action-on-enter + Action to perform when pressing Enter [possible values: save-to-clipboard, save-to-file] --save-after-copy After copying the screenshot, save it to a file as well -d, --default-hide-toolbars diff --git a/config.toml b/config.toml index cc71daf..e51a9fb 100644 --- a/config.toml +++ b/config.toml @@ -11,6 +11,8 @@ copy-command = "wl-copy" annotation-size-factor = 2 # 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 output-filename = "/tmp/test-%Y-%m-%d_%H:%M:%S.png" +# Action to perform when the Enter key is pressed [possible values: save-to-clipboard, save-to-file] +action-on-enter = "save-to-clipboard" # After copying the screenshot, save it to a file as well save-after-copy = false # Hide toolbars by default From a2dd840ee2e9bf0c36f4692b757e60aa8a4f4451 Mon Sep 17 00:00:00 2001 From: Pol Rivero <65060696+pol-rivero@users.noreply.github.com> Date: Sun, 29 Dec 2024 14:18:34 +0100 Subject: [PATCH 3/4] Run cargo fmt --- src/sketch_board.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/sketch_board.rs b/src/sketch_board.rs index 21aa6a9..8a9e0f4 100644 --- a/src/sketch_board.rs +++ b/src/sketch_board.rs @@ -444,9 +444,13 @@ impl Component for SketchBoard { ToolUpdateResult::Unmodified } else if ke.key == Key::Return || ke.key == Key::KP_Enter { // First, let the tool handle the event. If the tool does nothing, we can do our thing (otherwise require a second Enter) - let result: ToolUpdateResult = self.active_tool.borrow_mut().handle_event(ToolEvent::Input(ie)); + let result: ToolUpdateResult = self + .active_tool + .borrow_mut() + .handle_event(ToolEvent::Input(ie)); if let ToolUpdateResult::Unmodified = result { - self.renderer.request_render(APP_CONFIG.read().action_on_enter()); + self.renderer + .request_render(APP_CONFIG.read().action_on_enter()); } result } else { From 1a460b17835dd18840c0c2cee989ff93892cb35b Mon Sep 17 00:00:00 2001 From: Pol Rivero <65060696+pol-rivero@users.noreply.github.com> Date: Mon, 30 Dec 2024 07:35:24 +0100 Subject: [PATCH 4/4] Warn about using Unmodified to check if tool handled event --- src/sketch_board.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/sketch_board.rs b/src/sketch_board.rs index 8a9e0f4..0d441e6 100644 --- a/src/sketch_board.rs +++ b/src/sketch_board.rs @@ -444,6 +444,7 @@ impl Component for SketchBoard { ToolUpdateResult::Unmodified } else if ke.key == Key::Return || ke.key == Key::KP_Enter { // First, let the tool handle the event. If the tool does nothing, we can do our thing (otherwise require a second Enter) + // Relying on ToolUpdateResult::Unmodified is probably not a good idea, but it's the only way at the moment. See discussion in #144 let result: ToolUpdateResult = self .active_tool .borrow_mut()