only load thumbnails on play queue song bound

This commit is contained in:
Erica Z 2024-11-16 11:53:21 +01:00
parent d50cc74698
commit 850450fdb4
4 changed files with 39 additions and 43 deletions

View file

@ -106,10 +106,6 @@ template $AudreyUiWindow: Adw.ApplicationWindow {
pixel-size: 160;
halign: center;
hexpand: false;
styles [
"frame"
]
}
Label {

View file

@ -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(),
);
}));
}
}
}

View file

@ -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) {}

View file

@ -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();