From 2e8f37e42bb5c280d088b1889b2d95c0957be316 Mon Sep 17 00:00:00 2001 From: cyqsimon <28627918+cyqsimon@users.noreply.github.com> Date: Mon, 6 Jun 2022 05:59:42 +0800 Subject: [PATCH] Add desktop entry & notification icon hint (#822) * Add desktop entry & notification icon hint * Fix build for non-linux * Set notification priority with hints --- Cargo.toml | 1 + misc/ncspot.desktop | 10 ++++++++ src/queue.rs | 62 ++++++++++++++++++++++----------------------- 3 files changed, 42 insertions(+), 31 deletions(-) create mode 100755 misc/ncspot.desktop diff --git a/Cargo.toml b/Cargo.toml index 5c117ee..de72fbf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -88,5 +88,6 @@ It is heavily inspired by ncurses MPD clients, such as ncmpc.""" license-file = ["LICENSE"] assets = [ ["target/release/ncspot", "usr/bin/", "755"], + ["misc/ncspot.desktop", "usr/share/applications/", "644"], ["README.md", "usr/share/doc/ncspot/README.md", "644"], ] diff --git a/misc/ncspot.desktop b/misc/ncspot.desktop new file mode 100755 index 0000000..c4096e7 --- /dev/null +++ b/misc/ncspot.desktop @@ -0,0 +1,10 @@ +[Desktop Entry] +Type=Application +Name=ncspot +Comment=Cross-platform ncurses Spotify client written in Rust +TryExec=ncspot +Exec=ncspot +Icon=ncspot +Terminal=true +Categories=AudioVideo;Audio;Player;ConsoleOnly +Keywords=spotify;music;player diff --git a/src/queue.rs b/src/queue.rs index 0cb05ac..b876f9b 100644 --- a/src/queue.rs +++ b/src/queue.rs @@ -4,7 +4,7 @@ use std::sync::{Arc, RwLock}; use log::{debug, error, info}; #[cfg(feature = "notify")] -use notify_rust::Notification; +use notify_rust::{Hint, Notification, Urgency}; use rand::prelude::*; use strum_macros::Display; @@ -433,47 +433,47 @@ impl Queue { #[cfg(feature = "notify")] pub fn send_notification( track_name: &str, - _cover_url: Option, + cover_url: Option, notification_id: Arc, ) { let current_notification_id = notification_id.load(std::sync::atomic::Ordering::Relaxed); - let res = if let Some(u) = _cover_url { - // download album cover image - let path = crate::utils::cache_path_for_url(u.to_string()); + let mut n = Notification::new(); + n.appname("ncspot") + .id(current_notification_id) + .summary(track_name); + + // album cover image + if let Some(u) = cover_url { + let path = crate::utils::cache_path_for_url(u.to_string()); if !path.exists() { if let Err(e) = crate::utils::download(u, path.clone()) { error!("Failed to download cover: {}", e); } } + n.icon(path.to_str().unwrap()); + } - Notification::new() - .appname("ncspot") - .id(current_notification_id) - .summary(track_name) - .icon(path.to_str().unwrap()) - .show() - } else { - Notification::new() - .appname("ncspot") - .id(current_notification_id) - .summary(track_name) - .show() - }; + // XDG desktop entry hints + #[cfg(all(unix, not(target_os = "macos")))] + n.urgency(Urgency::Low) + .hint(Hint::Transient(true)) + .hint(Hint::DesktopEntry("ncspot".into())); - if let Ok(n) = res { - // only available for XDG - #[cfg(all(unix, not(target_os = "macos")))] - { - let new_notification_id = n.id(); - log::debug!( - "new notification id: {}, previously: {}", - new_notification_id, - current_notification_id - ); - notification_id.store(new_notification_id, std::sync::atomic::Ordering::Relaxed); + match n.show() { + Ok(_handle) => { + // only available for XDG + #[cfg(all(unix, not(target_os = "macos")))] + { + let new_notification_id = _handle.id(); + log::debug!( + "new notification id: {}, previously: {}", + new_notification_id, + current_notification_id + ); + notification_id.store(new_notification_id, std::sync::atomic::Ordering::Relaxed); + } } - } else if let Err(e) = res { - error!("Failed to send notification cover: {}", e); + Err(e) => error!("Failed to send notification cover: {}", e), } }