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, point, px};
7use schemars::JsonSchema;
8use serde::{Deserialize, Serialize};
9
10#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
11pub struct SemanticThemeTokens {
12    pub colors: ColorTokens,
13    pub radius: RadiusTokens,
14    pub spacing: SpacingTokens,
15    pub typography: TypographyTokens,
16    pub shadow: ShadowTokens,
17}
18
19#[derive(Debug, Default, Clone, Copy, PartialEq, Serialize, Deserialize, JsonSchema)]
20pub struct ColorTokens {
21    #[schemars(schema_with = "gpui::hsla_schemar")]
22    pub background: Hsla,
23    #[schemars(schema_with = "gpui::hsla_schemar")]
24    pub foreground: Hsla,
25    #[schemars(schema_with = "gpui::hsla_schemar")]
26    pub surface: Hsla,
27    #[schemars(schema_with = "gpui::hsla_schemar")]
28    pub surface_foreground: Hsla,
29    #[schemars(schema_with = "gpui::hsla_schemar")]
30    pub primary: Hsla,
31    #[schemars(schema_with = "gpui::hsla_schemar")]
32    pub primary_foreground: Hsla,
33    #[schemars(schema_with = "gpui::hsla_schemar")]
34    pub secondary: Hsla,
35    #[schemars(schema_with = "gpui::hsla_schemar")]
36    pub secondary_foreground: Hsla,
37    #[schemars(schema_with = "gpui::hsla_schemar")]
38    pub muted: Hsla,
39    #[schemars(schema_with = "gpui::hsla_schemar")]
40    pub muted_foreground: Hsla,
41    #[schemars(schema_with = "gpui::hsla_schemar")]
42    pub accent: Hsla,
43    #[schemars(schema_with = "gpui::hsla_schemar")]
44    pub accent_foreground: Hsla,
45    #[schemars(schema_with = "gpui::hsla_schemar")]
46    pub destructive: Hsla,
47    #[schemars(schema_with = "gpui::hsla_schemar")]
48    pub destructive_foreground: Hsla,
49    #[schemars(schema_with = "gpui::hsla_schemar")]
50    pub border: Hsla,
51    #[schemars(schema_with = "gpui::hsla_schemar")]
52    pub input: Hsla,
53    #[schemars(schema_with = "gpui::hsla_schemar")]
54    pub ring: Hsla,
55}
56
57#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, JsonSchema)]
58pub struct RadiusTokens {
59    pub none: Pixels,
60    pub sm: Pixels,
61    pub md: Pixels,
62    pub lg: Pixels,
63    pub xl: Pixels,
64    pub full: Pixels,
65}
66
67impl Default for RadiusTokens {
68    fn default() -> Self {
69        Self {
70            none: px(0.),
71            sm: px(3.),
72            md: px(6.),
73            lg: px(8.),
74            xl: px(12.),
75            full: px(9999.),
76        }
77    }
78}
79
80#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, JsonSchema)]
81pub struct SpacingTokens {
82    pub xxs: Pixels,
83    pub xs: Pixels,
84    pub sm: Pixels,
85    pub md: Pixels,
86    pub lg: Pixels,
87    pub xl: Pixels,
88    pub xxl: Pixels,
89}
90
91impl Default for SpacingTokens {
92    fn default() -> Self {
93        Self {
94            xxs: px(2.),
95            xs: px(4.),
96            sm: px(8.),
97            md: px(12.),
98            lg: px(16.),
99            xl: px(24.),
100            xxl: px(32.),
101        }
102    }
103}
104
105#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, JsonSchema)]
106pub struct TextStyleToken {
107    pub size: Pixels,
108    pub line_height: Pixels,
109    pub weight: FontWeight,
110}
111
112#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
113pub struct TypographyTokens {
114    pub sans: SharedString,
115    pub mono: SharedString,
116    pub xs: TextStyleToken,
117    pub sm: TextStyleToken,
118    pub md: TextStyleToken,
119    pub lg: TextStyleToken,
120    pub xl: TextStyleToken,
121    pub mono_md: TextStyleToken,
122}
123
124impl Default for TypographyTokens {
125    fn default() -> Self {
126        Self {
127            sans: ".SystemUIFont".into(),
128            mono: default_mono_font_family(),
129            xs: text_style(12., 16.),
130            sm: text_style(14., 20.),
131            md: text_style(16., 24.),
132            lg: text_style(18., 28.),
133            xl: text_style(20., 28.),
134            mono_md: text_style(13., 20.),
135        }
136    }
137}
138
139#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
140pub struct ShadowTokens {
141    #[schemars(with = "Vec<serde_json::Value>")]
142    pub sm: Vec<BoxShadow>,
143    #[schemars(with = "Vec<serde_json::Value>")]
144    pub md: Vec<BoxShadow>,
145    #[schemars(with = "Vec<serde_json::Value>")]
146    pub lg: Vec<BoxShadow>,
147}
148
149impl ShadowTokens {
150    pub fn elevations(color: Hsla) -> Self {
151        Self {
152            sm: vec![box_shadow(0., 1., 2., 0., color)],
153            md: vec![box_shadow(0., 4., 8., -2., color)],
154            lg: vec![box_shadow(0., 12., 24., -4., color)],
155        }
156    }
157}
158
159fn text_style(size: f32, line_height: f32) -> TextStyleToken {
160    TextStyleToken {
161        size: px(size),
162        line_height: px(line_height),
163        weight: FontWeight::NORMAL,
164    }
165}
166
167fn default_mono_font_family() -> SharedString {
168    if cfg!(target_os = "macos") {
169        "Menlo".into()
170    } else if cfg!(target_os = "windows") {
171        "Consolas".into()
172    } else {
173        "DejaVu Sans Mono".into()
174    }
175}
176
177fn box_shadow(x: f32, y: f32, blur: f32, spread: f32, color: Hsla) -> BoxShadow {
178    BoxShadow {
179        color,
180        offset: point(px(x), px(y)),
181        blur_radius: px(blur),
182        spread_radius: px(spread),
183        inset: false,
184    }
185}