mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 04:04:25 +00:00
The slider thumb lengths were the only component tokens. A round knob is the control's glyph across (StyleCx::glyph, shared with the icon-only button glyph); a tab knob is the control's icon side tall. Themes choose the shape with shape.knob; Classic sets the tab, its old 10 x 16 thumb. TokenRef collapses into ColorToken, and the theme file's components maps go.
228 lines
9.8 KiB
Rust
228 lines
9.8 KiB
Rust
//! Checking a [`ThemeDef`] against what [`parse`](super::parse) accepts, so that
|
|
//! [`write`](super::write) never produces a file that fails to parse.
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use super::{EDGE, ErrorKind, FormatError, FormatErrors, LENGTH, OFFSET};
|
|
use crate::ramp::Mode;
|
|
use crate::resolve;
|
|
use crate::theme::{Appearance, Contrast, EdgeSeed, ModePins, PinLayer, Pins, ShapeSeed, Size, TextSize, ThemeDef, Variants};
|
|
use crate::tokens::ColorToken;
|
|
|
|
impl ThemeDef {
|
|
/// Every value that [`parse`](super::parse) would reject in the file
|
|
/// [`write`](super::write) makes of this theme, with its path there: pins for a mode the
|
|
/// theme's `variants` leaves out, a translucent surface, a negative or non-finite length, a
|
|
/// bevel width that is not above 0.
|
|
pub fn validate(&self) -> Result<(), FormatErrors> {
|
|
let mut c = Check { errors: Vec::new(), path: Vec::new() };
|
|
if let Some(shape) = &self.shape {
|
|
c.at("shape", |c| c.shape(shape));
|
|
}
|
|
c.mode_pins(&self.pins, self.variants);
|
|
for (key, mode) in [("light", Mode::Light), ("dark", Mode::Dark)] {
|
|
if self.variants.mode_for(mode) != mode && !self.mode_seeds.get(mode).is_empty() {
|
|
c.at(key, |c| c.at("seeds", |c| c.fail(ErrorKind::NoSuchMode)));
|
|
}
|
|
}
|
|
c.at("high_contrast", |c| c.mode_pins(&self.high_contrast, self.variants));
|
|
c.at("apps", |c| {
|
|
for (id, app) in &self.apps {
|
|
c.at(id.as_str(), |c| {
|
|
c.mode_pins(&app.pins, self.variants);
|
|
c.at("high_contrast", |c| c.mode_pins(&app.high_contrast, self.variants));
|
|
});
|
|
}
|
|
});
|
|
c.references(self);
|
|
if c.errors.is_empty() { Ok(()) } else { Err(FormatErrors(c.errors)) }
|
|
}
|
|
|
|
/// The reference cycles of this theme, each at the path of a reference on the cycle. Layers
|
|
/// that can apply together are followed together: the theme's pins, its high-contrast pins,
|
|
/// and each app's over them.
|
|
pub(super) fn reference_errors(&self) -> Vec<FormatError> {
|
|
let mut c = Check { errors: Vec::new(), path: Vec::new() };
|
|
c.references(self);
|
|
c.errors
|
|
}
|
|
}
|
|
|
|
struct Check {
|
|
errors: Vec<FormatError>,
|
|
path: Vec<String>,
|
|
}
|
|
|
|
impl Check {
|
|
fn fail(&mut self, kind: ErrorKind) {
|
|
self.errors.push(FormatError { path: self.path.clone(), kind });
|
|
}
|
|
|
|
fn at(&mut self, key: &str, f: impl FnOnce(&mut Self)) {
|
|
self.path.push(key.to_owned());
|
|
f(self);
|
|
self.path.pop();
|
|
}
|
|
|
|
fn shape(&mut self, shape: &ShapeSeed) {
|
|
if let Some(r) = shape.radius {
|
|
self.at("radius", |c| {
|
|
for (key, v) in
|
|
[("control", r.control), ("surface", r.surface), ("window", r.window), ("panel", r.panel), ("button", r.button), ("pill", r.pill)]
|
|
{
|
|
c.at(key, |c| c.length(v));
|
|
}
|
|
});
|
|
}
|
|
if let Some(o) = shape.radius_offset {
|
|
self.at("radius_offset", |c| {
|
|
for (key, v) in [("control", o.control), ("surface", o.surface), ("window", o.window)] {
|
|
c.at(key, |c| {
|
|
if !v.is_finite() {
|
|
c.fail(ErrorKind::BadValue { expected: OFFSET });
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}
|
|
if let Some(r) = shape.button_radius {
|
|
self.at("button_radius", |c| c.length(r));
|
|
}
|
|
if let Some(e) = shape.edge {
|
|
self.at("edge", |c| c.edge(e));
|
|
}
|
|
}
|
|
|
|
fn length(&mut self, v: f32) {
|
|
if !(v.is_finite() && v >= 0.0) {
|
|
self.fail(ErrorKind::BadValue { expected: LENGTH });
|
|
}
|
|
}
|
|
|
|
fn edge(&mut self, e: EdgeSeed) {
|
|
if let EdgeSeed::Bevel { width } = e
|
|
&& !(width.is_finite() && width > 0.0)
|
|
{
|
|
self.fail(ErrorKind::BadValue { expected: EDGE });
|
|
}
|
|
}
|
|
|
|
fn mode_pins(&mut self, pins: &ModePins, variants: Variants) {
|
|
for (key, mode) in [("light", Mode::Light), ("dark", Mode::Dark)] {
|
|
let map = pins.get(mode);
|
|
self.at(key, |c| {
|
|
if variants.mode_for(mode) != mode && !map.is_empty() {
|
|
c.fail(ErrorKind::NoSuchMode);
|
|
} else {
|
|
c.pins(map);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
fn pins(&mut self, pins: &Pins) {
|
|
for (&token, colour) in &pins.colors {
|
|
if token.is_opaque_surface() && colour.a() < 255 {
|
|
self.at(token.path(), |c| c.fail(ErrorKind::NotOpaque));
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Reports a reference cycle once, at the first reference on it in file order.
|
|
fn references(&mut self, t: &ThemeDef) {
|
|
let before = self.errors.len();
|
|
type Layer<'a> = (Vec<&'a str>, &'a ModePins);
|
|
let theme: Layer = (vec![], &t.pins);
|
|
let theme_high: Layer = (vec!["high_contrast"], &t.high_contrast);
|
|
let mut stacks: Vec<Vec<Layer>> = vec![vec![theme.clone()], vec![theme.clone(), theme_high.clone()]];
|
|
for (id, app) in &t.apps {
|
|
let id = id.as_str();
|
|
let own: Layer = (vec!["apps", id], &app.pins);
|
|
let own_high: Layer = (vec!["apps", id, "high_contrast"], &app.high_contrast);
|
|
stacks.push(vec![theme.clone(), own.clone()]);
|
|
stacks.push(vec![theme.clone(), own, theme_high.clone(), own_high]);
|
|
}
|
|
let mut reported: Vec<Vec<String>> = Vec::new();
|
|
for stack in &stacks {
|
|
for (mode_key, mode) in [("light", Mode::Light), ("dark", Mode::Dark)] {
|
|
// The reference each token ends with, and where it is written.
|
|
let mut effective: BTreeMap<ColorToken, Option<(ColorToken, Vec<String>)>> = BTreeMap::new();
|
|
for (prefix, pins) in stack {
|
|
let pins = pins.get(mode);
|
|
for &k in pins.colors.keys() {
|
|
effective.insert(k, None);
|
|
}
|
|
for (&k, &target) in &pins.refs {
|
|
let path = prefix.iter().map(|s| (*s).to_owned()).chain([mode_key.to_owned(), k.path().to_owned()]).collect();
|
|
effective.insert(k, Some((target, path)));
|
|
}
|
|
}
|
|
for (&start, entry) in &effective {
|
|
let Some((_, path)) = entry else { continue };
|
|
let mut chain = vec![start];
|
|
let mut at = start;
|
|
while let Some(Some((next, _))) = effective.get(&at) {
|
|
at = *next;
|
|
if at == start {
|
|
chain.push(at);
|
|
let names: Vec<String> = chain.iter().map(|t| t.path().to_owned()).collect();
|
|
let mut key = names[..names.len() - 1].to_vec();
|
|
key.sort();
|
|
if !reported.contains(&key) {
|
|
reported.push(key);
|
|
self.errors.push(FormatError { path: path.clone(), kind: ErrorKind::ReferenceCycle { chain: names } });
|
|
}
|
|
break;
|
|
}
|
|
if chain.contains(&at) {
|
|
break;
|
|
}
|
|
chain.push(at);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if self.errors.len() == before {
|
|
self.unsettled_references(t);
|
|
}
|
|
}
|
|
|
|
/// Reports references that never settle (see `resolve::settle_references`), once per set of
|
|
/// references, at the first of them. Resolution runs only for themes that have references.
|
|
fn unsettled_references(&mut self, t: &ThemeDef) {
|
|
let has_refs = |m: &ModePins| !m.light.refs.is_empty() || !m.dark.refs.is_empty();
|
|
if !has_refs(&t.pins) && !has_refs(&t.high_contrast) && !t.apps.values().any(|a| has_refs(&a.pins) || has_refs(&a.high_contrast)) {
|
|
return;
|
|
}
|
|
let mut reported: Vec<Vec<String>> = Vec::new();
|
|
let apps = std::iter::once(None).chain(t.apps.keys().map(Some));
|
|
for app in apps {
|
|
for (mode_key, mode) in [("light", Mode::Light), ("dark", Mode::Dark)] {
|
|
if t.variants.mode_for(mode) != mode {
|
|
continue;
|
|
}
|
|
for contrast in [Contrast::Standard, Contrast::High] {
|
|
let appearance = Appearance { mode, contrast, size: Size::Compact, text_size: TextSize::Small, reduced_motion: false };
|
|
let unsettled = resolve::unsettled_references(t, appearance, app);
|
|
let mut names: Vec<String> = unsettled.iter().map(|&(_, key)| key.path().to_owned()).collect();
|
|
names.sort();
|
|
let Some(&(layer, key)) = unsettled.first() else { continue };
|
|
if reported.contains(&names) {
|
|
continue;
|
|
}
|
|
reported.push(names.clone());
|
|
let mut path: Vec<String> = match (layer, app) {
|
|
(PinLayer::Theme, _) => vec![],
|
|
(PinLayer::ThemeHighContrast, _) => vec!["high_contrast".to_owned()],
|
|
(PinLayer::App, Some(id)) => vec!["apps".to_owned(), id.as_str().to_owned()],
|
|
(PinLayer::AppHighContrast, Some(id)) => vec!["apps".to_owned(), id.as_str().to_owned(), "high_contrast".to_owned()],
|
|
_ => vec![],
|
|
};
|
|
path.extend([mode_key.to_owned(), key.path().to_owned()]);
|
|
self.errors.push(FormatError { path, kind: ErrorKind::ReferenceNeverSettles { tokens: names } });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|