Skip to content

Commit

Permalink
feat: option to disable notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
danielwerg committed Jul 28, 2024
1 parent fad68cc commit 07b15a5
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -126,6 +127,8 @@ Options:
Hide toolbars by default
--primary-highlighter <PRIMARY_HIGHLIGHTER>
The primary highlighter to use, secondary is accessible with CTRL [possible values: block, freehand]
--disable-notifications
Disable notifications
--font-family <FONT_FAMILY>
Font family to use for text annotations
--font-style <FONT_STYLE>
Expand Down
2 changes: 2 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 4 additions & 0 deletions src/command_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ pub struct CommandLine {
/// The primary highlighter to use, secondary is accessible with CTRL.
#[arg(long)]
pub primary_highlighter: Option<Highlighters>,

/// Disable notifications
#[arg(long)]
pub disable_notifications: bool,
}

#[derive(Debug, Clone, Copy, Default, ValueEnum)]
Expand Down
12 changes: 12 additions & 0 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub struct Configuration {
default_hide_toolbars: bool,
font: FontConfiguration,
primary_highlighter: Highlighters,
disable_notifications: bool,
}

#[derive(Default)]
Expand Down Expand Up @@ -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<ConfigurationFile>, command_line: CommandLine) {
// input_filename is required and needs to be overwritten
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -296,6 +306,7 @@ impl Default for Configuration {
default_hide_toolbars: false,
font: FontConfiguration::default(),
primary_highlighter: Highlighters::Block,
disable_notifications: false,
}
}
}
Expand Down Expand Up @@ -340,6 +351,7 @@ struct ConfiguationFileGeneral {
save_after_copy: Option<bool>,
default_hide_toolbars: Option<bool>,
primary_highlighter: Option<Highlighters>,
disable_notifications: Option<bool>
}

#[derive(Deserialize)]
Expand Down
6 changes: 4 additions & 2 deletions src/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions src/sketch_board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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()),
};
}

Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 07b15a5

Please sign in to comment.