[update/improve] Improve the timeout selection mecarthm

This commit is contained in:
Anas Elgarhy 2023-04-22 09:44:46 +02:00
parent 91878512ca
commit 44bb297e78
No known key found for this signature in database
GPG key ID: 0501802A1D496528
3 changed files with 34 additions and 50 deletions

View file

@ -22,34 +22,35 @@ impl CmusEvent {
settings: &Settings, settings: &Settings,
) -> Action { ) -> Action {
use CmusEvent::*; 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) => 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) => 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 => 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 => 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 => 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 => 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 }, _ => { return Action::None },
}; };
Action::Show { Action::Show {
notification_body: process_template_placeholders( body: process_template_placeholders(
body_template, body_template,
track, track,
player_settings, player_settings,
), ),
notification_summary: process_template_placeholders( summary: process_template_placeholders(
summary_template, summary_template,
track, track,
player_settings, player_settings,
), ),
save: false timeout: timeout * 1000 ,
save: false,
} }
} }
} }

View file

@ -11,8 +11,9 @@ use crate::settings::Settings;
pub enum Action { pub enum Action {
Show { Show {
notification_body: String, body: String,
notification_summary: String, summary: String,
timeout: i32,
save: bool, save: bool,
}, },
None, None,
@ -42,12 +43,11 @@ impl NotificationsHandler {
response: &CmusQueryResponse, response: &CmusQueryResponse,
) -> Result<(), notify_rust::error::Error> { ) -> Result<(), notify_rust::error::Error> {
for event in events { for event in events {
self.setup_notification_timeout(&event);
#[cfg(feature = "debug")] #[cfg(feature = "debug")]
info!("event: {:?}", event); info!("event: {:?}", event);
match event.build_notification(&self.settings) { match event.build_notification(&self.settings) {
Action::Show { notification_body, notification_summary, save } => { Action::Show { body, summary, timeout, save } => {
// Setup the notification cover // Setup the notification cover
if self.settings.show_track_cover { if self.settings.show_track_cover {
self.update_cover(&event, response); self.update_cover(&event, response);
@ -58,7 +58,7 @@ impl NotificationsHandler {
self.cover_set = true; self.cover_set = true;
} }
self.notification.summary(&notification_summary).body(&notification_body); self.notification.timeout(timeout).summary(&summary).body(&body);
// Show the notification // Show the notification
let handle = self.notification.show()?; let handle = self.notification.show()?;
@ -128,21 +128,4 @@ impl NotificationsHandler {
.hint(notify_rust::Hint::DesktopEntry("cmus.desktop".to_string())) .hint(notify_rust::Hint::DesktopEntry("cmus.desktop".to_string()))
.hint(notify_rust::Hint::Resident(true)); .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,
);
}
} }

View file

@ -3,7 +3,7 @@ use clap::Parser;
use log::{debug, info}; use log::{debug, info};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
const NOTIFICATION_TIMEOUT: u8 = 5; const NOTIFICATION_TIMEOUT: i32 = 5;
const NOTIFICATION_BODY: &str = const NOTIFICATION_BODY: &str =
"<b>album:</b> {album} \n <b>Artist:</b> {artist} - {date}"; "<b>album:</b> {album} \n <b>Artist:</b> {artist} - {date}";
const NOTIFICATION_SUMMARY: &str = "{title}"; 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_INTERVAL_TIME: u64 = 1000; // 1000 ms
const DEFAULT_STATUS_CHANGE_NOTIFICATION_BODY: &str = "<b>{status}</b>"; const DEFAULT_STATUS_CHANGE_NOTIFICATION_BODY: &str = "<b>{status}</b>";
const DEFAULT_STATUS_CHANGE_NOTIFICATION_SUMMARY: &str = "{title}"; 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_BODY: &str = "Volume changed to {volume}%";
const DEFAULT_VOLUME_CHANGE_NOTIFICATION_SUMMARY: &str = "{title}"; 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_BODY: &str = "Shuffle mode changed to {shuffle}";
const DEFAULT_SHUFFLE_NOTIFICATION_SUMMARY: &str = "{title}"; 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_BODY: &str = "Repeat mode changed to {repeat}";
const DEFAULT_REPEAT_NOTIFICATION_SUMMARY: &str = "{title}"; 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_BODY: &str = "AAA mode changed to {aaa_mode}";
const DEFAULT_AAAMODE_NOTIFICATION_SUMMARY: &str = "{title}"; const DEFAULT_AAAMODE_NOTIFICATION_SUMMARY: &str = "{title}";
const DEFAULT_AAAMODE_NOTIFICATION_TIMEOUT: u8 = 1; const DEFAULT_AAAMODE_NOTIFICATION_TIMEOUT: i32 = 1;
#[cfg(feature = "lyrics")] #[cfg(feature = "lyrics")]
const DEFAULT_LYRICS_NOTIFICATION_BODY: &str = "{lyrics}"; const DEFAULT_LYRICS_NOTIFICATION_BODY: &str = "{lyrics}";
#[cfg(feature = "lyrics")] #[cfg(feature = "lyrics")]
@ -37,7 +37,7 @@ const DEFAULT_LYRICS_NOTIFICATION_SUMMARY: &str = "Lyrics";
pub struct Settings { pub struct Settings {
/// The notification timeout, in seconds /// The notification timeout, in seconds
#[arg(short, long)] #[arg(short, long)]
timeout: Option<u8>, timeout: Option<i32>,
/// Make the notification persistent, i.e. not disappear after a timeout (you can dismiss it manually) /// Make the notification persistent, i.e. not disappear after a timeout (you can dismiss it manually)
#[arg(short, long)] #[arg(short, long)]
pub persistent: bool, pub persistent: bool,
@ -185,7 +185,7 @@ pub struct Settings {
volume_notification_summary: Option<String>, volume_notification_summary: Option<String>,
/// The time out of the volume change notification, in seconds. /// The time out of the volume change notification, in seconds.
#[arg(short = 'T', long)] #[arg(short = 'T', long)]
volume_notification_timeout: Option<u8>, volume_notification_timeout: Option<i32>,
/// The shuffle mode change notification body. /// The shuffle mode change notification body.
/// you can use the placeholders like "{shuffle}" in the body, it will be replaced with the shuffle mode. /// 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<String>, shuffle_notification_summary: Option<String>,
/// The time out of the shuffle mode change notification, in seconds. /// The time out of the shuffle mode change notification, in seconds.
#[arg(short = 'Y', long)] #[arg(short = 'Y', long)]
shuffle_notification_timeout: Option<u8>, shuffle_notification_timeout: Option<i32>,
/// The repeat mode change notification body. /// The repeat mode change notification body.
/// you can use the placeholders like "{repeat}" in the body, it will be replaced with the repeat mode. /// 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<String>, repeat_notification_summary: Option<String>,
/// The time out of the repeat mode change notification, in seconds. /// The time out of the repeat mode change notification, in seconds.
#[arg(short = 'H', long)] #[arg(short = 'H', long)]
repeat_notification_timeout: Option<u8>, repeat_notification_timeout: Option<i32>,
/// The aaa mode change notification body. /// 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. /// 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<String>, aaa_mode_notification_summary: Option<String>,
/// The time out of the aaa mode change notification, in seconds. /// The time out of the aaa mode change notification, in seconds.
#[arg(short = 'F', long)] #[arg(short = 'F', long)]
aaa_mode_notification_timeout: Option<u8>, aaa_mode_notification_timeout: Option<i32>,
#[cfg(feature = "lyrics")] #[cfg(feature = "lyrics")]
/// The lyrics notification body, if you want to show the lyrics separate notification. /// 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. /// 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<String>, status_notification_summary: Option<String>,
/// The time out of the status change notification, in seconds. /// The time out of the status change notification, in seconds.
#[arg(short = 'Q', long)] #[arg(short = 'Q', long)]
status_notification_timeout: Option<u8>, status_notification_timeout: Option<i32>,
#[cfg(feature = "docs")] #[cfg(feature = "docs")]
#[arg(long, hide = true)] #[arg(long, hide = true)]
#[serde(skip)] #[serde(skip)]
@ -443,7 +443,7 @@ impl Settings {
} }
#[inline(always)] #[inline(always)]
pub fn timeout(&self) -> u8 { pub fn timeout(&self) -> i32 {
self.timeout.unwrap_or(NOTIFICATION_TIMEOUT) self.timeout.unwrap_or(NOTIFICATION_TIMEOUT)
} }
@ -490,7 +490,7 @@ impl Settings {
} }
#[inline(always)] #[inline(always)]
pub fn status_notification_timeout(&self) -> u8 { pub fn status_notification_timeout(&self) -> i32 {
self.status_notification_timeout self.status_notification_timeout
.unwrap_or(DEFAULT_STATUS_CHANGE_NOTIFICATION_TIMEOUT) .unwrap_or(DEFAULT_STATUS_CHANGE_NOTIFICATION_TIMEOUT)
} }
@ -528,7 +528,7 @@ impl Settings {
} }
#[inline(always)] #[inline(always)]
pub fn volume_notification_timeout(&self) -> u8 { pub fn volume_notification_timeout(&self) -> i32 {
self.volume_notification_timeout self.volume_notification_timeout
.unwrap_or(DEFAULT_VOLUME_CHANGE_NOTIFICATION_TIMEOUT) .unwrap_or(DEFAULT_VOLUME_CHANGE_NOTIFICATION_TIMEOUT)
} }
@ -550,7 +550,7 @@ impl Settings {
} }
#[inline(always)] #[inline(always)]
pub fn shuffle_notification_timeout(&self) -> u8 { pub fn shuffle_notification_timeout(&self) -> i32 {
self.shuffle_notification_timeout self.shuffle_notification_timeout
.unwrap_or(DEFAULT_SHUFFLE_NOTIFICATION_TIMEOUT) .unwrap_or(DEFAULT_SHUFFLE_NOTIFICATION_TIMEOUT)
} }
@ -572,7 +572,7 @@ impl Settings {
} }
#[inline(always)] #[inline(always)]
pub fn repeat_notification_timeout(&self) -> u8 { pub fn repeat_notification_timeout(&self) -> i32 {
self.repeat_notification_timeout self.repeat_notification_timeout
.unwrap_or(DEFAULT_REPEAT_NOTIFICATION_TIMEOUT) .unwrap_or(DEFAULT_REPEAT_NOTIFICATION_TIMEOUT)
} }
@ -594,7 +594,7 @@ impl Settings {
} }
#[inline(always)] #[inline(always)]
pub fn aaa_mode_notification_timeout(&self) -> u8 { pub fn aaa_mode_notification_timeout(&self) -> i32 {
self.aaa_mode_notification_timeout self.aaa_mode_notification_timeout
.unwrap_or(DEFAULT_AAAMODE_NOTIFICATION_TIMEOUT) .unwrap_or(DEFAULT_AAAMODE_NOTIFICATION_TIMEOUT)
} }