Skip to main content

theme/theme/
layout.rs

1//! Layout constants. Numbers drive layout, colors are paint: these live as
2//! plain numbers and never depend on which color is painted.
3
4use std::sync::atomic::{AtomicU32, Ordering};
5
6use gpui::{Styled, px};
7
8use crate::theme::{
9    Theme,
10    typography::{TextStyle, Typeset},
11};
12
13/// A control's size — SwiftUI's `ControlSize`. A closed pair, not a scale: the
14/// two that ship are the row of a form and the chip on an overlay.
15#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16pub enum ControlSize {
17    /// The chip: `Callout` on a `control_radius` corner.
18    Small,
19    /// The form row: `Body` on a `button_radius` corner.
20    Regular,
21}
22
23impl ControlSize {
24    /// The type role the size paints at, which decides the rest.
25    pub const fn text(self) -> TextStyle {
26        match self {
27            ControlSize::Small => TextStyle::Callout,
28            ControlSize::Regular => TextStyle::Body,
29        }
30    }
31
32    /// Height: the measured platform control, which [`Self::pad_y`] is then the
33    /// remainder of.
34    pub const fn height(self) -> f32 {
35        match self {
36            ControlSize::Small => Theme::CONTROL_HEIGHT_SMALL,
37            ControlSize::Regular => Theme::BUTTON_HEIGHT,
38        }
39    }
40
41    pub const fn pad_x(self) -> f32 {
42        match self {
43            ControlSize::Small => 8.0,
44            ControlSize::Regular => 12.0,
45        }
46    }
47
48    /// What the height has left over its role's line box, halved. Derived
49    /// rather than stored: the pair drifted once already, when the line box
50    /// moved off gpui's phi and the two heights stayed where phi had put them.
51    pub const fn pad_y(self) -> f32 {
52        (self.height() - self.text().line_height()) / 2.0
53    }
54
55    pub fn radius(self) -> f32 {
56        match self {
57            ControlSize::Small => Theme::control_radius(),
58            ControlSize::Regular => Theme::button_radius(),
59        }
60    }
61}
62
63/// The size ladder, on anything styled — SwiftUI's `.controlSize(..)`, and
64/// [`Typeset`]'s shape for the metrics that come with a type role.
65///
66/// It carries horizontal padding, so a control whose width is its height
67/// instead (an icon button) is not one of these.
68pub trait Sizing: Styled + Sized {
69    fn control_size(self, size: ControlSize) -> Self {
70        self.min_h(px(size.height()))
71            .px(px(size.pad_x()))
72            .py(px(size.pad_y()))
73            .rounded(px(size.radius()))
74            .text_style(size.text())
75    }
76}
77
78impl<E: Styled> Sizing for E {}
79
80/// The branded base radius behind [`Theme::radius`], as raw `f32` bits.
81static BASE: AtomicU32 = AtomicU32::new(Theme::BASE_RADIUS.to_bits());
82
83/// Point the radius accessors at a base. Called by
84/// [`Theme::install`](crate::theme::Theme::install).
85pub(crate) fn set_base_radius(radius: f32) {
86    BASE.store(radius.to_bits(), Ordering::Relaxed);
87}
88
89impl Theme {
90    // ---- numbers drive layout (px) ----
91    /// The alpha [`Brand::vibrancy_alpha`](crate::Brand::vibrancy_alpha) starts from.
92    /// Matched by eye to a reference Electron app's dark glass: its scrim is
93    /// 0.76 over `hsl(0 0% 3%)`, but sits on the `under-window` vibrancy
94    /// MATERIAL, which pre-darkens the blur; a bare backdrop blur has no such
95    /// layer, so ours runs heavier to land on the same perceived tone.
96    ///
97    /// Opaque off macOS: Linux and Windows get no compositor-blur guarantee,
98    /// and a merely transparent window would show raw desktop through the
99    /// sidebar. An app that knows its compositor sets the brand field anyway.
100    pub const VIBRANCY_ALPHA: f32 = if cfg!(any(target_os = "macos", target_family = "wasm")) {
101        0.80
102    } else {
103        1.0
104    };
105    /// Main-panel header height (the reference `h-11`) — in-card headers (changes pane).
106    pub const HEADER_HEIGHT: f32 = 44.0;
107    /// The unified window titlebar (traffic lights + cluster + tabs). Content
108    /// rides [`Self::TITLEBAR_TOP_PAD`] lower than center so the air above
109    /// matches the perceived gap to the inset card below (border + card body).
110    pub const TITLEBAR_HEIGHT: f32 = 38.0;
111    /// Downward shift of titlebar content within the bar.
112    pub const TITLEBAR_TOP_PAD: f32 = 2.0;
113    /// Leading room the macOS traffic lights need where AppKit puts them —
114    /// zed's `TRAFFIC_LIGHT_PADDING` on the macOS 26 SDK (71.0 before it), and
115    /// the same 78 `../desktop` measured for its Tauri window. An app that
116    /// *moves* the lights with `TitlebarOptions::traffic_light_position` owns
117    /// this number too.
118    pub const TRAFFIC_LIGHT_INSET: f32 = if cfg!(target_os = "macos") { 78.0 } else { 0.0 };
119    /// Reserved status strip under the content outlet (the reference `h-6`) — the
120    /// WorkingIndicator row; reserving it keeps the composer from shifting.
121    pub const STATUS_STRIP_HEIGHT: f32 = 24.0;
122    /// Height of the gradient that fades the transcript into the panel
123    /// background at its bottom edge. The transcript's last row must pad
124    /// itself past this band so settled content (message text, the
125    /// hover-revealed timestamp) never sits inside the fade when scrolled
126    /// to the bottom.
127    pub const TRANSCRIPT_FADE_BAND: f32 = 24.0;
128    /// Button, text field and select-trigger height. Measured 2026-09-01:
129    /// `NSButton`, `NSTextField` and `NSPopUpButton` all report 24 at
130    /// `.regular` — `Body`'s 16pt line box with 4 above and below.
131    pub const BUTTON_HEIGHT: f32 = 24.0;
132    /// The same controls at `.small`.
133    pub const CONTROL_HEIGHT_SMALL: f32 = 20.0;
134    /// Button, text field and select-trigger radius — the crate's most-used
135    /// corner after the derived ones, and unnamed until the concentric pass
136    /// separated the eight sites that *chose* 8.0 from the ones that only
137    /// arrived at it as `12 − 4`.
138    ///
139    /// Every other corner is a ratio of this one, so
140    /// [`Brand::radius`](crate::Brand::radius) moves the whole set together.
141    pub const BASE_RADIUS: f32 = 8.0;
142
143    /// Message bubble corner radius.
144    pub fn bubble_radius() -> f32 {
145        Self::radius(2.0)
146    }
147    /// Floating-surface corner radius — popovers, menus, the command palette,
148    /// group boxes.
149    ///
150    /// A glass surface paints this on its border **and** hands the same number
151    /// to `bezel::ui::surface`'s backdrop blur. The two must agree: a blur cut
152    /// to a different radius frosts square corners outside a round border, and
153    /// it shows only on glass and only at the corners. So the radius is named
154    /// once and read at both ends, rather than written twice sixty lines apart
155    /// — which is how three independent `12.0`s came to exist here.
156    pub fn surface_radius() -> f32 {
157        Self::radius(1.5)
158    }
159    /// Panel / card corner radius.
160    pub fn panel_radius() -> f32 {
161        Self::radius(1.25)
162    }
163    /// Button, text field and select-trigger radius.
164    pub fn button_radius() -> f32 {
165        Self::radius(1.0)
166    }
167    /// Small control radius (chips, tags, steppers) — a size down from
168    /// [`Self::button_radius`], for things that sit inside a control rather
169    /// than being one.
170    pub fn control_radius() -> f32 {
171        Self::radius(0.75)
172    }
173
174    /// A corner as a multiple of the branded base radius.
175    ///
176    /// Read from a process-wide mirror rather than the theme global for the
177    /// reason [`current_appearance`](crate::paint::current_appearance) is: the
178    /// element builders that round a corner are free functions with no `cx` in
179    /// scope, and a radius is one number for the whole app.
180    fn radius(ratio: f32) -> f32 {
181        f32::from_bits(BASE.load(Ordering::Relaxed)) * ratio
182    }
183
184    /// The concentric child of a surface: a row inset by `inset` inside a
185    /// container of radius `outer` keeps its corners parallel to the
186    /// container's, rather than looking pasted onto it.
187    ///
188    /// This is SwiftUI's `ContainerRelativeShape` rule done as arithmetic. gpui
189    /// has no container shape to inherit at paint time, so the relationship is
190    /// stated where the child is *defined* instead of resolved at runtime —
191    /// which means a container that changes its padding carries its rows with
192    /// it, and the derived value never becomes a constant of its own.
193    pub const fn inset_radius(outer: f32, inset: f32) -> f32 {
194        if outer > inset { outer - inset } else { 0.0 }
195    }
196    /// The gap between siblings. Measured on macOS 26, 2026-08-31:
197    /// `NSStackView().spacing`, visual format's `-`, and
198    /// `constraint(equalToSystemSpacingAfter:multiplier: 1)` all report 8.
199    ///
200    /// Carried by `ui::stack::row` and `ui::stack::column`, so a call site
201    /// that wants the standard gap writes no number at all — SwiftUI's shape,
202    /// where `VStack(spacing:)` takes the system's when given nothing.
203    pub const SPACE: f32 = 8.0;
204    /// The margin from content to its container's edge. Same measurement,
205    /// visual format's `|-`.
206    pub const CONTENT_MARGIN: f32 = 20.0;
207}