diff --git a/src/cmus/events.rs b/src/cmus/events.rs index 622d381..c896582 100644 --- a/src/cmus/events.rs +++ b/src/cmus/events.rs @@ -22,34 +22,35 @@ impl CmusEvent { settings: &Settings, ) -> Action { use CmusEvent::*; - let (body_template, summary_template, track, player_settings) = match self { + let (body_template, summary_template, timeout, track, player_settings) = match self { StatusChanged(track, player_settings) => - (settings.status_notification_body(), settings.status_notification_summary(), track, player_settings), + (settings.status_notification_body(), settings.status_notification_summary(), settings.status_notification_timeout(), track, player_settings), TrackChanged(track, player_settings) => - (settings.body(), settings.summary(), track, player_settings), + (settings.body(), settings.summary(), settings.timeout(), track, player_settings), VolumeChanged(track, player_settings) if settings.show_player_notifications => - (settings.volume_notification_body(), settings.volume_notification_summary(), track, player_settings), + (settings.volume_notification_body(), settings.volume_notification_summary(), settings.volume_notification_timeout(), track, player_settings), ShuffleChanged(track, player_settings) if settings.show_player_notifications => - (settings.shuffle_notification_body(), settings.shuffle_notification_summary(), track, player_settings), + (settings.shuffle_notification_body(), settings.shuffle_notification_summary(), settings.shuffle_notification_timeout(), track, player_settings), RepeatChanged(track, player_settings) if settings.show_player_notifications => - (settings.repeat_notification_body(), settings.repeat_notification_summary(), track, player_settings), + (settings.repeat_notification_body(), settings.repeat_notification_summary(), settings.repeat_notification_timeout(), track, player_settings), AAAModeChanged(track, player_settings) if settings.show_player_notifications => - (settings.aaa_mode_notification_body(), settings.aaa_mode_notification_summary(), track, player_settings), + (settings.aaa_mode_notification_body(), settings.aaa_mode_notification_summary(), settings.aaa_mode_notification_timeout(), track, player_settings), _ => { return Action::None }, }; Action::Show { - notification_body: process_template_placeholders( + body: process_template_placeholders( body_template, track, player_settings, ), - notification_summary: process_template_placeholders( + summary: process_template_placeholders( summary_template, track, player_settings, ), - save: false + timeout: timeout * 1000 , + save: false, } } } diff --git a/src/notification.rs b/src/notification.rs index 46d5e24..b1afb45 100644 --- a/src/notification.rs +++ b/src/notification.rs @@ -11,8 +11,9 @@ use crate::settings::Settings; pub enum Action { Show { - notification_body: String, - notification_summary: String, + body: String, + summary: String, + timeout: i32, save: bool, }, None, @@ -42,12 +43,11 @@ impl NotificationsHandler { response: &CmusQueryResponse, ) -> Result<(), notify_rust::error::Error> { for event in events { - self.setup_notification_timeout(&event); #[cfg(feature = "debug")] info!("event: {:?}", event); match event.build_notification(&self.settings) { - Action::Show { notification_body, notification_summary, save } => { + Action::Show { body, summary, timeout, save } => { // Setup the notification cover if self.settings.show_track_cover { self.update_cover(&event, response); @@ -58,7 +58,7 @@ impl NotificationsHandler { self.cover_set = true; } - self.notification.summary(¬ification_summary).body(¬ification_body); + self.notification.timeout(timeout).summary(&summary).body(&body); // Show the notification let handle = self.notification.show()?; @@ -128,21 +128,4 @@ impl NotificationsHandler { .hint(notify_rust::Hint::DesktopEntry("cmus.desktop".to_string())) .hint(notify_rust::Hint::Resident(true)); } - - #[inline(always)] - fn setup_notification_timeout(&mut self, event: &CmusEvent) { - use CmusEvent::*; - self.notification.timeout( - match event { - TrackChanged(_, _) => self.settings.timeout(), - StatusChanged(_, _) => self.settings.status_notification_timeout(), - AAAModeChanged(_, _) => self.settings.aaa_mode_notification_timeout(), - VolumeChanged(_, _) => self.settings.volume_notification_timeout(), - RepeatChanged(_, _) => self.settings.repeat_notification_timeout(), - ShuffleChanged(_, _) => self.settings.shuffle_notification_timeout(), - _ => self.settings.timeout(), - } as i32 - * 1000, - ); - } } diff --git a/src/settings.rs b/src/settings.rs index 02bb989..7e333d0 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -3,7 +3,7 @@ use clap::Parser; use log::{debug, info}; use serde::{Deserialize, Serialize}; -const NOTIFICATION_TIMEOUT: u8 = 5; +const NOTIFICATION_TIMEOUT: i32 = 5; const NOTIFICATION_BODY: &str = "album: {album} \n Artist: {artist} - {date}"; const NOTIFICATION_SUMMARY: &str = "{title}"; @@ -13,19 +13,19 @@ const DEFAULT_MAX_DEPTH: u8 = 3; const DEFAULT_INTERVAL_TIME: u64 = 1000; // 1000 ms const DEFAULT_STATUS_CHANGE_NOTIFICATION_BODY: &str = "{status}"; const DEFAULT_STATUS_CHANGE_NOTIFICATION_SUMMARY: &str = "{title}"; -const DEFAULT_STATUS_CHANGE_NOTIFICATION_TIMEOUT: u8 = 1; +const DEFAULT_STATUS_CHANGE_NOTIFICATION_TIMEOUT: i32 = 1; const DEFAULT_VOLUME_CHANGE_NOTIFICATION_BODY: &str = "Volume changed to {volume}%"; const DEFAULT_VOLUME_CHANGE_NOTIFICATION_SUMMARY: &str = "{title}"; -const DEFAULT_VOLUME_CHANGE_NOTIFICATION_TIMEOUT: u8 = 1; +const DEFAULT_VOLUME_CHANGE_NOTIFICATION_TIMEOUT: i32 = 1; const DEFAULT_SHUFFLE_NOTIFICATION_BODY: &str = "Shuffle mode changed to {shuffle}"; const DEFAULT_SHUFFLE_NOTIFICATION_SUMMARY: &str = "{title}"; -const DEFAULT_SHUFFLE_NOTIFICATION_TIMEOUT: u8 = 1; +const DEFAULT_SHUFFLE_NOTIFICATION_TIMEOUT: i32 = 1; const DEFAULT_REPEAT_NOTIFICATION_BODY: &str = "Repeat mode changed to {repeat}"; const DEFAULT_REPEAT_NOTIFICATION_SUMMARY: &str = "{title}"; -const DEFAULT_REPEAT_NOTIFICATION_TIMEOUT: u8 = 1; +const DEFAULT_REPEAT_NOTIFICATION_TIMEOUT: i32 = 1; const DEFAULT_AAAMODE_NOTIFICATION_BODY: &str = "AAA mode changed to {aaa_mode}"; const DEFAULT_AAAMODE_NOTIFICATION_SUMMARY: &str = "{title}"; -const DEFAULT_AAAMODE_NOTIFICATION_TIMEOUT: u8 = 1; +const DEFAULT_AAAMODE_NOTIFICATION_TIMEOUT: i32 = 1; #[cfg(feature = "lyrics")] const DEFAULT_LYRICS_NOTIFICATION_BODY: &str = "{lyrics}"; #[cfg(feature = "lyrics")] @@ -37,7 +37,7 @@ const DEFAULT_LYRICS_NOTIFICATION_SUMMARY: &str = "Lyrics"; pub struct Settings { /// The notification timeout, in seconds #[arg(short, long)] - timeout: Option, + timeout: Option, /// Make the notification persistent, i.e. not disappear after a timeout (you can dismiss it manually) #[arg(short, long)] pub persistent: bool, @@ -185,7 +185,7 @@ pub struct Settings { volume_notification_summary: Option, /// The time out of the volume change notification, in seconds. #[arg(short = 'T', long)] - volume_notification_timeout: Option, + volume_notification_timeout: Option, /// The shuffle mode change notification body. /// you can use the placeholders like "{shuffle}" in the body, it will be replaced with the shuffle mode. /// @@ -198,7 +198,7 @@ pub struct Settings { shuffle_notification_summary: Option, /// The time out of the shuffle mode change notification, in seconds. #[arg(short = 'Y', long)] - shuffle_notification_timeout: Option, + shuffle_notification_timeout: Option, /// The repeat mode change notification body. /// you can use the placeholders like "{repeat}" in the body, it will be replaced with the repeat mode. /// @@ -211,7 +211,7 @@ pub struct Settings { repeat_notification_summary: Option, /// The time out of the repeat mode change notification, in seconds. #[arg(short = 'H', long)] - repeat_notification_timeout: Option, + repeat_notification_timeout: Option, /// The aaa mode change notification body. /// you can use the placeholders like "{aaa_mode}" in the body, it will be replaced with the aaa mode. /// @@ -224,7 +224,7 @@ pub struct Settings { aaa_mode_notification_summary: Option, /// The time out of the aaa mode change notification, in seconds. #[arg(short = 'F', long)] - aaa_mode_notification_timeout: Option, + aaa_mode_notification_timeout: Option, #[cfg(feature = "lyrics")] /// The lyrics notification body, if you want to show the lyrics separate notification. /// you can use the placeholders like "{lyrics}" in the body, it will be replaced with the lyrics. @@ -249,7 +249,7 @@ pub struct Settings { status_notification_summary: Option, /// The time out of the status change notification, in seconds. #[arg(short = 'Q', long)] - status_notification_timeout: Option, + status_notification_timeout: Option, #[cfg(feature = "docs")] #[arg(long, hide = true)] #[serde(skip)] @@ -443,7 +443,7 @@ impl Settings { } #[inline(always)] - pub fn timeout(&self) -> u8 { + pub fn timeout(&self) -> i32 { self.timeout.unwrap_or(NOTIFICATION_TIMEOUT) } @@ -490,7 +490,7 @@ impl Settings { } #[inline(always)] - pub fn status_notification_timeout(&self) -> u8 { + pub fn status_notification_timeout(&self) -> i32 { self.status_notification_timeout .unwrap_or(DEFAULT_STATUS_CHANGE_NOTIFICATION_TIMEOUT) } @@ -528,7 +528,7 @@ impl Settings { } #[inline(always)] - pub fn volume_notification_timeout(&self) -> u8 { + pub fn volume_notification_timeout(&self) -> i32 { self.volume_notification_timeout .unwrap_or(DEFAULT_VOLUME_CHANGE_NOTIFICATION_TIMEOUT) } @@ -550,7 +550,7 @@ impl Settings { } #[inline(always)] - pub fn shuffle_notification_timeout(&self) -> u8 { + pub fn shuffle_notification_timeout(&self) -> i32 { self.shuffle_notification_timeout .unwrap_or(DEFAULT_SHUFFLE_NOTIFICATION_TIMEOUT) } @@ -572,7 +572,7 @@ impl Settings { } #[inline(always)] - pub fn repeat_notification_timeout(&self) -> u8 { + pub fn repeat_notification_timeout(&self) -> i32 { self.repeat_notification_timeout .unwrap_or(DEFAULT_REPEAT_NOTIFICATION_TIMEOUT) } @@ -594,7 +594,7 @@ impl Settings { } #[inline(always)] - pub fn aaa_mode_notification_timeout(&self) -> u8 { + pub fn aaa_mode_notification_timeout(&self) -> i32 { self.aaa_mode_notification_timeout .unwrap_or(DEFAULT_AAAMODE_NOTIFICATION_TIMEOUT) }