bezel-theme 0.2.25

Design tokens for gpui — an environment-driven theme with designed light and dark palettes
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! A palette as data: a named family with a dark variant, a light variant or
//! both, each a partial set of colour tokens laid over the shipped palette for
//! its appearance.

use std::{collections::BTreeMap, sync::LazyLock};

use gpui::Hsla;
use serde::Deserialize;

use crate::{
    Appearance, color,
    theme::{SyntaxPalette, Theme},
};

/// One theme family, as a file declares it:
///
/// ```toml
/// name = "Gruvbox"
///
/// [dark]
/// bg = "#282828"
/// text = "#ebdbb2"
///
/// [dark.syntax]
/// keyword = "#fb4934"
///
/// [light.seed]
/// surface = "#fbf1c7"
/// ink = "#3c3836"
/// accent = "#076678"
/// ```
///
/// Keys are the names [`Theme::tokens_mut`] gives; a nested table joins its
/// key onto its parent's with a dot, so `[dark.syntax]` `keyword` is
/// `syntax.keyword`. Values are `#rrggbb` or `#rrggbbaa`. A `seed` table is a
/// [`Seed`], applied before the variant's tokens.
///
/// A family has at least one variant; deserializing one with neither fails.
#[derive(Debug, Clone, Deserialize)]
#[serde(try_from = "RawFamily")]
pub struct ThemeFamily {
    pub name: String,
    pub author: Option<String>,
    pub dark: Option<Variant>,
    pub light: Option<Variant>,
}

#[derive(Deserialize)]
struct RawFamily {
    name: String,
    #[serde(default)]
    author: Option<String>,
    #[serde(default)]
    dark: Option<Variant>,
    #[serde(default)]
    light: Option<Variant>,
}

impl TryFrom<RawFamily> for ThemeFamily {
    type Error = String;

    fn try_from(raw: RawFamily) -> Result<Self, String> {
        if raw.dark.is_none() && raw.light.is_none() {
            return Err(format!(
                "theme family `{}` has neither a dark nor a light variant",
                raw.name
            ));
        }
        Ok(Self {
            name: raw.name,
            author: raw.author,
            dark: raw.dark,
            light: raw.light,
        })
    }
}

/// The families bezel ships, compiled in from `assets/themes/`.
const BUNDLED: [&str; 27] = [
    include_str!("../../assets/themes/absolutely.toml"),
    include_str!("../../assets/themes/ayu.toml"),
    include_str!("../../assets/themes/catppuccin.toml"),
    include_str!("../../assets/themes/codex.toml"),
    include_str!("../../assets/themes/dracula.toml"),
    include_str!("../../assets/themes/everforest.toml"),
    include_str!("../../assets/themes/github.toml"),
    include_str!("../../assets/themes/gruvbox.toml"),
    include_str!("../../assets/themes/linear.toml"),
    include_str!("../../assets/themes/lobster.toml"),
    include_str!("../../assets/themes/material.toml"),
    include_str!("../../assets/themes/matrix.toml"),
    include_str!("../../assets/themes/monokai.toml"),
    include_str!("../../assets/themes/night-owl.toml"),
    include_str!("../../assets/themes/nord.toml"),
    include_str!("../../assets/themes/notion.toml"),
    include_str!("../../assets/themes/one.toml"),
    include_str!("../../assets/themes/oscurange.toml"),
    include_str!("../../assets/themes/proof.toml"),
    include_str!("../../assets/themes/raycast.toml"),
    include_str!("../../assets/themes/rose-pine.toml"),
    include_str!("../../assets/themes/sentry.toml"),
    include_str!("../../assets/themes/solarized.toml"),
    include_str!("../../assets/themes/temple.toml"),
    include_str!("../../assets/themes/tokyo-night.toml"),
    include_str!("../../assets/themes/vercel.toml"),
    include_str!("../../assets/themes/vscode-plus.toml"),
];

static FAMILIES: LazyLock<Vec<ThemeFamily>> = LazyLock::new(|| {
    BUNDLED
        .iter()
        .map(|file| toml::from_str(file).expect("bundled theme"))
        .collect()
});

impl ThemeFamily {
    /// Every bundled family, in file-name order.
    pub fn bundled() -> &'static [ThemeFamily] {
        &FAMILIES
    }

    /// The bundled family `name` names, ignoring case.
    pub fn named(name: &str) -> Option<&'static ThemeFamily> {
        Self::bundled()
            .iter()
            .find(|family| family.name.eq_ignore_ascii_case(name))
    }

    /// The variant for `appearance`, if the family has one.
    pub fn variant(&self, appearance: Appearance) -> Option<&Variant> {
        match appearance {
            Appearance::Dark => self.dark.as_ref(),
            Appearance::Light => self.light.as_ref(),
        }
    }

    /// The shipped palette for `appearance` with this family's variant laid
    /// over it, carrying the family's name in [`Theme::family`]. `None` when
    /// the family has no variant for `appearance`.
    pub fn theme(&self, appearance: Appearance) -> Option<Theme> {
        let variant = self.variant(appearance)?;
        let mut theme = Theme::for_appearance(appearance);
        variant.apply(&mut theme);
        theme.family = Some(self.name.clone().into());
        Some(theme)
    }
}

/// What one variant sets: a [`Seed`] and the tokens laid over it. A token
/// neither sets keeps the shipped palette's value.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(from = "RawVariant")]
pub struct Variant {
    seed: Option<Seed>,
    tokens: BTreeMap<String, Hsla>,
}

#[derive(Deserialize)]
struct RawVariant {
    #[serde(default)]
    seed: Option<Seed>,
    #[serde(flatten)]
    tokens: BTreeMap<String, Node>,
}

/// A value in a variant's table: a colour, or a table of more.
#[derive(Deserialize)]
#[serde(untagged)]
enum Node {
    Color(Hsla),
    Table(BTreeMap<String, Node>),
}

impl From<RawVariant> for Variant {
    fn from(raw: RawVariant) -> Self {
        fn flatten(prefix: &str, table: BTreeMap<String, Node>, out: &mut BTreeMap<String, Hsla>) {
            for (key, node) in table {
                let name = match prefix {
                    "" => key,
                    _ => format!("{prefix}.{key}"),
                };
                match node {
                    Node::Color(color) => {
                        out.insert(name, color);
                    }
                    Node::Table(table) => flatten(&name, table, out),
                }
            }
        }
        let mut tokens = BTreeMap::new();
        flatten("", raw.tokens, &mut tokens);
        Self {
            seed: raw.seed,
            tokens,
        }
    }
}

impl Variant {
    /// The colour this variant's tokens give `token`, if they name one. A
    /// colour the seed decides is not one.
    pub fn get(&self, token: &str) -> Option<Hsla> {
        self.tokens.get(token).copied()
    }

    pub fn seed(&self) -> Option<&Seed> {
        self.seed.as_ref()
    }

    /// Apply the seed, then write every token this variant names into
    /// `theme`. Returns the names that are not tokens, which are skipped.
    pub fn apply(&self, theme: &mut Theme) -> Vec<&str> {
        if let Some(seed) = &self.seed {
            seed.apply(theme);
        }
        let mut slots: BTreeMap<&str, &mut Hsla> = theme.tokens_mut().into_iter().collect();
        self.tokens
            .iter()
            .filter_map(|(name, color)| match slots.get_mut(name.as_str()) {
                Some(slot) => {
                    **slot = *color;
                    None
                }
                None => Some(name.as_str()),
            })
            .collect()
    }
}

/// The few colours a whole variant is grown from: a background, the text on
/// it and an accent, with the diff and highlight colours optional.
#[derive(Debug, Clone, Copy, PartialEq, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Seed {
    /// The main panel, [`Theme::bg`].
    pub surface: Hsla,
    /// Body text, [`Theme::text`].
    pub ink: Hsla,
    pub accent: Hsla,
    /// Added lines and success. Unset keeps the shipped green.
    #[serde(default)]
    pub added: Option<Hsla>,
    /// Removed lines and danger. Unset keeps the shipped red.
    #[serde(default)]
    pub removed: Option<Hsla>,
    /// [`Theme::busy`]. Unset keeps the shipped pink.
    #[serde(default)]
    pub highlight: Option<Hsla>,
}

impl Seed {
    /// Write every opaque surface, text, accent and syntax token from the
    /// seed into `theme`, which is the shipped palette for its appearance.
    /// Translucent tokens (hovers, borders, washes) and warning keep the
    /// shipped values, as does the terminal's ANSI set.
    pub fn apply(&self, theme: &mut Theme) {
        let (bg, ink, accent) = (self.surface, self.ink, self.accent);
        let toward = |t: f32| color::mix_oklab(bg, ink, t);
        let dark = theme.appearance.is_dark();
        theme.bg = bg;
        theme.surface = toward(0.035);
        if dark {
            theme.surface_card = toward(0.04);
            theme.surface_dialog = toward(0.05);
            theme.surface_overlay = toward(0.08);
            theme.surface_raised = toward(0.10);
            theme.surface_raised_hover = toward(0.15);
        } else {
            theme.surface_card = bg;
            theme.surface_dialog = bg;
            theme.surface_overlay = bg;
            theme.surface_raised = toward(0.07);
            theme.surface_raised_hover = toward(0.12);
            theme.input_bg = bg;
        }
        theme.text = ink;
        theme.text_dim = toward(0.58);
        theme.solid = ink;
        theme.on_solid = bg;
        theme.cursor = ink;
        theme.code_text = ink;
        theme.accent = accent;
        theme.accent_strong = accent;
        theme.on_accent = color::legible_on(accent, bg, ink);
        theme.caret = accent;
        theme.selection = color::mix_oklab(bg, accent, if dark { 0.35 } else { 0.25 });
        if let Some(removed) = self.removed {
            theme.danger = removed;
            theme.danger_strong = removed;
            theme.danger_muted = color::mix_oklab(removed, ink, 0.3);
            theme.diff_del = removed;
        }
        if let Some(added) = self.added {
            theme.success = added;
            theme.success_muted = color::mix_oklab(added, ink, 0.3);
            theme.diff_add = added;
        }
        if let Some(highlight) = self.highlight {
            theme.busy = highlight;
        }
        theme.terminal_bg = bg;
        theme.vibrancy_tone = bg;
        // As far toward the ink as the shipped dark tone stands from its
        // `bg`; the shipped light tone is its chrome's.
        theme.content_background = match dark {
            true => toward(0.22),
            false => bg,
        };
        let comment = toward(0.45);
        theme.syntax = if dark {
            SyntaxPalette::dark(ink, comment, theme.danger)
        } else {
            SyntaxPalette::light(ink, comment, theme.danger)
        };
    }
}

impl Theme {
    /// Every colour token by the name a [`ThemeFamily`] file sets it under.
    ///
    /// `text_muted` and `text_faint` are absent: [`Brand::ink`](crate::Brand::ink)
    /// writes them from `text` on every install.
    pub fn tokens_mut(&mut self) -> Vec<(&'static str, &mut Hsla)> {
        let syntax = &mut self.syntax;
        let [
            black,
            red,
            green,
            yellow,
            blue,
            magenta,
            cyan,
            white,
            bright_black,
            bright_red,
            bright_green,
            bright_yellow,
            bright_blue,
            bright_magenta,
            bright_cyan,
            bright_white,
        ] = &mut self.terminal_ansi;
        vec![
            ("bg", &mut self.bg),
            ("surface", &mut self.surface),
            ("surface_raised", &mut self.surface_raised),
            ("surface_card", &mut self.surface_card),
            ("surface_dialog", &mut self.surface_dialog),
            ("surface_overlay", &mut self.surface_overlay),
            ("element_hover", &mut self.element_hover),
            ("element_active", &mut self.element_active),
            ("border_faint", &mut self.border_faint),
            ("border", &mut self.border),
            ("border_strong", &mut self.border_strong),
            ("canvas_bg", &mut self.canvas_bg),
            ("canvas_grid", &mut self.canvas_grid),
            ("text", &mut self.text),
            ("text_dim", &mut self.text_dim),
            ("solid", &mut self.solid),
            ("on_solid", &mut self.on_solid),
            ("accent", &mut self.accent),
            ("accent_strong", &mut self.accent_strong),
            ("on_accent", &mut self.on_accent),
            ("danger", &mut self.danger),
            ("danger_muted", &mut self.danger_muted),
            ("warning", &mut self.warning),
            ("warning_muted", &mut self.warning_muted),
            ("success", &mut self.success),
            ("busy", &mut self.busy),
            ("success_muted", &mut self.success_muted),
            ("surface_raised_hover", &mut self.surface_raised_hover),
            ("band", &mut self.band),
            ("input_bg", &mut self.input_bg),
            ("selection", &mut self.selection),
            ("cursor", &mut self.cursor),
            ("caret", &mut self.caret),
            ("ring", &mut self.ring),
            ("drop_line", &mut self.drop_line),
            ("drop_target", &mut self.drop_target),
            ("danger_strong", &mut self.danger_strong),
            ("code_text", &mut self.code_text),
            ("code_wash", &mut self.code_wash),
            ("diff_add", &mut self.diff_add),
            ("diff_del", &mut self.diff_del),
            ("diff_hunk_bg", &mut self.diff_hunk_bg),
            ("vibrancy_tone", &mut self.vibrancy_tone),
            ("content_background", &mut self.content_background),
            ("syntax.comment", &mut syntax.comment),
            ("syntax.keyword", &mut syntax.keyword),
            ("syntax.string", &mut syntax.string),
            ("syntax.string_special", &mut syntax.string_special),
            ("syntax.escape", &mut syntax.escape),
            ("syntax.number", &mut syntax.number),
            ("syntax.boolean", &mut syntax.boolean),
            ("syntax.type_name", &mut syntax.type_name),
            ("syntax.type_builtin", &mut syntax.type_builtin),
            ("syntax.constructor", &mut syntax.constructor),
            ("syntax.function", &mut syntax.function),
            ("syntax.function_builtin", &mut syntax.function_builtin),
            ("syntax.macro_name", &mut syntax.macro_name),
            ("syntax.property", &mut syntax.property),
            ("syntax.constant", &mut syntax.constant),
            ("syntax.variable", &mut syntax.variable),
            ("syntax.variable_special", &mut syntax.variable_special),
            ("syntax.parameter", &mut syntax.parameter),
            ("syntax.operator", &mut syntax.operator),
            ("syntax.punctuation", &mut syntax.punctuation),
            ("syntax.tag", &mut syntax.tag),
            ("syntax.attribute", &mut syntax.attribute),
            ("syntax.label", &mut syntax.label),
            ("syntax.invalid", &mut syntax.invalid),
            ("terminal.bg", &mut self.terminal_bg),
            ("terminal.ansi.black", black),
            ("terminal.ansi.red", red),
            ("terminal.ansi.green", green),
            ("terminal.ansi.yellow", yellow),
            ("terminal.ansi.blue", blue),
            ("terminal.ansi.magenta", magenta),
            ("terminal.ansi.cyan", cyan),
            ("terminal.ansi.white", white),
            ("terminal.ansi.bright_black", bright_black),
            ("terminal.ansi.bright_red", bright_red),
            ("terminal.ansi.bright_green", bright_green),
            ("terminal.ansi.bright_yellow", bright_yellow),
            ("terminal.ansi.bright_blue", bright_blue),
            ("terminal.ansi.bright_magenta", bright_magenta),
            ("terminal.ansi.bright_cyan", bright_cyan),
            ("terminal.ansi.bright_white", bright_white),
        ]
    }
}