Skip to content

Commit

Permalink
Merge pull request #144 from pol-rivero/action-on-enter
Browse files Browse the repository at this point in the history
Action (save) on Enter
  • Loading branch information
gabm authored Dec 30, 2024
2 parents b5cc352 + 1a460b1 commit 1aa8b8c
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 10 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -139,6 +141,8 @@ Options:
Configure the command to be called on copy, for example `wl-copy`
--annotation-size-factor <ANNOTATION_SIZE_FACTOR>
Increase or decrease the size of the annotations
--action-on-enter <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
Expand Down
2 changes: 2 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/command_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ pub struct CommandLine {
#[arg(long)]
pub annotation_size_factor: Option<f32>,

/// Action to perform when pressing Enter
#[arg(long)]
pub action_on_enter: Option<Action>,

/// After copying the screenshot, save it to a file as well
#[arg(long)]
pub save_after_copy: bool,
Expand Down Expand Up @@ -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]
Expand Down
31 changes: 30 additions & 1 deletion src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down Expand Up @@ -40,6 +40,7 @@ pub struct Configuration {
initial_tool: Tools,
copy_command: Option<String>,
annotation_size_factor: f32,
action_on_enter: Action,
save_after_copy: bool,
color_palette: ColorPalette,
default_hide_toolbars: bool,
Expand Down Expand Up @@ -95,6 +96,22 @@ impl ColorPalette {
}
}

#[derive(Debug, Clone, Copy, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Action {
SaveToClipboard,
SaveToFile,
}

impl From<CommandLineAction> 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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -331,6 +359,7 @@ struct ConfigurationFileGeneral {
copy_command: Option<String>,
annotation_size_factor: Option<f32>,
output_filename: Option<String>,
action_on_enter: Option<Action>,
save_after_copy: Option<bool>,
default_hide_toolbars: Option<bool>,
primary_highlighter: Option<Highlighters>,
Expand Down
3 changes: 2 additions & 1 deletion src/femtovg_area/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
3 changes: 2 additions & 1 deletion src/femtovg_area/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ use relm4::{
};

use crate::{
configuration::Action,
math::Vec2D,
sketch_board::{Action, SketchBoardInput},
sketch_board::SketchBoardInput,
tools::{CropTool, Drawable, Tool},
};

Expand Down
20 changes: 13 additions & 7 deletions src/sketch_board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -448,6 +442,18 @@ 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)
// 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()
.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()
Expand Down

0 comments on commit 1aa8b8c

Please sign in to comment.