diff --git a/src/bin/main.rs b/src/bin/main.rs index 78cd346..05737b7 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -1,8 +1,8 @@ use cmus_notify::{ - cmus::{self, query::CmusQueryResponse}, + cmus::{self, events::CmusEvent, query::CmusQueryResponse}, notification, settings::Settings, - TrackCover, + track_cover, TrackCover, }; #[cfg(feature = "debug")] @@ -80,11 +80,32 @@ fn main() { // Update the previous response. previous_response = response; + if events.len() == 1 { + // If the track is changed, we need to update the cover. + match &events[0] { + CmusEvent::TrackChanged(track) => { + previous_cover = track_cover(&track.path, settings.depth, + settings.force_use_external_cover, + settings.no_use_external_cover); + } + _ => { + if previous_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, + settings.force_use_external_cover, + settings.no_use_external_cover); + } + } + } + }; + } + notification::show_notification( events, &settings, &mut notification, - &mut previous_cover, + &previous_cover, ); // TODO: Handle the error. } diff --git a/src/cmus/events.rs b/src/cmus/events.rs index 91be53d..574801d 100644 --- a/src/cmus/events.rs +++ b/src/cmus/events.rs @@ -1,7 +1,7 @@ use crate::cmus::player_settings::{AAAMode, Shuffle}; use crate::cmus::{Track, TrackStatus}; -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub enum CmusEvent { StatusChanged(Track), TrackChanged(Track), diff --git a/src/cmus/query.rs b/src/cmus/query.rs index a309895..eea1b0d 100644 --- a/src/cmus/query.rs +++ b/src/cmus/query.rs @@ -67,6 +67,8 @@ impl CmusQueryResponse { #[cfg(feature = "debug")] debug!("Track changed: {:?} -> {:?}", other_track, track); events.push(CmusEvent::TrackChanged(other_track)); + // We don't need to check for other changes, since the track changed. + return Ok(events); } else if track.status != other_track.status { #[cfg(feature = "debug")] debug!( diff --git a/src/lib.rs b/src/lib.rs index 60201f5..fdc65ec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -104,7 +104,7 @@ pub fn search_for( } /// The cover of a track. -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub enum TrackCover { /// The cover is embedded in the track. /// The `TempFile` object contains the contents of the embedded picture. diff --git a/src/notification.rs b/src/notification.rs index 3741370..6756dee 100644 --- a/src/notification.rs +++ b/src/notification.rs @@ -3,14 +3,14 @@ use crate::cmus::{Track, TrackStatus}; use crate::settings::Settings; use crate::{process_template_placeholders, track_cover, TrackCover}; #[cfg(feature = "debug")] -use log::{info,debug}; +use log::{debug, info}; #[inline(always)] pub fn show_notification( events: Vec, settings: &Settings, notification: &mut notify_rust::Notification, - previous_cover: &mut TrackCover, + cover: &TrackCover, ) -> Result<(), notify_rust::error::Error> { if events.is_empty() { #[cfg(feature = "debug")] @@ -19,16 +19,13 @@ pub fn show_notification( } // Set the image of the notification. - previous_cover.set_notification_image(notification); + cover.set_notification_image(notification); for event in events { #[cfg(feature = "debug")] info!("event: {:?}", event); match event { - CmusEvent::TrackChanged(track) => { - *previous_cover = track_cover(track.path.as_str(), settings.depth, settings.force_use_external_cover, settings.no_use_external_cover); - } CmusEvent::StatusChanged(track) => { #[cfg(feature = "debug")] debug!("Status changed: {:?}", track.status);