Commit message: Update cover when track changes, and set a desktop entry and category for notifications

This commit is contained in:
Anas Elgarhy 2023-02-20 04:36:32 +02:00
parent a252a0727d
commit 6270ea9717
No known key found for this signature in database
GPG key ID: 0501802A1D496528
2 changed files with 59 additions and 47 deletions

View file

@ -54,10 +54,6 @@ fn main() {
// Initialize the buffer to store the response from cmus, to compare it with the next one.
let mut previous_response = CmusQueryResponse::default();
// 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 cover = TrackCover::None;
loop {
// Get the response from cmus.
@ -78,43 +74,8 @@ 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) => {
cover = track_cover(
&track.path,
settings.depth,
settings.force_use_external_cover,
settings.no_use_external_cover,
);
cover_changed = true;
}
_ => {
if cover == TrackCover::None {
// If the cover is not found, we need to update it.
if let Ok(track) = &previous_response.track() {
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 {
notification = notify_rust::Notification::new(); // Reset the notification.
cover.set_notification_image(&mut notification);
}
}
match notification::show_notification(events, &settings, &mut notification) {
match notification::show_notification(events, &settings, &mut notification, &previous_response) {
Ok(_) => {}
Err(e) => {
eprintln!("Error: {}", e);

View file

@ -4,12 +4,15 @@ use crate::settings::Settings;
use crate::{process_template_placeholders, track_cover, TrackCover};
#[cfg(feature = "debug")]
use log::{debug, info};
use notify_rust::Notification;
use crate::cmus::query::CmusQueryResponse;
#[inline(always)]
pub fn show_notification(
events: Vec<CmusEvent>,
settings: &Settings,
notification: &mut notify_rust::Notification,
notification: &mut Notification,
response: &CmusQueryResponse,
) -> Result<(), notify_rust::error::Error> {
if events.is_empty() {
#[cfg(feature = "debug")]
@ -17,6 +20,9 @@ pub fn show_notification(
return Ok(()); // No events to process.
}
//FIXME: Should check if the user has enabled the cover feature or use a static cover.
update_cover(&events[0], response, settings, notification);
for event in events {
#[cfg(feature = "debug")]
info!("event: {:?}", event);
@ -58,7 +64,7 @@ pub fn show_notification(
fn build_status_notification(
track: Track,
settings: &Settings,
notification: &mut notify_rust::Notification,
notification: &mut Notification,
) {
// Set the summary and body of the notification.
notification
@ -73,16 +79,61 @@ fn build_status_notification(
fn build_track_notification(
track: Track,
settings: &Settings,
notification: &mut notify_rust::Notification,
notification: &mut 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);
.body(process_template_placeholders(&settings.body, &track).as_str());
notification.hint(notify_rust::Hint::Category("music".to_string()));
notification.show()?;
let n = notification.show()?;
Ok(())
}
macro_rules! setup_notification {
($notification: expr, $settings: expr) => {
$notification
.appname("cmus-notify")
.timeout($settings.timeout as i32 * 1000)
.hint(notify_rust::Hint::Category("music".to_string()))
.hint(notify_rust::Hint::DesktopEntry("cmus-notify.desktop".to_string()))
.hint(notify_rust::Hint::Resident(true));
};
}
#[inline(always)]
fn update_cover(first_event: &CmusEvent, response: &CmusQueryResponse, settings: &Settings, notification: &mut Notification) {
let mut cover = TrackCover::None;
// If the track is changed, we need to update the cover.
match first_event {
CmusEvent::TrackChanged(track) => {
cover = track_cover(
&track.path,
settings.depth,
settings.force_use_external_cover,
settings.no_use_external_cover,
);
}
_ => {
if cover == TrackCover::None {
// If the cover is not found, we need to update it.
if let Ok(track) = &response.track() {
cover = track_cover(
&track.path,
settings.depth,
settings.force_use_external_cover,
settings.no_use_external_cover,
);
}
}
}
};
// Set the notification cover.
if cover != TrackCover::None {
*notification = Notification::new();
// Reset the notification.
setup_notification!(&mut *notification, &settings);
cover.set_notification_image(&mut *notification);
}
}