feat: add an option to colour the UI from the playing track's cover
A new Settings section in the sidebar turns it on and off. It is saved in settings.toml, apart from the login, so logging out keeps it. Each theme colour keeps the lightness it has in the brand palette and only takes the cover's dominant hue, so text stays readable on any cover. Covers with too little colour, and nothing playing, use the brand palette. Slint cannot animate a global, so a timer in Rust fades the Theme colours to the new palette over 800 ms.
This commit is contained in:
+62
-6
@@ -1,7 +1,7 @@
|
||||
use std::fs;
|
||||
use std::io::Write;
|
||||
use std::os::unix::fs::OpenOptionsExt;
|
||||
use std::path::PathBuf;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use directories::ProjectDirs;
|
||||
@@ -76,12 +76,53 @@ pub fn load() -> Result<Option<Config>> {
|
||||
|
||||
pub fn save(cfg: &Config) -> Result<()> {
|
||||
let path = config_path().context("no config directory")?;
|
||||
write_atomic(&path, &toml::to_string(cfg)?)?;
|
||||
log::info!("saved login to {}", path.display());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Preferences. Kept apart from the login, so logging out does not reset them.
|
||||
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(default)]
|
||||
pub struct Settings {
|
||||
/// Tint the UI with the colours of the playing track's cover.
|
||||
pub album_art_colours: bool,
|
||||
}
|
||||
|
||||
fn settings_path() -> Option<PathBuf> {
|
||||
dirs().map(|d| d.config_dir().join("settings.toml"))
|
||||
}
|
||||
|
||||
/// The saved settings, or the defaults when there are none or they cannot be read.
|
||||
pub fn load_settings() -> Settings {
|
||||
let Some(path) = settings_path() else {
|
||||
return Settings::default();
|
||||
};
|
||||
match fs::read_to_string(&path) {
|
||||
Ok(text) => toml::from_str(&text).unwrap_or_else(|e| {
|
||||
log::warn!("ignoring {}: {e}", path.display());
|
||||
Settings::default()
|
||||
}),
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Settings::default(),
|
||||
Err(e) => {
|
||||
log::warn!("reading {}: {e}", path.display());
|
||||
Settings::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save_settings(s: &Settings) -> Result<()> {
|
||||
let path = settings_path().context("no config directory")?;
|
||||
write_atomic(&path, &toml::to_string(s)?)
|
||||
}
|
||||
|
||||
/// Writes aside, syncs, then renames over the old file, so a power cut
|
||||
/// mid-save leaves either the old file or the new one, never a torn one.
|
||||
/// Owner-only, since the login file holds a token.
|
||||
fn write_atomic(path: &Path, text: &str) -> Result<()> {
|
||||
if let Some(dir) = path.parent() {
|
||||
fs::create_dir_all(dir)?;
|
||||
}
|
||||
let text = toml::to_string(cfg)?;
|
||||
// Write aside, sync, then rename over the old file, so a power cut
|
||||
// mid-save leaves either the old login or the new one, never a torn file.
|
||||
let tmp = path.with_extension("toml.tmp");
|
||||
let mut f = fs::OpenOptions::new()
|
||||
.write(true)
|
||||
@@ -92,13 +133,12 @@ pub fn save(cfg: &Config) -> Result<()> {
|
||||
.with_context(|| format!("writing {}", tmp.display()))?;
|
||||
f.write_all(text.as_bytes())?;
|
||||
f.sync_all()?;
|
||||
fs::rename(&tmp, &path).with_context(|| format!("replacing {}", path.display()))?;
|
||||
fs::rename(&tmp, path).with_context(|| format!("replacing {}", path.display()))?;
|
||||
if let Some(dir) = path.parent()
|
||||
&& let Ok(d) = fs::File::open(dir)
|
||||
{
|
||||
let _ = d.sync_all();
|
||||
}
|
||||
log::info!("saved login to {}", path.display());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -139,4 +179,20 @@ mod tests {
|
||||
let text = toml::to_string(&cfg).unwrap();
|
||||
assert!(!text.contains("salt") && !text.contains("token"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn settings_default_when_fields_are_missing() {
|
||||
let s: Settings = toml::from_str("").unwrap();
|
||||
assert_eq!(s, Settings::default());
|
||||
assert!(!s.album_art_colours);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn settings_round_trip() {
|
||||
let s = Settings {
|
||||
album_art_colours: true,
|
||||
};
|
||||
let back: Settings = toml::from_str(&toml::to_string(&s).unwrap()).unwrap();
|
||||
assert_eq!(back, s);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user