//! The swatch page: every colour token of the theme with its name and value, //! the radii, the spacing, the type scale, a few gpui-component widgets (drawn //! from the second layer of the theme) and the client's icons, in the mode //! the theme is in. A development page (`VOELIN_OPEN=swatch`): the screenshots //! of a theme change, and what the screenshots in docs/ui/ show. use std::collections::HashMap; use gpui_kit::component::button::{Button, ButtonVariants as _}; use gpui_kit::component::checkbox::Checkbox; use gpui_kit::component::switch::Switch; use gpui_kit::{ FontWeight, InteractiveElement as _, IntoElement, ParentElement as _, StatefulInteractiveElement as _, Styled as _, div, px, svg, }; use crate::theme::{VoelinTheme, hsla, tokens::Rgba}; /// The colour tokens by the groups of docs/ui.md's "Theme tokens" table. pub const GROUPS: [(&str, &[&str]); 6] = [ ( "Backgrounds", &[ "bg-app", "bg-rail", "surface", "surface-2", "surface-3", "surface-4", "scrim", "scrim-light", "overlay", ], ), ("Lines", &["border", "border-strong", "border-popup", "glow"]), ( "Accent", &[ "accent", "accent-hover", "accent-pressed", "accent-soft", "accent-soft-hover", "accent-text", "name-text", "on-accent", ], ), ( "States", &[ "live", "live-soft", "danger", "danger-soft", "success", "success-soft", "warning", "warning-soft", "idle", "dnd", "offline", "gold", "info", ], ), ("Text", &["text", "text-secondary", "text-muted", "text-disabled", "icon"]), ("Meter", &["meter-low", "meter-mid", "meter-high", "meter-off"]), ]; /// Icons shown, by the name of the client's file. const ICONS: [&str; 16] = [ "mic.svg", "mic-off.svg", "headphones.svg", "volume-2.svg", "settings.svg", "search.svg", "bell.svg", "users.svg", "message-square.svg", "monitor-up.svg", "hash.svg", "lock.svg", "crown.svg", "plus.svg", "x.svg", "check.svg", ]; fn heading(theme: &VoelinTheme, text: &str) -> impl IntoElement { let m = &theme.metrics; div() .text_size(px(m.font_md)) .font_weight(FontWeight::SEMIBOLD) .text_color(hsla(theme.colors.text)) .child(text.to_owned()) } fn chip(theme: &VoelinTheme, name: &str, color: Rgba) -> impl IntoElement { let (c, m) = (&theme.colors, &theme.metrics); div() .w(px(100.0)) .flex() .flex_col() .gap(px(2.0)) .child( div() .h(px(24.0)) .rounded(px(m.radius_xs)) .border_1() .border_color(hsla(c.border_strong)) .bg(hsla(color)), ) .child( div() .text_size(px(m.font_xs)) .text_color(hsla(c.text_secondary)) .child(name.to_owned()), ) .child( div().text_size(px(m.font_xs)).text_color(hsla(c.text_muted)).child(color.to_string()), ) } fn colours(theme: &VoelinTheme) -> impl IntoElement { let (c, m) = (&theme.colors, &theme.metrics); let by_name: HashMap<&str, Rgba> = c.entries().into_iter().collect(); let mut column = div().flex().flex_col().gap(px(m.space_4)); for (group, names) in GROUPS { let mut row = div().flex().flex_row().flex_wrap().gap(px(m.space_3)); for name in names { row = row.child(chip(theme, name, by_name[name])); } column = column.child( div().flex().flex_col().gap(px(m.space_2)).child(heading(theme, group)).child(row), ); } column } fn radii(theme: &VoelinTheme) -> impl IntoElement { let (c, m) = (&theme.colors, &theme.metrics); let mut row = div().flex().flex_row().gap(px(m.space_4)); for (name, radius) in [ ("xs", m.radius_xs), ("sm", m.radius_sm), ("md", m.radius_md), ("lg", m.radius_lg), ("xl", m.radius_xl), ("pill", m.radius_pill), ] { row = row.child( div() .flex() .flex_col() .items_center() .gap(px(m.space_2)) .child( div() .size(px(40.0)) .rounded(px(radius)) .bg(hsla(c.surface_4)) .border_1() .border_color(hsla(c.border_popup)), ) .child(div().text_size(px(m.font_xs)).text_color(hsla(c.text_muted)).child(name)), ); } div().flex().flex_col().gap(px(m.space_3)).child(heading(theme, "Radii")).child(row) } fn spacing(theme: &VoelinTheme) -> impl IntoElement { let (c, m) = (&theme.colors, &theme.metrics); let mut rows = div().flex().flex_col().gap(px(m.space_1)); for (name, space) in [ ("space-1", m.space_1), ("space-2", m.space_2), ("space-3", m.space_3), ("space-4", m.space_4), ("space-5", m.space_5), ("space-6", m.space_6), ("space-7", m.space_7), ("space-8", m.space_8), ] { rows = rows.child( div() .flex() .flex_row() .items_center() .gap(px(m.space_3)) .child( div() .w(px(56.0)) .text_size(px(m.font_xs)) .text_color(hsla(c.text_muted)) .child(name), ) .child(div().h(px(6.0)).w(px(space * 4.0)).rounded(px(2.0)).bg(hsla(c.accent))) .child( div() .text_size(px(m.font_xs)) .text_color(hsla(c.text_muted)) .child(format!("{space} px")), ), ); } div().flex().flex_col().gap(px(m.space_3)).child(heading(theme, "Spacing")).child(rows) } fn type_scale(theme: &VoelinTheme) -> impl IntoElement { let (c, m) = (&theme.colors, &theme.metrics); let mut rows = div().flex().flex_col(); for (name, size, weight) in [ ("font-3xl", m.font_3xl, FontWeight::BOLD), ("font-2xl", m.font_2xl, FontWeight::SEMIBOLD), ("font-xl", m.font_xl, FontWeight::SEMIBOLD), ("font-lg", m.font_lg, FontWeight::MEDIUM), ("font-md", m.font_md, FontWeight::MEDIUM), ("font-body", m.font_body, FontWeight::NORMAL), ("font-sm", m.font_sm, FontWeight::NORMAL), ("font-xs", m.font_xs, FontWeight::NORMAL), ] { rows = rows.child( div() .text_size(px(size)) .font_weight(weight) .text_color(hsla(c.text)) .child(format!("{name} {size}: Raid tonight at 21:00")), ); } div().flex().flex_col().gap(px(m.space_3)).child(heading(theme, "Type")).child(rows) } fn components(theme: &VoelinTheme) -> impl IntoElement { let m = &theme.metrics; div() .flex() .flex_col() .gap(px(m.space_3)) .child(heading(theme, "Components (gpui-component, through ThemeColor)")) .child( div() .flex() .flex_row() .flex_wrap() .gap(px(m.space_3)) .child(Button::new("primary").primary().label("Primary")) .child(Button::new("secondary").label("Secondary")) .child(Button::new("danger").danger().label("Danger")) .child(Button::new("success").success().label("Success")) .child(Button::new("warning").warning().label("Warning")) .child(Button::new("info").info().label("Info")), ) .child( div() .flex() .flex_row() .items_center() .gap(px(m.space_5)) .child(Switch::new("on").checked(true)) .child(Switch::new("off").checked(false)) .child(Checkbox::new("checked").checked(true).label("Checked")) .child(Checkbox::new("unchecked").checked(false).label("Unchecked")), ) } fn backdrop(theme: &VoelinTheme) -> impl IntoElement { let (c, m, b) = (&theme.colors, &theme.metrics, &theme.backdrop); let [first, _, last] = b.stops; div().flex().flex_col().gap(px(m.space_3)).child(heading(theme, "Backdrop")).child( div() .h(px(36.0)) .rounded(px(m.radius_md)) .border_1() .border_color(hsla(c.border)) .bg(b.to_gpui()) .px(px(m.space_3)) .py(px(m.space_2)) .text_size(px(m.font_xs)) .text_color(hsla(c.text_secondary)) .child(format!( "{}deg: {} to {}; the middle stop, {} at {}%, is not drawn", b.angle, first.0, last.0, b.stops[1].0, (b.stops[1].1 * 100.0).round() )), ) } fn icons(theme: &VoelinTheme) -> impl IntoElement { let (c, m) = (&theme.colors, &theme.metrics); let mut row = div().flex().flex_row().flex_wrap().gap(px(m.space_5)); for name in ICONS { row = row.child( svg().path(format!("icons/{name}")).size(px(m.icon_lg)).text_color(hsla(c.icon)), ); } div() .flex() .flex_col() .gap(px(m.space_3)) .child(heading(theme, "Icons (the client's Lucide 1.48, as gpui-component's icons/ paths)")) .child(row) } /// The page: the colours on the left, the rest on the right; it scrolls when /// the window is smaller than it. pub fn swatch(theme: &VoelinTheme) -> impl IntoElement { let (c, m) = (&theme.colors, &theme.metrics); div() .id("swatch") .size_full() .overflow_y_scroll() .flex() .flex_col() .gap(px(m.space_4)) .p(px(m.space_6)) .bg(hsla(c.bg_app)) .child( div() .flex() .flex_row() .items_baseline() .gap(px(m.space_4)) .child( div() .text_size(px(m.font_2xl)) .font_weight(FontWeight::BOLD) .text_color(hsla(c.text)) .child("Voelin theme"), ) .child(div().text_size(px(m.font_sm)).text_color(hsla(c.text_muted)).child( format!( "{} (setting {}), text size {}", if theme.is_dark() { "dark" } else { "light" }, theme.choice().name(), m.font_scale ), )), ) .child( div() .flex() .flex_row() .gap(px(m.space_8)) .child(div().w(px(752.0)).flex_none().child(colours(theme))) .child( div() .w(px(600.0)) .flex() .flex_col() .gap(px(m.space_4)) .child(backdrop(theme)) .child(radii(theme)) .child(spacing(theme)) .child(type_scale(theme)) .child(components(theme)) .child(icons(theme)), ), ) } #[cfg(test)] mod tests { use std::collections::BTreeSet; use super::*; use crate::assets::client_icon; use crate::theme::Palette; #[test] fn every_colour_token_is_in_exactly_one_group() { let shown: Vec<&str> = GROUPS.iter().flat_map(|(_, names)| names.iter().copied()).collect(); let unique: BTreeSet<&str> = shown.iter().copied().collect(); assert_eq!(shown.len(), unique.len(), "a token in two groups"); let tokens: BTreeSet<&str> = Palette::DARK.entries().into_iter().map(|(n, _)| n).collect(); assert_eq!(unique, tokens); } #[test] fn the_icons_shown_are_the_clients() { for name in ICONS { assert!(client_icon(name).is_some(), "{name}"); } } }