feat(audio): add fallback when title/artist not found

This commit is contained in:
jilv220 2024-02-20 22:36:00 -08:00 committed by Ashley Wulber
parent b7c6cc0a97
commit 59af0f1b8b
5 changed files with 21 additions and 2 deletions

7
Cargo.lock generated
View file

@ -879,6 +879,7 @@ dependencies = [
"tracing-log",
"tracing-subscriber",
"url",
"urlencoding",
"zbus",
]
@ -5646,6 +5647,12 @@ dependencies = [
"serde",
]
[[package]]
name = "urlencoding"
version = "2.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da"
[[package]]
name = "usvg"
version = "0.37.0"

View file

@ -23,3 +23,4 @@ mpris2-zbus = { git = "https://github.com/pop-os/dbus-settings-bindings" }
serde = "1.0.130"
url = "2"
zbus.workspace = true
urlencoding = "2.1.3"

View file

@ -4,3 +4,4 @@ show-media-controls = Show Media Controls on Top Panel
sound-settings = Sound Settings...
disconnected = PulseAudio Disconnected
no-device = No device selected
unknown-artist = Unknown

View file

@ -718,7 +718,7 @@ impl cosmic::Application for Audio {
artists
}
} else {
String::new()
fl!("unknown-artist")
};
elements.push(column![text(title).size(14), text(artists).size(10),].into());

View file

@ -9,6 +9,7 @@ use mpris2_zbus::{
player::{PlaybackStatus, Player},
};
use tokio::join;
use urlencoding::decode;
use zbus::{fdo::DBusProxy, Connection};
#[derive(Clone, Debug)]
@ -27,7 +28,16 @@ pub struct PlayerStatus {
impl PlayerStatus {
async fn new(player: Player) -> Option<Self> {
let metadata = player.metadata().await.ok()?;
let title = metadata.title().map(Cow::from);
let pathname = metadata.url().unwrap_or("".into());
let pathbuf = PathBuf::from(pathname);
let title = metadata
.title()
.or(pathbuf
.file_name()
.and_then(|s| s.to_str())
.and_then(|s| decode(s).map_or(None, |s| Some(s.into_owned()))))
.map(Cow::from);
let artists = metadata
.artists()
.map(|a| a.into_iter().map(Cow::from).collect::<Vec<_>>());