Files
VoelinClient/crates/voelin-gpui/src/theme/components.rs
T
faumarayandClaude Sonnet 5.5 2690b8a802 voelin-gpui: VoelinTheme (every token of theme.slint, light and dark) written into gpui-component, Inter, the client's Lucide icons, the swatch page
Two layers, as the spikes' report settled:
1. `VoelinTheme`, a GPUI global (theme/tokens.rs): the 43 colour tokens of
   ui/theme.slint for light and dark (generated from the Slint file by a
   script, then checked by the test below), the backdrop gradient, and
   `Metrics`: the radii, spacing, type sizes, weights, control and shell
   sizes and the two durations at a text size factor (font-scale: the
   sidebar's `264 * max(1, scale)` and the top bar's `max(44, control + 8)`
   included).
2. gpui-component's `ThemeColor`, written on every change (theme/components.rs):
   the base of the report's table (background, foreground, border, input,
   ring, overlay; primary with hover, active and foreground from the client's
   accent; secondary and muted from surface-2 and its raised steps;
   gpui-component's "accent", which is a hover background, from surface-3;
   popover; list, table, sidebar, title and status bar, tabs, switch, slider,
   scrollbar, link, selection; danger, success, warning and info with a hover
   and active made by mixing and a text that is white or the window's ink,
   whichever contrasts more) and the 28 `button_*` fields a theme file would
   derive from them (without them the buttons keep the default theme's
   blue). The mixing, the WCAG contrast and `readable` are `Rgba` methods with
   tests. The window's background is bg-app; the backdrop and bg-rail stay in
   `VoelinTheme` (gpui-component has no rail, and GPUI's gradient has two
   stops against Slint's three: the swatch draws the first and the last and
   says so).
The mode (`Choice`: system, light, dark) follows the window's appearance in
`system` (`observe_window_appearance` calls `set_system_appearance`; a light
or dark choice ignores the window) and sets the platform's window appearance
where the platform draws the frame (macOS; a no-op elsewhere). The text size
factor rewrites the metrics, the theme's font size (font-body) and radii.

theme/slint_parity.rs reads ui/theme.slint at test time (`include_str!`) and
compares every declaration of the `Theme` global with tokens.rs by name: the
colours (dark and light), every length with the file's own arithmetic (a
small evaluator of `*`, `+`, `max()` and token references) at four text
sizes, the weights, durations, the font family and the backdrop's stops, and
fails on a name on one side only or a declaration kind it does not know. A
mutation check (accent, radius-md and the top bar's formula changed in
tokens.rs) failed the tests with the names of the tokens, then was reverted.

fonts.rs adds the five Inter TTFs of the Slint front end (`include_bytes!`)
to GPUI's text system; the theme names the family. build.rs turns the 147
Lucide 1.48 SVGs of crates/voelin-ui/ui/assets/icons into a sorted table of
`include_bytes!` (std only, no new dependency) that assets.rs serves as
gpui-component's `icons/<name>.svg` before gpui-kit-assets' Lucide 1.43
catalogue (1,830 paths, which answers the rest and, as it does, an error for
a path it lacks); a test checks that `mic.svg` is the client's, differs from
the catalogue's, and that other icons fall through.

Development switches (dev.rs): `VOELIN_OPEN=swatch`, `VOELIN_DEV_THEME=
dark|light|system`, `VOELIN_FONT_SCALE`, `VOELIN_WINDOW_SIZE` (the Slint
front end's names where they exist). swatch.rs is the page: the 43 colours in
the groups of docs/ui.md's "Theme tokens" table with name and value (a test
checks each token is in exactly one group), the backdrop, radii, spacing,
the type scale, buttons, switches and checkboxes of gpui-component and 16
icons; it scrolls when the window is smaller. scripts/gpui-shot.sh makes the
screenshot headless the way scripts/shots.sh does for Slint (xvfb-run, or
GPUI_SHOT_HEADLESS=1 under headless.sh on NixOS; Wayland, the session bus
and the XDG directories out of the environment), then closes the window with
Alt+F4 and reports how the app ended. docs/ui/gpui-swatch-dark.png and
-light.png are its output (Xvfb :97, openbox, lavapipe via wgpu's Vulkan
backend, the debug binary, 1440x960, truecolour: a 256-colour reduction would
change the tokens' values; the Slint pictures live in docs/screenshots/, the
task asked for docs/ui/).

Checked on Rust 1.95.0: fmt, clippy -D warnings --no-deps (all targets, the
build script included), `cargo test -p voelin-gpui` 26 tests, cargo deny,
`scripts/notices.sh --check`.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-27 07:07:10 +03:00

279 lines
9.8 KiB
Rust

//! The second layer of the theme: the colours gpui-component's widgets read,
//! written from the Voelin palette. gpui-component's `ThemeColor` is shadcn's
//! vocabulary (about 135 colours: a background, a primary, a muted, one set
//! for the list, one for the table, one for each kind of button); the
//! client's tokens map onto it as docs/ui.md ("The GPUI front end") lists.
//! What has no counterpart stays as the registered theme of the mode has it
//! (charts' bullish and bearish colours, the cyan and magenta of the base
//! palette).
//!
//! gpui-component's "accent" is a hover background, not a brand colour: the
//! client's `accent` is its `primary`.
use gpui_kit::Hsla;
use gpui_kit::component::ThemeColor;
use super::tokens::{Palette, Rgba};
/// A palette colour as GPUI's.
pub fn hsla(color: Rgba) -> Hsla {
gpui_kit::rgba(color.0).into()
}
/// Of `base`, the colours a status button needs: hover, active, and a text
/// that reads on it (white or the window's ink, whichever contrasts more).
struct Status {
base: Hsla,
hover: Hsla,
active: Hsla,
foreground: Hsla,
}
fn status(base: Rgba, ink: Rgba, dark: bool) -> Status {
let toward_hover = if dark { Rgba(0xffff_ffff) } else { Rgba(0x0000_00ff) };
Status {
base: hsla(base),
hover: hsla(base.mix(toward_hover, 0.14)),
active: hsla(base.mix(Rgba(0x0000_00ff), 0.18)),
foreground: hsla(base.readable(ink)),
}
}
/// Write the palette's colours into `colors`, over what the registered theme
/// of the mode left there. Every field that gpui-component's themes derive
/// from another (the buttons fall back to `primary`, `danger`, ...) is written
/// too, so that none keeps the default theme's blue.
pub fn write(p: &Palette, dark: bool, colors: &mut ThemeColor) {
let c = colors;
let h = hsla;
let clear = Hsla::transparent_black();
// The window and its text.
c.background = h(p.bg_app);
c.foreground = h(p.text);
c.border = h(p.border);
c.input = h(p.border_strong);
c.ring = h(p.glow);
c.overlay = h(p.scrim);
c.window_border = h(p.border);
c.caret = h(p.text);
c.selection = h(p.accent_soft);
c.drag_border = h(p.accent);
c.drop_target = h(p.accent_soft);
// Links: one colour for all three states, as the client has one.
c.link = h(p.accent_text);
c.link_hover = h(p.accent_text);
c.link_active = h(p.accent_text);
// The brand colour, and gpui-component's hover "accent".
c.primary = h(p.accent);
c.primary_hover = h(p.accent_hover);
c.primary_active = h(p.accent_pressed);
c.primary_foreground = h(p.on_accent);
c.accent = h(p.surface_3);
c.accent_foreground = h(p.text);
// Cards, inputs, menus, popovers.
c.secondary = h(p.surface_2);
c.secondary_hover = h(p.surface_3);
c.secondary_active = h(p.surface_4);
c.secondary_foreground = h(p.text);
c.muted = h(p.surface_2);
c.muted_foreground = h(p.text_muted);
c.popover = h(p.surface_3);
c.popover_foreground = h(p.text);
c.group_box = h(p.surface_2);
c.group_box_foreground = h(p.text);
c.accordion = h(p.surface_2);
c.description_list_label = h(p.surface_2);
c.description_list_label_foreground = h(p.text_secondary);
// The status colours, with the text that reads on each.
let [danger, success, warning, info] =
[p.danger, p.success, p.warning, p.info].map(|base| status(base, p.bg_app, dark));
(c.danger, c.danger_hover, c.danger_active, c.danger_foreground) =
(danger.base, danger.hover, danger.active, danger.foreground);
(c.success, c.success_hover, c.success_active, c.success_foreground) =
(success.base, success.hover, success.active, success.foreground);
(c.warning, c.warning_hover, c.warning_active, c.warning_foreground) =
(warning.base, warning.hover, warning.active, warning.foreground);
(c.info, c.info_hover, c.info_active, c.info_foreground) =
(info.base, info.hover, info.active, info.foreground);
// Lists and tables: rows on the panel, hovered rows raised, the selected
// row the soft accent with the accent's edge.
c.list = h(p.surface);
c.list_hover = h(p.surface_3);
c.list_active = h(p.accent_soft);
c.list_active_border = h(p.accent);
c.list_even = h(p.surface);
c.list_head = h(p.surface_2);
c.table = h(p.surface);
c.table_hover = h(p.surface_3);
c.table_active = h(p.accent_soft);
c.table_active_border = h(p.accent);
c.table_even = h(p.surface);
c.table_head = h(p.surface_2);
c.table_head_foreground = h(p.text_secondary);
c.table_foot = h(p.surface_2);
c.table_foot_foreground = h(p.text_secondary);
c.table_row_border = h(p.border);
// The panels around the content: the sidebar, the title and status bars.
c.sidebar = h(p.surface);
c.sidebar_foreground = h(p.text);
c.sidebar_border = h(p.border);
c.sidebar_accent = h(p.surface_3);
c.sidebar_accent_foreground = h(p.text);
c.sidebar_primary = h(p.accent);
c.sidebar_primary_foreground = h(p.on_accent);
c.title_bar = h(p.surface);
c.title_bar_border = h(p.border);
c.status_bar = h(p.surface);
c.status_bar_border = h(p.border);
// Small widgets.
c.scrollbar = clear;
c.scrollbar_thumb = h(p.border_strong);
c.scrollbar_thumb_hover = h(p.text_muted);
c.slider_bar = h(p.accent);
c.slider_thumb = h(p.on_accent);
c.switch = h(p.surface_4);
c.switch_thumb = h(p.on_accent);
c.progress_bar = h(p.accent);
c.skeleton = h(p.surface_3);
c.tab = clear;
c.tab_active = h(p.surface_2);
c.tab_active_foreground = h(p.text);
c.tab_bar = h(p.surface);
c.tab_bar_segmented = h(p.surface_2);
c.tab_foreground = h(p.text_secondary);
// The base palette and the charts: the client's state colours.
c.red = h(p.danger);
c.red_light = h(p.danger_soft);
c.green = h(p.success);
c.green_light = h(p.success_soft);
c.yellow = h(p.warning);
c.yellow_light = h(p.warning_soft);
c.blue = h(p.info);
c.blue_light = h(p.accent_soft);
c.chart_1 = h(p.accent);
c.chart_2 = h(p.success);
c.chart_3 = h(p.warning);
c.chart_4 = h(p.danger);
c.chart_5 = h(p.info);
// The buttons: what a theme file leaves out they take from the colours
// above, so they are written from them here.
c.button = c.secondary;
c.button_hover = c.secondary_hover;
c.button_active = c.secondary_active;
c.button_foreground = c.secondary_foreground;
c.button_primary = c.primary;
c.button_primary_hover = c.primary_hover;
c.button_primary_active = c.primary_active;
c.button_primary_foreground = c.primary_foreground;
c.button_secondary = c.secondary;
c.button_secondary_hover = c.secondary_hover;
c.button_secondary_active = c.secondary_active;
c.button_secondary_foreground = c.secondary_foreground;
c.button_danger = c.danger;
c.button_danger_hover = c.danger_hover;
c.button_danger_active = c.danger_active;
c.button_danger_foreground = c.danger_foreground;
c.button_success = c.success;
c.button_success_hover = c.success_hover;
c.button_success_active = c.success_active;
c.button_success_foreground = c.success_foreground;
c.button_warning = c.warning;
c.button_warning_hover = c.warning_hover;
c.button_warning_active = c.warning_active;
c.button_warning_foreground = c.warning_foreground;
c.button_info = c.info;
c.button_info_hover = c.info_hover;
c.button_info_active = c.info_active;
c.button_info_foreground = c.info_foreground;
}
#[cfg(test)]
mod tests {
use super::*;
fn written(dark: bool) -> (Palette, ThemeColor) {
let palette = if dark { Palette::DARK } else { Palette::LIGHT };
let mut colors = ThemeColor::default();
write(&palette, dark, &mut colors);
(palette, colors)
}
#[test]
fn the_client_tokens_land_where_the_table_says() {
for dark in [true, false] {
let (p, c) = written(dark);
assert_eq!(c.background, hsla(p.bg_app));
assert_eq!(c.foreground, hsla(p.text));
assert_eq!(c.primary, hsla(p.accent));
assert_eq!(c.primary_hover, hsla(p.accent_hover));
assert_eq!(c.primary_active, hsla(p.accent_pressed));
assert_eq!(c.primary_foreground, hsla(p.on_accent));
assert_eq!(
c.accent,
hsla(p.surface_3),
"gpui-component's accent is a hover background"
);
assert_eq!(c.secondary, hsla(p.surface_2));
assert_eq!(c.secondary_active, hsla(p.surface_4));
assert_eq!(c.border, hsla(p.border));
assert_eq!(c.input, hsla(p.border_strong));
assert_eq!(c.ring, hsla(p.glow));
assert_eq!(c.overlay, hsla(p.scrim));
assert_eq!(c.list_active, hsla(p.accent_soft));
assert_eq!(c.link, hsla(p.accent_text));
assert_eq!(c.muted_foreground, hsla(p.text_muted));
assert_eq!(c.sidebar, hsla(p.surface));
assert_eq!(c.danger, hsla(p.danger));
assert_eq!(c.success, hsla(p.success));
}
}
#[test]
fn the_buttons_follow_the_colours_they_fall_back_to() {
let (_, c) = written(true);
assert_eq!(c.button_primary, c.primary);
assert_eq!(c.button_primary_hover, c.primary_hover);
assert_eq!(c.button_danger_foreground, c.danger_foreground);
assert_eq!(c.button, c.secondary);
assert_eq!(c.button_info_active, c.info_active);
}
#[test]
fn the_text_on_a_status_colour_is_the_one_that_reads_better() {
for (p, dark) in [(Palette::DARK, true), (Palette::LIGHT, false)] {
for base in [p.danger, p.success, p.warning, p.info] {
let s = status(base, p.bg_app, dark);
let chosen = base.readable(p.bg_app);
let other = if chosen == Rgba(0xffff_ffff) { p.bg_app } else { Rgba(0xffff_ffff) };
assert!(base.contrast(chosen) >= base.contrast(other), "{base} on {chosen}");
assert_eq!(s.foreground, hsla(chosen));
assert_ne!(s.hover, s.base);
assert_ne!(s.active, s.base);
}
}
}
#[test]
fn mixing_and_contrast_do_what_they_say() {
let (black, white) = (Rgba(0x0000_00ff), Rgba(0xffff_ffff));
assert_eq!(black.mix(white, 0.0), black);
assert_eq!(black.mix(white, 1.0), white);
assert_eq!(Rgba(0x1020_30c0).mix(white, 0.0), Rgba(0x1020_30c0), "opacity stays");
assert!((black.contrast(white) - 21.0).abs() < 0.01);
assert_eq!(black.readable(black), white);
assert_eq!(Rgba(0xf5b3_01ff).readable(Rgba(0x060a_18ff)), Rgba(0x060a_18ff));
assert_eq!(Rgba(0x12ab_34ff).to_string(), "#12ab34");
assert_eq!(Rgba(0x12ab_3480).to_string(), "#12ab3480");
}
}