From f7e6c21f7edff79d3c391888b9f57790a591ca68 Mon Sep 17 00:00:00 2001 From: ZortexSenpai Date: Fri, 4 Dec 2026 23:52:47 +0100 Subject: [PATCH 1/2] feat: added option to start on a chosen device config A device config can be set as the startup config: every launch begins on it, whichever config was active when Reverb closed. Switching configs during a session works as before; only the next launch goes back. "Last used" (the default) keeps today's behaviour. - Backend: Preferences.startupDeviceConfig. At launch, before the engine is built from the preferences, the startup config becomes the active one, so the engine opens that output first. A startup config that no longer exists is ignored. - App: "Config at startup" picker under the device config list (shown once there is a config). Deleting the startup config resets it to "Last used". - Strings in all four languages; GLOSSARY.md updated. Co-Authored-By: Claude Opus 5.5 --- GLOSSARY.md | 2 +- src-tauri/src/lib.rs | 9 +++++ src-tauri/src/persistence/settings.rs | 48 +++++++++++++++++++++++++++ src/lib/deviceConfigs.test.ts | 35 ++++++++++++++++++- src/lib/deviceConfigs.ts | 23 +++++++++++-- src/lib/i18n/messages/de.ts | 4 +++ src/lib/i18n/messages/en.ts | 4 +++ src/lib/i18n/messages/es.ts | 4 +++ src/lib/i18n/messages/fr.ts | 4 +++ src/lib/subsonic/types.ts | 6 ++++ src/stores/prefsStore.ts | 1 + src/views/settings/SettingsView.tsx | 23 +++++++++++++ 12 files changed, 158 insertions(+), 5 deletions(-) diff --git a/GLOSSARY.md b/GLOSSARY.md index 5023bff..8ab9cb3 100644 --- a/GLOSSARY.md +++ b/GLOSSARY.md @@ -37,7 +37,7 @@ Sending Plays straight to the Scrobbling account instead of to Navidrome, which _Avoid_: Last.fm only **Device config**: -A named output setup: one output device (or the system default), and whether Reverb matches each Song's sample rate and takes the device exclusively. Or instead an HQPlayer, on this computer or another, that Reverb hands playback to: HQPlayer then fetches and plays the Songs itself, while the Queue, transport and Plays stay in Reverb. One is active at a time; with none active, Reverb plays on the system default, shared, at the device's own rate — which is also where playback goes when the active one's device is unplugged. The config keeps its device, so picking it again once the device is back is all it takes. A config locked to its device never moves playback elsewhere: while the device is away, playback pauses and the config stays active, ready again the moment the device is back. Switching configs carries the playing Song across, from where it was. +A named output setup: one output device (or the system default), and whether Reverb matches each Song's sample rate and takes the device exclusively. Or instead an HQPlayer, on this computer or another, that Reverb hands playback to: HQPlayer then fetches and plays the Songs itself, while the Queue, transport and Plays stay in Reverb. One is active at a time; with none active, Reverb plays on the system default, shared, at the device's own rate — which is also where playback goes when the active one's device is unplugged. The config keeps its device, so picking it again once the device is back is all it takes. A config locked to its device never moves playback elsewhere: while the device is away, playback pauses and the config stays active, ready again the moment the device is back. Switching configs carries the playing Song across, from where it was. One config can be the startup config: every launch begins on it, whichever was active when Reverb closed; without one, a launch picks up the config last active. _Avoid_: Output profile, audio preset, zone ### Stats diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 570eb45..6d91a66 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -58,6 +58,15 @@ pub fn run() { } } + // A startup device config is where every launch begins, whatever + // was active at the last quit. Switch before the engine is built + // from the preferences, so it opens that output first. + if let Some(id) = settings.get().prefs.startup_switch() { + if let Err(e) = settings.update(|s| s.prefs.active_device_config = Some(id)) { + log::warn!("could not switch to the startup device config: {e}"); + } + } + let keychain = Keychain::new(config_dir); let subsonic = Arc::new(SubsonicClient::new()); diff --git a/src-tauri/src/persistence/settings.rs b/src-tauri/src/persistence/settings.rs index 18c0835..c82810d 100644 --- a/src-tauri/src/persistence/settings.rs +++ b/src-tauri/src/persistence/settings.rs @@ -95,6 +95,10 @@ pub struct Preferences { /// The device config playing now, by id. `None` — or an id no config has — /// plays on the system default, shared, at the device's own rate. pub active_device_config: Option, + /// The device config every launch starts on, by id, whatever was active + /// when Reverb closed. `None` — or an id no config has — starts on the + /// one last active. + pub startup_device_config: Option, /// "raw" for the original file, or "mp3-320" / "mp3-192" / "mp3-128" /// for server-side transcoding. pub streaming_format: String, @@ -176,6 +180,7 @@ impl Default for Preferences { playlist_layout: "grid".into(), device_configs: Vec::new(), active_device_config: None, + startup_device_config: None, streaming_format: "raw".into(), replay_gain: ReplayGainSettings::default(), crossfade_secs: 0, @@ -208,6 +213,14 @@ impl Preferences { self.device_configs.iter().find(|config| config.id == id) } + /// The config a launch switches to: the startup config, unless it is + /// active already or no longer exists — then the last one active stays. + pub fn startup_switch(&self) -> Option { + let id = self.startup_device_config.as_deref()?; + let exists = self.device_configs.iter().any(|config| config.id == id); + (exists && self.active_device_config.as_deref() != Some(id)).then(|| id.to_owned()) + } + /// What the local engine should be playing through. An HQPlayer config /// has no device here, so with one active this is the system default — /// which the engine, in standby, does not open. @@ -706,6 +719,40 @@ mod tests { assert_eq!(back["deviceConfigs"][1]["lockDevice"], serde_json::json!(false)); } + #[test] + fn a_launch_switches_to_the_startup_config() { + let raw = r#"{ + "deviceConfigs": [ + {"id": "a", "name": "Config A", "device": "coreaudio:dac"}, + {"id": "b", "name": "Config B", "device": null} + ], + "activeDeviceConfig": "b", + "startupDeviceConfig": "a" + }"#; + let mut prefs: Preferences = serde_json::from_str(raw).unwrap(); + assert_eq!(prefs.startup_switch().as_deref(), Some("a")); + + // From the system default too. + prefs.active_device_config = None; + assert_eq!(prefs.startup_switch().as_deref(), Some("a")); + + // Already on it: nothing to switch. + prefs.active_device_config = Some("a".into()); + assert_eq!(prefs.startup_switch(), None); + + // No startup config, or a deleted one: the last one active stays. + prefs.active_device_config = Some("b".into()); + prefs.startup_device_config = None; + assert_eq!(prefs.startup_switch(), None); + prefs.startup_device_config = Some("gone".into()); + assert_eq!(prefs.startup_switch(), None); + + // And the field round-trips under its name. + prefs.startup_device_config = Some("a".into()); + let back = serde_json::to_value(&prefs).unwrap(); + assert_eq!(back["startupDeviceConfig"], serde_json::json!("a")); + } + #[test] fn old_settings_files_parse_with_pref_defaults() { // A settings.json written before the preferences existed. @@ -728,6 +775,7 @@ mod tests { assert!(!s.prefs.direct_scrobble_reset_on_start); assert!(!s.prefs.direct_scrobble_pinned); assert!(s.prefs.device_configs.is_empty()); + assert_eq!(s.prefs.startup_device_config, None, "no startup config: start on the last one"); assert_eq!(s.prefs.output(), OutputSettings::default()); assert!(s.prefs.check_for_updates, "update checks default to on"); assert!(!s.prefs.discord_presence, "Discord presence defaults to off"); diff --git a/src/lib/deviceConfigs.test.ts b/src/lib/deviceConfigs.test.ts index 0c8ab63..af3f742 100644 --- a/src/lib/deviceConfigs.test.ts +++ b/src/lib/deviceConfigs.test.ts @@ -9,6 +9,7 @@ import { isConnected, parsePort, saveDeviceConfig, + startupDeviceConfig, } from "./deviceConfigs"; import type { DeviceConfig, OutputDevice } from "./subsonic/types"; @@ -30,7 +31,11 @@ const speakers: DeviceConfig = { lockDevice: false, hqplayer: null, }; -const prefs = { deviceConfigs: [desk, speakers], activeDeviceConfig: "desk" }; +const prefs = { + deviceConfigs: [desk, speakers], + activeDeviceConfig: "desk", + startupDeviceConfig: null, +}; describe("activeDeviceConfig", () => { it("finds the config by id", () => { @@ -49,6 +54,7 @@ describe("saveDeviceConfig", () => { expect(saveDeviceConfig(prefs, fresh)).toEqual({ deviceConfigs: [desk, speakers, fresh], activeDeviceConfig: "new", + startupDeviceConfig: null, }); }); @@ -57,8 +63,16 @@ describe("saveDeviceConfig", () => { expect(saveDeviceConfig(prefs, renamed)).toEqual({ deviceConfigs: [desk, renamed], activeDeviceConfig: "desk", + startupDeviceConfig: null, }); }); + + it("keeps the startup config", () => { + const withStartup = { ...prefs, startupDeviceConfig: "speakers" }; + const fresh = { ...speakers, id: "new", name: "Headphones" }; + expect(saveDeviceConfig(withStartup, fresh).startupDeviceConfig).toBe("speakers"); + expect(saveDeviceConfig(withStartup, desk).startupDeviceConfig).toBe("speakers"); + }); }); describe("deleteDeviceConfig", () => { @@ -66,6 +80,7 @@ describe("deleteDeviceConfig", () => { expect(deleteDeviceConfig(prefs, "speakers")).toEqual({ deviceConfigs: [desk], activeDeviceConfig: "desk", + startupDeviceConfig: null, }); }); @@ -73,8 +88,26 @@ describe("deleteDeviceConfig", () => { expect(deleteDeviceConfig(prefs, "desk")).toEqual({ deviceConfigs: [speakers], activeDeviceConfig: null, + startupDeviceConfig: null, }); }); + + it("drops the startup config when it goes, and only then", () => { + const withStartup = { ...prefs, startupDeviceConfig: "speakers" }; + expect(deleteDeviceConfig(withStartup, "speakers").startupDeviceConfig).toBeNull(); + expect(deleteDeviceConfig(withStartup, "desk").startupDeviceConfig).toBe("speakers"); + }); +}); + +describe("startupDeviceConfig", () => { + it("finds the config by id", () => { + expect(startupDeviceConfig({ ...prefs, startupDeviceConfig: "speakers" })).toBe(speakers); + }); + + it("is none without one, or with one that was deleted", () => { + expect(startupDeviceConfig(prefs)).toBeNull(); + expect(startupDeviceConfig({ ...prefs, startupDeviceConfig: "gone" })).toBeNull(); + }); }); describe("afterDeviceLost", () => { diff --git a/src/lib/deviceConfigs.ts b/src/lib/deviceConfigs.ts index fd78511..448d6fb 100644 --- a/src/lib/deviceConfigs.ts +++ b/src/lib/deviceConfigs.ts @@ -7,7 +7,10 @@ import type { DeviceConfig, HqPlayerEndpoint, OutputDevice, Preferences } from " /** The port HQPlayer listens for control on unless told otherwise. */ export const HQPLAYER_DEFAULT_PORT = 4321; -type ConfigPrefs = Pick; +type ConfigPrefs = Pick< + Preferences, + "deviceConfigs" | "activeDeviceConfig" | "startupDeviceConfig" +>; /** The device config playing now; `null` means the system default. */ export function activeDeviceConfig(prefs: ConfigPrefs): DeviceConfig | null { @@ -24,19 +27,33 @@ export function saveDeviceConfig(prefs: ConfigPrefs, config: DeviceConfig): Conf return { deviceConfigs: prefs.deviceConfigs.map((c) => (c.id === config.id ? config : c)), activeDeviceConfig: prefs.activeDeviceConfig, + startupDeviceConfig: prefs.startupDeviceConfig, }; } - return { deviceConfigs: [...prefs.deviceConfigs, config], activeDeviceConfig: config.id }; + return { + deviceConfigs: [...prefs.deviceConfigs, config], + activeDeviceConfig: config.id, + startupDeviceConfig: prefs.startupDeviceConfig, + }; } -/** Delete a config. Deleting the active one moves playback to the system default. */ +/** + * Delete a config. Deleting the active one moves playback to the system + * default; deleting the startup one leaves launches on the one last active. + */ export function deleteDeviceConfig(prefs: ConfigPrefs, id: string): ConfigPrefs { return { deviceConfigs: prefs.deviceConfigs.filter((c) => c.id !== id), activeDeviceConfig: prefs.activeDeviceConfig === id ? null : prefs.activeDeviceConfig, + startupDeviceConfig: prefs.startupDeviceConfig === id ? null : prefs.startupDeviceConfig, }; } +/** The config launches start on, or `null` to start on the one last active. */ +export function startupDeviceConfig(prefs: ConfigPrefs): DeviceConfig | null { + return prefs.deviceConfigs.find((c) => c.id === prefs.startupDeviceConfig) ?? null; +} + /** * Mirror the backend after `device` was unplugged: playback fell back to the * system default, so that is what is active now. A config switched to since diff --git a/src/lib/i18n/messages/de.ts b/src/lib/i18n/messages/de.ts index baa2bd6..fb0feae 100644 --- a/src/lib/i18n/messages/de.ts +++ b/src/lib/i18n/messages/de.ts @@ -466,6 +466,10 @@ export const de: Messages = { "settings.deleteDeviceConfigTitle": "„{name}“ löschen?", "settings.deleteActiveDeviceConfigBody": "Sie ist gerade aktiv, daher wechselt die Wiedergabe auf den Systemstandard.", + "settings.startupDeviceConfig": "Konfiguration beim Start", + "settings.startupDeviceConfigHint": + "Die Gerätekonfiguration, mit der Reverb startet, egal welche beim Beenden aktiv war. „Zuletzt verwendet“ macht dort weiter, wo du aufgehört hast.", + "settings.startupDeviceConfigLastUsed": "Zuletzt verwendet", "settings.deviceConfigName": "Name", "settings.deviceConfigNameHint": "Leer lassen, um den Namen des Geräts zu verwenden.", "settings.systemDefaultConfigHint": diff --git a/src/lib/i18n/messages/en.ts b/src/lib/i18n/messages/en.ts index 201d1f4..22ad7aa 100644 --- a/src/lib/i18n/messages/en.ts +++ b/src/lib/i18n/messages/en.ts @@ -491,6 +491,10 @@ export const en = { "settings.deleteDeviceConfig": "Delete {name}", "settings.deleteDeviceConfigTitle": "Delete “{name}”?", "settings.deleteActiveDeviceConfigBody": "It is in use, so playback moves to the system default.", + "settings.startupDeviceConfig": "Config at startup", + "settings.startupDeviceConfigHint": + "Reverb starts on this config every time, whichever one was in use when it closed. “Last used” picks up where you left off.", + "settings.startupDeviceConfigLastUsed": "Last used", "settings.deviceConfigName": "Name", "settings.deviceConfigNameHint": "Leave empty to use the device's name.", "settings.systemDefaultConfigHint": diff --git a/src/lib/i18n/messages/es.ts b/src/lib/i18n/messages/es.ts index 84a5557..d250f1c 100644 --- a/src/lib/i18n/messages/es.ts +++ b/src/lib/i18n/messages/es.ts @@ -466,6 +466,10 @@ export const es: Messages = { "settings.deleteDeviceConfigTitle": "¿Eliminar “{name}”?", "settings.deleteActiveDeviceConfigBody": "Está en uso, así que la reproducción pasa al predeterminado del sistema.", + "settings.startupDeviceConfig": "Configuración al iniciar", + "settings.startupDeviceConfigHint": + "La configuración de dispositivo con la que arranca Reverb, sea cual sea la que estaba en uso al cerrarlo. “Última usada” continúa donde lo dejaste.", + "settings.startupDeviceConfigLastUsed": "Última usada", "settings.deviceConfigName": "Nombre", "settings.deviceConfigNameHint": "Déjalo vacío para usar el nombre del dispositivo.", "settings.systemDefaultConfigHint": diff --git a/src/lib/i18n/messages/fr.ts b/src/lib/i18n/messages/fr.ts index 5cfea46..0b41880 100644 --- a/src/lib/i18n/messages/fr.ts +++ b/src/lib/i18n/messages/fr.ts @@ -467,6 +467,10 @@ export const fr: Messages = { "settings.deleteDeviceConfigTitle": "Supprimer « {name} » ?", "settings.deleteActiveDeviceConfigBody": "Elle est en cours d'utilisation : la lecture passe sur le périphérique par défaut.", + "settings.startupDeviceConfig": "Configuration au démarrage", + "settings.startupDeviceConfigHint": + "La configuration de périphérique avec laquelle Reverb démarre, quelle que soit celle utilisée à sa fermeture. « Dernière utilisée » reprend là où vous vous étiez arrêté.", + "settings.startupDeviceConfigLastUsed": "Dernière utilisée", "settings.deviceConfigName": "Nom", "settings.deviceConfigNameHint": "Laissez vide pour utiliser le nom du périphérique.", "settings.systemDefaultConfigHint": diff --git a/src/lib/subsonic/types.ts b/src/lib/subsonic/types.ts index df01c4e..4203f34 100644 --- a/src/lib/subsonic/types.ts +++ b/src/lib/subsonic/types.ts @@ -426,6 +426,12 @@ export interface Preferences { * plays on the system default, shared, at the device's own rate. */ activeDeviceConfig: string | null; + /** + * The device config every launch starts on, by id, whatever was active when + * Reverb closed. `null` — or an id no config has — starts on the one last + * active. The backend switches to it at launch, before the engine opens. + */ + startupDeviceConfig: string | null; streamingFormat: StreamingFormat; replayGain: ReplayGainSettings; /** Crossfade between tracks in seconds (0 = gapless). */ diff --git a/src/stores/prefsStore.ts b/src/stores/prefsStore.ts index 06614e1..2bb143c 100644 --- a/src/stores/prefsStore.ts +++ b/src/stores/prefsStore.ts @@ -146,6 +146,7 @@ const DEFAULT_PREFS: Preferences = { playlistLayout: "grid", deviceConfigs: [], activeDeviceConfig: null, + startupDeviceConfig: null, streamingFormat: "raw", replayGain: { mode: "off", preampDb: 0, clipProtection: true, fallbackDb: 0 }, crossfadeSecs: 0, diff --git a/src/views/settings/SettingsView.tsx b/src/views/settings/SettingsView.tsx index 4ccffe8..ac8c055 100644 --- a/src/views/settings/SettingsView.tsx +++ b/src/views/settings/SettingsView.tsx @@ -46,6 +46,7 @@ import { isConnected, parsePort, saveDeviceConfig, + startupDeviceConfig, } from "../../lib/deviceConfigs"; import { formatSampleRate } from "../../lib/format"; import { LANGUAGE_NAMES, LOCALES, useT, type MessageKey, type T } from "../../lib/i18n"; @@ -479,6 +480,28 @@ function AudioSection() { })} + {/* Only worth asking once there is a config to start on. Reads the + resolved config, so one deleted elsewhere shows as "Last used". */} + {prefs.deviceConfigs.length > 0 && ( + + + + )} + {/* Reverb's own stream; with HQPlayer playing there is none, and the signal path shows HQPlayer's rate instead. */} {!active?.hqplayer && ( From 2e8528ce6a9660f72b69be976434ad8beb14490d Mon Sep 17 00:00:00 2001 From: ZortexSenpai Date: Fri, 4 Dec 2026 23:54:26 +0100 Subject: [PATCH 2/2] feat: added System default to the startup config picker Launches can now be set to start on the system default too, whichever config was active when Reverb closed. Stored as the reserved value "system-default" in startupDeviceConfig; config ids are nanoids or UUIDs, so none can collide with it. Co-Authored-By: Claude Opus 5.5 --- GLOSSARY.md | 2 +- src-tauri/src/lib.rs | 17 +++++++---- src-tauri/src/persistence/settings.rs | 43 +++++++++++++++++---------- src/lib/deviceConfigs.test.ts | 7 +++-- src/lib/deviceConfigs.ts | 17 +++++++++-- src/lib/subsonic/types.ts | 5 ++-- src/views/settings/SettingsView.tsx | 9 ++++-- 7 files changed, 67 insertions(+), 33 deletions(-) diff --git a/GLOSSARY.md b/GLOSSARY.md index 8ab9cb3..034dd31 100644 --- a/GLOSSARY.md +++ b/GLOSSARY.md @@ -37,7 +37,7 @@ Sending Plays straight to the Scrobbling account instead of to Navidrome, which _Avoid_: Last.fm only **Device config**: -A named output setup: one output device (or the system default), and whether Reverb matches each Song's sample rate and takes the device exclusively. Or instead an HQPlayer, on this computer or another, that Reverb hands playback to: HQPlayer then fetches and plays the Songs itself, while the Queue, transport and Plays stay in Reverb. One is active at a time; with none active, Reverb plays on the system default, shared, at the device's own rate — which is also where playback goes when the active one's device is unplugged. The config keeps its device, so picking it again once the device is back is all it takes. A config locked to its device never moves playback elsewhere: while the device is away, playback pauses and the config stays active, ready again the moment the device is back. Switching configs carries the playing Song across, from where it was. One config can be the startup config: every launch begins on it, whichever was active when Reverb closed; without one, a launch picks up the config last active. +A named output setup: one output device (or the system default), and whether Reverb matches each Song's sample rate and takes the device exclusively. Or instead an HQPlayer, on this computer or another, that Reverb hands playback to: HQPlayer then fetches and plays the Songs itself, while the Queue, transport and Plays stay in Reverb. One is active at a time; with none active, Reverb plays on the system default, shared, at the device's own rate — which is also where playback goes when the active one's device is unplugged. The config keeps its device, so picking it again once the device is back is all it takes. A config locked to its device never moves playback elsewhere: while the device is away, playback pauses and the config stays active, ready again the moment the device is back. Switching configs carries the playing Song across, from where it was. Launches can be set to begin on one config, or on the system default, whichever was active when Reverb closed; otherwise a launch picks up the config last active. _Avoid_: Output profile, audio preset, zone ### Stats diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 6d91a66..d00c74a 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -58,12 +58,17 @@ pub fn run() { } } - // A startup device config is where every launch begins, whatever - // was active at the last quit. Switch before the engine is built - // from the preferences, so it opens that output first. - if let Some(id) = settings.get().prefs.startup_switch() { - if let Err(e) = settings.update(|s| s.prefs.active_device_config = Some(id)) { - log::warn!("could not switch to the startup device config: {e}"); + // A launch can be set to begin on one device config (or the + // system default), whatever was active at the last quit. Switch + // before the engine is built from the preferences, so it opens + // that output first. + { + let prefs = settings.get().prefs; + let launch = prefs.launch_device_config(); + if launch != prefs.active_device_config { + if let Err(e) = settings.update(|s| s.prefs.active_device_config = launch) { + log::warn!("could not switch to the startup device config: {e}"); + } } } diff --git a/src-tauri/src/persistence/settings.rs b/src-tauri/src/persistence/settings.rs index c82810d..cdba015 100644 --- a/src-tauri/src/persistence/settings.rs +++ b/src-tauri/src/persistence/settings.rs @@ -22,6 +22,10 @@ use serde::{Deserialize, Serialize}; use crate::error::ReverbError; +/// [`Preferences::startup_device_config`] value that starts every launch on +/// the system default. Config ids are nanoids or UUIDs, so none can be this. +pub const STARTUP_SYSTEM_DEFAULT: &str = "system-default"; + #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct EqPreset { @@ -96,8 +100,8 @@ pub struct Preferences { /// plays on the system default, shared, at the device's own rate. pub active_device_config: Option, /// The device config every launch starts on, by id, whatever was active - /// when Reverb closed. `None` — or an id no config has — starts on the - /// one last active. + /// when Reverb closed — or [`STARTUP_SYSTEM_DEFAULT`] for none. `None` — + /// or an id no config has — starts on the one last active. pub startup_device_config: Option, /// "raw" for the original file, or "mp3-320" / "mp3-192" / "mp3-128" /// for server-side transcoding. @@ -213,12 +217,17 @@ impl Preferences { self.device_configs.iter().find(|config| config.id == id) } - /// The config a launch switches to: the startup config, unless it is - /// active already or no longer exists — then the last one active stays. - pub fn startup_switch(&self) -> Option { - let id = self.startup_device_config.as_deref()?; - let exists = self.device_configs.iter().any(|config| config.id == id); - (exists && self.active_device_config.as_deref() != Some(id)).then(|| id.to_owned()) + /// The config a launch starts on, as `active_device_config` should then + /// read (`None` = system default): the startup choice, or the one last + /// active when there is none or it names a config that no longer exists. + pub fn launch_device_config(&self) -> Option { + match self.startup_device_config.as_deref() { + Some(STARTUP_SYSTEM_DEFAULT) => None, + Some(id) if self.device_configs.iter().any(|config| config.id == id) => { + Some(id.to_owned()) + } + _ => self.active_device_config.clone(), + } } /// What the local engine should be playing through. An HQPlayer config @@ -730,22 +739,24 @@ mod tests { "startupDeviceConfig": "a" }"#; let mut prefs: Preferences = serde_json::from_str(raw).unwrap(); - assert_eq!(prefs.startup_switch().as_deref(), Some("a")); + assert_eq!(prefs.launch_device_config().as_deref(), Some("a")); // From the system default too. prefs.active_device_config = None; - assert_eq!(prefs.startup_switch().as_deref(), Some("a")); + assert_eq!(prefs.launch_device_config().as_deref(), Some("a")); - // Already on it: nothing to switch. - prefs.active_device_config = Some("a".into()); - assert_eq!(prefs.startup_switch(), None); + // The system default can be the startup choice as well. + prefs.active_device_config = Some("b".into()); + prefs.startup_device_config = Some(STARTUP_SYSTEM_DEFAULT.into()); + assert_eq!(prefs.launch_device_config(), None); // No startup config, or a deleted one: the last one active stays. - prefs.active_device_config = Some("b".into()); prefs.startup_device_config = None; - assert_eq!(prefs.startup_switch(), None); + assert_eq!(prefs.launch_device_config().as_deref(), Some("b")); prefs.startup_device_config = Some("gone".into()); - assert_eq!(prefs.startup_switch(), None); + assert_eq!(prefs.launch_device_config().as_deref(), Some("b")); + prefs.active_device_config = None; + assert_eq!(prefs.launch_device_config(), None); // And the field round-trips under its name. prefs.startup_device_config = Some("a".into()); diff --git a/src/lib/deviceConfigs.test.ts b/src/lib/deviceConfigs.test.ts index af3f742..e2390b2 100644 --- a/src/lib/deviceConfigs.test.ts +++ b/src/lib/deviceConfigs.test.ts @@ -9,6 +9,7 @@ import { isConnected, parsePort, saveDeviceConfig, + STARTUP_SYSTEM_DEFAULT, startupDeviceConfig, } from "./deviceConfigs"; import type { DeviceConfig, OutputDevice } from "./subsonic/types"; @@ -100,8 +101,10 @@ describe("deleteDeviceConfig", () => { }); describe("startupDeviceConfig", () => { - it("finds the config by id", () => { - expect(startupDeviceConfig({ ...prefs, startupDeviceConfig: "speakers" })).toBe(speakers); + it("is a config's id, or the system default", () => { + expect(startupDeviceConfig({ ...prefs, startupDeviceConfig: "speakers" })).toBe("speakers"); + const systemDefault = { ...prefs, startupDeviceConfig: STARTUP_SYSTEM_DEFAULT }; + expect(startupDeviceConfig(systemDefault)).toBe(STARTUP_SYSTEM_DEFAULT); }); it("is none without one, or with one that was deleted", () => { diff --git a/src/lib/deviceConfigs.ts b/src/lib/deviceConfigs.ts index 448d6fb..f83a14d 100644 --- a/src/lib/deviceConfigs.ts +++ b/src/lib/deviceConfigs.ts @@ -4,6 +4,12 @@ import type { DeviceConfig, HqPlayerEndpoint, OutputDevice, Preferences } from "./subsonic/types"; +/** + * `startupDeviceConfig` value that starts every launch on the system default. + * Config ids are nanoids or UUIDs, so none can be this. + */ +export const STARTUP_SYSTEM_DEFAULT = "system-default"; + /** The port HQPlayer listens for control on unless told otherwise. */ export const HQPLAYER_DEFAULT_PORT = 4321; @@ -49,9 +55,14 @@ export function deleteDeviceConfig(prefs: ConfigPrefs, id: string): ConfigPrefs }; } -/** The config launches start on, or `null` to start on the one last active. */ -export function startupDeviceConfig(prefs: ConfigPrefs): DeviceConfig | null { - return prefs.deviceConfigs.find((c) => c.id === prefs.startupDeviceConfig) ?? null; +/** + * What launches start on: a config's id, `STARTUP_SYSTEM_DEFAULT`, or `null` + * for the one last active — which is also what an id no config has means. + */ +export function startupDeviceConfig(prefs: ConfigPrefs): string | null { + const id = prefs.startupDeviceConfig; + if (id === STARTUP_SYSTEM_DEFAULT) return id; + return prefs.deviceConfigs.some((c) => c.id === id) ? id : null; } /** diff --git a/src/lib/subsonic/types.ts b/src/lib/subsonic/types.ts index 4203f34..2624334 100644 --- a/src/lib/subsonic/types.ts +++ b/src/lib/subsonic/types.ts @@ -428,8 +428,9 @@ export interface Preferences { activeDeviceConfig: string | null; /** * The device config every launch starts on, by id, whatever was active when - * Reverb closed. `null` — or an id no config has — starts on the one last - * active. The backend switches to it at launch, before the engine opens. + * Reverb closed — or `STARTUP_SYSTEM_DEFAULT` for none. `null` — or an id no + * config has — starts on the one last active. The backend switches at + * launch, before the engine opens. */ startupDeviceConfig: string | null; streamingFormat: StreamingFormat; diff --git a/src/views/settings/SettingsView.tsx b/src/views/settings/SettingsView.tsx index ac8c055..ef61a3e 100644 --- a/src/views/settings/SettingsView.tsx +++ b/src/views/settings/SettingsView.tsx @@ -46,6 +46,7 @@ import { isConnected, parsePort, saveDeviceConfig, + STARTUP_SYSTEM_DEFAULT, startupDeviceConfig, } from "../../lib/deviceConfigs"; import { formatSampleRate } from "../../lib/format"; @@ -480,8 +481,9 @@ function AudioSection() { })} - {/* Only worth asking once there is a config to start on. Reads the - resolved config, so one deleted elsewhere shows as "Last used". */} + {/* Only worth asking once there is a config: until then every launch + is on the system default anyway. Reads the resolved choice, so a + config deleted elsewhere shows as "Last used". */} {prefs.deviceConfigs.length > 0 && (