diff --git a/crates/Cargo.lock b/crates/Cargo.lock index 9080cbe2e..dfcac1040 100644 --- a/crates/Cargo.lock +++ b/crates/Cargo.lock @@ -1803,13 +1803,34 @@ dependencies = [ "crypto-common 0.2.1", ] +[[package]] +name = "dirs" +version = "5.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225" +dependencies = [ + "dirs-sys 0.4.1", +] + [[package]] name = "dirs" version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3e8aa94d75141228480295a7d0e7feb620b1a5ad9f12bc40be62411e38cce4e" dependencies = [ - "dirs-sys", + "dirs-sys 0.5.0", +] + +[[package]] +name = "dirs-sys" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c" +dependencies = [ + "libc", + "option-ext", + "redox_users 0.4.6", + "windows-sys 0.48.0", ] [[package]] @@ -1820,7 +1841,7 @@ checksum = "e01a3366d27ee9890022452ee61b2b63a67e6f13f58900b651ff5665f0bb1fab" dependencies = [ "libc", "option-ext", - "redox_users", + "redox_users 0.5.2", "windows-sys 0.61.2", ] @@ -5228,7 +5249,7 @@ version = "0.1.0" dependencies = [ "base64", "chrono", - "dirs", + "dirs 6.0.0", "log", "md-5", "qbz-audio", @@ -5250,7 +5271,7 @@ dependencies = [ "alsa-sys", "async-trait", "coreaudio-rs 0.14.1", - "dirs", + "dirs 6.0.0", "ebur128", "log", "objc2-core-audio", @@ -5270,7 +5291,7 @@ dependencies = [ name = "qbz-cache" version = "0.1.0" dependencies = [ - "dirs", + "dirs 6.0.0", "filetime", "log", "md-5", @@ -5332,7 +5353,7 @@ dependencies = [ "aes-gcm", "ashpd", "base64", - "dirs", + "dirs 6.0.0", "keyring", "log", "rand 0.10.1", @@ -5361,7 +5382,7 @@ dependencies = [ name = "qbz-library" version = "0.1.0" dependencies = [ - "dirs", + "dirs 6.0.0", "glob", "image", "lofty", @@ -5449,6 +5470,7 @@ name = "qbz-slint" version = "0.1.0" dependencies = [ "async-trait", + "dirs 5.0.1", "env_logger", "image", "log", @@ -5460,6 +5482,8 @@ dependencies = [ "qbz-credentials", "qbz-models", "reqwest", + "serde", + "serde_json", "slint", "slint-build", "tokio", @@ -5474,7 +5498,7 @@ dependencies = [ "axum", "clap", "crossterm", - "dirs", + "dirs 6.0.0", "env_logger", "futures-util", "hostname", @@ -5915,6 +5939,17 @@ dependencies = [ "bitflags 2.11.0", ] +[[package]] +name = "redox_users" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43" +dependencies = [ + "getrandom 0.2.17", + "libredox", + "thiserror 1.0.69", +] + [[package]] name = "redox_users" version = "0.5.2" diff --git a/crates/qbz-slint/Cargo.toml b/crates/qbz-slint/Cargo.toml index ac7bc7225..b1fbc60ed 100644 --- a/crates/qbz-slint/Cargo.toml +++ b/crates/qbz-slint/Cargo.toml @@ -20,6 +20,9 @@ reqwest = { workspace = true } image = { version = "0.25", default-features = false, features = ["jpeg", "png"] } urlencoding = "2" open = "5" +serde = { workspace = true } +serde_json = { workspace = true } +dirs = "5" qbz-app = { path = "../qbz-app" } qbz-audio = { path = "../qbz-audio" } qbz-core = { path = "../qbz-core" } diff --git a/crates/qbz-slint/src/artwork.rs b/crates/qbz-slint/src/artwork.rs index 7ad8a16f3..c7a57d4ab 100644 --- a/crates/qbz-slint/src/artwork.rs +++ b/crates/qbz-slint/src/artwork.rs @@ -35,6 +35,8 @@ pub enum ArtworkTarget { Section { section_idx: usize, album_idx: usize }, /// A card in `HomeState.popular[idx]`. Popular { idx: usize }, + /// A card in `HomeState.recent[idx]`. + Recent { idx: usize }, } /// An artwork download job: which card, and the image URL. @@ -188,5 +190,13 @@ fn apply_artwork( item.artwork = image; popular.set_row_data(idx, item); } + ArtworkTarget::Recent { idx } => { + let recent = home.get_recent(); + let Some(mut item) = recent.row_data(idx) else { + return; + }; + item.artwork = image; + recent.set_row_data(idx, item); + } } } diff --git a/crates/qbz-slint/src/home.rs b/crates/qbz-slint/src/home.rs index 059a5dc86..d23b78720 100644 --- a/crates/qbz-slint/src/home.rs +++ b/crates/qbz-slint/src/home.rs @@ -18,6 +18,7 @@ use crate::{AlbumCardItem, AppWindow, DiscoverSection, HomeState, SlimItem}; pub struct HomeData { pub sections: Vec, pub popular: Vec, + pub recent: Vec, } pub struct SectionData { @@ -85,7 +86,24 @@ where .map(|(index, album)| map_slim(index, album)) .collect(); - Ok(HomeData { sections, popular }) + // Recently played comes from the local play-history store, not the + // discover index. Empty until the playback session records plays. + let recent = crate::recently::load() + .into_iter() + .map(|track| SlimData { + id: track.id, + title: track.title, + subtitle: track.subtitle, + rank: String::new(), + artwork_url: track.artwork_url, + }) + .collect(); + + Ok(HomeData { + sections, + popular, + recent, + }) } fn push_section( @@ -254,22 +272,26 @@ pub fn apply_home(window: &AppWindow, data: HomeData) { }) .collect(); - let popular: Vec = data - .popular - .into_iter() - .map(|slim| SlimItem { - id: slim.id.into(), - title: slim.title.into(), - subtitle: slim.subtitle.into(), - rank: slim.rank.into(), - artwork_url: slim.artwork_url.into(), - artwork: slint::Image::default(), - }) - .collect(); + let to_slim_items = |items: Vec| -> Vec { + items + .into_iter() + .map(|slim| SlimItem { + id: slim.id.into(), + title: slim.title.into(), + subtitle: slim.subtitle.into(), + rank: slim.rank.into(), + artwork_url: slim.artwork_url.into(), + artwork: slint::Image::default(), + }) + .collect() + }; + let popular = to_slim_items(data.popular); + let recent = to_slim_items(data.recent); let state = window.global::(); state.set_sections(ModelRc::new(VecModel::from(sections))); state.set_popular(ModelRc::new(VecModel::from(popular))); + state.set_recent(ModelRc::new(VecModel::from(recent))); } #[cfg(test)] diff --git a/crates/qbz-slint/src/main.rs b/crates/qbz-slint/src/main.rs index e70bff0a6..235b130eb 100644 --- a/crates/qbz-slint/src/main.rs +++ b/crates/qbz-slint/src/main.rs @@ -21,6 +21,7 @@ mod artwork; mod auth; mod commands; mod home; +mod recently; mod settings; use std::sync::Arc; @@ -108,6 +109,16 @@ async fn enter_shell( }) } })); + jobs.extend(data.recent.iter().enumerate().filter_map(|(idx, slim)| { + if slim.artwork_url.is_empty() { + None + } else { + Some(artwork::ArtworkJob { + target: artwork::ArtworkTarget::Recent { idx }, + url: slim.artwork_url.clone(), + }) + } + })); let weak_for_artwork = weak.clone(); let _ = weak.upgrade_in_event_loop(move |w| { home::apply_home(&w, data); diff --git a/crates/qbz-slint/src/recently.rs b/crates/qbz-slint/src/recently.rs new file mode 100644 index 000000000..c43cab174 --- /dev/null +++ b/crates/qbz-slint/src/recently.rs @@ -0,0 +1,70 @@ +//! Recently-played tracks store. +//! +//! A small JSON file at the shared QBZ data path holding the last few +//! played tracks, newest first. The Discover Home renders it as a slim +//! section; the playback session calls [`record`] when a track starts. +//! +//! Until playback is wired the store is simply empty, and the Home +//! section that reads it hides itself — the data path exists end to end +//! so playback only has to call `record`. + +use std::path::PathBuf; + +use serde::{Deserialize, Serialize}; + +/// How many recent tracks to keep. +const MAX_RECENT: usize = 12; + +/// One recently-played track. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct RecentTrack { + pub id: String, + pub title: String, + pub subtitle: String, + pub artwork_url: String, +} + +fn store_path() -> Option { + Some(dirs::data_dir()?.join("qbz").join("recently_played.json")) +} + +/// Load the recently-played tracks, newest first. Returns an empty list +/// when the store does not exist yet or cannot be read. +pub fn load() -> Vec { + let Some(path) = store_path() else { + return Vec::new(); + }; + match std::fs::read(&path) { + Ok(bytes) => serde_json::from_slice(&bytes).unwrap_or_default(), + Err(_) => Vec::new(), + } +} + +/// Record a played track at the front of the list. Deduplicates by id and +/// caps the list at [`MAX_RECENT`]. Called by the playback session when a +/// track starts. +#[allow(dead_code)] // wired by the playback session +pub fn record(track: RecentTrack) { + let Some(path) = store_path() else { + return; + }; + let mut list = load(); + list.retain(|t| t.id != track.id); + list.insert(0, track); + list.truncate(MAX_RECENT); + + if let Some(parent) = path.parent() { + if let Err(e) = std::fs::create_dir_all(parent) { + log::warn!("[qbz-slint] recently-played store dir failed: {e}"); + return; + } + } + match serde_json::to_vec_pretty(&list) { + Ok(json) => { + if let Err(e) = std::fs::write(&path, json) { + log::warn!("[qbz-slint] recently-played write failed: {e}"); + } + } + Err(e) => log::warn!("[qbz-slint] recently-played serialize failed: {e}"), + } +} diff --git a/crates/qbz-slint/ui/discover/HomeView.slint b/crates/qbz-slint/ui/discover/HomeView.slint index f23e7cac9..c653e6ef0 100644 --- a/crates/qbz-slint/ui/discover/HomeView.slint +++ b/crates/qbz-slint/ui/discover/HomeView.slint @@ -188,6 +188,14 @@ export component HomeView inherits Rectangle { } } + if HomeState.recent.length > 0: GridSection { + title: @tr("Recently Played"); + items: HomeState.recent; + item-clicked(id) => { + root.media-action("track", id, "play"); + } + } + if HomeState.popular.length > 0: GridSection { title: @tr("Popular albums"); items: HomeState.popular; diff --git a/crates/qbz-slint/ui/state.slint b/crates/qbz-slint/ui/state.slint index 68b7a771f..13309937f 100644 --- a/crates/qbz-slint/ui/state.slint +++ b/crates/qbz-slint/ui/state.slint @@ -37,6 +37,7 @@ export struct SlimItem { export global HomeState { in property <[DiscoverSection]> sections: []; in property <[SlimItem]> popular: []; + in property <[SlimItem]> recent: []; in property loading: false; }