1use documented::Documented;
2use gpui::{
3 AnyElement, AnyView, ClickEvent, CursorStyle, DefiniteLength, FocusHandle, Hsla, MouseButton,
4 MouseClickEvent, MouseDownEvent, MouseUpEvent, Rems, StyleRefinement, relative,
5 transparent_black,
6};
7use smallvec::SmallVec;
8
9use crate::{DynamicSpacing, ElevationIndex, prelude::*};
10
11/// A trait for buttons that can be Selected. Enables setting the [`ButtonStyle`] of a button when it is selected.
12pub trait SelectableButton: Toggleable {
13 fn selected_style(self, style: ButtonStyle) -> Self;
14}
15
16/// A common set of traits all buttons must implement.
17pub trait ButtonCommon: Clickable + Disableable {
18 /// A unique element ID to identify the button.
19 fn id(&self) -> &ElementId;
20
21 /// The visual style of the button.
22 ///
23 /// Most commonly will be [`ButtonStyle::Subtle`], or [`ButtonStyle::Filled`]
24 /// for an emphasized button.
25 fn style(self, style: ButtonStyle) -> Self;
26
27 /// The size of the button.
28 ///
29 /// Most buttons will use the default size.
30 ///
31 /// [`ButtonSize`] can also be used to help build non-button elements
32 /// that are consistently sized with buttons.
33 fn size(self, size: ButtonSize) -> Self;
34
35 /// The tooltip that shows when a user hovers over the button.
36 ///
37 /// Nearly all interactable elements should have a tooltip. Some example
38 /// exceptions might a scroll bar, or a slider.
39 fn tooltip(self, tooltip: impl Fn(&mut Window, &mut App) -> AnyView + 'static) -> Self;
40
41 fn tab_index(self, tab_index: impl Into<isize>) -> Self;
42
43 fn layer(self, elevation: ElevationIndex) -> Self;
44
45 fn track_focus(self, focus_handle: &FocusHandle) -> Self;
46}
47
48#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Default)]
49pub enum IconPosition {
50 #[default]
51 Start,
52 End,
53}
54
55#[derive(Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
56pub enum KeybindingPosition {
57 Start,
58 #[default]
59 End,
60}
61
62#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Default)]
63pub enum TintColor {
64 #[default]
65 Accent,
66 Error,
67 Warning,
68 Success,
69}
70
71impl TintColor {
72 fn button_like_style(self, cx: &mut App) -> ButtonLikeStyles {
73 match self {
74 TintColor::Accent => ButtonLikeStyles {
75 background: cx.theme().status().info_background,
76 border_color: cx.theme().status().info_border,
77 label_color: cx.theme().colors().text,
78 icon_color: cx.theme().colors().text,
79 },
80 TintColor::Error => ButtonLikeStyles {
81 background: cx.theme().status().error_background,
82 border_color: cx.theme().status().error_border,
83 label_color: cx.theme().colors().text,
84 icon_color: cx.theme().colors().text,
85 },
86 TintColor::Warning => ButtonLikeStyles {
87 background: cx.theme().status().warning_background,
88 border_color: cx.theme().status().warning_border,
89 label_color: cx.theme().colors().text,
90 icon_color: cx.theme().colors().text,
91 },
92 TintColor::Success => ButtonLikeStyles {
93 background: cx.theme().status().success_background,
94 border_color: cx.theme().status().success_border,
95 label_color: cx.theme().colors().text,
96 icon_color: cx.theme().colors().text,
97 },
98 }
99 }
100}
101
102impl From<TintColor> for Color {
103 fn from(tint: TintColor) -> Self {
104 match tint {
105 TintColor::Accent => Color::Accent,
106 TintColor::Error => Color::Error,
107 TintColor::Warning => Color::Warning,
108 TintColor::Success => Color::Success,
109 }
110 }
111}
112
113// Used to go from ButtonStyle -> Color through tint colors.
114impl From<ButtonStyle> for Color {
115 fn from(style: ButtonStyle) -> Self {
116 match style {
117 ButtonStyle::Tinted(tint) => tint.into(),
118 _ => Color::Default,
119 }
120 }
121}
122
123/// The visual appearance of a button.
124#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Default)]
125pub enum ButtonStyle {
126 /// A filled button with a solid background color. Provides emphasis versus
127 /// the more common subtle button.
128 Filled,
129
130 /// Used to emphasize a button in some way, like a selected state, or a semantic
131 /// coloring like an error or success button.
132 Tinted(TintColor),
133
134 /// Usually used as a secondary action that should have more emphasis than
135 /// a fully transparent button.
136 Outlined,
137
138 /// A more de-emphasized version of the outlined button.
139 OutlinedGhost,
140
141 /// Like [`ButtonStyle::Outlined`], but with a caller-provided border color.
142 OutlinedCustom(Hsla),
143
144 /// The default button style, used for most buttons. Has a transparent background,
145 /// but has a background color to indicate states like hover and active.
146 #[default]
147 Subtle,
148
149 /// Used for buttons that only change foreground color on hover and active states.
150 ///
151 /// TODO: Better docs for this.
152 Transparent,
153}
154
155/// Rounding for a button that may have straight edges.
156#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
157pub(crate) struct ButtonLikeRounding {
158 /// Top-left corner rounding
159 pub top_left: bool,
160 /// Top-right corner rounding
161 pub top_right: bool,
162 /// Bottom-right corner rounding
163 pub bottom_right: bool,
164 /// Bottom-left corner rounding
165 pub bottom_left: bool,
166}
167
168impl ButtonLikeRounding {
169 pub const ALL: Self = Self {
170 top_left: true,
171 top_right: true,
172 bottom_right: true,
173 bottom_left: true,
174 };
175 pub const LEFT: Self = Self {
176 top_left: true,
177 top_right: false,
178 bottom_right: false,
179 bottom_left: true,
180 };
181 pub const RIGHT: Self = Self {
182 top_left: false,
183 top_right: true,
184 bottom_right: true,
185 bottom_left: false,
186 };
187}
188
189#[derive(Debug, Clone)]
190pub(crate) struct ButtonLikeStyles {
191 pub background: Hsla,
192 #[allow(unused)]
193 pub border_color: Hsla,
194 #[allow(unused)]
195 pub label_color: Hsla,
196 #[allow(unused)]
197 pub icon_color: Hsla,
198}
199
200fn element_bg_from_elevation(elevation: Option<ElevationIndex>, cx: &mut App) -> Hsla {
201 match elevation {
202 Some(ElevationIndex::Background) => cx.theme().colors().element_background,
203 Some(ElevationIndex::ElevatedSurface) => cx.theme().colors().elevated_surface_background,
204 Some(ElevationIndex::Surface) => cx.theme().colors().surface_background,
205 Some(ElevationIndex::ModalSurface) => cx.theme().colors().background,
206 _ => cx.theme().colors().element_background,
207 }
208}
209
210impl ButtonStyle {
211 pub(crate) fn enabled(
212 self,
213 elevation: Option<ElevationIndex>,
214 cx: &mut App,
215 ) -> ButtonLikeStyles {
216 match self {
217 ButtonStyle::Filled => ButtonLikeStyles {
218 background: element_bg_from_elevation(elevation, cx),
219 border_color: transparent_black(),
220 label_color: Color::Default.color(cx),
221 icon_color: Color::Default.color(cx),
222 },
223 ButtonStyle::Tinted(tint) => tint.button_like_style(cx),
224 ButtonStyle::Outlined => ButtonLikeStyles {
225 background: element_bg_from_elevation(elevation, cx),
226 border_color: cx.theme().colors().border_variant,
227 label_color: Color::Default.color(cx),
228 icon_color: Color::Default.color(cx),
229 },
230 ButtonStyle::OutlinedGhost => ButtonLikeStyles {
231 background: transparent_black(),
232 border_color: cx.theme().colors().border_variant,
233 label_color: Color::Default.color(cx),
234 icon_color: Color::Default.color(cx),
235 },
236 ButtonStyle::OutlinedCustom(border_color) => ButtonLikeStyles {
237 background: transparent_black(),
238 border_color,
239 label_color: Color::Default.color(cx),
240 icon_color: Color::Default.color(cx),
241 },
242 ButtonStyle::Subtle => ButtonLikeStyles {
243 background: cx.theme().colors().ghost_element_background,
244 border_color: transparent_black(),
245 label_color: Color::Default.color(cx),
246 icon_color: Color::Default.color(cx),
247 },
248 ButtonStyle::Transparent => ButtonLikeStyles {
249 background: transparent_black(),
250 border_color: transparent_black(),
251 label_color: Color::Default.color(cx),
252 icon_color: Color::Default.color(cx),
253 },
254 }
255 }
256
257 pub(crate) fn hovered(
258 self,
259 elevation: Option<ElevationIndex>,
260 cx: &mut App,
261 ) -> ButtonLikeStyles {
262 match self {
263 ButtonStyle::Filled => {
264 let mut filled_background = element_bg_from_elevation(elevation, cx);
265 filled_background.fade_out(0.5);
266
267 ButtonLikeStyles {
268 background: filled_background,
269 border_color: transparent_black(),
270 label_color: Color::Default.color(cx),
271 icon_color: Color::Default.color(cx),
272 }
273 }
274 ButtonStyle::Tinted(tint) => {
275 let mut styles = tint.button_like_style(cx);
276 let theme = cx.theme();
277 styles.background = theme.darken(styles.background, 0.05, 0.2);
278 styles
279 }
280 ButtonStyle::Outlined => ButtonLikeStyles {
281 background: cx.theme().colors().ghost_element_hover,
282 border_color: cx.theme().colors().border,
283 label_color: Color::Default.color(cx),
284 icon_color: Color::Default.color(cx),
285 },
286 ButtonStyle::OutlinedGhost => ButtonLikeStyles {
287 background: cx.theme().colors().ghost_element_hover,
288 border_color: cx.theme().colors().border,
289 label_color: Color::Default.color(cx),
290 icon_color: Color::Default.color(cx),
291 },
292 ButtonStyle::OutlinedCustom(border_color) => ButtonLikeStyles {
293 background: cx.theme().colors().ghost_element_hover,
294 border_color,
295 label_color: Color::Default.color(cx),
296 icon_color: Color::Default.color(cx),
297 },
298 ButtonStyle::Subtle => ButtonLikeStyles {
299 background: cx.theme().colors().ghost_element_hover,
300 border_color: transparent_black(),
301 label_color: Color::Default.color(cx),
302 icon_color: Color::Default.color(cx),
303 },
304 ButtonStyle::Transparent => ButtonLikeStyles {
305 background: transparent_black(),
306 border_color: transparent_black(),
307 // TODO: These are not great
308 label_color: Color::Muted.color(cx),
309 // TODO: These are not great
310 icon_color: Color::Muted.color(cx),
311 },
312 }
313 }
314
315 pub(crate) fn active(self, cx: &mut App) -> ButtonLikeStyles {
316 match self {
317 ButtonStyle::Filled => ButtonLikeStyles {
318 background: cx.theme().colors().element_active,
319 border_color: transparent_black(),
320 label_color: Color::Default.color(cx),
321 icon_color: Color::Default.color(cx),
322 },
323 ButtonStyle::Tinted(tint) => tint.button_like_style(cx),
324 ButtonStyle::Subtle => ButtonLikeStyles {
325 background: cx.theme().colors().ghost_element_active,
326 border_color: transparent_black(),
327 label_color: Color::Default.color(cx),
328 icon_color: Color::Default.color(cx),
329 },
330 ButtonStyle::Outlined => ButtonLikeStyles {
331 background: cx.theme().colors().element_active,
332 border_color: cx.theme().colors().border_variant,
333 label_color: Color::Default.color(cx),
334 icon_color: Color::Default.color(cx),
335 },
336 ButtonStyle::OutlinedGhost => ButtonLikeStyles {
337 background: transparent_black(),
338 border_color: cx.theme().colors().border_variant,
339 label_color: Color::Default.color(cx),
340 icon_color: Color::Default.color(cx),
341 },
342 ButtonStyle::OutlinedCustom(border_color) => ButtonLikeStyles {
343 background: cx.theme().colors().element_active,
344 border_color,
345 label_color: Color::Default.color(cx),
346 icon_color: Color::Default.color(cx),
347 },
348 ButtonStyle::Transparent => ButtonLikeStyles {
349 background: transparent_black(),
350 border_color: transparent_black(),
351 // TODO: These are not great
352 label_color: Color::Muted.color(cx),
353 // TODO: These are not great
354 icon_color: Color::Muted.color(cx),
355 },
356 }
357 }
358
359 #[allow(unused)]
360 pub(crate) fn focused(self, window: &mut Window, cx: &mut App) -> ButtonLikeStyles {
361 match self {
362 ButtonStyle::Filled => ButtonLikeStyles {
363 background: cx.theme().colors().element_background,
364 border_color: cx.theme().colors().border_focused,
365 label_color: Color::Default.color(cx),
366 icon_color: Color::Default.color(cx),
367 },
368 ButtonStyle::Tinted(tint) => tint.button_like_style(cx),
369 ButtonStyle::Subtle => ButtonLikeStyles {
370 background: cx.theme().colors().ghost_element_background,
371 border_color: cx.theme().colors().border_focused,
372 label_color: Color::Default.color(cx),
373 icon_color: Color::Default.color(cx),
374 },
375 ButtonStyle::Outlined => ButtonLikeStyles {
376 background: cx.theme().colors().ghost_element_background,
377 border_color: cx.theme().colors().border,
378 label_color: Color::Default.color(cx),
379 icon_color: Color::Default.color(cx),
380 },
381 ButtonStyle::OutlinedGhost => ButtonLikeStyles {
382 background: transparent_black(),
383 border_color: cx.theme().colors().border,
384 label_color: Color::Default.color(cx),
385 icon_color: Color::Default.color(cx),
386 },
387 ButtonStyle::OutlinedCustom(border_color) => ButtonLikeStyles {
388 background: cx.theme().colors().ghost_element_background,
389 border_color,
390 label_color: Color::Default.color(cx),
391 icon_color: Color::Default.color(cx),
392 },
393 ButtonStyle::Transparent => ButtonLikeStyles {
394 background: transparent_black(),
395 border_color: cx.theme().colors().border_focused,
396 label_color: Color::Accent.color(cx),
397 icon_color: Color::Accent.color(cx),
398 },
399 }
400 }
401
402 #[allow(unused)]
403 pub(crate) fn disabled(
404 self,
405 elevation: Option<ElevationIndex>,
406 window: &mut Window,
407 cx: &mut App,
408 ) -> ButtonLikeStyles {
409 match self {
410 ButtonStyle::Filled => ButtonLikeStyles {
411 background: cx.theme().colors().element_disabled,
412 border_color: cx.theme().colors().border_disabled,
413 label_color: Color::Disabled.color(cx),
414 icon_color: Color::Disabled.color(cx),
415 },
416 ButtonStyle::Tinted(tint) => tint.button_like_style(cx),
417 ButtonStyle::Subtle => ButtonLikeStyles {
418 background: cx.theme().colors().ghost_element_disabled,
419 border_color: cx.theme().colors().border_disabled,
420 label_color: Color::Disabled.color(cx),
421 icon_color: Color::Disabled.color(cx),
422 },
423 ButtonStyle::Outlined => ButtonLikeStyles {
424 background: cx.theme().colors().element_disabled,
425 border_color: cx.theme().colors().border_disabled,
426 label_color: Color::Default.color(cx),
427 icon_color: Color::Default.color(cx),
428 },
429 ButtonStyle::OutlinedGhost => ButtonLikeStyles {
430 background: transparent_black(),
431 border_color: cx.theme().colors().border_disabled,
432 label_color: Color::Default.color(cx),
433 icon_color: Color::Default.color(cx),
434 },
435 ButtonStyle::OutlinedCustom(_) => ButtonLikeStyles {
436 background: cx.theme().colors().element_disabled,
437 border_color: cx.theme().colors().border_disabled,
438 label_color: Color::Default.color(cx),
439 icon_color: Color::Default.color(cx),
440 },
441 ButtonStyle::Transparent => ButtonLikeStyles {
442 background: transparent_black(),
443 border_color: transparent_black(),
444 label_color: Color::Disabled.color(cx),
445 icon_color: Color::Disabled.color(cx),
446 },
447 }
448 }
449}
450
451/// The height of a button.
452///
453/// Can also be used to size non-button elements to align with [`Button`]s.
454#[derive(Default, PartialEq, Clone, Copy)]
455pub enum ButtonSize {
456 Large,
457 Medium,
458 #[default]
459 Default,
460 Compact,
461 None,
462}
463
464impl ButtonSize {
465 pub fn rems(self) -> Rems {
466 match self {
467 ButtonSize::Large => rems_from_px(32.),
468 ButtonSize::Medium => rems_from_px(28.),
469 ButtonSize::Default => rems_from_px(22.),
470 ButtonSize::Compact => rems_from_px(18.),
471 ButtonSize::None => rems_from_px(16.),
472 }
473 }
474}
475
476/// A button-like element that can be used to create a custom button when
477/// prebuilt buttons are not sufficient. Use this sparingly, as it is
478/// unconstrained and may make the UI feel less consistent.
479///
480/// This is also used to build the prebuilt buttons.
481#[derive(IntoElement, Documented, RegisterComponent)]
482pub struct ButtonLike {
483 pub(super) base: Div,
484 id: ElementId,
485 pub(super) style: ButtonStyle,
486 pub(super) disabled: bool,
487 pub(super) selected: bool,
488 pub(super) selected_style: Option<ButtonStyle>,
489 pub(super) width: Option<DefiniteLength>,
490 pub(super) height: Option<DefiniteLength>,
491 pub(super) layer: Option<ElevationIndex>,
492 tab_index: Option<isize>,
493 size: ButtonSize,
494 rounding: Option<ButtonLikeRounding>,
495 tooltip: Option<Box<dyn Fn(&mut Window, &mut App) -> AnyView>>,
496 hoverable_tooltip: Option<Box<dyn Fn(&mut Window, &mut App) -> AnyView>>,
497 cursor_style: CursorStyle,
498 on_click: Option<Box<dyn Fn(&ClickEvent, &mut Window, &mut App) + 'static>>,
499 on_right_click: Option<Box<dyn Fn(&ClickEvent, &mut Window, &mut App) + 'static>>,
500 children: SmallVec<[AnyElement; 2]>,
501 focus_handle: Option<FocusHandle>,
502}
503
504impl ButtonLike {
505 pub fn new(id: impl Into<ElementId>) -> Self {
506 Self {
507 base: div(),
508 id: id.into(),
509 style: ButtonStyle::default(),
510 disabled: false,
511 selected: false,
512 selected_style: None,
513 width: None,
514 height: None,
515 size: ButtonSize::Default,
516 rounding: Some(ButtonLikeRounding::ALL),
517 tooltip: None,
518 hoverable_tooltip: None,
519 children: SmallVec::new(),
520 cursor_style: CursorStyle::PointingHand,
521 on_click: None,
522 on_right_click: None,
523 layer: None,
524 tab_index: None,
525 focus_handle: None,
526 }
527 }
528
529 pub fn new_rounded_left(id: impl Into<ElementId>) -> Self {
530 Self::new(id).rounding(ButtonLikeRounding::LEFT)
531 }
532
533 pub fn new_rounded_right(id: impl Into<ElementId>) -> Self {
534 Self::new(id).rounding(ButtonLikeRounding::RIGHT)
535 }
536
537 pub fn new_rounded_all(id: impl Into<ElementId>) -> Self {
538 Self::new(id).rounding(ButtonLikeRounding::ALL)
539 }
540
541 pub fn opacity(mut self, opacity: f32) -> Self {
542 self.base = self.base.opacity(opacity);
543 self
544 }
545
546 pub fn height(mut self, height: DefiniteLength) -> Self {
547 self.height = Some(height);
548 self
549 }
550
551 pub(crate) fn rounding(mut self, rounding: impl Into<Option<ButtonLikeRounding>>) -> Self {
552 self.rounding = rounding.into();
553 self
554 }
555
556 pub fn on_right_click(
557 mut self,
558 handler: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
559 ) -> Self {
560 self.on_right_click = Some(Box::new(handler));
561 self
562 }
563
564 pub fn hoverable_tooltip(
565 mut self,
566 tooltip: impl Fn(&mut Window, &mut App) -> AnyView + 'static,
567 ) -> Self {
568 self.hoverable_tooltip = Some(Box::new(tooltip));
569 self
570 }
571}
572
573impl Disableable for ButtonLike {
574 fn disabled(mut self, disabled: bool) -> Self {
575 self.disabled = disabled;
576 self
577 }
578}
579
580impl Toggleable for ButtonLike {
581 fn toggle_state(mut self, selected: bool) -> Self {
582 self.selected = selected;
583 self
584 }
585}
586
587impl SelectableButton for ButtonLike {
588 fn selected_style(mut self, style: ButtonStyle) -> Self {
589 self.selected_style = Some(style);
590 self
591 }
592}
593
594impl Clickable for ButtonLike {
595 fn on_click(mut self, handler: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static) -> Self {
596 self.on_click = Some(Box::new(handler));
597 self
598 }
599
600 fn cursor_style(mut self, cursor_style: CursorStyle) -> Self {
601 self.cursor_style = cursor_style;
602 self
603 }
604}
605
606impl FixedWidth for ButtonLike {
607 fn width(mut self, width: impl Into<DefiniteLength>) -> Self {
608 self.width = Some(width.into());
609 self
610 }
611
612 fn full_width(mut self) -> Self {
613 self.width = Some(relative(1.));
614 self
615 }
616}
617
618impl ButtonCommon for ButtonLike {
619 fn id(&self) -> &ElementId {
620 &self.id
621 }
622
623 fn style(mut self, style: ButtonStyle) -> Self {
624 self.style = style;
625 self
626 }
627
628 fn size(mut self, size: ButtonSize) -> Self {
629 self.size = size;
630 self
631 }
632
633 fn tooltip(mut self, tooltip: impl Fn(&mut Window, &mut App) -> AnyView + 'static) -> Self {
634 self.tooltip = Some(Box::new(tooltip));
635 self
636 }
637
638 fn tab_index(mut self, tab_index: impl Into<isize>) -> Self {
639 self.tab_index = Some(tab_index.into());
640 self
641 }
642
643 fn layer(mut self, elevation: ElevationIndex) -> Self {
644 self.layer = Some(elevation);
645 self
646 }
647
648 fn track_focus(mut self, focus_handle: &gpui::FocusHandle) -> Self {
649 self.focus_handle = Some(focus_handle.clone());
650 self
651 }
652}
653
654impl VisibleOnHover for ButtonLike {
655 fn visible_on_hover(mut self, group_name: impl Into<SharedString>) -> Self {
656 self.base = self.base.visible_on_hover(group_name);
657 self
658 }
659}
660
661impl ParentElement for ButtonLike {
662 fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
663 self.children.extend(elements)
664 }
665}
666
667impl RenderOnce for ButtonLike {
668 fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
669 let style = self
670 .selected_style
671 .filter(|_| self.selected)
672 .unwrap_or(self.style);
673
674 let is_outlined = matches!(
675 self.style,
676 ButtonStyle::Outlined | ButtonStyle::OutlinedGhost | ButtonStyle::OutlinedCustom(_)
677 );
678
679 self.base
680 .h_flex()
681 .id(self.id.clone())
682 .when_some(self.tab_index, |this, tab_index| this.tab_index(tab_index))
683 .when_some(self.focus_handle, |this, focus_handle| {
684 this.track_focus(&focus_handle)
685 })
686 .font_ui(cx)
687 .group("")
688 .flex_none()
689 .h(self.height.unwrap_or(self.size.rems().into()))
690 .when_some(self.width, |this, width| {
691 this.w(width).justify_center().text_center()
692 })
693 .when(is_outlined, |this| this.border_1())
694 .when_some(self.rounding, |this, rounding| {
695 this.when(rounding.top_left, |this| this.rounded_tl_sm())
696 .when(rounding.top_right, |this| this.rounded_tr_sm())
697 .when(rounding.bottom_right, |this| this.rounded_br_sm())
698 .when(rounding.bottom_left, |this| this.rounded_bl_sm())
699 })
700 .gap(DynamicSpacing::Base04.rems(cx))
701 .map(|this| match self.size {
702 ButtonSize::Large | ButtonSize::Medium => this.px(DynamicSpacing::Base08.rems(cx)),
703 ButtonSize::Default | ButtonSize::Compact => {
704 this.px(DynamicSpacing::Base04.rems(cx))
705 }
706 ButtonSize::None => this.px_px(),
707 })
708 .border_color(style.enabled(self.layer, cx).border_color)
709 .bg(style.enabled(self.layer, cx).background)
710 .when(self.disabled, |this| {
711 if self.cursor_style == CursorStyle::PointingHand {
712 this.cursor_not_allowed()
713 } else {
714 this.cursor(self.cursor_style)
715 }
716 })
717 .when(!self.disabled, |this| {
718 let hovered_style = style.hovered(self.layer, cx);
719 let focus_color =
720 |refinement: StyleRefinement| refinement.bg(hovered_style.background);
721
722 this.cursor(self.cursor_style)
723 .hover(focus_color)
724 .map(|this| {
725 if is_outlined {
726 this.focus_visible(|s| {
727 s.border_color(cx.theme().colors().border_focused)
728 })
729 } else {
730 this.focus_visible(focus_color)
731 }
732 })
733 .active(|active| active.bg(style.active(cx).background))
734 })
735 .when_some(
736 self.on_right_click.filter(|_| !self.disabled),
737 |this, on_right_click| {
738 this.on_mouse_down(MouseButton::Right, |_event, window, cx| {
739 window.prevent_default();
740 cx.stop_propagation();
741 })
742 .on_mouse_up(
743 MouseButton::Right,
744 move |event, window, cx| {
745 cx.stop_propagation();
746 let click_event = ClickEvent::Mouse(MouseClickEvent {
747 down: MouseDownEvent {
748 button: MouseButton::Right,
749 position: event.position,
750 modifiers: event.modifiers,
751 click_count: 1,
752 first_mouse: false,
753 },
754 up: MouseUpEvent {
755 button: MouseButton::Right,
756 position: event.position,
757 modifiers: event.modifiers,
758 click_count: 1,
759 },
760 });
761 (on_right_click)(&click_event, window, cx)
762 },
763 )
764 },
765 )
766 .when_some(
767 self.on_click.filter(|_| !self.disabled),
768 |this, on_click| {
769 this.on_mouse_down(MouseButton::Left, |_, window, _| window.prevent_default())
770 .on_click(move |event, window, cx| {
771 cx.stop_propagation();
772 (on_click)(event, window, cx)
773 })
774 },
775 )
776 .when_some(self.tooltip, |this, tooltip| {
777 this.tooltip(move |window, cx| tooltip(window, cx))
778 })
779 .when_some(self.hoverable_tooltip, |this, tooltip| {
780 this.hoverable_tooltip(move |window, cx| tooltip(window, cx))
781 })
782 .children(self.children)
783 }
784}
785
786impl Component for ButtonLike {
787 fn scope() -> ComponentScope {
788 ComponentScope::Input
789 }
790
791 fn sort_name() -> &'static str {
792 // ButtonLike should be at the bottom of the button list
793 "ButtonZ"
794 }
795
796 fn description() -> Option<&'static str> {
797 Some(ButtonLike::DOCS)
798 }
799
800 fn preview(_window: &mut Window, _cx: &mut App) -> Option<AnyElement> {
801 Some(
802 v_flex()
803 .gap_6()
804 .children(vec![
805 example_group(vec![
806 single_example(
807 "Default",
808 ButtonLike::new("default")
809 .child(Label::new("Default"))
810 .into_any_element(),
811 ),
812 single_example(
813 "Filled",
814 ButtonLike::new("filled")
815 .style(ButtonStyle::Filled)
816 .child(Label::new("Filled"))
817 .into_any_element(),
818 ),
819 single_example(
820 "Subtle",
821 ButtonLike::new("outline")
822 .style(ButtonStyle::Subtle)
823 .child(Label::new("Subtle"))
824 .into_any_element(),
825 ),
826 single_example(
827 "Tinted",
828 ButtonLike::new("tinted_accent_style")
829 .style(ButtonStyle::Tinted(TintColor::Accent))
830 .child(Label::new("Accent"))
831 .into_any_element(),
832 ),
833 single_example(
834 "Transparent",
835 ButtonLike::new("transparent")
836 .style(ButtonStyle::Transparent)
837 .child(Label::new("Transparent"))
838 .into_any_element(),
839 ),
840 ]),
841 example_group_with_title(
842 "Button Group Constructors",
843 vec![
844 single_example(
845 "Left Rounded",
846 ButtonLike::new_rounded_left("left_rounded")
847 .child(Label::new("Left Rounded"))
848 .style(ButtonStyle::Filled)
849 .into_any_element(),
850 ),
851 single_example(
852 "Right Rounded",
853 ButtonLike::new_rounded_right("right_rounded")
854 .child(Label::new("Right Rounded"))
855 .style(ButtonStyle::Filled)
856 .into_any_element(),
857 ),
858 single_example(
859 "Button Group",
860 h_flex()
861 .gap_px()
862 .child(
863 ButtonLike::new_rounded_left("bg_left")
864 .child(Label::new("Left"))
865 .style(ButtonStyle::Filled),
866 )
867 .child(
868 ButtonLike::new_rounded_right("bg_right")
869 .child(Label::new("Right"))
870 .style(ButtonStyle::Filled),
871 )
872 .into_any_element(),
873 ),
874 ],
875 ),
876 ])
877 .into_any_element(),
878 )
879 }
880}