Files
wanderbooks/crates/ledger-gui/src/prefs.rs
T
swaitsandClaude Opus 5.5 af684c3612 viewer: color schemes, twenty families and every variant, with a light/dark filter
Owner, 2026-10-19: full color-scheme support for the viewer, the top
Neovim-style schemes, about twenty families each with a flyout of all its
variants, a light/dark/all filter, Ayu included, alphabetical, a whole row
to click.

- The sidebar's corner button (the light/dark toggle before) opens the
  menu: the filter, then, alphabetically and all on screen at once (in
  columns in a short window), Ayu, Catppuccin, Cyberdream, Dracula,
  Everforest, GitHub, Gruvbox, Kanagawa, Material, Melange, Monokai Pro,
  Night Owl, Nightfox, Nord, One Dark, Oxocarbon, Rosé Pine, Solarized,
  Sonokai, Tokyo Night and Wanderbooks (the viewer's own Light and Dark):
  80 schemes, each variant a row with a strip of its colors.
- Colors from each scheme's official source, checked value by value
  (docs/color-schemes.md; the table names the sources).
- Every palette, the viewer's own included, keeps every text color
  (figures, gains and losses, warnings, links, secondary and faint text)
  at WCAG AA (4.5:1) on every ground it sits on, soft fills included; a
  color that falls short keeps its hue and only changes lightness. Text,
  secondary and faint text stay apart (each 1.2 times the next), gains,
  losses and text and the charts' series shown together stay plainly
  different (CIE76 ΔE 20).
- The scheme sets both egui styles and pins its mode, so the system
  turning light or dark changes nothing (it made figures unreadable).
- Preferences now live in the platform's place for app settings
  ($XDG_CONFIG_HOME/wanderbooks-viewer on Linux), not eframe's default,
  which was $XDG_DATA_HOME/wanderbooks: the data root's own place. The
  old file is moved once.
- Tests: every page of thirteen painted in all 80 schemes, the same words
  in the same places, each text readable on what was painted under it;
  the menu's order, reach and whole-row clicks in a tall and a short
  window; the system flip; preferences restored. Two review rounds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01T3gXMVt7XJPi6ens1or9Dj
2026-10-19 12:51:58 -06:00

149 lines
5.6 KiB
Rust

//! Where the viewer keeps its preferences (window size, color scheme and
//! its filter): the platform's own place for an app's settings, in a
//! folder of the viewer's own.
//!
//! ```text
//! GOAL: the viewer's preferences live where the platform keeps app
//! settings, and never in the books' private folder.
//! SOURCE: owner, 2026-10-19 (preferences "in the OS's platform-native
//! location (like XDG on Linux)"); plan I-8 (the viewer writes
//! nothing under the data root; only eframe preferences under
//! $HOME). Found 2026-10-19: eframe's default, named after the app
//! id "wanderbooks", is $XDG_DATA_HOME/wanderbooks, the data
//! root's own default place.
//! INVARIANTS:
//! 1. Linux and the BSDs: $XDG_CONFIG_HOME/wanderbooks-viewer/viewer.ron
//! ($XDG_CONFIG_HOME only if absolute, else ~/.config); macOS:
//! ~/Library/Application Support/wanderbooks-viewer/viewer.ron;
//! Windows: %APPDATA%\wanderbooks-viewer\viewer.ron.
//! 2. Preferences eframe kept in its old place (app.ron in
//! $XDG_DATA_HOME/wanderbooks and the like) are moved once, when the
//! new place has none: only that one file, nothing else there is
//! read or touched.
//! ```
use std::path::{Path, PathBuf};
/// The viewer's own folder for its preferences.
pub const DIR: &str = "wanderbooks-viewer";
/// Its preferences file.
pub const FILE: &str = "viewer.ron";
/// eframe's own file name, in its old place.
const OLD_FILE: &str = "app.ron";
/// The preferences file for an account whose home is `home`, given
/// `$XDG_CONFIG_HOME` (or `%APPDATA%` on Windows) (INVARIANT 1).
pub fn path_in(home: &Path, config_home: Option<&Path>) -> PathBuf {
let base = if cfg!(target_os = "macos") {
home.join("Library").join("Application Support")
} else if cfg!(windows) {
config_home
.map(Path::to_path_buf)
.unwrap_or_else(|| home.join("AppData").join("Roaming"))
} else {
config_home
.filter(|p| p.is_absolute())
.map(Path::to_path_buf)
.unwrap_or_else(|| home.join(".config"))
};
base.join(DIR).join(FILE)
}
/// The preferences file for this account, if it has a home.
pub fn path() -> Option<PathBuf> {
let home = std::env::home_dir()?;
let var = if cfg!(windows) {
"APPDATA"
} else {
"XDG_CONFIG_HOME"
};
let config = std::env::var_os(var).map(PathBuf::from);
Some(path_in(&home, config.as_deref()))
}
/// Move preferences from `old_dir/app.ron` to `new` if `new` doesn't
/// exist yet (INVARIANT 2). Failures change nothing that matters: the
/// viewer starts with its defaults.
pub fn move_old(old_dir: &Path, new: &Path) {
let old = old_dir.join(OLD_FILE);
if new.exists() || !old.is_file() {
return;
}
let Some(dir) = new.parent() else {
return;
};
if std::fs::create_dir_all(dir).is_err() {
return;
}
if std::fs::copy(&old, new).is_ok() {
let _ = std::fs::remove_file(&old);
}
}
/// Where the viewer keeps its preferences, its old ones moved in
/// (INVARIANT 2): None when the account has no home.
pub fn prepare() -> Option<PathBuf> {
let new = path()?;
if let Some(old_dir) = eframe::storage_dir("wanderbooks") {
move_old(&old_dir, &new);
}
Some(new)
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(all(unix, not(target_os = "macos")))]
#[test]
fn on_linux_it_is_the_xdg_config_folder_of_its_own() {
let home = Path::new("/home/someone");
assert_eq!(
path_in(home, None),
Path::new("/home/someone/.config/wanderbooks-viewer/viewer.ron")
);
assert_eq!(
path_in(home, Some(Path::new("/cfg"))),
Path::new("/cfg/wanderbooks-viewer/viewer.ron")
);
// A relative XDG_CONFIG_HOME is ignored (the spec says so).
assert_eq!(
path_in(home, Some(Path::new("cfg"))),
Path::new("/home/someone/.config/wanderbooks-viewer/viewer.ron")
);
// Never the data root's place.
assert!(!path_in(home, None).starts_with("/home/someone/.local/share/wanderbooks"));
}
#[test]
fn old_preferences_are_moved_once_and_nothing_else_is_touched() {
let dir = crate::test_support::TempDir(
std::env::temp_dir().join(format!("ledger-gui-prefs-{}", std::process::id())),
);
let _ = std::fs::remove_dir_all(&dir.0);
std::fs::create_dir_all(&dir.0).unwrap();
let old = dir.0.join("old");
std::fs::create_dir(&old).unwrap();
std::fs::write(old.join("app.ron"), "(scheme: \"Ayu/Mirage\")").unwrap();
std::fs::write(old.join("other"), "kept").unwrap();
let new = dir.0.join("cfg/wanderbooks-viewer/viewer.ron");
move_old(&old, &new);
assert_eq!(
std::fs::read_to_string(&new).unwrap(),
"(scheme: \"Ayu/Mirage\")"
);
assert!(!old.join("app.ron").exists(), "moved, not copied");
assert_eq!(std::fs::read_to_string(old.join("other")).unwrap(), "kept");
// Once: new preferences are never overwritten by old ones.
std::fs::write(old.join("app.ron"), "older").unwrap();
std::fs::write(&new, "newer").unwrap();
move_old(&old, &new);
assert_eq!(std::fs::read_to_string(&new).unwrap(), "newer");
assert!(old.join("app.ron").exists());
// Nothing to move: nothing made.
let none = dir.0.join("none/viewer.ron");
move_old(&dir.0.join("missing"), &none);
assert!(!none.exists() && !dir.0.join("none").exists());
}
}