Skip to main content

theme/theme/
mod.rs

1//! The app theme: one token set, two concrete instances.
2
3use gpui::{Global, Hsla, SharedString};
4
5use crate::{Appearance, paint};
6
7mod glass;
8mod install;
9mod layout;
10mod palettes;
11mod syntax;
12mod typography;
13
14pub use install::set_palette;
15pub use layout::{ControlSize, Sizing};
16pub use syntax::{HighlightKind, SyntaxPalette};
17pub use typography::{Metrics, TextStyle, Typeset, base_text_size, set_base_text_size};
18
19/// The two shipped glasses — SwiftUI's `Glass.regular` and `Glass.clear`. A
20/// closed variant rather than knobs: Apple exposes no numbers on glass either,
21/// only the variant and a tint.
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub enum Glass {
24    /// The everyday material: it blurs what it covers and dims it hard.
25    Regular,
26    /// Near-transparent — the backdrop reads through, bent only at the rim.
27    Clear,
28}
29
30/// SwiftUI's frost scale. Measured 2026-08-31: the five thicknesses are ONE
31/// material at five opacities — the tone implied by `tint / (1 - gain)` holds
32/// to within 9% across the scale, and the sigma does not move at all
33/// (23.1/20.2/21.0pt). So this is a knob, not five looks.
34#[derive(Debug, Clone, Copy, PartialEq, Eq)]
35pub enum Material {
36    UltraThin,
37    Thin,
38    Regular,
39    Thick,
40    UltraThick,
41}
42
43impl Material {
44    /// How much of the backdrop the material covers. Measured off SwiftUI in
45    /// dark; the steps come out even to within a point.
46    pub fn opacity(self) -> f32 {
47        match self {
48            Material::UltraThin => 0.440,
49            Material::Thin => 0.543,
50            Material::Regular => 0.638,
51            Material::Thick => 0.737,
52            Material::UltraThick => 0.825,
53        }
54    }
55}
56
57/// Which surface a caller names. Material and glass are different things with
58/// different vocabularies — a material has thickness, a glass has a variant —
59/// and they meet only at the numbers they resolve to.
60#[derive(Debug, Clone, Copy, PartialEq, Eq)]
61pub enum SurfaceStyle {
62    Material(Material),
63    Glass(Glass),
64}
65
66/// The frost material, before a thickness picks its opacity.
67#[derive(Debug, Clone, Copy, PartialEq)]
68pub struct MaterialSpec {
69    /// The material's own tone, at full coverage.
70    pub tone: Hsla,
71    /// Its chroma push, as [`SurfaceSpec::saturation`].
72    pub saturation: f32,
73    /// Its sigma, which does not move with thickness.
74    pub blur: f32,
75    /// SwiftUI's frost has no lit rim at all — measured +0 at the boundary.
76    /// What is here is bezel's own hairline, the one the popover card used to
77    /// draw, moved to the surface that owes it.
78    pub edge: f32,
79    pub edge_width: f32,
80    pub edge_aa: f32,
81}
82
83impl MaterialSpec {
84    /// This material at one thickness.
85    pub fn at(&self, thickness: Material) -> SurfaceSpec {
86        let opacity = thickness.opacity();
87        SurfaceSpec {
88            gain: 1.0 - opacity,
89            saturation: self.saturation,
90            tint: self.tone.opacity(opacity),
91            blur: self.blur,
92            rim: 0.0,
93            reach: 0.0,
94            edge: self.edge,
95            edge_width: self.edge_width,
96            edge_aa: self.edge_aa,
97            // A wash has nothing to bend, so it needs one to lift off the page.
98            shadow: true,
99        }
100    }
101}
102
103/// One surface's numbers. `out = gain * saturated(backdrop) + tint`, over a
104/// backdrop blurred at `blur`.
105#[derive(Debug, Clone, Copy, PartialEq)]
106pub struct SurfaceSpec {
107    /// Slope of the transfer line. Below 1 compresses contrast toward
108    /// `tint`; above 1 it brightens and slightly expands, which is what light
109    /// `Clear` measures. Named for what it is, not for one of its directions.
110    pub gain: f32,
111    /// How far the backdrop's chroma is pushed from its own grey, before the
112    /// gain drops the level. A gain alone moves level and colour together, so
113    /// this is the only way a surface goes dark and keeps its colours. 1 is
114    /// the pass-through `Clear` measures.
115    pub saturation: f32,
116    /// Its offset — the look's own tone.
117    pub tint: Hsla,
118    /// Gaussian sigma under the lens, in logical pixels. It stands in for the
119    /// average attenuation of fine detail, which the real material gets from
120    /// snapshotting its backdrop coarsely.
121    pub blur: f32,
122    /// How far in from the rim the lens bends the backdrop, in logical pixels.
123    /// A length rather than a share of the box: the displacement curve measured
124    /// 2026-08-30 is one curve, the same from a 96pt box to a 320pt one and
125    /// from r24 to r84. Past it the backdrop is untouched.
126    pub rim: f32,
127    /// How far the outermost pixel drags what it samples, in logical pixels —
128    /// the far end of that same curve, measured at ~47pt.
129    pub reach: f32,
130    /// How much white the lit rim adds at the edge itself, 0..1.
131    pub edge: f32,
132    /// How far in that light falls off to nothing, in logical pixels. Even the
133    /// whole way round: measured 2026-08-30, the real rim reads the same on all
134    /// four sides.
135    pub edge_width: f32,
136    /// The coverage ramp at the shape's own boundary, in logical pixels. 0.5
137    /// is one device pixel at 2x — the plain rasteriser's answer; 0 is the hard
138    /// edge it replaced.
139    pub edge_aa: f32,
140    /// Whether the surface casts a shadow. A lens separates itself from the
141    /// page by bending it, and reads as a slab under one; a wash has nothing
142    /// to bend and needs it.
143    pub shadow: bool,
144}
145
146impl SurfaceStyle {
147    /// The numbers this style resolves to against a theme.
148    pub fn spec(self, theme: &Theme) -> SurfaceSpec {
149        match self {
150            SurfaceStyle::Material(thickness) => theme.material.at(thickness),
151            SurfaceStyle::Glass(Glass::Regular) => theme.glass_regular,
152            SurfaceStyle::Glass(Glass::Clear) => theme.glass_clear,
153        }
154    }
155}
156
157/// The app theme. Two concrete instances — [`Theme::dark`] and [`Theme::light`].
158#[derive(Debug, Clone)]
159pub struct Theme {
160    /// Which appearance these tokens were built for.
161    pub appearance: Appearance,
162
163    // ---- paint: neutral surfaces ----
164    /// Main content panel. Dark: the deepest plane (#060606). Light: pure white —
165    /// long-form content reads best on an unbroken white field.
166    pub bg: Hsla,
167    /// Shell / sidebar surface. Dark: one step *up* from `bg`. Light: one step
168    /// *down* (grey) — chrome recedes from the content plane in both, which is
169    /// the direction a naive invert gets backwards.
170    pub surface: Hsla,
171    /// Raised surface: opaque pills and chips that sit proud of the panel.
172    /// Dark: lighter than `surface`. Light: white, separated by `border` +
173    /// shadow rather than by lightness.
174    pub surface_raised: Hsla,
175
176    // ---- paint: elevation ladder ----
177    //
178    // Dark mode distinguishes floating planes by lightness, and the steps are
179    // *small* (#0e → #10 → #16 → #1e). They are not interchangeable: collapsing
180    // them onto one token visibly lifts popovers off their intended plane.
181    //
182    // Light mode cannot use the same trick, because the content plane is already
183    // white and there is nothing lighter to climb to. All three land on white and
184    // let `border` + shadow carry the separation instead — the standard light-UI
185    // answer, and the reason this is a ladder of tokens rather than an arithmetic
186    // offset applied to one.
187    /// Inline card resting on the main panel (auth gate, empty-state cards).
188    pub surface_card: Hsla,
189    /// Modal dialog, floating over a [`Theme::scrim`].
190    pub surface_dialog: Hsla,
191    /// Popover, menu and command-palette surface — the highest plane.
192    pub surface_overlay: Hsla,
193    /// Hover wash for interactive rows and buttons, on glass and off it alike:
194    /// `../desktop`'s `--color-hover`.
195    pub element_hover: Hsla,
196    /// Active/selected wash, one rung over the hover — `--color-active`.
197    pub element_active: Hsla,
198    /// Hairline border.
199    pub border: Hsla,
200    /// Stronger border for focused/raised edges.
201    pub border_strong: Hsla,
202
203    // ---- paint: text ----
204    /// Primary text. ~17.5:1 on its own background in both appearances.
205    pub text: Hsla,
206    /// Muted text: timestamps, secondary labels. ~7.5–8:1.
207    pub text_muted: Hsla,
208    /// Faint text: placeholders, disabled. ~4.5:1 — AA for body copy.
209    pub text_faint: Hsla,
210    /// One notch below `text_muted` — the diff file-path tone. It exists as its
211    /// own token rather than being folded into `text_muted` because the dark
212    /// value was sampled (#989898) and folding it would shift that label, which
213    /// is a palette change dressed up as a refactor.
214    pub text_dim: Hsla,
215
216    // ---- paint: high-contrast solid (primary buttons) ----
217    /// The maximum-contrast solid fill: near-white on dark, near-black on light.
218    /// This is the primary button plate.
219    pub solid: Hsla,
220    /// Label/icon color on top of [`Self::solid`] — its inverse.
221    pub on_solid: Hsla,
222
223    // ---- paint: accents ----
224    /// Accent — the emphasis weight for text and icons.
225    ///
226    /// **Neutral by default, and deliberately.** A component library that ships
227    /// a hue puts that hue in every app that installs it, and bezel had an
228    /// indigo running through spinners, pagination, date selection and list
229    /// markers before anyone chose it. The default is now the same lightness
230    /// with the chroma at zero.
231    ///
232    /// This is the token to brand:
233    ///
234    /// ```ignore
235    /// let mut theme = Theme::for_appearance(appearance);
236    /// theme.accent = my_brand_accent(appearance);
237    /// ```
238    ///
239    /// See [`set_palette`](crate::theme::install::set_palette), which is what
240    /// makes an override survive an appearance switch.
241    pub accent: Hsla,
242    /// Stronger accent for fills that carry [`Self::on_accent`] text. Neutral by
243    /// default, it is the maximum-contrast plate — a mid grey would not carry a
244    /// label the way the indigo it replaced did.
245    pub accent_strong: Hsla,
246    /// Label color on top of [`Self::accent_strong`].
247    pub on_accent: Hsla,
248    /// Danger — red (errors, stop button).
249    pub danger: Hsla,
250    /// Softer danger for secondary/inline error copy.
251    pub danger_muted: Hsla,
252    /// Warning — amber (offline notices, awaiting-input).
253    pub warning: Hsla,
254    /// Softer warning for secondary copy.
255    pub warning_muted: Hsla,
256    /// Success / online — emerald.
257    pub success: Hsla,
258    /// Working / streaming indicator — pink.
259    pub busy: Hsla,
260    /// Softer success for text on a success-tinted chip.
261    pub success_muted: Hsla,
262
263    // ---- paint: components ----
264    /// Hover tone for an *opaque* raised pill. Hover must brighten the plate in
265    /// dark mode, never swap it for a translucent wash (that made pills go
266    /// see-through — user-reported); in light mode it darkens instead, same idea.
267    pub surface_raised_hover: Hsla,
268    /// Recessed band behind a palette/picker header or footer strip. Translucent
269    /// so the glass still reads through.
270    pub band: Hsla,
271    /// The composer pill and other input plates.
272    ///
273    /// Its own token because "lifted" inverts between appearances. On dark, a
274    /// faint *white* wash over near-black reads as raised. The literal light
275    /// translation — a faint *black* wash on white — reads as **recessed**, a dent
276    /// rather than a plate, which is why the prompt looked like bare text on a
277    /// smudge. Light mode lifts the way light UIs actually do: pure white, with
278    /// the border and shadow carrying the elevation.
279    pub input_bg: Hsla,
280    /// Text-selection highlight in the composer and inputs.
281    pub selection: Hsla,
282    /// Terminal block cursor.
283    pub cursor: Hsla,
284    /// Text caret — [`Self::accent`]'s lightness.
285    ///
286    /// Measured macOS 26, 2026-08-31: `NSColor.textInsertionPointColor` is the
287    /// accent in both appearances, where `NSColor.textColor` is white and
288    /// black. So a caret is not the next glyph before you type it, which is
289    /// what this used to carry; the platform gives it its own role, and a brand
290    /// tints it here the way the system accent tints it there.
291    pub caret: Hsla,
292    /// Keyboard focus ring — a hairline, so it marks the control without
293    /// restating the label inside it.
294    pub ring: Hsla,
295    /// Destructive-action button fill (danger plate, carries [`Self::on_accent`]).
296    pub danger_strong: Hsla,
297
298    // ---- paint: code & diff ----
299    /// Inline-code text. Neutral: code is already set apart by the mono face
300    /// and its wash, and a hue on top reads as a link rather than as code.
301    pub code_text: Hsla,
302    /// Inline-code wash behind [`Self::code_text`].
303    pub code_wash: Hsla,
304    /// Shared paint-only syntax palette.
305    pub syntax: SyntaxPalette,
306    /// Diff: added lines.
307    pub diff_add: Hsla,
308    /// Diff: deleted lines.
309    pub diff_del: Hsla,
310    /// Diff: hunk-header wash (bluish grey).
311    pub diff_hunk_bg: Hsla,
312
313    // ---- glass ----
314    //
315    // Numbers, so they flow with the appearance the way every other token
316    // does. `Glass::glass_effect` reads them off the theme it is handed;
317    // nothing here is a parameter on a component.
318    /// How opaque the tint over the blurred window is.
319    pub vibrancy_alpha: f32,
320    /// Whether the window composites translucent, so the desktop reaches what
321    /// is painted over it — AppKit's vibrancy.
322    pub vibrancy: bool,
323    /// Whether components paint glass — translucent popovers and cards, and
324    /// the lens. An opaque window can still carry it.
325    pub glass: bool,
326    /// The surfaces this theme can paint. Blur belongs to the look, not to
327    /// the caller: Apple exposes no blur parameter on either family, only the
328    /// thickness or the variant.
329    pub material: MaterialSpec,
330    pub glass_regular: SurfaceSpec,
331    pub glass_clear: SurfaceSpec,
332    /// What the popover surfaces — menus, dialogs, sheets, tooltips — mount
333    /// on. They take no theme of their own, so this is where the choice
334    /// lives; a component that owns its surface names its own style instead.
335    pub popover_surface: SurfaceStyle,
336    /// Lens displacement amplitude, signed; negative inverts it.
337    pub glass_magnify: f32,
338    /// Per-channel spread of that displacement — the chromatic fringe.
339    pub glass_dispersion: f32,
340
341    // ---- fonts ----
342    /// UI font family — the name the text system resolves, not the bytes.
343    /// Defaults to the platform's own UI face; point it at your own family
344    /// once you have registered that font with the text system.
345    pub font_sans: SharedString,
346    /// Monospace family for code/terminal.
347    pub font_mono: SharedString,
348}
349
350impl Theme {
351    /// Overlay ink at `alpha` — see [`ink`](crate::paint::ink).
352    pub fn ink(&self, alpha: f32) -> Hsla {
353        paint::ink_for(self.appearance, alpha)
354    }
355
356    /// Hairline ink at `alpha` — see [`hairline`](crate::paint::hairline).
357    pub fn hairline(&self, alpha: f32) -> Hsla {
358        paint::hairline_for(self.appearance, alpha)
359    }
360
361    /// State wash at `alpha` — see [`wash`](crate::paint::wash).
362    pub fn wash(&self, alpha: f32) -> Hsla {
363        paint::wash_for(self.appearance, alpha)
364    }
365}
366
367impl Default for Theme {
368    fn default() -> Self {
369        Self::dark()
370    }
371}
372
373impl Global for Theme {}