1use std::marker::PhantomData;
2use std::sync::Arc;
3
4use gpui3::{DefiniteLength, Hsla, Interactive, MouseButton, WindowContext};
5
6use crate::prelude::*;
7use crate::{h_stack, theme, Icon, IconColor, IconElement, Label, LabelColor, LabelSize};
8
9#[derive(Default, PartialEq, Clone, Copy)]
10pub enum IconPosition {
11 #[default]
12 Left,
13 Right,
14}
15
16#[derive(Default, Copy, Clone, PartialEq)]
17pub enum ButtonVariant {
18 #[default]
19 Ghost,
20 Filled,
21}
22
23pub type ClickHandler<S> = Arc<dyn Fn(&mut S, &mut ViewContext<S>) + 'static + Send + Sync>;
24
25struct ButtonHandlers<S: 'static + Send + Sync> {
26 click: Option<ClickHandler<S>>,
27}
28
29impl<S: 'static + Send + Sync> Default for ButtonHandlers<S> {
30 fn default() -> Self {
31 Self { click: None }
32 }
33}
34
35#[derive(Element)]
36pub struct Button<S: 'static + Send + Sync + Clone> {
37 state_type: PhantomData<S>,
38 label: String,
39 variant: ButtonVariant,
40 state: InteractionState,
41 icon: Option<Icon>,
42 icon_position: Option<IconPosition>,
43 width: Option<DefiniteLength>,
44 handlers: ButtonHandlers<S>,
45}
46
47impl<S: 'static + Send + Sync + Clone> Button<S> {
48 pub fn new<L>(label: L) -> Self
49 where
50 L: Into<String>,
51 {
52 Self {
53 state_type: PhantomData,
54 label: label.into(),
55 variant: Default::default(),
56 state: Default::default(),
57 icon: None,
58 icon_position: None,
59 width: Default::default(),
60 handlers: ButtonHandlers::default(),
61 }
62 }
63
64 pub fn ghost<L>(label: L) -> Self
65 where
66 L: Into<String>,
67 {
68 Self::new(label).variant(ButtonVariant::Ghost)
69 }
70
71 pub fn variant(mut self, variant: ButtonVariant) -> Self {
72 self.variant = variant;
73 self
74 }
75
76 pub fn state(mut self, state: InteractionState) -> Self {
77 self.state = state;
78 self
79 }
80
81 pub fn icon(mut self, icon: Icon) -> Self {
82 self.icon = Some(icon);
83 self
84 }
85
86 pub fn icon_position(mut self, icon_position: IconPosition) -> Self {
87 if self.icon.is_none() {
88 panic!("An icon must be present if an icon_position is provided.");
89 }
90 self.icon_position = Some(icon_position);
91 self
92 }
93
94 pub fn width(mut self, width: Option<DefiniteLength>) -> Self {
95 self.width = width;
96 self
97 }
98
99 pub fn on_click(mut self, handler: ClickHandler<S>) -> Self {
100 self.handlers.click = Some(handler);
101 self
102 }
103
104 fn background_color(&self, cx: &mut ViewContext<S>) -> Hsla {
105 let theme = theme(cx);
106 let system_color = SystemColor::new();
107
108 match (self.variant, self.state) {
109 (ButtonVariant::Ghost, InteractionState::Hovered) => {
110 theme.lowest.base.hovered.background
111 }
112 (ButtonVariant::Ghost, InteractionState::Active) => {
113 theme.lowest.base.pressed.background
114 }
115 (ButtonVariant::Filled, InteractionState::Enabled) => {
116 theme.lowest.on.default.background
117 }
118 (ButtonVariant::Filled, InteractionState::Hovered) => {
119 theme.lowest.on.hovered.background
120 }
121 (ButtonVariant::Filled, InteractionState::Active) => theme.lowest.on.pressed.background,
122 (ButtonVariant::Filled, InteractionState::Disabled) => {
123 theme.lowest.on.disabled.background
124 }
125 _ => system_color.transparent,
126 }
127 }
128
129 fn label_color(&self) -> LabelColor {
130 match self.state {
131 InteractionState::Disabled => LabelColor::Disabled,
132 _ => Default::default(),
133 }
134 }
135
136 fn icon_color(&self) -> IconColor {
137 match self.state {
138 InteractionState::Disabled => IconColor::Disabled,
139 _ => Default::default(),
140 }
141 }
142
143 fn border_color(&self, cx: &WindowContext) -> Hsla {
144 let theme = theme(cx);
145 let system_color = SystemColor::new();
146
147 match self.state {
148 InteractionState::Focused => theme.lowest.accent.default.border,
149 _ => system_color.transparent,
150 }
151 }
152
153 fn render_label(&self) -> Label<S> {
154 Label::new(self.label.clone())
155 .size(LabelSize::Small)
156 .color(self.label_color())
157 }
158
159 fn render_icon(&self, icon_color: IconColor) -> Option<IconElement<S>> {
160 self.icon.map(|i| IconElement::new(i).color(icon_color))
161 }
162
163 fn render(&mut self, cx: &mut ViewContext<S>) -> impl Element<ViewState = S> {
164 let theme = theme(cx);
165 let icon_color = self.icon_color();
166 let system_color = SystemColor::new();
167 let border_color = self.border_color(cx);
168
169 let mut el = h_stack()
170 .h_6()
171 .px_1()
172 .items_center()
173 .rounded_md()
174 .border()
175 .border_color(border_color)
176 .fill(self.background_color(cx));
177
178 match (self.icon, self.icon_position) {
179 (Some(_), Some(IconPosition::Left)) => {
180 el = el
181 .gap_1()
182 .child(self.render_label())
183 .children(self.render_icon(icon_color))
184 }
185 (Some(_), Some(IconPosition::Right)) => {
186 el = el
187 .gap_1()
188 .children(self.render_icon(icon_color))
189 .child(self.render_label())
190 }
191 (_, _) => el = el.child(self.render_label()),
192 }
193
194 if let Some(width) = self.width {
195 el = el.w(width).justify_center();
196 }
197
198 if let Some(click_handler) = self.handlers.click.clone() {
199 el = el.on_click(MouseButton::Left, move |state, event, cx| {
200 click_handler(state, cx);
201 });
202 }
203
204 el
205 }
206}
207
208#[cfg(feature = "stories")]
209pub use stories::*;
210
211#[cfg(feature = "stories")]
212mod stories {
213 use gpui3::rems;
214 use strum::IntoEnumIterator;
215
216 use crate::{h_stack, v_stack, LabelColor, LabelSize, Story};
217
218 use super::*;
219
220 #[derive(Element)]
221 pub struct ButtonStory<S: 'static + Send + Sync + Clone> {
222 state_type: PhantomData<S>,
223 }
224
225 impl<S: 'static + Send + Sync + Clone> ButtonStory<S> {
226 pub fn new() -> Self {
227 Self {
228 state_type: PhantomData,
229 }
230 }
231
232 fn render(&mut self, cx: &mut ViewContext<S>) -> impl Element<ViewState = S> {
233 let states = InteractionState::iter();
234
235 Story::container(cx)
236 .child(Story::title_for::<_, Button<S>>(cx))
237 .child(
238 div()
239 .flex()
240 .gap_8()
241 .child(
242 div()
243 .child(Story::label(cx, "Ghost (Default)"))
244 .child(h_stack().gap_2().children(states.clone().map(|state| {
245 v_stack()
246 .gap_1()
247 .child(
248 Label::new(state.to_string())
249 .color(LabelColor::Muted)
250 .size(LabelSize::Small),
251 )
252 .child(
253 Button::new("Label")
254 .variant(ButtonVariant::Ghost)
255 .state(state),
256 )
257 })))
258 .child(Story::label(cx, "Ghost – Left Icon"))
259 .child(h_stack().gap_2().children(states.clone().map(|state| {
260 v_stack()
261 .gap_1()
262 .child(
263 Label::new(state.to_string())
264 .color(LabelColor::Muted)
265 .size(LabelSize::Small),
266 )
267 .child(
268 Button::new("Label")
269 .variant(ButtonVariant::Ghost)
270 .icon(Icon::Plus)
271 .icon_position(IconPosition::Left)
272 .state(state),
273 )
274 })))
275 .child(Story::label(cx, "Ghost – Right Icon"))
276 .child(h_stack().gap_2().children(states.clone().map(|state| {
277 v_stack()
278 .gap_1()
279 .child(
280 Label::new(state.to_string())
281 .color(LabelColor::Muted)
282 .size(LabelSize::Small),
283 )
284 .child(
285 Button::new("Label")
286 .variant(ButtonVariant::Ghost)
287 .icon(Icon::Plus)
288 .icon_position(IconPosition::Right)
289 .state(state),
290 )
291 }))),
292 )
293 .child(
294 div()
295 .child(Story::label(cx, "Filled"))
296 .child(h_stack().gap_2().children(states.clone().map(|state| {
297 v_stack()
298 .gap_1()
299 .child(
300 Label::new(state.to_string())
301 .color(LabelColor::Muted)
302 .size(LabelSize::Small),
303 )
304 .child(
305 Button::new("Label")
306 .variant(ButtonVariant::Filled)
307 .state(state),
308 )
309 })))
310 .child(Story::label(cx, "Filled – Left Button"))
311 .child(h_stack().gap_2().children(states.clone().map(|state| {
312 v_stack()
313 .gap_1()
314 .child(
315 Label::new(state.to_string())
316 .color(LabelColor::Muted)
317 .size(LabelSize::Small),
318 )
319 .child(
320 Button::new("Label")
321 .variant(ButtonVariant::Filled)
322 .icon(Icon::Plus)
323 .icon_position(IconPosition::Left)
324 .state(state),
325 )
326 })))
327 .child(Story::label(cx, "Filled – Right Button"))
328 .child(h_stack().gap_2().children(states.clone().map(|state| {
329 v_stack()
330 .gap_1()
331 .child(
332 Label::new(state.to_string())
333 .color(LabelColor::Muted)
334 .size(LabelSize::Small),
335 )
336 .child(
337 Button::new("Label")
338 .variant(ButtonVariant::Filled)
339 .icon(Icon::Plus)
340 .icon_position(IconPosition::Right)
341 .state(state),
342 )
343 }))),
344 )
345 .child(
346 div()
347 .child(Story::label(cx, "Fixed With"))
348 .child(h_stack().gap_2().children(states.clone().map(|state| {
349 v_stack()
350 .gap_1()
351 .child(
352 Label::new(state.to_string())
353 .color(LabelColor::Muted)
354 .size(LabelSize::Small),
355 )
356 .child(
357 Button::new("Label")
358 .variant(ButtonVariant::Filled)
359 .state(state)
360 .width(Some(rems(6.).into())),
361 )
362 })))
363 .child(Story::label(cx, "Fixed With – Left Icon"))
364 .child(h_stack().gap_2().children(states.clone().map(|state| {
365 v_stack()
366 .gap_1()
367 .child(
368 Label::new(state.to_string())
369 .color(LabelColor::Muted)
370 .size(LabelSize::Small),
371 )
372 .child(
373 Button::new("Label")
374 .variant(ButtonVariant::Filled)
375 .state(state)
376 .icon(Icon::Plus)
377 .icon_position(IconPosition::Left)
378 .width(Some(rems(6.).into())),
379 )
380 })))
381 .child(Story::label(cx, "Fixed With – Right Icon"))
382 .child(h_stack().gap_2().children(states.clone().map(|state| {
383 v_stack()
384 .gap_1()
385 .child(
386 Label::new(state.to_string())
387 .color(LabelColor::Muted)
388 .size(LabelSize::Small),
389 )
390 .child(
391 Button::new("Label")
392 .variant(ButtonVariant::Filled)
393 .state(state)
394 .icon(Icon::Plus)
395 .icon_position(IconPosition::Right)
396 .width(Some(rems(6.).into())),
397 )
398 }))),
399 ),
400 )
401 .child(Story::label(cx, "Button with `on_click`"))
402 .child(
403 Button::new("Label")
404 .variant(ButtonVariant::Ghost)
405 .on_click(Arc::new(|_view, _cx| println!("Button clicked."))),
406 )
407 }
408 }
409}