Files
craft-libs/crates/theme
Dylan M. TaylorandClaude 7c0ae3e639 fw sync: merge craft-libs next e3ca63165 (149 commits) into pdf-port/sync
Conflicts resolved (upstream's design and defaults win; the port's opt-ins stay):
- ui-contract fonts: upstream's served-faces provider, catalog job queue and byte budget;
  the port's FaceBytes::chain (Word subset siblings, F-8) re-done on the provider: a served
  face lists its served chain faces before the interface faces, a sibling landing after its
  owner remakes egui's fonts once, drawing a face marks its chain drawn. The port's chain
  trial moves onto upstream's rig, plus a late-sibling trial.
- text editor HostCx follows FacesCx's job-based document_face and document_face_bytes.
- ui-widgets Swatch: upstream's SwatchFill (split, spectrum, image, empty) with the port's
  Swatch::optional / SwatchFace::optional (new SwatchFill::None, the "no colour" diagonal)
  and Swatch::icon (glyph on the empty well) on top; both word sets kept.
- draw PaintBar: upstream's MixerLoad fill.
- font-manager: upstream's sample band; host rows (port) use it too.
- ui-panels / ui-patterns dock grips: upstream's drag_origin and named slack.
- platform clipboard tests: both trials kept.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-11-23 12:54:08 -05:00
..

craft-theme

The Craft design system's theme model. A theme is a few seeds and optional pins; resolve turns it, with an appearance (mode, contrast, size, text size), into tokens that apps map onto their own styles.

Pure data and functions: no egui, no I/O, no threads, and it builds for wasm. Theme files on disk, the suite folder and change watching belong to craft-theme-store; craft_ui::style converts tokens for egui. docs/suite-themes.md is the reference for theme authors and app developers.

use craft_theme::{Appearance, Contrast, Mode, Size, TextSize, builtins, resolve};

let theme = builtins().iter().find(|t| t.id.as_str() == "craft.graphite").unwrap();
let appearance = Appearance { mode: Mode::Dark, contrast: Contrast::Standard, size: Size::Compact, text_size: TextSize::Small, reduced_motion: false };
let resolved = resolve(theme, appearance, None);
// resolved.tokens: colours, shape, size, space and type tokens; resolved.warnings: contrast misses.

Tokens

Two layers, the second derived from the first:

  1. Primitives: sRGB and OKLCH colour, WCAG contrast, 12-step colour ramps, fixed scales (space, radius, stroke, opacity, duration, easing, elevation).
  2. Semantic tokens: surface, text, line, state, accent, status, overlay, data and caption colours; shape, size, space, motion and type tokens. Seeds (neutral, accent, four status colours) generate ramps, and ramps fill the tokens.

A theme sets seeds and may pin any colour token ("surface.panel": "#F0F0F0") per mode. Pins win over derivation. Apps can pin per app (apps.<id>).

Contrast

Text, state indicators, canvas overlays and data graphics have rules: text on surfaces, state fills and solid fills (4.5, high contrast 7); focus rings, accent marks and a list's drop marker on its halo (3, high contrast 4.5); overlay strokes on their halo and the marquee on its dashes (3, high contrast 4.5); and the channel colours a curve editor draws (data.red, data.green, data.blue, data.cyan, data.magenta, data.yellow) on surface.sunken (3, high contrast 4.5). Hover, pressed, dividers, edges, knob faces and row selection fills have none, and neither have guide and smart-guide colours, which are the user's choice, nor overlay.line, the bare accent line of boxes, paths and meshes on the canvas. In standard contrast an unpinned colour is moved to the minimum and a failing pin is kept and reported, with every component pair it makes fail; in high contrast every colour is fixed, pins included.

Files, built-ins, selection

Module What it is
format the craft-design-theme v1 file: parse, write, to_file; every error with its path
builtins() twelve craft.* themes, embedded from themes/*.json
selection Selection, SuiteChoice, AppChoice (an app's own theme, mode and size over the suite's), AppDefaults (with the app's AppSizes), resolve_selection: what the user picked, filled in and resolved against what the OS reports
ThemeId, AppId, APP_DEFAULT, SUITE_DEFAULT ids [a-z0-9][a-z0-9._-]{1,63}; craft. is reserved for built-ins

The craft-theme check <file>... [--json] binary parses and validates theme files and prints contrast warnings for every appearance. It exits 1 if any file has an error.

The selection types and the enums they hold implement Serialize/Deserialize: colours as "#rrggbb[aa]", ids as strings, enums in snake_case.