[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,
) -> 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,
}
}
}

View file

@ -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(&notification_summary).body(&notification_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,
);
}
}

View file

@ -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 =
"<b>album:</b> {album} \n <b>Artist:</b> {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 = "<b>{status}</b>";
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<u8>,
timeout: Option<i32>,
/// 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<String>,
/// The time out of the volume change notification, in seconds.
#[arg(short = 'T', long)]
volume_notification_timeout: Option<u8>,
volume_notification_timeout: Option<i32>,
/// 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<String>,
/// The time out of the shuffle mode change notification, in seconds.
#[arg(short = 'Y', long)]
shuffle_notification_timeout: Option<u8>,
shuffle_notification_timeout: Option<i32>,
/// 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<String>,
/// The time out of the repeat mode change notification, in seconds.
#[arg(short = 'H', long)]
repeat_notification_timeout: Option<u8>,
repeat_notification_timeout: Option<i32>,
/// 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<String>,
/// The time out of the aaa mode change notification, in seconds.
#[arg(short = 'F', long)]
aaa_mode_notification_timeout: Option<u8>,
aaa_mode_notification_timeout: Option<i32>,
#[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<String>,
/// The time out of the status change notification, in seconds.
#[arg(short = 'Q', long)]
status_notification_timeout: Option<u8>,
status_notification_timeout: Option<i32>,
#[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)
}