mirror of
https://github.com/storytold/craft-libs.git
synced 2026-12-01 02:36:54 +01:00
The WordCraft and PdfCraft palettes in artcraft.json pinned text.faint at 3.4 to 4.4:1 on their hover, pressed and background fills. Their light faint and muted pins and WordCraft's dark pins move along their own hue so faint text reads at 4.5:1 there and muted text stays 1.25 times above it. The faint text test now resolves every built-in alone and with each app palette it carries, and the fills it leaves out must match a named list. ArtCraft dark pins surface.raised to #5e5e5e, so raised fills stand above the #535353 panel again (the contrast pass had darkened them to #545454). Normal text reads at 5.59:1 on that fill, which leaves no room for a muted grey between faint and normal, so faint text on it stays listed as a warning, like the control, hover and pressed fills. High contrast keeps its #595959 raised fill, and the WordCraft and PdfCraft palettes keep their own raised fills (#353535 and #2e2e33) in both contrasts.
263 lines
11 KiB
Rust
263 lines
11 KiB
Rust
//! Token dumps: every token of every built-in, as text under `tests/dumps/tokens/`, so a change
|
|
//! to derivation or a theme file shows up as a reviewable diff.
|
|
//!
|
|
//! One file per built-in, mode it has, contrast and size, at the default text size. Run
|
|
//! `UPDATE_DUMPS=1 cargo test -p craft-theme dump` to rewrite them.
|
|
//!
|
|
//! The struct destructuring below is exhaustive on purpose: a new token doesn't compile until it
|
|
//! is dumped.
|
|
|
|
use std::collections::BTreeSet;
|
|
use std::fmt::Write as _;
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use craft_theme::dump::tokens_text;
|
|
use craft_theme::{
|
|
AppSizes, Appearance, Contrast, ContrastCheck, Mode, Size, TextSize, ThemeDef, Tokens, Variants, builtins, contrast_ratio, resolve,
|
|
resolve_sized, text_contrast_min,
|
|
};
|
|
|
|
const HEADER: &str = "# Generated by crates/theme/tests/dumps.rs; rewrite with UPDATE_DUMPS=1 cargo test -p craft-theme dump";
|
|
|
|
/// Every built-in theme variant, resolved, with its dump file name.
|
|
fn resolved_variants() -> Vec<(String, Tokens, Vec<ContrastCheck>)> {
|
|
let mut out = Vec::new();
|
|
for theme in builtins() {
|
|
for mode in modes(theme) {
|
|
for contrast in [Contrast::Standard, Contrast::High] {
|
|
for size in [Size::Compact, Size::Comfortable] {
|
|
let appearance = Appearance { mode, contrast, size, text_size: TextSize::Small, reduced_motion: false };
|
|
let resolved = resolve(theme, appearance, None);
|
|
let name = format!("{}-{}-{}-{}.txt", short_name(theme), mode_name(mode), contrast_name(contrast), size_name(size));
|
|
out.push((name, resolved.tokens, resolved.warnings));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
out
|
|
}
|
|
|
|
#[test]
|
|
fn token_dumps() {
|
|
let dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/dumps/tokens");
|
|
let mut files: Vec<(String, String)> =
|
|
resolved_variants().iter().map(|(name, tokens, warnings)| (name.clone(), dump(tokens, warnings))).collect();
|
|
files.push(app_sizes_dump());
|
|
check_dir(&dir, &files);
|
|
}
|
|
|
|
/// The sample app's size overrides: a base override for both sets and one for Comfortable.
|
|
const SAMPLE_SIZES: &str = r#"{ "all": { "size.control.md": 28 }, "comfortable": { "space.pad.regular": 14 } }"#;
|
|
|
|
/// Comfortable with [`SAMPLE_SIZES`]: the app's base overrides on one theme.
|
|
fn app_sizes_dump() -> (String, String) {
|
|
let sizes = AppSizes::parse(SAMPLE_SIZES.as_bytes()).unwrap();
|
|
let theme = builtins().iter().find(|t| t.id.as_str() == "craft.pro").unwrap();
|
|
let appearance =
|
|
Appearance { mode: Mode::Dark, contrast: Contrast::Standard, size: Size::Comfortable, text_size: TextSize::Small, reduced_motion: false };
|
|
let resolved = resolve_sized(theme, appearance, None, &sizes);
|
|
("pro-dark-standard-comfortable-app-sizes.txt".to_owned(), dump(&resolved.tokens, &resolved.warnings))
|
|
}
|
|
|
|
fn modes(theme: &ThemeDef) -> Vec<Mode> {
|
|
match theme.variants {
|
|
Variants::Both => vec![Mode::Light, Mode::Dark],
|
|
Variants::LightOnly => vec![Mode::Light],
|
|
Variants::DarkOnly => vec![Mode::Dark],
|
|
}
|
|
}
|
|
|
|
fn short_name(theme: &ThemeDef) -> &str {
|
|
let id = theme.id.as_str();
|
|
id.strip_prefix("craft.").unwrap_or(id)
|
|
}
|
|
|
|
fn mode_name(mode: Mode) -> &'static str {
|
|
match mode {
|
|
Mode::Light => "light",
|
|
Mode::Dark => "dark",
|
|
}
|
|
}
|
|
|
|
fn contrast_name(contrast: Contrast) -> &'static str {
|
|
match contrast {
|
|
Contrast::Standard => "standard",
|
|
Contrast::High => "high",
|
|
}
|
|
}
|
|
|
|
fn size_name(size: Size) -> &'static str {
|
|
match size {
|
|
Size::Compact => "compact",
|
|
Size::Comfortable => "comfortable",
|
|
}
|
|
}
|
|
|
|
fn dump(tokens: &Tokens, warnings: &[ContrastCheck]) -> String {
|
|
let mut out = String::from(HEADER);
|
|
out.push('\n');
|
|
out.push_str(&tokens_text(tokens));
|
|
for w in warnings {
|
|
let ContrastCheck { fg, bg, ratio, minimum } = w;
|
|
let _ = writeln!(out, "warning = {} on {}: {ratio:.2} < {minimum:.2}", fg.path(), bg.path());
|
|
}
|
|
out
|
|
}
|
|
|
|
/// Compares `files` with the dump directory, or rewrites it with `UPDATE_DUMPS=1`. Files in the
|
|
/// directory that no longer belong to a dump are reported (and removed on update).
|
|
fn check_dir(dir: &Path, files: &[(String, String)]) {
|
|
let update = std::env::var_os("UPDATE_DUMPS").is_some_and(|v| v != "0");
|
|
let names: BTreeSet<&str> = files.iter().map(|(n, _)| n.as_str()).collect();
|
|
let stale: Vec<PathBuf> = std::fs::read_dir(dir)
|
|
.map(|entries| {
|
|
entries
|
|
.filter_map(Result::ok)
|
|
.map(|e| e.path())
|
|
.filter(|p| p.file_name().and_then(|n| n.to_str()).is_some_and(|n| !names.contains(n)))
|
|
.collect()
|
|
})
|
|
.unwrap_or_default();
|
|
|
|
if update {
|
|
std::fs::create_dir_all(dir).unwrap();
|
|
for (name, text) in files {
|
|
let path = dir.join(name);
|
|
if std::fs::read_to_string(&path).ok().as_deref() != Some(text) {
|
|
std::fs::write(&path, text).unwrap();
|
|
}
|
|
}
|
|
for path in stale {
|
|
std::fs::remove_file(path).unwrap();
|
|
}
|
|
return;
|
|
}
|
|
|
|
let mut report = String::new();
|
|
let mut failed = 0;
|
|
for (name, text) in files {
|
|
let path = dir.join(name);
|
|
match std::fs::read_to_string(&path) {
|
|
Ok(old) if old == *text => {}
|
|
Ok(old) => {
|
|
failed += 1;
|
|
if failed <= 3 {
|
|
let _ = writeln!(report, "{name}:\n{}", diff(&old, text));
|
|
}
|
|
}
|
|
Err(_) => {
|
|
failed += 1;
|
|
let _ = writeln!(report, "{name}: missing");
|
|
}
|
|
}
|
|
}
|
|
for path in &stale {
|
|
failed += 1;
|
|
let _ = writeln!(report, "{}: no longer generated", path.display());
|
|
}
|
|
assert!(failed == 0, "{failed} token dump(s) differ; rerun with UPDATE_DUMPS=1 to rewrite them and review the diff\n{report}");
|
|
}
|
|
|
|
/// The lines only in `old` (`-`) and only in `new` (`+`), at most 20.
|
|
fn diff(old: &str, new: &str) -> String {
|
|
let (a, b): (BTreeSet<&str>, BTreeSet<&str>) = (old.lines().collect(), new.lines().collect());
|
|
let removed = old.lines().filter(|l| !b.contains(l)).map(|l| format!("- {l}"));
|
|
let added = new.lines().filter(|l| !a.contains(l)).map(|l| format!("+ {l}"));
|
|
let all: Vec<String> = removed.chain(added).collect();
|
|
let mut out: Vec<String> = all.iter().take(20).cloned().collect();
|
|
if all.len() > 20 {
|
|
out.push(format!("… {} more", all.len() - 20));
|
|
}
|
|
out.join("\n")
|
|
}
|
|
|
|
/// L-310: at the comfortable density the page view keeps production's 70 pt side margins, 28 pt above the first page and
|
|
/// 14 pt between pages.
|
|
#[test]
|
|
fn comfortable_page_spacing_is_production_s() {
|
|
let theme = builtins().iter().find(|t| t.id.as_str() == "craft.artcraft").unwrap();
|
|
let appearance =
|
|
Appearance { mode: Mode::Dark, contrast: Contrast::Standard, size: Size::Comfortable, text_size: TextSize::Small, reduced_motion: false };
|
|
let inset = resolve(theme, appearance, None).tokens.space.inset;
|
|
assert_eq!((inset.page_side, inset.page_margin, inset.page_gap), (70.0, 28.0, 14.0));
|
|
}
|
|
|
|
/// The contrast muted text keeps over faint text on the same ground.
|
|
const FAINT_TO_MUTED_GAP: f64 = 1.25;
|
|
|
|
/// The fills [`faint_text_meets_the_text_minimum_below_muted_text`] leaves out, as `theme[ app] mode on fill`.
|
|
const NO_ROOM_FOR_MUTED: &[&str] = &[
|
|
"craft.artcraft Dark on surface.raised",
|
|
"craft.artcraft Dark on surface.control",
|
|
"craft.artcraft Dark on state.hover",
|
|
"craft.artcraft Dark on state.pressed",
|
|
"craft.artcraft deckcraft Dark on surface.raised",
|
|
"craft.artcraft deckcraft Dark on surface.control",
|
|
"craft.artcraft deckcraft Dark on state.hover",
|
|
"craft.artcraft deckcraft Dark on state.pressed",
|
|
"craft.artcraft pdfcraft Dark on surface.control",
|
|
"craft.artcraft pdfcraft Dark on state.hover",
|
|
"craft.artcraft pdfcraft Dark on state.pressed",
|
|
"craft.artcraft soundcraft Dark on surface.raised",
|
|
"craft.artcraft soundcraft Dark on surface.control",
|
|
"craft.artcraft soundcraft Dark on state.hover",
|
|
"craft.artcraft soundcraft Dark on state.pressed",
|
|
"craft.solarized-dark Dark on surface.background",
|
|
"craft.solarized-dark Dark on surface.panel",
|
|
"craft.solarized-dark Dark on surface.chrome",
|
|
"craft.solarized-dark Dark on surface.raised",
|
|
"craft.solarized-dark Dark on surface.sunken",
|
|
"craft.solarized-dark Dark on surface.control",
|
|
"craft.solarized-dark Dark on state.hover",
|
|
"craft.solarized-dark Dark on state.pressed",
|
|
];
|
|
|
|
/// In every built-in at standard contrast, alone and with each app palette it carries, faint text reads at the
|
|
/// body-text minimum on each fill the theme pairs text with, muted text has at least [`FAINT_TO_MUTED_GAP`] times faint
|
|
/// text's contrast there, and normal text has more contrast than muted text. A fill where normal text itself is at or
|
|
/// below the minimum times the gap is left out, since no muted grey fits between the two there; those pairs stay listed
|
|
/// as warnings in the dumps, and the left-out fills must match [`NO_ROOM_FOR_MUTED`].
|
|
#[test]
|
|
fn faint_text_meets_the_text_minimum_below_muted_text() {
|
|
let min = text_contrast_min(Contrast::Standard);
|
|
let mut checked = 0;
|
|
let mut skipped = Vec::new();
|
|
for theme in builtins() {
|
|
let apps = std::iter::once(None).chain(theme.apps.keys().map(Some));
|
|
for (app, mode) in apps.flat_map(|app| modes(theme).into_iter().map(move |mode| (app, mode))) {
|
|
let appearance =
|
|
Appearance { mode, contrast: Contrast::Standard, size: Size::Compact, text_size: TextSize::Small, reduced_motion: false };
|
|
let c = resolve(theme, appearance, app).tokens.color;
|
|
let (s, text) = (&c.surface, &c.text);
|
|
let grounds = [
|
|
("surface.background", s.background),
|
|
("surface.panel", s.panel),
|
|
("surface.chrome", s.chrome),
|
|
("surface.raised", s.raised),
|
|
("surface.sunken", s.sunken),
|
|
("surface.control", s.control),
|
|
("state.hover", c.state.hover),
|
|
("state.pressed", c.state.pressed),
|
|
];
|
|
for (name, ground) in grounds {
|
|
let (faint, muted, normal) =
|
|
(contrast_ratio(text.faint, ground), contrast_ratio(text.muted, ground), contrast_ratio(text.normal, ground));
|
|
let app = app.map_or(String::new(), |a| format!(" {}", a.as_str()));
|
|
let fill = format!("{}{app} {mode:?} on {name}", theme.id.as_str());
|
|
if normal <= min * FAINT_TO_MUTED_GAP {
|
|
skipped.push(fill);
|
|
continue;
|
|
}
|
|
let at = format!("{fill}: faint {faint:.2}, muted {muted:.2}, normal {normal:.2}");
|
|
assert!(faint >= min, "{at}: faint is below {min:.2}:1");
|
|
assert!(muted >= faint * FAINT_TO_MUTED_GAP, "{at}: muted is less than {FAINT_TO_MUTED_GAP} times faint");
|
|
assert!(muted < normal, "{at}: muted is not below normal");
|
|
checked += 1;
|
|
}
|
|
}
|
|
}
|
|
assert!(checked > 0, "no text ground was checked");
|
|
let skipped: BTreeSet<&str> = skipped.iter().map(String::as_str).collect();
|
|
assert_eq!(skipped, NO_ROOM_FOR_MUTED.iter().copied().collect(), "the fills left out changed");
|
|
}
|