Add desktop entry & notification icon hint (#822)

* Add desktop entry & notification icon hint

* Fix build for non-linux

* Set notification priority with hints
This commit is contained in:
cyqsimon
2022-06-06 05:59:42 +08:00
committed by GitHub
parent 58e2436314
commit 2e8f37e42b
3 changed files with 42 additions and 31 deletions

View File

@@ -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"],
]

10
misc/ncspot.desktop Executable file
View File

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

View File

@@ -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<String>,
cover_url: Option<String>,
notification_id: Arc<AtomicU32>,
) {
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),
}
}