From ce858dff5ffa98b6b5ef2430ddefdd3b71201888 Mon Sep 17 00:00:00 2001 From: Anas Elgarhy Date: Fri, 17 Feb 2023 22:57:20 +0200 Subject: [PATCH] Just create the fucking track change notification --- src/bin/main.rs | 27 +++++++++++++++++++-------- src/notification.rs | 39 +++++++++++++++++++++++++++++---------- 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index 05737b7..fab82b4 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -59,7 +59,7 @@ fn main() { // Initialize the buffer to store the cover path, to compare it with the next one. // This is used to speed up the main loop, because we don't need to process the template and search for the cover every time. // We only need to do it when the track directory changes. - let mut previous_cover = TrackCover::None; + let mut cover = TrackCover::None; loop { // Get the response from cmus. @@ -80,34 +80,45 @@ fn main() { // Update the previous response. previous_response = response; + //FIXME: Should check if the user has enabled the cover feature or use a static cover. if events.len() == 1 { // If the track is changed, we need to update the cover. + let mut cover_changed = false; match &events[0] { CmusEvent::TrackChanged(track) => { - previous_cover = track_cover(&track.path, settings.depth, + cover = track_cover(&track.path, settings.depth, settings.force_use_external_cover, settings.no_use_external_cover); + cover_changed = true; } _ => { - if previous_cover == TrackCover::None { + if cover == TrackCover::None { // If the cover is not found, we need to update it. if let Ok(track) = &previous_response.track() { - previous_cover = track_cover(&track.path, settings.depth, + cover = track_cover(&track.path, settings.depth, settings.force_use_external_cover, settings.no_use_external_cover); + cover_changed = true; } } } }; + // Set the notification cover. + if cover_changed { + cover.set_notification_image(&mut notification); + } } - notification::show_notification( + match notification::show_notification( events, &settings, &mut notification, - &previous_cover, - ); - // TODO: Handle the error. + ) { + Ok(_) => {} + Err(e) => { + eprintln!("Error: {}", e); + } + } } } sleep!(settings.interval); diff --git a/src/notification.rs b/src/notification.rs index 6756dee..37b148d 100644 --- a/src/notification.rs +++ b/src/notification.rs @@ -10,7 +10,6 @@ pub fn show_notification( events: Vec, settings: &Settings, notification: &mut notify_rust::Notification, - cover: &TrackCover, ) -> Result<(), notify_rust::error::Error> { if events.is_empty() { #[cfg(feature = "debug")] @@ -18,9 +17,6 @@ pub fn show_notification( return Ok(()); // No events to process. } - // Set the image of the notification. - cover.set_notification_image(notification); - for event in events { #[cfg(feature = "debug")] info!("event: {:?}", event); @@ -29,12 +25,15 @@ pub fn show_notification( CmusEvent::StatusChanged(track) => { #[cfg(feature = "debug")] debug!("Status changed: {:?}", track.status); - build_status_notification(track, settings, notification)?; + build_status_notification(track, settings, notification); notification.show()?; } - /* CmusEvent::TrackChanged(track) => { - bulid_track_notification(track, settings, notification, previous_cover)? - } + CmusEvent::TrackChanged(track) => { + #[cfg(feature = "debug")] + debug!("Track changed: {:?}", track); + build_track_notification(track, settings, notification)? + } + /* CmusEvent::VolumeChanged { left, right } if settings.show_player_notifications => { build_volume_notification(left, right, settings, notification)? } @@ -60,12 +59,32 @@ fn build_status_notification( track: Track, settings: &Settings, notification: &mut notify_rust::Notification, -) -> Result<(), notify_rust::error::Error> { +) { // Set the summary and body of the notification. notification .summary( process_template_placeholders(&settings.status_notification_summary, &track).as_str(), ) - .body(process_template_placeholders(&settings.status_notification_body, &track).as_str()); + .body(process_template_placeholders(&settings.status_notification_body, &track).as_str()) + .timeout(settings.status_notification_timeout as i32 * 1000); +} + +#[inline(always)] +fn build_track_notification( + track: Track, + settings: &Settings, + notification: &mut notify_rust::Notification, +) -> Result<(), notify_rust::error::Error> { + // Set the summary and body of the notification. + notification + .summary( + process_template_placeholders(&settings.summary, &track).as_str(), + ) + .body(process_template_placeholders(&settings.body, &track).as_str()) + .timeout(settings.timeout as i32 * 1000); + + notification.hint(notify_rust::Hint::Category("music".to_string())); + notification.show()?; + Ok(()) }