Skip to main content

gpui_base/
theme_tokens.rs

1//! Semantic design tokens shared by application-owned components.
2//!
3//! These tokens describe visual roles and scales. They intentionally do not
4//! contain component names such as `button`, `table`, or `sidebar`.
5
6use gpui::{BoxShadow, FontWeight, Hsla, Pixels, SharedString, hsla, point, px, rgb, rgb_to_hsla};
7use palette::WithAlpha as _;
8
9use schemars::JsonSchema;
10use serde::{Deserialize, Serialize};
11
12#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
13pub struct SemanticThemeTokens {
14    pub colors: ColorTokens,
15    pub radius: RadiusTokens,
16    pub spacing: SpacingTokens,
17    pub typography: TypographyTokens,
18    pub shadow: ShadowTokens,
19}
20
21#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, JsonSchema)]
22pub struct ColorTokens {
23    #[schemars(schema_with = "gpui::hsla_schemar")]
24    pub background: Hsla,
25    #[schemars(schema_with = "gpui::hsla_schemar")]
26    pub foreground: Hsla,
27    #[schemars(schema_with = "gpui::hsla_schemar")]
28    pub surface: Hsla,
29    #[schemars(schema_with = "gpui::hsla_schemar")]
30    pub surface_foreground: Hsla,
31    #[schemars(schema_with = "gpui::hsla_schemar")]
32    pub primary: Hsla,
33    #[schemars(schema_with = "gpui::hsla_schemar")]
34    pub primary_foreground: Hsla,
35    #[schemars(schema_with = "gpui::hsla_schemar")]
36    pub secondary: Hsla,
37    #[schemars(schema_with = "gpui::hsla_schemar")]
38    pub secondary_foreground: Hsla,
39    #[schemars(schema_with = "gpui::hsla_schemar")]
40    pub muted: Hsla,
41    #[schemars(schema_with = "gpui::hsla_schemar")]
42    pub muted_foreground: Hsla,
43    #[schemars(schema_with = "gpui::hsla_schemar")]
44    pub accent: Hsla,
45    #[schemars(schema_with = "gpui::hsla_schemar")]
46    pub accent_foreground: Hsla,
47    #[schemars(schema_with = "gpui::hsla_schemar")]
48    pub destructive: Hsla,
49    #[schemars(schema_with = "gpui::hsla_schemar")]
50    pub destructive_foreground: Hsla,
51    #[schemars(schema_with = "gpui::hsla_schemar")]
52    pub border: Hsla,
53    #[schemars(schema_with = "gpui::hsla_schemar")]
54    pub input: Hsla,
55    #[schemars(schema_with = "gpui::hsla_schemar")]
56    pub ring: Hsla,
57    /// Background painted behind selected text.
58    ///
59    /// Selection quads are painted under the glyphs, so this is a translucent
60    /// wash that leaves the text legible. It carries a serde default so
61    /// palettes written before the token existed still load.
62    #[serde(default = "ColorTokens::default_selection")]
63    #[schemars(schema_with = "gpui::hsla_schemar")]
64    pub selection: Hsla,
65}
66
67impl Default for ColorTokens {
68    fn default() -> Self {
69        Self::light()
70    }
71}
72
73impl ColorTokens {
74    /// Default light palette, aligned with gpui-component's Default Light theme.
75    pub fn light() -> Self {
76        Self {
77            background: hsla(0., 0., 1., 1.),
78            foreground: hsla(0., 0., 0.039, 1.),
79            surface: hsla(0., 0., 1., 1.),
80            surface_foreground: hsla(0., 0., 0.039, 1.),
81            primary: hsla(0., 0., 0.09, 1.),
82            primary_foreground: hsla(0., 0., 0.98, 1.),
83            secondary: hsla(0., 0., 0.898, 1.),
84            secondary_foreground: hsla(0., 0., 0.09, 1.),
85            muted: hsla(0., 0., 0.961, 1.),
86            muted_foreground: hsla(0., 0., 0.451, 1.),
87            accent: hsla(0., 0., 0.961, 1.),
88            accent_foreground: hsla(0., 0., 0.09, 1.),
89            destructive: hsla(0., 0.842, 0.602, 1.),
90            destructive_foreground: hsla(0., 0., 0.98, 1.),
91            border: hsla(0., 0., 0.898, 1.),
92            input: hsla(0., 0., 0.898, 1.),
93            ring: hsla(0., 0., 0.639, 1.),
94            selection: rgb_to_hsla(rgb(0x55a0fc)).with_alpha(0.3),
95        }
96    }
97
98    /// Default dark palette, aligned with gpui-component's Default Dark theme.
99    pub fn dark() -> Self {
100        Self {
101            background: hsla(0., 0., 0.039, 1.),
102            foreground: hsla(0., 0., 0.98, 1.),
103            surface: hsla(0., 0., 0.039, 1.),
104            surface_foreground: hsla(0., 0., 0.98, 1.),
105            primary: hsla(0., 0., 0.98, 1.),
106            primary_foreground: hsla(0., 0., 0.09, 1.),
107            secondary: hsla(0., 0., 0.149, 1.),
108            secondary_foreground: hsla(0., 0., 0.98, 1.),
109            muted: hsla(0., 0., 0.149, 1.),
110            muted_foreground: hsla(0., 0., 0.639, 1.),
111            accent: hsla(0., 0., 0.149, 1.),
112            accent_foreground: hsla(0., 0., 0.98, 1.),
113            destructive: hsla(0., 0.906, 0.708, 1.),
114            destructive_foreground: hsla(0., 0.722, 0.506, 1.),
115            border: hsla(0., 0., 0.149, 1.),
116            input: hsla(0., 0., 47. / 255., 1.),
117            ring: hsla(0., 0., 0.451, 1.),
118            selection: rgb_to_hsla(rgb(0x1d4ed8)).with_alpha(0.3),
119        }
120    }
121
122    /// The selection color a palette falls back to when it predates the token.
123    fn default_selection() -> Hsla {
124        Self::light().selection
125    }
126}
127
128#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, JsonSchema)]
129pub struct RadiusTokens {
130    pub none: Pixels,
131    pub sm: Pixels,
132    pub md: Pixels,
133    pub lg: Pixels,
134    pub xl: Pixels,
135    pub full: Pixels,
136}
137
138impl Default for RadiusTokens {
139    fn default() -> Self {
140        Self {
141            none: px(0.),
142            sm: px(3.),
143            md: px(6.),
144            lg: px(8.),
145            xl: px(12.),
146            full: px(9999.),
147        }
148    }
149}
150
151#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, JsonSchema)]
152pub struct SpacingTokens {
153    pub xxs: Pixels,
154    pub xs: Pixels,
155    pub sm: Pixels,
156    pub md: Pixels,
157    pub lg: Pixels,
158    pub xl: Pixels,
159    pub xxl: Pixels,
160}
161
162impl Default for SpacingTokens {
163    fn default() -> Self {
164        Self {
165            xxs: px(2.),
166            xs: px(4.),
167            sm: px(8.),
168            md: px(12.),
169            lg: px(16.),
170            xl: px(24.),
171            xxl: px(32.),
172        }
173    }
174}
175
176#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, JsonSchema)]
177pub struct TextStyleToken {
178    pub size: Pixels,
179    pub line_height: Pixels,
180    pub weight: FontWeight,
181}
182
183#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
184pub struct TypographyTokens {
185    pub sans: SharedString,
186    pub mono: SharedString,
187    pub xs: TextStyleToken,
188    pub sm: TextStyleToken,
189    pub md: TextStyleToken,
190    pub lg: TextStyleToken,
191    pub xl: TextStyleToken,
192    pub mono_md: TextStyleToken,
193}
194
195impl Default for TypographyTokens {
196    fn default() -> Self {
197        Self {
198            sans: ".SystemUIFont".into(),
199            mono: default_mono_font_family(),
200            xs: text_style(12., 16.),
201            sm: text_style(14., 20.),
202            md: text_style(16., 24.),
203            lg: text_style(18., 28.),
204            xl: text_style(20., 28.),
205            mono_md: text_style(13., 20.),
206        }
207    }
208}
209
210#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
211pub struct ShadowTokens {
212    #[schemars(with = "Vec<serde_json::Value>")]
213    pub sm: Vec<BoxShadow>,
214    #[schemars(with = "Vec<serde_json::Value>")]
215    pub md: Vec<BoxShadow>,
216    #[schemars(with = "Vec<serde_json::Value>")]
217    pub lg: Vec<BoxShadow>,
218}
219
220impl ShadowTokens {
221    pub fn elevations(color: Hsla) -> Self {
222        Self {
223            sm: vec![box_shadow(0., 1., 2., 0., color)],
224            md: vec![box_shadow(0., 4., 8., -2., color)],
225            lg: vec![box_shadow(0., 12., 24., -4., color)],
226        }
227    }
228}
229
230fn text_style(size: f32, line_height: f32) -> TextStyleToken {
231    TextStyleToken {
232        size: px(size),
233        line_height: px(line_height),
234        weight: FontWeight::NORMAL,
235    }
236}
237
238fn default_mono_font_family() -> SharedString {
239    if cfg!(target_os = "macos") {
240        "Menlo".into()
241    } else if cfg!(target_os = "windows") {
242        "Consolas".into()
243    } else {
244        "DejaVu Sans Mono".into()
245    }
246}
247
248fn box_shadow(x: f32, y: f32, blur: f32, spread: f32, color: Hsla) -> BoxShadow {
249    BoxShadow {
250        color: color.into(),
251        offset: point(px(x), px(y)),
252        blur_radius: px(blur),
253        spread_radius: px(spread),
254        inset: false,
255    }
256}
257
258#[cfg(test)]
259mod tests {
260    use super::ColorTokens;
261
262    #[test]
263    fn default_colors_are_the_light_palette_and_both_palettes_are_readable() {
264        let light = ColorTokens::light();
265        let dark = ColorTokens::dark();
266
267        assert_eq!(ColorTokens::default(), light);
268        assert_eq!(light.background.color.lightness, 1.);
269        assert!(light.foreground.color.lightness < light.background.color.lightness);
270        assert!(dark.background.color.lightness < dark.foreground.color.lightness);
271        assert_eq!(light.primary.alpha, 1.);
272        assert_eq!(dark.primary.alpha, 1.);
273    }
274}