ui/widgets/buttons.rs
1//! The button — one component, three shipped looks — in its labelled and
2//! glyph-only forms, the [`Buttons::control_group`] that gathers adjacent ones
3//! onto one shared background, and [`Buttons::ghost`], the open frame a quiet
4//! control paints around children of its own. A catalog trait like every widget
5//! group: `use ui::widgets::{ButtonStyle, Buttons};` →
6//! `theme.button("Save", ButtonStyle::Prominent, None)`.
7//!
8//! [`ButtonStyle`] is a closed enum, not free-form knobs: it selects between
9//! the looks that ship, while per-call overrides stay chain modifiers.
10
11use gpui::{Div, ElementId, SharedString, Stateful, div, prelude::*, px};
12use icons::Icon;
13use motion::{self, Fade};
14use theme::{ControlSize, Sizing, Theme, ThemeExt};
15
16/// The shipped looks (the reference `btnGhost` / `btnPrimary` /
17/// `btnDestructive`).
18#[derive(Clone, Copy, Debug, PartialEq, Eq)]
19pub enum ButtonStyle {
20 /// Quiet text on a translucent wash.
21 Ghost,
22 /// The maximum-contrast plate — the primary action.
23 Prominent,
24 /// The muted red fill — carries the destructive semantics with the paint.
25 Destructive,
26}
27
28/// The glyph a [`Buttons::icon_button`] carries, at the size every other
29/// control in this crate paints one.
30pub const ICON_GLYPH: f32 = 14.0;
31
32/// The padding that sets an [`Buttons::icon_button`]'s glyph [`Theme::EDGE`]
33/// from its container's edge.
34pub const ICON_EDGE_PAD: f32 = Theme::EDGE - (Theme::BUTTON_HEIGHT - ICON_GLYPH) / 2.0;
35
36/// What a [`Buttons::control_group`] insets its items by, and the gap between
37/// them — so the first sits as far from the track's edge as from its neighbour.
38const GROUP_PAD: f32 = 2.0;
39
40/// The frame every style shares — [`ControlSize::Regular`], which a caller
41/// moves with [`Sizing::control_size`].
42fn frame() -> Div {
43 div()
44 .control_size(ControlSize::Regular)
45 .flex()
46 .items_center()
47 .cursor_pointer()
48}
49
50/// [`frame`] squared to a glyph: [`Buttons::icon_button`]'s shape.
51fn square() -> Div {
52 frame()
53 .px(px(0.0))
54 .w(px(Theme::BUTTON_HEIGHT))
55 .justify_center()
56}
57
58pub trait Buttons: ThemeExt {
59 /// A labeled button in one of the shipped styles. `fade` matters only for
60 /// [`ButtonStyle::Ghost`]: `Some` animates the hover wash per instance,
61 /// `None` is the plain ghost with the hover left to the caller.
62 fn button(
63 &self,
64 label: impl Into<SharedString>,
65 style: ButtonStyle,
66 fade: Option<Fade>,
67 ) -> Div {
68 let (button, _) = appearance(self.theme(), frame(), style, None, fade, true);
69 button.child(label.into())
70 }
71
72 /// A button that is only a glyph — SwiftUI's toolbar `Button` over an icon
73 /// `Label`. Square at [`Theme::BUTTON_HEIGHT`], so it stands the same
74 /// height as a [`Self::button`] beside it. `fade` reads as it does there.
75 ///
76 /// It builds the glyph rather than taking one: gpui reads an svg's colour
77 /// off that element's own style and paints **nothing** when it is unset, so
78 /// a colour set on this button would silently not reach it.
79 ///
80 /// An icon carries no accessible name — reach for
81 /// [`crate::tooltip`] on the way past.
82 fn icon_button(&self, icon: impl Into<Icon>, style: ButtonStyle, fade: Option<Fade>) -> Div {
83 let (button, tint) = appearance(self.theme(), square(), style, None, fade, true);
84 button.child(
85 crate::icons::icon(icon)
86 .size(px(ICON_GLYPH))
87 .text_color(tint),
88 )
89 }
90
91 /// [`Self::icon_button`]'s square with its glyph in `tint`, and no hover:
92 /// neither the glyph nor the fill moves under the pointer.
93 fn tinted_icon_button(&self, icon: impl Into<Icon>, tint: gpui::Hsla) -> Div {
94 square().text_color(tint).child(
95 crate::icons::icon(icon)
96 .size(px(ICON_GLYPH))
97 .text_color(tint),
98 )
99 }
100
101 /// SwiftUI's `ControlGroup`, and what a toolbar paints behind the items it
102 /// finds side by side: one shared background, its buttons inset in it. A
103 /// second cluster is a second call — the break between them is the spacing,
104 /// the way `ToolbarSpacer` puts it there.
105 ///
106 /// The track's radius is the item's plus the inset, so an ordinary
107 /// [`Self::button`] or [`Self::icon_button`] drops in already concentric.
108 ///
109 /// Items are left to stretch: that is what holds a glyph and a label to one
110 /// height when the type ladder moves under them. `self_start` because the
111 /// group must hug them — dropped into a `flex_col`, flexbox's default
112 /// `align-items: stretch` would otherwise blow it out to the column's full
113 /// width.
114 ///
115 /// Glass is chained, not baked: `.surface(theme, theme.popover_surface)`
116 /// turns the track into the capsule a macOS 26 toolbar floats.
117 fn control_group(&self) -> Div {
118 let theme = self.theme();
119 div()
120 .self_start()
121 .flex()
122 .flex_row()
123 .gap(px(GROUP_PAD))
124 .p(px(GROUP_PAD))
125 .rounded(px(Theme::button_radius() + GROUP_PAD))
126 .bg(theme.surface_raised)
127 .border_1()
128 .border_color(theme.border)
129 }
130
131 /// A quiet control: nothing at rest, a wash on hover. Stateful, so it
132 /// carries its own click and tooltip; padding and children are the
133 /// caller's, which is what lets a glyph sit before the text.
134 fn ghost(&self, id: impl Into<ElementId>) -> Stateful<Div> {
135 let tint = self.theme().element_hover;
136 div()
137 .id(id)
138 .flex()
139 .flex_row()
140 .items_center()
141 .rounded(px(Theme::control_radius()))
142 .cursor_pointer()
143 .hover(move |el| el.bg(tint))
144 }
145}
146
147impl Buttons for Theme {}
148
149/// Purpose is independent of emphasis: a destructive action may be quiet.
150#[derive(Clone, Copy, Debug, PartialEq, Eq)]
151pub enum ButtonRole {
152 Cancel,
153 Destructive,
154}
155
156pub(super) fn appearance(
157 theme: &Theme,
158 frame: Div,
159 style: ButtonStyle,
160 role: Option<ButtonRole>,
161 fade: Option<Fade>,
162 interactive: bool,
163) -> (Div, gpui::Hsla) {
164 let destructive = role == Some(ButtonRole::Destructive) || style == ButtonStyle::Destructive;
165 if style == ButtonStyle::Ghost {
166 let rest = if destructive {
167 theme.danger_strong
168 } else {
169 theme.text_muted
170 };
171 let hot = if destructive {
172 theme.danger_strong
173 } else {
174 theme.text
175 };
176 if let Some(fade) = fade {
177 let tint = motion::hover_blend(&fade, rest, hot);
178 let mut button = frame.text_color(tint).bg(motion::hover_blend(
179 &fade,
180 theme.ink(0.0),
181 theme.element_hover,
182 ));
183 button
184 .interactivity()
185 .on_hover(motion::hover_listener(fade));
186 (button, tint)
187 } else {
188 (frame.text_color(rest), rest)
189 }
190 } else {
191 let (fill, tint) = if destructive {
192 (theme.danger_strong, gpui::white())
193 } else {
194 (theme.text, theme.on_solid)
195 };
196 (
197 frame
198 .bg(fill)
199 .font_weight(gpui::FontWeight::MEDIUM)
200 .text_color(tint)
201 .when(interactive, |button| button.hover(|s| s.opacity(0.9))),
202 tint,
203 )
204 }
205}