Commit message: Update cover when track changes, and set a desktop entry and category for notifications
This commit is contained in:
parent
a252a0727d
commit
6270ea9717
2 changed files with 59 additions and 47 deletions
|
@ -54,10 +54,6 @@ fn main() {
|
||||||
|
|
||||||
// Initialize the buffer to store the response from cmus, to compare it with the next one.
|
// Initialize the buffer to store the response from cmus, to compare it with the next one.
|
||||||
let mut previous_response = CmusQueryResponse::default();
|
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 {
|
loop {
|
||||||
// Get the response from cmus.
|
// Get the response from cmus.
|
||||||
|
@ -78,43 +74,8 @@ 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 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(_) => {}
|
Ok(_) => {}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("Error: {}", e);
|
eprintln!("Error: {}", e);
|
||||||
|
|
|
@ -4,12 +4,15 @@ use crate::settings::Settings;
|
||||||
use crate::{process_template_placeholders, track_cover, TrackCover};
|
use crate::{process_template_placeholders, track_cover, TrackCover};
|
||||||
#[cfg(feature = "debug")]
|
#[cfg(feature = "debug")]
|
||||||
use log::{debug, info};
|
use log::{debug, info};
|
||||||
|
use notify_rust::Notification;
|
||||||
|
use crate::cmus::query::CmusQueryResponse;
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn show_notification(
|
pub fn show_notification(
|
||||||
events: Vec<CmusEvent>,
|
events: Vec<CmusEvent>,
|
||||||
settings: &Settings,
|
settings: &Settings,
|
||||||
notification: &mut notify_rust::Notification,
|
notification: &mut Notification,
|
||||||
|
response: &CmusQueryResponse,
|
||||||
) -> Result<(), notify_rust::error::Error> {
|
) -> Result<(), notify_rust::error::Error> {
|
||||||
if events.is_empty() {
|
if events.is_empty() {
|
||||||
#[cfg(feature = "debug")]
|
#[cfg(feature = "debug")]
|
||||||
|
@ -17,6 +20,9 @@ pub fn show_notification(
|
||||||
return Ok(()); // No events to process.
|
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 {
|
for event in events {
|
||||||
#[cfg(feature = "debug")]
|
#[cfg(feature = "debug")]
|
||||||
info!("event: {:?}", event);
|
info!("event: {:?}", event);
|
||||||
|
@ -58,7 +64,7 @@ pub fn show_notification(
|
||||||
fn build_status_notification(
|
fn build_status_notification(
|
||||||
track: Track,
|
track: Track,
|
||||||
settings: &Settings,
|
settings: &Settings,
|
||||||
notification: &mut notify_rust::Notification,
|
notification: &mut Notification,
|
||||||
) {
|
) {
|
||||||
// Set the summary and body of the notification.
|
// Set the summary and body of the notification.
|
||||||
notification
|
notification
|
||||||
|
@ -73,16 +79,61 @@ fn build_status_notification(
|
||||||
fn build_track_notification(
|
fn build_track_notification(
|
||||||
track: Track,
|
track: Track,
|
||||||
settings: &Settings,
|
settings: &Settings,
|
||||||
notification: &mut notify_rust::Notification,
|
notification: &mut Notification,
|
||||||
) -> Result<(), notify_rust::error::Error> {
|
) -> Result<(), notify_rust::error::Error> {
|
||||||
// Set the summary and body of the notification.
|
// Set the summary and body of the notification.
|
||||||
notification
|
notification
|
||||||
.summary(process_template_placeholders(&settings.summary, &track).as_str())
|
.summary(process_template_placeholders(&settings.summary, &track).as_str())
|
||||||
.body(process_template_placeholders(&settings.body, &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()));
|
let n = notification.show()?;
|
||||||
notification.show()?;
|
|
||||||
|
|
||||||
Ok(())
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue