feat(slint): progressive artwork loading for search results
The artwork module gains SearchAlbum/Track/Artist/Playlist targets; navigate_search and load-more build cover-download jobs and hand them to spawn_loads. Covers resolve through the shared qbz_cache image cache (disk-backed, never re-downloaded) and apply per row, so a late arrival never resets a list. cc
This commit is contained in:
3 files changed
+135
-2
No files matched your search
@@ -12,7 +12,7 @@ use qbz_cache::ImageCacheService;
|
|||||||
use slint::{ComponentHandle, Model};
|
use slint::{ComponentHandle, Model};
|
||||||
use tokio::sync::Semaphore;
|
use tokio::sync::Semaphore;
|
||||||
|
|
||||||
use crate::{AppWindow, HomeState};
|
use crate::{AppWindow, HomeState, SearchState};
|
||||||
|
|
||||||
/// Cap on simultaneous artwork downloads.
|
/// Cap on simultaneous artwork downloads.
|
||||||
const MAX_CONCURRENT: usize = 16;
|
const MAX_CONCURRENT: usize = 16;
|
||||||
@@ -39,6 +39,14 @@ pub enum ArtworkTarget {
|
|||||||
Recent { idx: usize },
|
Recent { idx: usize },
|
||||||
/// A card in `HomeState.recent-albums[idx]`.
|
/// A card in `HomeState.recent-albums[idx]`.
|
||||||
RecentAlbum { idx: usize },
|
RecentAlbum { idx: usize },
|
||||||
|
/// A row in `SearchState.albums[idx]`.
|
||||||
|
SearchAlbum { idx: usize },
|
||||||
|
/// A row in `SearchState.tracks[idx]`.
|
||||||
|
SearchTrack { idx: usize },
|
||||||
|
/// A row in `SearchState.artists[idx]`.
|
||||||
|
SearchArtist { idx: usize },
|
||||||
|
/// A row in `SearchState.playlists[idx]`.
|
||||||
|
SearchPlaylist { idx: usize },
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An artwork download job: which card, and the image URL.
|
/// An artwork download job: which card, and the image URL.
|
||||||
@@ -258,5 +266,33 @@ fn apply_artwork(
|
|||||||
item.artwork = image;
|
item.artwork = image;
|
||||||
albums.set_row_data(idx, item);
|
albums.set_row_data(idx, item);
|
||||||
}
|
}
|
||||||
|
ArtworkTarget::SearchAlbum { idx } => {
|
||||||
|
let model = window.global::<SearchState>().get_albums();
|
||||||
|
if let Some(mut item) = model.row_data(idx) {
|
||||||
|
item.artwork = image;
|
||||||
|
model.set_row_data(idx, item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ArtworkTarget::SearchTrack { idx } => {
|
||||||
|
let model = window.global::<SearchState>().get_tracks();
|
||||||
|
if let Some(mut item) = model.row_data(idx) {
|
||||||
|
item.artwork = image;
|
||||||
|
model.set_row_data(idx, item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ArtworkTarget::SearchArtist { idx } => {
|
||||||
|
let model = window.global::<SearchState>().get_artists();
|
||||||
|
if let Some(mut item) = model.row_data(idx) {
|
||||||
|
item.artwork = image;
|
||||||
|
model.set_row_data(idx, item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ArtworkTarget::SearchPlaylist { idx } => {
|
||||||
|
let model = window.global::<SearchState>().get_playlists();
|
||||||
|
if let Some(mut item) = model.row_data(idx) {
|
||||||
|
item.artwork = image;
|
||||||
|
model.set_row_data(idx, item);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -263,7 +263,7 @@ fn navigate_search(
|
|||||||
runtime: Arc<AppRuntime<SlintAdapter>>,
|
runtime: Arc<AppRuntime<SlintAdapter>>,
|
||||||
weak: slint::Weak<AppWindow>,
|
weak: slint::Weak<AppWindow>,
|
||||||
handle: &tokio::runtime::Handle,
|
handle: &tokio::runtime::Handle,
|
||||||
_image_cache: artwork::ImageCache,
|
image_cache: artwork::ImageCache,
|
||||||
query: String,
|
query: String,
|
||||||
) {
|
) {
|
||||||
// Capture a version so a slow, stale load cannot overwrite a newer
|
// Capture a version so a slow, stale load cannot overwrite a newer
|
||||||
@@ -276,12 +276,14 @@ fn navigate_search(
|
|||||||
});
|
});
|
||||||
match search::load_search(&runtime, &query).await {
|
match search::load_search(&runtime, &query).await {
|
||||||
Ok(data) => {
|
Ok(data) => {
|
||||||
|
let jobs = search::artwork_jobs(&data);
|
||||||
let _ = weak.upgrade_in_event_loop(move |w| {
|
let _ = weak.upgrade_in_event_loop(move |w| {
|
||||||
if search::is_current_version(version) {
|
if search::is_current_version(version) {
|
||||||
search::apply_search(&w, data);
|
search::apply_search(&w, data);
|
||||||
w.global::<SearchState>().set_loading(false);
|
w.global::<SearchState>().set_loading(false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
artwork::spawn_loads(jobs, weak.clone(), image_cache);
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::error!("[qbz-slint] search load failed: {e}");
|
log::error!("[qbz-slint] search load failed: {e}");
|
||||||
@@ -597,6 +599,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
let runtime = app_runtime.clone();
|
let runtime = app_runtime.clone();
|
||||||
let weak = window.as_weak();
|
let weak = window.as_weak();
|
||||||
let handle = tokio_rt.handle().clone();
|
let handle = tokio_rt.handle().clone();
|
||||||
|
let image_cache = image_cache.clone();
|
||||||
window.global::<SearchActions>().on_load_more(move |tab| {
|
window.global::<SearchActions>().on_load_more(move |tab| {
|
||||||
let Some(w) = weak.upgrade() else {
|
let Some(w) = weak.upgrade() else {
|
||||||
return;
|
return;
|
||||||
@@ -614,12 +617,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
} as u32;
|
} as u32;
|
||||||
let runtime = runtime.clone();
|
let runtime = runtime.clone();
|
||||||
let weak = weak.clone();
|
let weak = weak.clone();
|
||||||
|
let image_cache = image_cache.clone();
|
||||||
handle.spawn(async move {
|
handle.spawn(async move {
|
||||||
match search::load_more(&runtime, &query, category, offset).await {
|
match search::load_more(&runtime, &query, category, offset).await {
|
||||||
Ok(more) => {
|
Ok(more) => {
|
||||||
|
let jobs = search::artwork_jobs_for_more(&more, offset as usize);
|
||||||
let _ = weak.upgrade_in_event_loop(move |w| {
|
let _ = weak.upgrade_in_event_loop(move |w| {
|
||||||
search::append_results(&w, more);
|
search::append_results(&w, more);
|
||||||
});
|
});
|
||||||
|
artwork::spawn_loads(jobs, weak.clone(), image_cache);
|
||||||
}
|
}
|
||||||
Err(e) => log::error!("[qbz-slint] search load-more failed: {e}"),
|
Err(e) => log::error!("[qbz-slint] search load-more failed: {e}"),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ use qbz_core::FrontendAdapter;
|
|||||||
use qbz_models::{Album, Artist, MostPopularItem, Playlist, SearchAllResults, Track};
|
use qbz_models::{Album, Artist, MostPopularItem, Playlist, SearchAllResults, Track};
|
||||||
use slint::{ComponentHandle, Model, ModelRc, VecModel};
|
use slint::{ComponentHandle, Model, ModelRc, VecModel};
|
||||||
|
|
||||||
|
use crate::artwork::{ArtworkJob, ArtworkTarget};
|
||||||
use crate::{AlbumCardItem, AppWindow, SearchPlaylistItem, SearchState, SearchTrackItem, SlimItem};
|
use crate::{AlbumCardItem, AppWindow, SearchPlaylistItem, SearchState, SearchTrackItem, SlimItem};
|
||||||
|
|
||||||
thread_local! {
|
thread_local! {
|
||||||
@@ -515,6 +516,96 @@ pub fn append_results(window: &AppWindow, more: MoreRows) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ==================== Artwork jobs ====================
|
||||||
|
|
||||||
|
/// Build artwork download jobs for a freshly applied `SearchData` — one
|
||||||
|
/// per result row that carries a cover URL.
|
||||||
|
pub fn artwork_jobs(data: &SearchData) -> Vec<ArtworkJob> {
|
||||||
|
let mut jobs = Vec::new();
|
||||||
|
for (idx, row) in data.albums.iter().enumerate() {
|
||||||
|
if !row.artwork_url.is_empty() {
|
||||||
|
jobs.push(ArtworkJob {
|
||||||
|
target: ArtworkTarget::SearchAlbum { idx },
|
||||||
|
url: row.artwork_url.clone(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (idx, row) in data.tracks.iter().enumerate() {
|
||||||
|
if !row.artwork_url.is_empty() {
|
||||||
|
jobs.push(ArtworkJob {
|
||||||
|
target: ArtworkTarget::SearchTrack { idx },
|
||||||
|
url: row.artwork_url.clone(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (idx, row) in data.artists.iter().enumerate() {
|
||||||
|
if !row.artwork_url.is_empty() {
|
||||||
|
jobs.push(ArtworkJob {
|
||||||
|
target: ArtworkTarget::SearchArtist { idx },
|
||||||
|
url: row.artwork_url.clone(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (idx, row) in data.playlists.iter().enumerate() {
|
||||||
|
if !row.artwork_url.is_empty() {
|
||||||
|
jobs.push(ArtworkJob {
|
||||||
|
target: ArtworkTarget::SearchPlaylist { idx },
|
||||||
|
url: row.artwork_url.clone(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
jobs
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Build artwork jobs for a load-more page, targeting the rows that were
|
||||||
|
/// just appended (`start` is the index of the first appended row).
|
||||||
|
pub fn artwork_jobs_for_more(more: &MoreRows, start: usize) -> Vec<ArtworkJob> {
|
||||||
|
let mut jobs = Vec::new();
|
||||||
|
match more {
|
||||||
|
MoreRows::Albums(rows) => {
|
||||||
|
for (i, row) in rows.iter().enumerate() {
|
||||||
|
if !row.artwork_url.is_empty() {
|
||||||
|
jobs.push(ArtworkJob {
|
||||||
|
target: ArtworkTarget::SearchAlbum { idx: start + i },
|
||||||
|
url: row.artwork_url.clone(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
MoreRows::Tracks(rows) => {
|
||||||
|
for (i, row) in rows.iter().enumerate() {
|
||||||
|
if !row.artwork_url.is_empty() {
|
||||||
|
jobs.push(ArtworkJob {
|
||||||
|
target: ArtworkTarget::SearchTrack { idx: start + i },
|
||||||
|
url: row.artwork_url.clone(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
MoreRows::Artists(rows) => {
|
||||||
|
for (i, row) in rows.iter().enumerate() {
|
||||||
|
if !row.artwork_url.is_empty() {
|
||||||
|
jobs.push(ArtworkJob {
|
||||||
|
target: ArtworkTarget::SearchArtist { idx: start + i },
|
||||||
|
url: row.artwork_url.clone(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
MoreRows::Playlists(rows) => {
|
||||||
|
for (i, row) in rows.iter().enumerate() {
|
||||||
|
if !row.artwork_url.is_empty() {
|
||||||
|
jobs.push(ArtworkJob {
|
||||||
|
target: ArtworkTarget::SearchPlaylist { idx: start + i },
|
||||||
|
url: row.artwork_url.clone(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
jobs
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|||||||
Reference in new issue
Block a user