Skip to main content

kitchen_ui/theme/
mod.rs

1//! Design tokens shared by the neuro-kitchen apps, and their application to gpui-kit's theme, so
2//! every gpui-kit component (buttons, menus, inputs, tooltips) and the apps' own layout use one
3//! set of values.
4//!
5//! - Spacing on the 8 px grid with a 4 px half-step: [`XS`] 4, [`SM`] 8, [`MD`] 12, [`LG`] 16,
6//!   [`XL`] 24, [`XXL`] 32 (designparser `8pt-grid`, `4pt-system`).
7//! - Type: base [`FONT_SIZE`] 15 px, so gpui's `text_xs` ≈ 11 (captions), `text_sm` ≈ 13 (body),
8//!   `text_base` 15 (titles), `text_lg` ≈ 17 (headings); weights 400 and 600 only. The sizes are
9//!   relative to the base, so a larger base in Settings scales every one of them.
10//! - Radii by tier: [`RADIUS_SM`] 4 (chips, swatches), [`RADIUS`] 6 (controls), [`RADIUS_LG`] 8
11//!   (sections, panes, dialogs) (designparser `corner-radius-tone`: neutral, one per tier).
12//! - Panes are rounded cards [`PANE_GAP`] apart on the chrome, with [`PANE_PADDING`] inside
13//!   (padding ≥ 1.5 × gap: designparser `padding-gap-ratio`).
14//! - Type: IBM Plex Sans for the interface, IBM Plex Mono for code, paths, ids and numbers in
15//!   columns ([`fonts`]: bundled through the app's assets, the system fonts until they load).
16//! - Neutrals are shared greys with a slight cool bias ([`color`]); data sits on [`color::canvas`]
17//!   and takes the [`color::DATA`] colours in order; a selection is the accent at 16 % with a
18//!   1 px edge ([`color::selection`]); the accent is the app's ([`Palette`]) unless
19//!   the user picked another colour ([`Look::accent`], any [`Rgb`]: made to pass contrast).
20//!
21//! The look (dark or light, palette, accent, text size) is process-wide: set it with [`set_look`], then
22//! [`apply`] hands it to gpui-kit and redraws every window.
23
24pub mod color;
25pub mod fonts;
26pub mod palette;
27
28use std::sync::RwLock;
29
30use gpui_kit::component::{Theme, ThemeMode};
31use gpui_kit::{App, Pixels, Window, px, rgba};
32
33pub use color::{Colors, DATA, colors, contrast, data, mix, rgb_hsla, rgb8};
34pub use palette::{Accent, Palette, Rgb};
35
36/// Spacing: a hairline step, icon gaps (px).
37pub const XS: f32 = 4.;
38/// Spacing: the base unit; gaps between controls and rows (px).
39pub const SM: f32 = 8.;
40/// Spacing: padding inside sections and boxes (px).
41pub const MD: f32 = 12.;
42/// Spacing: padding of panels (px).
43pub const LG: f32 = 16.;
44/// Spacing: between groups (px).
45pub const XL: f32 = 24.;
46/// Spacing: around page-level blocks (px).
47pub const XXL: f32 = 32.;
48
49/// Base text size (px) by default.
50pub const FONT_SIZE: f32 = 15.;
51/// Smallest and largest base text size Settings may choose (px).
52pub const FONT_RANGE: (f32, f32) = (12., 18.);
53
54/// Corner radius of small things: chips, swatches, scrub labels (px).
55pub const RADIUS_SM: f32 = 4.;
56/// Corner radius of controls: buttons, inputs, menus (px).
57pub const RADIUS: f32 = 6.;
58/// Corner radius of large things: boxed sections, panes, dialogs (px).
59pub const RADIUS_LG: f32 = 8.;
60
61/// Gap between panes on the chrome (px).
62pub const PANE_GAP: f32 = 4.;
63/// Padding inside a pane (px).
64pub const PANE_PADDING: f32 = 8.;
65
66/// Width of a row's label ([`crate::widgets::row`]) (px).
67pub const LABEL_W: f32 = 112.;
68/// Narrowest a row's control gets before it drops under its label (px).
69pub const CONTROL_MIN_W: f32 = 140.;
70/// Smallest pointer target (px): WCAG 2.2 AA (designparser `touch-target`).
71pub const TARGET_MIN: f32 = 24.;
72
73/// What the user chose: dark or light, an accent of their own or the app's, the base text size.
74#[derive(Clone, Copy, Debug, PartialEq)]
75pub struct Look {
76    /// The dark theme (`false`: light).
77    pub dark: bool,
78    /// The app's accent colours (its default).
79    pub palette: Palette,
80    /// The accent the user picked instead of the app's, if any (any colour: its light and dark
81    /// accents are made with [`Palette::from_rgb`]). Store it in the app's preferences.
82    pub accent: Option<Rgb>,
83    /// Base text size (px), within [`FONT_RANGE`].
84    pub font_size: f32,
85}
86
87impl Look {
88    /// Dark, `palette`, the default text size.
89    pub const fn new(palette: Palette) -> Self {
90        Look { dark: true, palette, accent: None, font_size: FONT_SIZE }
91    }
92
93    /// The palette in use: the user's accent ([`Palette::custom`]) or the app's.
94    pub fn active_palette(&self) -> Palette {
95        match self.accent {
96            Some(rgb) => Palette::custom(rgb),
97            None => self.palette,
98        }
99    }
100
101    /// The same look, dark or light.
102    pub const fn with_dark(self, dark: bool) -> Self {
103        Look { dark, ..self }
104    }
105
106    /// The same look with another app palette.
107    pub const fn with_palette(self, palette: Palette) -> Self {
108        Look { palette, ..self }
109    }
110
111    /// The same look with the user's accent (`None`: the app's palette).
112    pub const fn with_accent(self, accent: Option<Rgb>) -> Self {
113        Look { accent, ..self }
114    }
115
116    /// The same look with another base text size (kept within [`FONT_RANGE`]).
117    pub fn with_font_size(self, font_size: f32) -> Self {
118        Look { font_size: font_size.round().clamp(FONT_RANGE.0, FONT_RANGE.1), ..self }
119    }
120}
121
122impl Default for Look {
123    fn default() -> Self {
124        Look::new(Palette::LIME)
125    }
126}
127
128static LOOK: RwLock<Look> = RwLock::new(Look::new(Palette::LIME));
129
130/// Sets the look every colour reads. [`apply`] then hands it to gpui-kit.
131pub fn set_look(look: Look) {
132    let look = look.with_font_size(look.font_size);
133    *LOOK.write().unwrap_or_else(|e| e.into_inner()) = look;
134}
135
136/// The current look.
137pub fn look() -> Look {
138    *LOOK.read().unwrap_or_else(|e| e.into_inner())
139}
140
141/// Whether the current look is dark.
142pub fn is_dark() -> bool {
143    look().dark
144}
145
146/// Whether the system shows `window` dark (for a theme that follows the system).
147pub fn system_dark(window: &Window) -> bool {
148    use gpui_kit::WindowAppearance as W;
149    matches!(window.appearance(), W::Dark | W::VibrantDark)
150}
151
152/// Sets the look and applies it ([`set_look`], then [`apply`]).
153pub fn use_look(look: Look, cx: &mut App) {
154    set_look(look);
155    apply(cx);
156}
157
158/// Applies the current look ([`set_look`]) and the tokens to gpui-kit's theme, and redraws every
159/// window.
160pub fn apply(cx: &mut App) {
161    let l = look();
162    Theme::change(if l.dark { ThemeMode::Dark } else { ThemeMode::Light }, None, cx);
163    let c = Colors::resolve(&l);
164    let (on_danger, on_success, on_warning) =
165        (palette::best_text_on(c.danger), palette::best_text_on(c.success), palette::best_text_on(c.warning));
166    Theme::update(cx, |t| {
167        t.font_size = px(l.font_size);
168        t.radius = px(RADIUS);
169        t.radius_lg = px(RADIUS_LG);
170        t.shadow = true;
171        if fonts::loaded() {
172            t.font_family = fonts::UI.into();
173            t.mono_font_family = fonts::MONO.into();
174        }
175        // Code (CodeView, fenced code in text): two steps under the base size
176        t.mono_font_size = px((l.font_size - 2.).max(10.));
177        let mut code = (*t.highlight_theme).clone();
178        code.style.editor_background = Some(c.raised);
179        code.style.editor_gutter_background = Some(c.raised);
180        code.style.editor_foreground = Some(c.text);
181        code.style.editor_line_number = Some(c.text_muted);
182        code.style.editor_active_line_number = Some(c.text);
183        code.style.editor_active_line = Some(c.hover.opacity(0.5));
184        t.highlight_theme = std::sync::Arc::new(code);
185        let k = &mut t.colors;
186        k.background = c.panel;
187        k.foreground = c.text;
188        k.muted = c.raised;
189        k.muted_foreground = c.text_muted;
190        k.border = c.border;
191        k.input = c.hover;
192        k.ring = c.focus;
193        k.caret = c.text;
194        k.selection = c.accent.opacity(0.45);
195        k.popover = c.raised;
196        k.popover_foreground = c.text;
197        k.sidebar = c.chrome;
198        k.sidebar_foreground = c.text;
199        k.sidebar_border = c.border;
200        k.sidebar_accent = c.control;
201        k.sidebar_accent_foreground = c.text;
202        k.sidebar_primary = c.accent;
203        k.sidebar_primary_foreground = c.on_accent;
204        k.primary = c.accent;
205        k.primary_hover = c.accent_hover;
206        k.primary_active = c.accent;
207        k.primary_foreground = c.on_accent;
208        k.secondary = c.control;
209        k.secondary_hover = c.hover;
210        k.secondary_active = c.hover;
211        k.secondary_foreground = c.text;
212        k.accent = c.control;
213        k.accent_foreground = c.text;
214        k.title_bar = c.chrome;
215        k.title_bar_border = c.border;
216        k.tab_bar = c.chrome;
217        k.tab = c.chrome;
218        k.tab_active = c.panel;
219        k.tab_foreground = c.text_muted;
220        k.tab_active_foreground = c.text;
221        k.list = c.panel;
222        k.list_even = c.panel;
223        k.list_hover = c.control;
224        k.list_active = c.selection;
225        k.list_active_border = c.accent_mark;
226        k.list_head = c.raised;
227        k.table = c.panel;
228        k.table_even = c.raised;
229        k.table_head = c.raised;
230        k.table_head_foreground = c.text_muted;
231        k.table_active = c.selection;
232        k.table_active_border = c.accent_mark;
233        k.danger = c.danger;
234        k.danger_foreground = on_danger;
235        k.success = c.success;
236        k.success_foreground = on_success;
237        k.warning = c.warning;
238        k.warning_foreground = on_warning;
239        // Diagnostics (error underlines) and status marks in gpui-kit's editor
240        k.red = c.danger;
241        k.green = c.success;
242        k.info = c.info;
243        k.link = c.accent_mark;
244        k.scrollbar = rgba(0x00000000).into();
245        k.scrollbar_thumb = if l.dark { rgba(0xffffff33).into() } else { rgba(0x00000033).into() };
246        k.scrollbar_thumb_hover = if l.dark { rgba(0xffffff55).into() } else { rgba(0x00000055).into() };
247        k.window_border = c.border;
248        k.overlay = rgba(0x00000080).into();
249        k.drag_border = c.accent_mark;
250        k.drop_target = c.selection;
251        k.chart_grid = c.grid;
252    });
253}
254
255/// `px` of a token.
256pub fn sp(v: f32) -> Pixels {
257    px(v)
258}