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    /// A coverage, not a switch: whether a window frosts at all is
98    /// [`frosted_window`](crate::frosted_window).
99    pub const VIBRANCY_ALPHA: f32 = 0.80;
100    /// Main-panel header height (the reference `h-11`) — in-card headers (changes pane).
101    pub const HEADER_HEIGHT: f32 = 44.0;
102    /// The unified window titlebar (traffic lights + cluster + tabs). Content
103    /// rides [`Self::TITLEBAR_TOP_PAD`] lower than center so the air above
104    /// matches the perceived gap to the inset card below (border + card body).
105    pub const TITLEBAR_HEIGHT: f32 = 38.0;
106    /// Downward shift of titlebar content within the bar.
107    pub const TITLEBAR_TOP_PAD: f32 = 2.0;
108    /// Leading room the macOS traffic lights need where AppKit puts them —
109    /// zed's `TRAFFIC_LIGHT_PADDING` on the macOS 26 SDK (71.0 before it), and
110    /// the same 78 `../desktop` measured for its Tauri window. An app that
111    /// *moves* the lights with `TitlebarOptions::traffic_light_position` owns
112    /// this number too.
113    pub const TRAFFIC_LIGHT_INSET: f32 = if cfg!(target_os = "macos") { 78.0 } else { 0.0 };
114    /// One caption button — minimize, maximize, close — where the app paints
115    /// them itself. Windows' own metric; the height is the bar's.
116    pub const CAPTION_BUTTON_WIDTH: f32 = 46.0;
117    /// The band a client-decorated window keeps outside its content: the
118    /// resize target on all four edges, and the room its shadow falls in.
119    ///
120    /// Handed to `Window::set_client_inset`, so the compositor's own resize
121    /// area and the one `bezel::ui::window` hit-tests are the same band.
122    pub const CLIENT_INSET: f32 = 10.0;
123    /// Reserved status strip under the content outlet (the reference `h-6`) — the
124    /// WorkingIndicator row; reserving it keeps the composer from shifting.
125    pub const STATUS_STRIP_HEIGHT: f32 = 24.0;
126    /// Height of the gradient that fades the transcript into the panel
127    /// background at its bottom edge. The transcript's last row must pad
128    /// itself past this band so settled content (message text, the
129    /// hover-revealed timestamp) never sits inside the fade when scrolled
130    /// to the bottom.
131    pub const TRANSCRIPT_FADE_BAND: f32 = 24.0;
132    /// Button, text field and select-trigger height. Measured 2026-09-01:
133    /// `NSButton`, `NSTextField` and `NSPopUpButton` all report 24 at
134    /// `.regular` — `Body`'s 16pt line box with 4 above and below.
135    pub const BUTTON_HEIGHT: f32 = 24.0;
136    /// The same controls at `.small`.
137    pub const CONTROL_HEIGHT_SMALL: f32 = 20.0;
138    /// Button, text field and select-trigger radius — the crate's most-used
139    /// corner after the derived ones, and unnamed until the concentric pass
140    /// separated the eight sites that *chose* 8.0 from the ones that only
141    /// arrived at it as `12 − 4`.
142    ///
143    /// Every other corner is a ratio of this one, so
144    /// [`Brand::radius`](crate::Brand::radius) moves the whole set together.
145    pub const BASE_RADIUS: f32 = 8.0;
146
147    /// Message bubble corner radius.
148    pub fn bubble_radius() -> f32 {
149        Self::radius(2.0)
150    }
151    /// Floating-surface corner radius — popovers, menus, the command palette,
152    /// group boxes.
153    ///
154    /// A glass surface paints this on its border **and** hands the same number
155    /// to `bezel::ui::surface`'s backdrop blur. The two must agree: a blur cut
156    /// to a different radius frosts square corners outside a round border, and
157    /// it shows only on glass and only at the corners. So the radius is named
158    /// once and read at both ends, rather than written twice sixty lines apart
159    /// — which is how three independent `12.0`s came to exist here.
160    pub fn surface_radius() -> f32 {
161        Self::radius(1.5)
162    }
163    /// Panel / card corner radius.
164    pub fn panel_radius() -> f32 {
165        Self::radius(1.25)
166    }
167    /// Button, text field and select-trigger radius.
168    pub fn button_radius() -> f32 {
169        Self::radius(1.0)
170    }
171    /// Small control radius (chips, tags, steppers) — a size down from
172    /// [`Self::button_radius`], for things that sit inside a control rather
173    /// than being one.
174    pub fn control_radius() -> f32 {
175        Self::radius(0.75)
176    }
177
178    /// A corner as a multiple of the branded base radius.
179    ///
180    /// Read from a process-wide mirror rather than the theme global for the
181    /// reason [`current_appearance`](crate::paint::current_appearance) is: the
182    /// element builders that round a corner are free functions with no `cx` in
183    /// scope, and a radius is one number for the whole app.
184    fn radius(ratio: f32) -> f32 {
185        f32::from_bits(BASE.load(Ordering::Relaxed)) * ratio
186    }
187
188    /// The concentric child of a surface: a row inset by `inset` inside a
189    /// container of radius `outer` keeps its corners parallel to the
190    /// container's, rather than looking pasted onto it.
191    ///
192    /// This is SwiftUI's `ContainerRelativeShape` rule done as arithmetic. gpui
193    /// has no container shape to inherit at paint time, so the relationship is
194    /// stated where the child is *defined* instead of resolved at runtime —
195    /// which means a container that changes its padding carries its rows with
196    /// it, and the derived value never becomes a constant of its own.
197    pub const fn inset_radius(outer: f32, inset: f32) -> f32 {
198        if outer > inset { outer - inset } else { 0.0 }
199    }
200    /// The gap between siblings. Measured on macOS 26, 2026-08-31:
201    /// `NSStackView().spacing`, visual format's `-`, and
202    /// `constraint(equalToSystemSpacingAfter:multiplier: 1)` all report 8.
203    ///
204    /// Carried by `ui::stack::row` and `ui::stack::column`, so a call site
205    /// that wants the standard gap writes no number at all — SwiftUI's shape,
206    /// where `VStack(spacing:)` takes the system's when given nothing.
207    pub const SPACE: f32 = 8.0;
208    /// The margin from content to its container's edge. Same measurement,
209    /// visual format's `|-`.
210    pub const CONTENT_MARGIN: f32 = 20.0;
211}