Files
qbz/crates/qbz-slint/src/custom_artwork.rs
T
vicrodh c4b6acb760 fix(slint): smooth sticky scroll + artwork right-click menu
Two unrelated things landing together.

Sticky scroll fix:
  - The natural-top binding on both jump-nav and the sidebar reads
    from `body-row.absolute-position.y` (a live, scroll-aware
    lookup). The y formula was `max(natural-top + viewport-y, ...)`
    — adding viewport-y on top of an already scroll-aware value
    double-counted the scroll. During mid-scroll the bar drifted
    ahead of the body row, opening a visible gap and briefly
    overlapping the header.
  - Drops `+ viewport-y` from both formulas. natural-top alone IS
    the viewport-relative y the elements want to occupy; the
    max() clamp just keeps them from going above the top.
  - Initial position and sticky position are unchanged; only the
    transition is fixed.

Artwork right-click menu (artist portrait + album cover):
  - Both headers get a TouchArea on the artwork that opens a
    PopupWindow + ContextMenu on right-click, positioned at the
    cursor. Mirrors Tauri's `oncontextmenu` flow on the artist
    portrait and album cover.
  - Menu items match Tauri 1:1: Add image / cover, Change + Remove
    (when a custom image is registered), divider, Open in
    browser, Save as.
  - New `crate::custom_artwork` JSON-backed store at
    `<data-dir>/qbz/custom_artwork.json` keyed by artist name +
    album id (mirror of the Tauri library's
    `v2_library_set/remove_custom_*` persistence).
  - ArtistState / AlbumState expose `artwork-url` (network URL for
    Open / Save) + `has-custom-image` / `has-custom-cover` (drives
    the Add vs Change/Remove menu rendering), both populated from
    Rust at apply_artist / apply_album time.
  - ArtworkActions global + main.rs handlers:
      * open-in-browser → `open::that(url)`.
      * save-as → `rfd::AsyncFileDialog::save_file()` + reqwest
        download + tokio::fs::write.
      * add-custom → `rfd::AsyncFileDialog::pick_file()` then
        `custom_artwork::set_*` + flip the has-custom flag.
      * remove-custom → `custom_artwork::remove_*` + reset flag.
  - rfd added with xdg-portal + tokio features. external-link and
    image-plus SVGs ported from Lucide.

Custom-image RENDERING (using the picked file in place of the
network artwork) is deferred — the store persists the choice and
the menu reflects it, but the displayed image still comes from
Qobuz. Next pass.

cc
2026-05-20 18:24:39 -06:00

109 lines
3.3 KiB
Rust

//! Persistent overrides for artist photos and album covers.
//!
//! The right-click menu on each artwork lets the user pick a local
//! image to replace what Qobuz serves. The replacement is kept in a
//! small JSON store at `<data-dir>/qbz/custom_artwork.json`, keyed by
//! artist name and album id. Two pairs of helpers mirror the
//! `v2_library_set/remove_custom_*` Tauri commands.
//!
//! The store records the absolute file path of the picked image. The
//! decode/render path that actually shows the override lives in
//! `crate::artwork`; this module just owns the persistence layer.
use std::collections::HashMap;
use std::path::PathBuf;
use serde::{Deserialize, Serialize};
#[derive(Default, Serialize, Deserialize)]
struct Store {
#[serde(default)]
artists: HashMap<String, String>,
#[serde(default)]
albums: HashMap<String, String>,
}
fn store_path() -> Option<PathBuf> {
Some(
dirs::data_dir()?
.join("qbz")
.join("custom_artwork.json"),
)
}
fn load_store() -> Store {
let Some(path) = store_path() else {
return Store::default();
};
match std::fs::read(&path) {
Ok(bytes) => serde_json::from_slice(&bytes).unwrap_or_default(),
Err(_) => Store::default(),
}
}
fn write_store(store: &Store) {
let Some(path) = store_path() else {
return;
};
if let Some(parent) = path.parent() {
if let Err(e) = std::fs::create_dir_all(parent) {
log::warn!("[qbz-slint] custom-artwork dir failed: {e}");
return;
}
}
match serde_json::to_vec_pretty(store) {
Ok(json) => {
if let Err(e) = std::fs::write(&path, json) {
log::warn!("[qbz-slint] custom-artwork write failed: {e}");
}
}
Err(e) => log::warn!("[qbz-slint] custom-artwork serialize failed: {e}"),
}
}
/// Returns the absolute file path of the user-picked artist image,
/// if one is registered. Called by the artist controller to swap the
/// header portrait + flip the menu's "Add" / "Change" / "Remove"
/// rendering.
#[allow(dead_code)] // wired by artist controller
pub fn artist_image(name: &str) -> Option<String> {
load_store().artists.get(name).cloned()
}
#[allow(dead_code)] // wired by artist controller
pub fn set_artist_image(name: &str, path: &str) {
let mut store = load_store();
store.artists.insert(name.to_string(), path.to_string());
write_store(&store);
}
#[allow(dead_code)] // wired by artist controller
pub fn remove_artist_image(name: &str) {
let mut store = load_store();
if store.artists.remove(name).is_some() {
write_store(&store);
}
}
/// Returns the absolute file path of the user-picked album cover,
/// if one is registered. Called by the album controller.
#[allow(dead_code)] // wired by album controller
pub fn album_cover(album_id: &str) -> Option<String> {
load_store().albums.get(album_id).cloned()
}
#[allow(dead_code)] // wired by album controller
pub fn set_album_cover(album_id: &str, path: &str) {
let mut store = load_store();
store.albums.insert(album_id.to_string(), path.to_string());
write_store(&store);
}
#[allow(dead_code)] // wired by album controller
pub fn remove_album_cover(album_id: &str) {
let mut store = load_store();
if store.albums.remove(album_id).is_some() {
write_store(&store);
}
}