Just create the fucking track change notification

This commit is contained in:
Anas Elgarhy 2023-02-17 22:57:20 +02:00
parent 9b629fc238
commit ce858dff5f
No known key found for this signature in database
GPG key ID: 0501802A1D496528
2 changed files with 48 additions and 18 deletions

View file

@ -59,7 +59,7 @@ fn main() {
// Initialize the buffer to store the cover path, to compare it with the next one. // 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. // 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. // We only need to do it when the track directory changes.
let mut previous_cover = TrackCover::None; let mut cover = TrackCover::None;
loop { loop {
// Get the response from cmus. // Get the response from cmus.
@ -80,34 +80,45 @@ fn main() {
// Update the previous response. // Update the previous response.
previous_response = 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 events.len() == 1 {
// If the track is changed, we need to update the cover. // If the track is changed, we need to update the cover.
let mut cover_changed = false;
match &events[0] { match &events[0] {
CmusEvent::TrackChanged(track) => { CmusEvent::TrackChanged(track) => {
previous_cover = track_cover(&track.path, settings.depth, cover = track_cover(&track.path, settings.depth,
settings.force_use_external_cover, settings.force_use_external_cover,
settings.no_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 the cover is not found, we need to update it.
if let Ok(track) = &previous_response.track() { 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.force_use_external_cover,
settings.no_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, events,
&settings, &settings,
&mut notification, &mut notification,
&previous_cover, ) {
); Ok(_) => {}
// TODO: Handle the error. Err(e) => {
eprintln!("Error: {}", e);
}
}
} }
} }
sleep!(settings.interval); sleep!(settings.interval);

View file

@ -10,7 +10,6 @@ pub fn show_notification(
events: Vec<CmusEvent>, events: Vec<CmusEvent>,
settings: &Settings, settings: &Settings,
notification: &mut notify_rust::Notification, notification: &mut notify_rust::Notification,
cover: &TrackCover,
) -> Result<(), notify_rust::error::Error> { ) -> Result<(), notify_rust::error::Error> {
if events.is_empty() { if events.is_empty() {
#[cfg(feature = "debug")] #[cfg(feature = "debug")]
@ -18,9 +17,6 @@ pub fn show_notification(
return Ok(()); // No events to process. return Ok(()); // No events to process.
} }
// Set the image of the notification.
cover.set_notification_image(notification);
for event in events { for event in events {
#[cfg(feature = "debug")] #[cfg(feature = "debug")]
info!("event: {:?}", event); info!("event: {:?}", event);
@ -29,12 +25,15 @@ pub fn show_notification(
CmusEvent::StatusChanged(track) => { CmusEvent::StatusChanged(track) => {
#[cfg(feature = "debug")] #[cfg(feature = "debug")]
debug!("Status changed: {:?}", track.status); debug!("Status changed: {:?}", track.status);
build_status_notification(track, settings, notification)?; build_status_notification(track, settings, notification);
notification.show()?; notification.show()?;
} }
/* CmusEvent::TrackChanged(track) => { CmusEvent::TrackChanged(track) => {
bulid_track_notification(track, settings, notification, previous_cover)? #[cfg(feature = "debug")]
} debug!("Track changed: {:?}", track);
build_track_notification(track, settings, notification)?
}
/*
CmusEvent::VolumeChanged { left, right } if settings.show_player_notifications => { CmusEvent::VolumeChanged { left, right } if settings.show_player_notifications => {
build_volume_notification(left, right, settings, notification)? build_volume_notification(left, right, settings, notification)?
} }
@ -60,12 +59,32 @@ fn build_status_notification(
track: Track, track: Track,
settings: &Settings, settings: &Settings,
notification: &mut notify_rust::Notification, notification: &mut notify_rust::Notification,
) -> Result<(), notify_rust::error::Error> { ) {
// Set the summary and body of the notification. // Set the summary and body of the notification.
notification notification
.summary( .summary(
process_template_placeholders(&settings.status_notification_summary, &track).as_str(), 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(()) Ok(())
} }