From 4241bbeab4f4eace8bb0c7b3962422d9d467dbc8 Mon Sep 17 00:00:00 2001 From: Erica Z Date: Tue, 5 Nov 2024 12:19:33 +0100 Subject: [PATCH] cover works agen --- src/subsonic.rs | 2 +- src/ui/window.rs | 31 ++++++++++++++++++++++++------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/subsonic.rs b/src/subsonic.rs index 83c8680..f91cafa 100644 --- a/src/subsonic.rs +++ b/src/subsonic.rs @@ -178,7 +178,7 @@ impl Client { } pub fn cover_art_url(&self, id: &str) -> url::Url { - self.url(&["rest", "coverArt"], &[("id", id)]) + self.url(&["rest", "getCoverArt"], &[("id", id)]) } pub async fn cover_art(&self, id: &str) -> Result { diff --git a/src/ui/window.rs b/src/ui/window.rs index 2170b9f..e188b01 100644 --- a/src/ui/window.rs +++ b/src/ui/window.rs @@ -62,7 +62,7 @@ mod imp { mpv_event_loop_handle: RefCell>>, // really !, not () zbus_executor_loop_handle: RefCell>>, // same - loading_cover_handle: RefCell>>>, + loading_cover_handle: RefCell>>, } impl Default for Window { @@ -210,15 +210,30 @@ mod imp { window.notify("song"); window.imp().buffering_start(); - /* - let window = window.clone(); + let window2 = window.clone(); + let song_id = window.song().unwrap().id(); window .imp() .loading_cover_handle - .replace(Some(glib::spawn_future_local( - async move { todo!() }, - ))) - .map(|handle| handle.abort());*/ + .replace(Some(glib::spawn_future_local(async move { + let api = window2.imp().api.borrow().as_ref().unwrap().clone(); + let bytes = + api + .cover_art(&song_id) + .await + .expect("could not load cover art for song {song_id}"); + + match window2.song() { + Some(song) if song.id() == song_id => { + let texture = gdk::Texture::from_bytes(&glib::Bytes::from_owned(bytes)).expect( + "could not create texture from cover art for {song_id}", + ); + window2.set_playing_cover_art(Some(texture)); + } + _ => { event!(Level::WARN, "was too late to fetch cover for song {song_id}") }, + } + }))) + .map(|handle| handle.abort()); } Event::Hook(event) => match event.reply_userdata { @@ -297,6 +312,8 @@ mod imp { // cancel queued seek always window.imp().queued_seek.set(None); + + window.set_playing_cover_art(None::); } _ => event!(Level::DEBUG, "unhandled {event:?}"),