feat(slint): lyrics controls flyout, per-user prefs, cache settings row
Sidebar header gains the controls flyout (auto-follow, font size on the 280px-adapted scale, dimming, active color, uppercase, copy lyrics, reset). Every setting persists per user via the new lyrics_prefs store (loaded on all session activation paths, applied into LyricsState, Tauri defaults). Settings gains the lyrics cache row with honest per-user stats and clear (the F1 fix shipped in qbz-lyrics). Parity and @tr sweep over the sidebar surfaces per the review inventory. Closes the S1-S5 implementation run of the Lyrics epic: Qobuz-first word-synced lyrics (real per-word timestamps - richer than Tauri's line-level estimation), LRCLIB/lyrics.ovh fallback, shared per-user cache with additive wsync column, 30Hz sync engine with word-anchored karaoke fill, auto-center scroll, and QConnect peer-following. cc
This commit is contained in:
9 files changed
+867
-43
No files matched your search
@@ -168,6 +168,80 @@ pub fn teardown() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Join the current doc's lines with `\n` and copy them to the clipboard —
|
||||
/// the flyout's "Copy lyrics" action (Tauri `copyLyrics`,
|
||||
/// `LyricsControlsPopover.svelte:39-55`). The button is gated UI-side on
|
||||
/// ready + lines>0; an empty doc is a silent no-op. The clipboard write is
|
||||
/// fire-and-forget on a blocking thread (arboard), so the toast reports the
|
||||
/// dispatch like Tauri reports its `writeText` success.
|
||||
pub fn copy_current_lyrics(weak: &slint::Weak<AppWindow>) {
|
||||
let text = with_current_doc(|doc| {
|
||||
doc.map(|d| {
|
||||
d.lines
|
||||
.iter()
|
||||
.map(|line| line.text.as_str())
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")
|
||||
})
|
||||
})
|
||||
.unwrap_or_default();
|
||||
if text.is_empty() {
|
||||
return;
|
||||
}
|
||||
crate::share::copy_to_clipboard(text);
|
||||
crate::toast::success_weak(weak, "Lyrics copied");
|
||||
}
|
||||
|
||||
/// Refresh the Settings cache row from the REAL per-user lyrics.db (F1
|
||||
/// fix-forward: entries AND size from the bound per-user path, via
|
||||
/// `qbz_lyrics` cache_stats). Pushes `cache-loaded=false` when the service
|
||||
/// or cache is unavailable (the row then shows no stats line).
|
||||
pub fn refresh_cache_stats(handle: &tokio::runtime::Handle, weak: slint::Weak<AppWindow>) {
|
||||
let Some(service) = SERVICE.get().cloned() else {
|
||||
return;
|
||||
};
|
||||
handle.spawn(async move {
|
||||
let stats = service.cache_stats().await;
|
||||
let _ = weak.upgrade_in_event_loop(move |w| {
|
||||
let state = w.global::<LyricsState>();
|
||||
match stats {
|
||||
Ok(stats) => {
|
||||
state.set_cache_entries_text(stats.entries.to_string().into());
|
||||
state.set_cache_size(
|
||||
crate::offline_manager::human_size(stats.size_bytes).into(),
|
||||
);
|
||||
state.set_cache_loaded(true);
|
||||
}
|
||||
Err(e) => {
|
||||
log::warn!("[qbz-slint] lyrics cache stats failed: {e}");
|
||||
state.set_cache_loaded(false);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/// Clear the per-user lyrics cache (Settings row action), then re-push the
|
||||
/// stats. Mirrors Tauri's `v2_lyrics_clear_cache` flow with a result toast.
|
||||
pub fn clear_cache(handle: &tokio::runtime::Handle, weak: slint::Weak<AppWindow>) {
|
||||
let Some(service) = SERVICE.get().cloned() else {
|
||||
return;
|
||||
};
|
||||
let handle_for_refresh = handle.clone();
|
||||
handle.spawn(async move {
|
||||
match service.clear_cache().await {
|
||||
Ok(()) => {
|
||||
crate::toast::success_weak(&weak, "Lyrics cache cleared");
|
||||
refresh_cache_stats(&handle_for_refresh, weak);
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("[qbz-slint] lyrics cache clear failed: {e}");
|
||||
crate::toast::error_weak(&weak, "Failed to clear lyrics cache");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Qobuz vs non-Qobuz from the queue track's source tag. `qobuz_download`
|
||||
/// rows carry the REAL Qobuz catalog id (`local_queue_track`), so the Qobuz
|
||||
/// primary applies to them too; local user files / ephemeral folders / Plex
|
||||
|
||||
@@ -0,0 +1,347 @@
|
||||
//! Per-user lyrics display-prefs persistence (S5).
|
||||
//!
|
||||
//! Mirrors Tauri's `lyricsDisplayStore` (per-user localStorage key
|
||||
//! `qbz-lyrics-display`, `lyricsDisplayStore.ts:35`): the six display prefs
|
||||
//! the controls flyout edits, persisted per user so different Qobuz accounts
|
||||
//! keep independent settings. Field values use Tauri's exact strings
|
||||
//! (`font: 'line-seed-jp'`, `fontSize: 'medium'`, ...) so the shape stays
|
||||
//! diff-able against the Tauri store; defaults are Tauri's
|
||||
//! (`lyricsDisplayStore.ts:37-44`) and loads are sanitized the same way
|
||||
//! (`:64-85` — any unknown value falls back to its default, `activeColor`
|
||||
//! must be `#RRGGBB` or empty).
|
||||
//!
|
||||
//! Storage follows the `myqbz_view_prefs.rs` template — one tiny per-user
|
||||
//! JSON, synchronous best-effort IO:
|
||||
//!
|
||||
//! <data_dir>/qbz/users/<user_id>/lyrics_prefs.json
|
||||
//!
|
||||
//! Lifecycle:
|
||||
//! - `init_for_user` binds the store on session activation (all paths run
|
||||
//! through `init_shell_for_user`), then the caller seeds the UI via
|
||||
//! [`apply_to_ui`] on the event loop.
|
||||
//! - The flyout fires `LyricsState.prefs-changed()` after any mutation →
|
||||
//! [`persist_from_ui`] reads the in-out props back and saves.
|
||||
//! - `LyricsState.reset-prefs()` → [`reset`] re-seeds the defaults and
|
||||
//! persists them (Tauri `resetLyricsDisplay`).
|
||||
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{LazyLock, Mutex};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use slint::ComponentHandle;
|
||||
|
||||
use crate::{AppWindow, LyricsState};
|
||||
|
||||
/// The active user id, set by `init_for_user`. `None` before login (the
|
||||
/// store degrades to defaults).
|
||||
static USER_ID: LazyLock<Mutex<Option<u64>>> = LazyLock::new(|| Mutex::new(None));
|
||||
|
||||
/// The persisted pref set — field names and value strings match Tauri's
|
||||
/// `LyricsDisplayPrefs` (`lyricsDisplayStore.ts`).
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct LyricsPrefs {
|
||||
#[serde(default = "d_true")]
|
||||
pub auto_follow: bool,
|
||||
#[serde(default = "d_font")]
|
||||
pub font: String, // system | line-seed-jp | montserrat | noto-sans | source-sans-3
|
||||
#[serde(default = "d_size")]
|
||||
pub font_size: String, // small | medium | large | xl
|
||||
#[serde(default = "d_dimming")]
|
||||
pub dimming: String, // off | soft | strong
|
||||
#[serde(default)]
|
||||
pub active_color: String, // "#RRGGBB" or "" = theme accent
|
||||
#[serde(default)]
|
||||
pub uppercase: bool,
|
||||
}
|
||||
|
||||
fn d_true() -> bool {
|
||||
true
|
||||
}
|
||||
fn d_font() -> String {
|
||||
"system".to_string()
|
||||
}
|
||||
fn d_size() -> String {
|
||||
"medium".to_string()
|
||||
}
|
||||
fn d_dimming() -> String {
|
||||
"strong".to_string()
|
||||
}
|
||||
|
||||
impl Default for LyricsPrefs {
|
||||
/// Tauri defaults (`lyricsDisplayStore.ts:37-44`).
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
auto_follow: true,
|
||||
font: d_font(),
|
||||
font_size: d_size(),
|
||||
dimming: d_dimming(),
|
||||
active_color: String::new(),
|
||||
uppercase: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const FONTS: [&str; 5] = ["system", "line-seed-jp", "montserrat", "noto-sans", "source-sans-3"];
|
||||
const SIZES: [&str; 4] = ["small", "medium", "large", "xl"];
|
||||
const DIMMINGS: [&str; 3] = ["off", "soft", "strong"];
|
||||
|
||||
/// `#RRGGBB` (or empty = theme) — the Tauri validator
|
||||
/// (`lyricsDisplayStore.ts:46-52`).
|
||||
fn valid_color(value: &str) -> bool {
|
||||
value.is_empty()
|
||||
|| (value.len() == 7
|
||||
&& value.starts_with('#')
|
||||
&& value[1..].chars().all(|c| c.is_ascii_hexdigit()))
|
||||
}
|
||||
|
||||
impl LyricsPrefs {
|
||||
/// Clamp every field to its known value set (Tauri's sanitize-on-load,
|
||||
/// `lyricsDisplayStore.ts:64-85`): unknown values fall back to defaults.
|
||||
fn sanitized(mut self) -> Self {
|
||||
if !FONTS.contains(&self.font.as_str()) {
|
||||
self.font = d_font();
|
||||
}
|
||||
if !SIZES.contains(&self.font_size.as_str()) {
|
||||
self.font_size = d_size();
|
||||
}
|
||||
if !DIMMINGS.contains(&self.dimming.as_str()) {
|
||||
self.dimming = d_dimming();
|
||||
}
|
||||
if !valid_color(&self.active_color) {
|
||||
self.active_color.clear();
|
||||
}
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// `<data_dir>/qbz/users/<user_id>/lyrics_prefs.json` for the active user.
|
||||
fn store_path() -> Option<PathBuf> {
|
||||
let user_id = (*USER_ID.lock().ok()?)?;
|
||||
Some(
|
||||
dirs::data_dir()?
|
||||
.join("qbz")
|
||||
.join("users")
|
||||
.join(user_id.to_string())
|
||||
.join("lyrics_prefs.json"),
|
||||
)
|
||||
}
|
||||
|
||||
/// Bind the store to `user_id` on session activation.
|
||||
pub fn init_for_user(user_id: u64) {
|
||||
if let Ok(mut guard) = USER_ID.lock() {
|
||||
*guard = Some(user_id);
|
||||
}
|
||||
}
|
||||
|
||||
/// Load the stored prefs, sanitized; missing/unreadable/unparseable file →
|
||||
/// defaults.
|
||||
pub fn load() -> LyricsPrefs {
|
||||
let Some(path) = store_path() else {
|
||||
return LyricsPrefs::default();
|
||||
};
|
||||
match std::fs::read(&path) {
|
||||
Ok(bytes) => serde_json::from_slice::<LyricsPrefs>(&bytes)
|
||||
.map(LyricsPrefs::sanitized)
|
||||
.unwrap_or_default(),
|
||||
Err(_) => LyricsPrefs::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Persist the prefs (best-effort — failures are logged).
|
||||
pub fn save(prefs: &LyricsPrefs) {
|
||||
let Some(path) = store_path() else {
|
||||
log::warn!("[qbz-slint] lyrics prefs: no active user, not saving");
|
||||
return;
|
||||
};
|
||||
if let Some(parent) = path.parent() {
|
||||
if let Err(e) = std::fs::create_dir_all(parent) {
|
||||
log::error!("[qbz-slint] lyrics prefs: create dir failed: {e}");
|
||||
return;
|
||||
}
|
||||
}
|
||||
match serde_json::to_vec_pretty(prefs) {
|
||||
Ok(json) => {
|
||||
if let Err(e) = std::fs::write(&path, json) {
|
||||
log::error!("[qbz-slint] lyrics prefs: write failed: {e}");
|
||||
}
|
||||
}
|
||||
Err(e) => log::error!("[qbz-slint] lyrics prefs: serialize failed: {e}"),
|
||||
}
|
||||
}
|
||||
|
||||
// ---- LyricsState <-> LyricsPrefs mapping (UI thread only) ------------------
|
||||
|
||||
fn font_index(font: &str) -> i32 {
|
||||
FONTS.iter().position(|f| *f == font).unwrap_or(0) as i32
|
||||
}
|
||||
|
||||
fn size_index(size: &str) -> i32 {
|
||||
SIZES.iter().position(|s| *s == size).unwrap_or(1) as i32
|
||||
}
|
||||
|
||||
fn dimming_index(dimming: &str) -> i32 {
|
||||
DIMMINGS.iter().position(|d| *d == dimming).unwrap_or(2) as i32
|
||||
}
|
||||
|
||||
fn parse_color(hex: &str) -> Option<slint::Color> {
|
||||
if hex.len() != 7 || !hex.starts_with('#') {
|
||||
return None;
|
||||
}
|
||||
let r = u8::from_str_radix(&hex[1..3], 16).ok()?;
|
||||
let g = u8::from_str_radix(&hex[3..5], 16).ok()?;
|
||||
let b = u8::from_str_radix(&hex[5..7], 16).ok()?;
|
||||
Some(slint::Color::from_rgb_u8(r, g, b))
|
||||
}
|
||||
|
||||
fn format_color(color: slint::Color) -> String {
|
||||
format!("#{:02x}{:02x}{:02x}", color.red(), color.green(), color.blue())
|
||||
}
|
||||
|
||||
/// Seed `LyricsState` from a loaded pref set. UI thread only.
|
||||
pub fn apply_to_ui(window: &AppWindow, prefs: &LyricsPrefs) {
|
||||
let state = window.global::<LyricsState>();
|
||||
state.set_auto_follow(prefs.auto_follow);
|
||||
state.set_font_index(font_index(&prefs.font));
|
||||
state.set_size_index(size_index(&prefs.font_size));
|
||||
state.set_dimming_mode(dimming_index(&prefs.dimming));
|
||||
state.set_uppercase(prefs.uppercase);
|
||||
match parse_color(&prefs.active_color) {
|
||||
Some(color) => {
|
||||
state.set_use_custom_color(true);
|
||||
state.set_custom_color(color);
|
||||
}
|
||||
None => state.set_use_custom_color(false),
|
||||
}
|
||||
}
|
||||
|
||||
/// Read the in-out props back into a pref set and persist — the
|
||||
/// `prefs-changed()` handler. UI thread only.
|
||||
pub fn persist_from_ui(window: &AppWindow) {
|
||||
let state = window.global::<LyricsState>();
|
||||
let prefs = LyricsPrefs {
|
||||
auto_follow: state.get_auto_follow(),
|
||||
font: FONTS
|
||||
.get(state.get_font_index().max(0) as usize)
|
||||
.copied()
|
||||
.unwrap_or("system")
|
||||
.to_string(),
|
||||
font_size: SIZES
|
||||
.get(state.get_size_index().max(0) as usize)
|
||||
.copied()
|
||||
.unwrap_or("medium")
|
||||
.to_string(),
|
||||
dimming: DIMMINGS
|
||||
.get(state.get_dimming_mode().max(0) as usize)
|
||||
.copied()
|
||||
.unwrap_or("strong")
|
||||
.to_string(),
|
||||
active_color: if state.get_use_custom_color() {
|
||||
format_color(state.get_custom_color())
|
||||
} else {
|
||||
String::new()
|
||||
},
|
||||
uppercase: state.get_uppercase(),
|
||||
};
|
||||
save(&prefs);
|
||||
}
|
||||
|
||||
/// Restore + persist the Tauri defaults — the `reset-prefs()` handler
|
||||
/// (Tauri `resetLyricsDisplay`). UI thread only.
|
||||
pub fn reset(window: &AppWindow) {
|
||||
let prefs = LyricsPrefs::default();
|
||||
apply_to_ui(window, &prefs);
|
||||
save(&prefs);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn defaults_match_tauri() {
|
||||
let p = LyricsPrefs::default();
|
||||
assert!(p.auto_follow);
|
||||
assert_eq!(p.font, "system");
|
||||
assert_eq!(p.font_size, "medium");
|
||||
assert_eq!(p.dimming, "strong");
|
||||
assert_eq!(p.active_color, "");
|
||||
assert!(!p.uppercase);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_json_deserializes_to_defaults() {
|
||||
let p: LyricsPrefs = serde_json::from_str("{}").expect("empty object deserializes");
|
||||
assert_eq!(p, LyricsPrefs::default());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sanitize_clamps_unknown_values() {
|
||||
let p = LyricsPrefs {
|
||||
auto_follow: false,
|
||||
font: "comic-sans".into(),
|
||||
font_size: "huge".into(),
|
||||
dimming: "max".into(),
|
||||
active_color: "purple".into(),
|
||||
uppercase: true,
|
||||
}
|
||||
.sanitized();
|
||||
assert!(!p.auto_follow); // bools pass through
|
||||
assert!(p.uppercase);
|
||||
assert_eq!(p.font, "system");
|
||||
assert_eq!(p.font_size, "medium");
|
||||
assert_eq!(p.dimming, "strong");
|
||||
assert_eq!(p.active_color, "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sanitize_keeps_valid_values() {
|
||||
let p = LyricsPrefs {
|
||||
auto_follow: false,
|
||||
font: "line-seed-jp".into(),
|
||||
font_size: "xl".into(),
|
||||
dimming: "off".into(),
|
||||
active_color: "#8b5cf6".into(),
|
||||
uppercase: false,
|
||||
}
|
||||
.sanitized();
|
||||
assert_eq!(p.font, "line-seed-jp");
|
||||
assert_eq!(p.font_size, "xl");
|
||||
assert_eq!(p.dimming, "off");
|
||||
assert_eq!(p.active_color, "#8b5cf6");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn color_validation() {
|
||||
assert!(valid_color(""));
|
||||
assert!(valid_color("#8b5cf6"));
|
||||
assert!(valid_color("#FFFFFF"));
|
||||
assert!(!valid_color("#fff"));
|
||||
assert!(!valid_color("8b5cf6"));
|
||||
assert!(!valid_color("#8b5cg6"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn index_round_trips() {
|
||||
for (i, f) in FONTS.iter().enumerate() {
|
||||
assert_eq!(font_index(f), i as i32);
|
||||
}
|
||||
for (i, s) in SIZES.iter().enumerate() {
|
||||
assert_eq!(size_index(s), i as i32);
|
||||
}
|
||||
for (i, d) in DIMMINGS.iter().enumerate() {
|
||||
assert_eq!(dimming_index(d), i as i32);
|
||||
}
|
||||
// Unknowns fall back to the defaults' indices.
|
||||
assert_eq!(font_index("nope"), 0);
|
||||
assert_eq!(size_index("nope"), 1);
|
||||
assert_eq!(dimming_index("nope"), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn color_round_trip() {
|
||||
let c = parse_color("#8b5cf6").expect("parses");
|
||||
assert_eq!(format_color(c), "#8b5cf6");
|
||||
assert!(parse_color("").is_none());
|
||||
assert!(parse_color("#xyzxyz").is_none());
|
||||
}
|
||||
}
|
||||
@@ -63,6 +63,7 @@ mod local_library;
|
||||
mod local_playlist;
|
||||
mod local_library_settings;
|
||||
mod lyrics;
|
||||
mod lyrics_prefs;
|
||||
mod lyrics_sync;
|
||||
mod media_controls;
|
||||
mod locallibrary_prefs;
|
||||
@@ -158,6 +159,18 @@ fn init_shell_for_user(
|
||||
// open, cleared on delete (spec 12 §18).
|
||||
myqbz_view_prefs::init_for_user(user_id);
|
||||
|
||||
// Bind the lyrics display prefs (auto-follow / font / size / dimming /
|
||||
// active color / uppercase — per-user lyrics_prefs.json) and seed them
|
||||
// into LyricsState so the sidebar + controls flyout reflect the
|
||||
// persisted values from the first open (defaults = Tauri's).
|
||||
lyrics_prefs::init_for_user(user_id);
|
||||
{
|
||||
let prefs = lyrics_prefs::load();
|
||||
let _ = weak.upgrade_in_event_loop(move |w| {
|
||||
lyrics_prefs::apply_to_ui(&w, &prefs);
|
||||
});
|
||||
}
|
||||
|
||||
// Create the system tray from this user's persisted settings (gated by
|
||||
// enable_tray). Reflects the chosen icon variant. On Linux the ksni
|
||||
// service runs on its own thread; macOS/Windows are no-ops until the
|
||||
@@ -6427,6 +6440,50 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
});
|
||||
}
|
||||
|
||||
// S5 lyrics controls flyout + prefs + settings cache row.
|
||||
{
|
||||
// Persist any flyout mutation (the flyout writes the in-out props
|
||||
// directly for live preview, then fires prefs-changed).
|
||||
let weak = window.as_weak();
|
||||
window.global::<LyricsState>().on_prefs_changed(move || {
|
||||
if let Some(w) = weak.upgrade() {
|
||||
lyrics_prefs::persist_from_ui(&w);
|
||||
}
|
||||
});
|
||||
}
|
||||
{
|
||||
// Reset to the Tauri defaults + persist (flyout footer).
|
||||
let weak = window.as_weak();
|
||||
window.global::<LyricsState>().on_reset_prefs(move || {
|
||||
if let Some(w) = weak.upgrade() {
|
||||
lyrics_prefs::reset(&w);
|
||||
}
|
||||
});
|
||||
}
|
||||
{
|
||||
// Copy the current lyrics to the clipboard (flyout footer).
|
||||
let weak = window.as_weak();
|
||||
window.global::<LyricsState>().on_copy_lyrics(move || {
|
||||
lyrics::copy_current_lyrics(&weak);
|
||||
});
|
||||
}
|
||||
{
|
||||
// Settings > Offline lyrics-cache row: stats refresh on section
|
||||
// mount + clear action (F1: stats from the real per-user DB).
|
||||
let weak = window.as_weak();
|
||||
let handle = tokio_rt.handle().clone();
|
||||
window.global::<LyricsState>().on_cache_refresh(move || {
|
||||
lyrics::refresh_cache_stats(&handle, weak.clone());
|
||||
});
|
||||
}
|
||||
{
|
||||
let weak = window.as_weak();
|
||||
let handle = tokio_rt.handle().clone();
|
||||
window.global::<LyricsState>().on_cache_clear(move || {
|
||||
lyrics::clear_cache(&handle, weak.clone());
|
||||
});
|
||||
}
|
||||
|
||||
// S4 lyrics sync engine — a UI-thread `slint::Timer` driving
|
||||
// `LyricsState.active-index` / `line-progress` at ~30Hz while the panel
|
||||
// is open + the doc is synced + playback is live (idle gate polling
|
||||
|
||||
@@ -48,7 +48,9 @@ fn current_filters() -> Filters {
|
||||
|
||||
// --- Formatting ---------------------------------------------------------
|
||||
|
||||
fn human_size(bytes: u64) -> String {
|
||||
// pub(crate): also formats the lyrics-cache size in the Settings row
|
||||
// (crate::lyrics::refresh_cache_stats).
|
||||
pub(crate) fn human_size(bytes: u64) -> String {
|
||||
let b = bytes as f64;
|
||||
if bytes >= GB {
|
||||
format!("{:.1} GB", b / GB as f64)
|
||||
|
||||
Reference in new issue
Block a user