From 07b15a55a237e8aa2ae825bcf9eab31d09946e2b Mon Sep 17 00:00:00 2001 From: danielwerg <35052399+danielwerg@users.noreply.github.com> Date: Sun, 28 Jul 2024 17:48:45 +0300 Subject: [PATCH 1/2] feat: option to disable notifications --- README.md | 3 +++ config.toml | 2 ++ src/command_line.rs | 4 ++++ src/configuration.rs | 12 ++++++++++++ src/notification.rs | 6 ++++-- src/sketch_board.rs | 8 ++++---- 6 files changed, 29 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index babe48b..21dd1d5 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,7 @@ save-after-copy = false default-hide-toolbars = false # The primary highlighter to use, the other is accessible by holding CTRL at the start of a highlight [possible values: block, freehand] primary-highlighter = "block" +disable-notifications = false # Font to use for text annotations [font] @@ -126,6 +127,8 @@ Options: Hide toolbars by default --primary-highlighter The primary highlighter to use, secondary is accessible with CTRL [possible values: block, freehand] + --disable-notifications + Disable notifications --font-family Font family to use for text annotations --font-style diff --git a/config.toml b/config.toml index d72ad50..cc71daf 100644 --- a/config.toml +++ b/config.toml @@ -17,6 +17,8 @@ save-after-copy = false default-hide-toolbars = false # The primary highlighter to use, the other is accessible by holding CTRL at the start of a highlight [possible values: block, freehand] primary-highlighter = "block" +# Disable notifications +disable-notifications = false # Font to use for text annotations [font] diff --git a/src/command_line.rs b/src/command_line.rs index fe5c868..c7b505f 100644 --- a/src/command_line.rs +++ b/src/command_line.rs @@ -55,6 +55,10 @@ pub struct CommandLine { /// The primary highlighter to use, secondary is accessible with CTRL. #[arg(long)] pub primary_highlighter: Option, + + /// Disable notifications + #[arg(long)] + pub disable_notifications: bool, } #[derive(Debug, Clone, Copy, Default, ValueEnum)] diff --git a/src/configuration.rs b/src/configuration.rs index ddd87b5..935fc15 100644 --- a/src/configuration.rs +++ b/src/configuration.rs @@ -44,6 +44,7 @@ pub struct Configuration { default_hide_toolbars: bool, font: FontConfiguration, primary_highlighter: Highlighters, + disable_notifications: bool, } #[derive(Default)] @@ -178,6 +179,9 @@ impl Configuration { if let Some(v) = general.primary_highlighter { self.primary_highlighter = v; } + if let Some(v) = general.disable_notifications { + self.disable_notifications = v; + } } fn merge(&mut self, file: Option, command_line: CommandLine) { // input_filename is required and needs to be overwritten @@ -231,6 +235,9 @@ impl Configuration { if let Some(v) = command_line.primary_highlighter { self.primary_highlighter = v.into(); } + if command_line.disable_notifications { + self.disable_notifications = command_line.disable_notifications; + } } pub fn early_exit(&self) -> bool { @@ -276,6 +283,9 @@ impl Configuration { pub fn primary_highlighter(&self) -> Highlighters { self.primary_highlighter } + pub fn disable_notifications(&self) -> bool { + self.disable_notifications + } pub fn font(&self) -> &FontConfiguration { &self.font } @@ -296,6 +306,7 @@ impl Default for Configuration { default_hide_toolbars: false, font: FontConfiguration::default(), primary_highlighter: Highlighters::Block, + disable_notifications: false, } } } @@ -340,6 +351,7 @@ struct ConfiguationFileGeneral { save_after_copy: Option, default_hide_toolbars: Option, primary_highlighter: Option, + disable_notifications: Option } #[derive(Deserialize)] diff --git a/src/notification.rs b/src/notification.rs index 2e463b6..3ab9771 100644 --- a/src/notification.rs +++ b/src/notification.rs @@ -3,9 +3,11 @@ use relm4::gtk::gio::{prelude::ApplicationExt, Notification}; use relm4::gtk::{IconLookupFlags, IconTheme, TextDirection}; -pub fn log_result(msg: &str) { +pub fn log_result(msg: &str, notify: bool) { println!("{}", msg); - show_notification(msg); + if notify { + show_notification(msg); + } } fn show_notification(msg: &str) { diff --git a/src/sketch_board.rs b/src/sketch_board.rs index 224695d..9f3a070 100644 --- a/src/sketch_board.rs +++ b/src/sketch_board.rs @@ -191,7 +191,7 @@ impl SketchBoard { // TODO: we could support more data types if !output_filename.ends_with(".png") { - log_result("The only supported format is png, but the filename does not end in png"); + log_result("The only supported format is png, but the filename does not end in png", !APP_CONFIG.read().disable_notifications()); return; } @@ -204,8 +204,8 @@ impl SketchBoard { }; match fs::write(&output_filename, data) { - Err(e) => log_result(&format!("Error while saving file: {e}")), - Ok(_) => log_result(&format!("File saved to '{}'.", &output_filename)), + Err(e) => log_result(&format!("Error while saving file: {e}"), !APP_CONFIG.read().disable_notifications()), + Ok(_) => log_result(&format!("File saved to '{}'.", &output_filename), !APP_CONFIG.read().disable_notifications()), }; } @@ -250,7 +250,7 @@ impl SketchBoard { match result { Err(e) => println!("Error saving {e}"), Ok(()) => { - log_result("Copied to clipboard."); + log_result("Copied to clipboard.", !APP_CONFIG.read().disable_notifications()); // TODO: rethink order and messaging patterns if APP_CONFIG.read().save_after_copy() { From cae159bcceeefa5f01055b819068edba50d245ef Mon Sep 17 00:00:00 2001 From: danielwerg <35052399+danielwerg@users.noreply.github.com> Date: Sun, 28 Jul 2024 18:26:15 +0300 Subject: [PATCH 2/2] style: lint --- src/configuration.rs | 2 +- src/sketch_board.rs | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/configuration.rs b/src/configuration.rs index 935fc15..eabb533 100644 --- a/src/configuration.rs +++ b/src/configuration.rs @@ -351,7 +351,7 @@ struct ConfiguationFileGeneral { save_after_copy: Option, default_hide_toolbars: Option, primary_highlighter: Option, - disable_notifications: Option + disable_notifications: Option, } #[derive(Deserialize)] diff --git a/src/sketch_board.rs b/src/sketch_board.rs index 9f3a070..d68996a 100644 --- a/src/sketch_board.rs +++ b/src/sketch_board.rs @@ -191,7 +191,10 @@ impl SketchBoard { // TODO: we could support more data types if !output_filename.ends_with(".png") { - log_result("The only supported format is png, but the filename does not end in png", !APP_CONFIG.read().disable_notifications()); + log_result( + "The only supported format is png, but the filename does not end in png", + !APP_CONFIG.read().disable_notifications(), + ); return; } @@ -204,8 +207,14 @@ impl SketchBoard { }; match fs::write(&output_filename, data) { - Err(e) => log_result(&format!("Error while saving file: {e}"), !APP_CONFIG.read().disable_notifications()), - Ok(_) => log_result(&format!("File saved to '{}'.", &output_filename), !APP_CONFIG.read().disable_notifications()), + Err(e) => log_result( + &format!("Error while saving file: {e}"), + !APP_CONFIG.read().disable_notifications(), + ), + Ok(_) => log_result( + &format!("File saved to '{}'.", &output_filename), + !APP_CONFIG.read().disable_notifications(), + ), }; } @@ -250,7 +259,10 @@ impl SketchBoard { match result { Err(e) => println!("Error saving {e}"), Ok(()) => { - log_result("Copied to clipboard.", !APP_CONFIG.read().disable_notifications()); + log_result( + "Copied to clipboard.", + !APP_CONFIG.read().disable_notifications(), + ); // TODO: rethink order and messaging patterns if APP_CONFIG.read().save_after_copy() {