Improve the get cover mecarthim

This commit is contained in:
Anas Elgarhy 2023-02-17 10:51:59 +02:00
parent b75d56485c
commit 391ca67890
No known key found for this signature in database
GPG key ID: 0501802A1D496528
5 changed files with 31 additions and 11 deletions

View file

@ -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.
}

View file

@ -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),

View file

@ -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!(

View file

@ -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.

View file

@ -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<CmusEvent>,
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);