From 850450fdb42164efe4bce7ffaccfd618eac7ba9c Mon Sep 17 00:00:00 2001 From: Erica Z Date: Sat, 16 Nov 2024 11:53:21 +0100 Subject: [PATCH] only load thumbnails on play queue song bound --- resources/window.blp | 4 --- src/model/song.rs | 74 +++++++++++++++++++-------------------- src/ui/play_queue/song.rs | 2 ++ src/ui/window.rs | 2 +- 4 files changed, 39 insertions(+), 43 deletions(-) diff --git a/resources/window.blp b/resources/window.blp index fefa2b5..c54e16f 100644 --- a/resources/window.blp +++ b/resources/window.blp @@ -106,10 +106,6 @@ template $AudreyUiWindow: Adw.ApplicationWindow { pixel-size: 160; halign: center; hexpand: false; - - styles [ - "frame" - ] } Label { diff --git a/src/model/song.rs b/src/model/song.rs index e7f6a7f..2f00833 100644 --- a/src/model/song.rs +++ b/src/model/song.rs @@ -75,11 +75,7 @@ glib::wrapper! { } impl Song { - pub fn from_child( - window: &crate::ui::Window, - song: &subsonic::schema::Child, - load_thumbnail: bool, - ) -> Self { + pub fn from_child(window: &crate::ui::Window, song: &subsonic::schema::Child) -> Self { let api = window.api(); let song: Song = Object::builder() .property("id", &song.id) @@ -92,39 +88,41 @@ impl Song { .property("stream-url", api.stream_url(&song.id).as_str()) .build(); - if load_thumbnail { - let id = song.id(); - let song_weak = song.downgrade(); - let scale_factor = window.scale_factor() as u32; // NOTE: assumed constant - song.imp() - .thumbnail_loading - .replace(Some(glib::spawn_future_local(async move { - let _permit = SEM.acquire().await.unwrap(); - - let bytes = match api - .cover_art(&id, Some(50 * scale_factor)) // see pixel-size in - // play_queue_song.blp - .await - { - Ok(bytes) => bytes, - Err(err) => { - event!( - Level::ERROR, - "could not fetch thumbnail for song {id}: {err}" - ); - return; - } - }; - let song = match song_weak.upgrade() { - None => return, - Some(song) => song, - }; - song.set_thumbnail( - gdk::Texture::from_bytes(&glib::Bytes::from_owned(bytes)).unwrap(), - ); - }))); - } - song } + + pub fn need_thumbnail(&self, window: &crate::ui::Window) { + let api = window.api(); + let mut thumbnail_loading = self.imp().thumbnail_loading.borrow_mut(); + if thumbnail_loading.is_none() { + let id = self.id(); + let song_weak = self.downgrade(); + let scale_factor = window.scale_factor() as u32; // NOTE: assumed constant + *thumbnail_loading = Some(glib::spawn_future_local(async move { + let _permit = SEM.acquire().await.unwrap(); + + let bytes = match api + .cover_art(&id, Some(50 * scale_factor)) // see pixel-size in + // play_queue_song.blp + .await + { + Ok(bytes) => bytes, + Err(err) => { + event!( + Level::ERROR, + "could not fetch thumbnail for song {id}: {err}" + ); + return; + } + }; + let song = match song_weak.upgrade() { + None => return, + Some(song) => song, + }; + song.set_thumbnail( + gdk::Texture::from_bytes(&glib::Bytes::from_owned(bytes)).unwrap(), + ); + })); + } + } } diff --git a/src/ui/play_queue/song.rs b/src/ui/play_queue/song.rs index 1a2ee7c..ec1b229 100644 --- a/src/ui/play_queue/song.rs +++ b/src/ui/play_queue/song.rs @@ -200,6 +200,8 @@ impl Song { self.set_song(&song); self.set_position(position); self.set_playlist_pos(window.playlist_pos()); + + song.need_thumbnail(window); } pub fn unbind(&self) {} diff --git a/src/ui/window.rs b/src/ui/window.rs index 3e48c9e..3bb1cd8 100644 --- a/src/ui/window.rs +++ b/src/ui/window.rs @@ -394,7 +394,7 @@ mod imp { Rc::clone(api.as_ref().unwrap()) }; for song in api.random_songs(500).await.unwrap().into_iter() { - let song = Song::from_child(&self.obj(), &song, true); + let song = Song::from_child(&self.obj(), &song); self.mpv .command(["loadfile", &song.stream_url(), "append-play"]) .unwrap();