feat(slint): Settings Audio/Playback 1:1 with Tauri
Rebuild both Settings panels to match the Tauri app — every option present, on the correct tab, and fully functional (persisted and applied to the live player). Audio: streaming quality, limit-quality-to-device, maximum sample rate, backend, output device, ALSA plugin, hardware volume, exclusive mode, reserve DAC, DAC passthrough, force bit-perfect, allow quality fallback, sync-on-startup, lock output device, and a reset button. Conditional rows (ALSA plugin, hardware volume, max sample rate, force bit-perfect, lock output device) are gated on Rust-computed flags. Playback: continue-after-track, show context icon, gapless, restore session, resume position, stream uncached, initial buffer size (slider), streaming only, quality-retry behavior. Note gapless and the streaming options live on the Playback tab though the qbz-audio struct stores them. - ui_prefs.rs: JSON store for the frontend-only streaming-quality pref - QbzSlider primitive for the integer buffer-size control - volume normalization row removed (Tauri hides it) Caveat: toggling reserve-DAC persists and is read live, but re-arming the device reservation guard mid-session needs a player-side API that does not exist yet; it takes effect on next launch. cc
This commit is contained in:
9 files changed
+799
-138
No files matched your search
@@ -0,0 +1,138 @@
|
||||
//! Tiny JSON-backed UI preference store.
|
||||
//!
|
||||
//! Some settings the Tauri app exposes are not part of any domain store
|
||||
//! (`AudioSettings`, `PlaybackPreferences`). Streaming Quality is one: it
|
||||
//! is a pure UI/request preference. Rather than thread it into a domain
|
||||
//! store, this module persists those preferences to a small JSON file
|
||||
//! next to the other QBZ data (`<data_dir>/qbz/ui_prefs.json`).
|
||||
//!
|
||||
//! The store is intentionally minimal — read-modify-write the whole file
|
||||
//! on every set. The file is tiny and writes are rare (a settings change).
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Streaming-quality tiers, mirroring the Tauri app's dropdown. The
|
||||
/// `format_id` is the Qobuz format identifier the request layer expects.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct StreamingQuality {
|
||||
/// Stable key persisted to JSON.
|
||||
pub key: &'static str,
|
||||
/// Human-facing label for the dropdown.
|
||||
pub label: &'static str,
|
||||
}
|
||||
|
||||
/// The four streaming-quality options, in on-screen order.
|
||||
pub const STREAMING_QUALITIES: &[StreamingQuality] = &[
|
||||
StreamingQuality { key: "mp3", label: "MP3" },
|
||||
StreamingQuality { key: "cd", label: "CD Quality" },
|
||||
StreamingQuality { key: "hires", label: "Hi-Res" },
|
||||
StreamingQuality { key: "hires_plus", label: "Hi-Res+" },
|
||||
];
|
||||
|
||||
/// Default streaming-quality key (`Hi-Res+`).
|
||||
pub const DEFAULT_STREAMING_QUALITY: &str = "hires_plus";
|
||||
|
||||
/// Persisted UI preferences. New fields must default sanely so an older
|
||||
/// file (missing the field) still deserializes.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct UiPrefs {
|
||||
/// Streaming-quality key — one of `STREAMING_QUALITIES[*].key`.
|
||||
#[serde(default = "default_streaming_quality")]
|
||||
pub streaming_quality: String,
|
||||
}
|
||||
|
||||
fn default_streaming_quality() -> String {
|
||||
DEFAULT_STREAMING_QUALITY.to_string()
|
||||
}
|
||||
|
||||
impl Default for UiPrefs {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
streaming_quality: default_streaming_quality(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolve `<data_dir>/qbz/ui_prefs.json`.
|
||||
fn prefs_path() -> Option<PathBuf> {
|
||||
Some(dirs::data_dir()?.join("qbz").join("ui_prefs.json"))
|
||||
}
|
||||
|
||||
/// Load the UI preferences. A missing or unreadable file degrades to
|
||||
/// `UiPrefs::default()` rather than erroring.
|
||||
pub fn load() -> UiPrefs {
|
||||
let Some(path) = prefs_path() else {
|
||||
return UiPrefs::default();
|
||||
};
|
||||
match std::fs::read_to_string(&path) {
|
||||
Ok(text) => serde_json::from_str(&text).unwrap_or_else(|e| {
|
||||
log::warn!("[qbz-slint] ui_prefs.json parse failed, using defaults: {e}");
|
||||
UiPrefs::default()
|
||||
}),
|
||||
Err(_) => UiPrefs::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Persist the UI preferences. Best-effort — failures are logged.
|
||||
pub fn save(prefs: &UiPrefs) {
|
||||
let Some(path) = prefs_path() else {
|
||||
log::warn!("[qbz-slint] ui_prefs.json: data dir unavailable, not saving");
|
||||
return;
|
||||
};
|
||||
if let Some(parent) = path.parent() {
|
||||
if let Err(e) = std::fs::create_dir_all(parent) {
|
||||
log::error!("[qbz-slint] ui_prefs.json: create dir failed: {e}");
|
||||
return;
|
||||
}
|
||||
}
|
||||
match serde_json::to_string_pretty(prefs) {
|
||||
Ok(text) => {
|
||||
if let Err(e) = std::fs::write(&path, text) {
|
||||
log::error!("[qbz-slint] ui_prefs.json: write failed: {e}");
|
||||
}
|
||||
}
|
||||
Err(e) => log::error!("[qbz-slint] ui_prefs.json: serialize failed: {e}"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Index of `key` in `STREAMING_QUALITIES`, falling back to the default
|
||||
/// (`Hi-Res+`) when the stored key is unknown.
|
||||
pub fn streaming_quality_index(key: &str) -> usize {
|
||||
STREAMING_QUALITIES
|
||||
.iter()
|
||||
.position(|q| q.key == key)
|
||||
.unwrap_or_else(|| {
|
||||
STREAMING_QUALITIES
|
||||
.iter()
|
||||
.position(|q| q.key == DEFAULT_STREAMING_QUALITY)
|
||||
.unwrap_or(0)
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn default_is_hires_plus() {
|
||||
assert_eq!(UiPrefs::default().streaming_quality, "hires_plus");
|
||||
assert_eq!(STREAMING_QUALITIES.len(), 4);
|
||||
assert_eq!(STREAMING_QUALITIES[3].key, "hires_plus");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_key_resolves_to_default_index() {
|
||||
// Default is hires_plus, which is index 3.
|
||||
assert_eq!(streaming_quality_index("bogus"), 3);
|
||||
assert_eq!(streaming_quality_index("mp3"), 0);
|
||||
assert_eq!(streaming_quality_index("cd"), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_json_without_field_deserializes() {
|
||||
let prefs: UiPrefs = serde_json::from_str("{}").expect("empty object deserializes");
|
||||
assert_eq!(prefs.streaming_quality, "hires_plus");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user