1mod appearance;
2mod spinner;
3
4use std::rc::Rc;
5
6use gpui::{
7 AnyElement, App, ElementId, Entity, FocusHandle, InteractiveElement, IntoElement,
8 ParentElement, Pixels, RenderOnce, SharedString, Styled, Window, div, px,
9};
10use gpui_base::TestSupportExt;
11use gpui_material_foundation::{InteractionSource, current_material_theme};
12use gpui_material_theme::{
13 CornerRole, Direction, MotionKind, MotionSpeed, TypeRole, TypographyEmphasis,
14};
15
16use crate::action::{
17 Action, ActionControl, ActionLayout, ActionStyle, Activate, Change, SegmentShape, icon_slot,
18};
19use crate::button_style::{
20 ButtonBorder, ButtonColors, ButtonElevations, ButtonPadding, ButtonShapes, ButtonStyle,
21 ControlSize, finite_length,
22};
23
24use appearance::{button_appearance, icon_appearance};
25use spinner::render_loading_spinner;
26
27#[derive(Clone, Copy)]
28struct Metrics {
29 height: f32,
30 icon: f32,
31 gap: f32,
32 padding: f32,
33 outline: f32,
34 square: CornerRole,
35 pressed: CornerRole,
36 label: TypeRole,
37}
38
39impl ControlSize {
40 fn metrics(self) -> Metrics {
42 use CornerRole::*;
43 use TypeRole::*;
44 let (height, icon, gap, padding, outline, square, pressed, label) = match self {
45 Self::ExtraSmall => (32., 20., 8., 16., 1., Medium, Small, LabelLarge),
46 Self::Small => (40., 20., 8., 16., 1., Medium, Small, LabelLarge),
47 Self::Medium => (56., 24., 8., 24., 1., Large, Medium, TitleMedium),
48 Self::Large => (96., 32., 12., 48., 2., ExtraLarge, Large, HeadlineSmall),
49 Self::ExtraLarge => (136., 40., 16., 64., 3., ExtraLarge, Large, HeadlineLarge),
50 };
51 Metrics {
52 height,
53 icon,
54 gap,
55 padding,
56 outline,
57 square,
58 pressed,
59 label,
60 }
61 }
62
63 fn icon_metrics(self, width: IconButtonWidth) -> (Metrics, f32) {
64 let mut metrics = self.metrics();
66 let (icon, narrow, uniform, wide) = match self {
67 Self::ExtraSmall => (20., 4., 6., 10.),
68 Self::Small => (24., 4., 8., 14.),
69 Self::Medium => (24., 12., 16., 24.),
70 Self::Large => (32., 16., 32., 48.),
71 Self::ExtraLarge => (40., 32., 48., 72.),
72 };
73 metrics.icon = icon;
74 let space = match width {
75 IconButtonWidth::Narrow => narrow,
76 IconButtonWidth::Uniform => uniform,
77 IconButtonWidth::Wide => wide,
78 };
79 (metrics, icon + 2. * space)
80 }
81}
82
83#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
84pub enum ButtonShape {
85 #[default]
86 Round,
87 Square,
88}
89
90impl ButtonShape {
91 fn corner(self, square: CornerRole, invert: bool) -> CornerRole {
92 if (self == Self::Round) != invert {
93 CornerRole::Full
94 } else {
95 square
96 }
97 }
98}
99
100pub type ButtonTone = crate::SemanticTone;
104
105#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
106pub enum ButtonVariant {
107 Elevated,
108 #[default]
109 Filled,
110 FilledTonal,
111 Outlined,
112 Text,
113}
114
115#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
116pub enum ToggleButtonVariant {
117 Elevated,
118 #[default]
119 Filled,
120 FilledTonal,
121 Outlined,
122}
123
124#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
125pub enum IconButtonVariant {
126 #[default]
127 Standard,
128 Filled,
129 FilledTonal,
130 Outlined,
131}
132
133#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
134pub enum IconButtonWidth {
135 Narrow,
136 #[default]
137 Uniform,
138 Wide,
139}
140
141struct Common {
142 id: ElementId,
143 label: SharedString,
144 disabled: bool,
145 loading: bool,
146 focus: Option<FocusHandle>,
147 interaction_source: Option<Entity<InteractionSource>>,
148 appearance: ButtonStyle,
149 size: ControlSize,
150 shape: ButtonShape,
151 tone: ButtonTone,
152}
153
154impl Common {
155 fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
156 Self {
157 id: id.into(),
158 label: label.into(),
159 disabled: false,
160 loading: false,
161 focus: None,
162 interaction_source: None,
163 appearance: ButtonStyle::default(),
164 size: ControlSize::default(),
165 shape: ButtonShape::default(),
166 tone: ButtonTone::default(),
167 }
168 }
169
170 fn render(
171 self,
172 content: AnyElement,
173 style: ActionStyle,
174 action: Action,
175 window: &mut Window,
176 cx: &mut App,
177 ) -> AnyElement {
178 self.render_scaled(content, style, action, 1., window, cx)
179 }
180
181 fn render_scaled(
182 self,
183 content: AnyElement,
184 style: ActionStyle,
185 action: Action,
186 scale: f32,
187 window: &mut Window,
188 cx: &mut App,
189 ) -> AnyElement {
190 ActionControl {
191 id: self.id,
192 label: self.label,
193 focus: self.focus,
194 disabled: self.disabled,
195 action,
196 style,
197 content,
198 }
199 .render_layout(
200 scale,
201 ActionLayout {
202 tab_stop: true,
203 loading: self.loading,
204 appearance: self.appearance,
205 ..Default::default()
206 },
207 self.interaction_source.as_ref(),
208 window,
209 cx,
210 )
211 }
212}
213
214macro_rules! common_builders {
216 () => {
217 pub fn size(mut self, size: ControlSize) -> Self {
219 self.common.size = size;
220 self
221 }
222
223 pub fn shape(mut self, shape: ButtonShape) -> Self {
225 self.common.shape = shape;
226 self
227 }
228
229 pub fn tone(mut self, tone: ButtonTone) -> Self {
232 self.common.tone = tone;
233 self
234 }
235
236 pub fn disabled(mut self, disabled: bool) -> Self {
238 self.common.disabled = disabled;
239 self
240 }
241
242 pub fn track_focus(mut self, focus: &FocusHandle) -> Self {
244 self.common.focus = Some(focus.clone());
245 self
246 }
247 };
248}
249
250#[derive(Default)]
251struct Slots {
252 leading: Option<AnyElement>,
253 trailing: Option<AnyElement>,
254 content: Option<AnyElement>,
255 padding: Option<ButtonPadding>,
256 width: Option<Pixels>,
257}
258
259impl Slots {
260 fn render(
261 self,
262 label: SharedString,
263 metrics: Metrics,
264 scale: f32,
265 window: &Window,
266 tint: gpui::Hsla,
267 cx: &App,
268 ) -> AnyElement {
269 let direction = current_material_theme(window, cx).layout.direction;
270 let typography = metrics.label.style(TypographyEmphasis::Baseline);
271 let max_padding = self.width.map_or_else(
272 || f32::from(window.viewport_size().width) * 0.125,
273 |width| (f32::from(width) - 16.).max(0.) / 2.,
274 );
275 let horizontal = px(metrics.padding.min(max_padding));
276 let ButtonPadding {
277 top,
278 end,
279 bottom,
280 start,
281 } = self.padding.unwrap_or(ButtonPadding {
282 top: px(4.),
283 end: horizontal,
284 bottom: px(4.),
285 start: horizontal,
286 });
287 let (left, right) = if direction == Direction::Rtl {
288 (end, start)
289 } else {
290 (start, end)
291 };
292 let icons = usize::from(self.leading.is_some()) + usize::from(self.trailing.is_some());
293 let label_width = self.width.map(|width| {
294 (width
295 - finite_length(left)
296 - finite_length(right)
297 - px((metrics.icon + metrics.gap) * icons as f32))
298 .max(px(0.))
299 });
300 let row_typography = current_material_theme(window, cx)
301 .typography
302 .scale_style(typography);
303 let mut row = div()
304 .flex()
305 .items_center()
306 .justify_center()
307 .min_w_0()
308 .max_w_full()
309 .gap(px(metrics.gap * scale))
310 .pl(finite_length(left) * scale)
311 .pr(finite_length(right) * scale)
312 .pt(finite_length(top) * scale)
313 .pb(finite_length(bottom) * scale)
314 .text_size(px(row_typography.size * scale))
315 .line_height(px(row_typography.line_height * scale));
316 if direction == Direction::Rtl {
317 row = row.flex_row_reverse();
318 }
319 if let Some(width) = self.width {
320 row = row.w(width).overflow_hidden();
321 }
322 row.children(
323 self.leading
324 .map(|icon| icon_slot(icon, metrics.icon * scale)),
325 )
326 .child(self.content.unwrap_or_else(|| {
327 crate::text::single_line(
328 label,
329 typography,
330 scale,
331 tint,
332 (label_width, 1., 1.),
333 window,
334 cx,
335 )
336 .into_any_element()
337 }))
338 .children(
339 self.trailing
340 .map(|icon| icon_slot(icon, metrics.icon * scale)),
341 )
342 .into_any_element()
343 }
344}
345
346macro_rules! appearance_builders {
347 () => {
348 pub fn interaction_source(mut self, source: &Entity<InteractionSource>) -> Self {
351 self.common.interaction_source = Some(source.clone());
352 self
353 }
354
355 pub fn shapes(mut self, shapes: ButtonShapes) -> Self {
357 self.common.appearance.shapes = shapes;
358 self
359 }
360
361 pub fn colors(mut self, colors: ButtonColors) -> Self {
363 self.common.appearance.colors = colors;
364 self
365 }
366
367 pub fn border(mut self, border: ButtonBorder) -> Self {
369 self.common.appearance.border = Some(border);
370 self
371 }
372 };
373}
374
375macro_rules! slot_builders {
376 () => {
377 pub fn content(mut self, content: impl IntoElement) -> Self {
380 self.slots.content = Some(content.into_any_element());
381 self
382 }
383
384 pub fn content_padding(mut self, padding: ButtonPadding) -> Self {
386 self.slots.padding = Some(padding);
387 self
388 }
389
390 pub fn elevations(mut self, elevations: ButtonElevations) -> Self {
392 self.common.appearance.elevations = elevations;
393 self
394 }
395
396 pub fn leading_icon(mut self, icon: impl IntoElement) -> Self {
398 self.slots.leading = Some(icon.into_any_element());
399 self
400 }
401
402 pub fn trailing_icon(mut self, icon: impl IntoElement) -> Self {
404 self.slots.trailing = Some(icon.into_any_element());
405 self
406 }
407 };
408}
409
410#[doc(hidden)]
412pub mod composition;
413
414#[derive(IntoElement)]
417pub struct Button {
418 common: Common,
419 variant: ButtonVariant,
420 slots: Slots,
421 on_activate: Option<Activate>,
422 scale: f32,
423}
424
425impl Button {
426 pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
427 Self {
428 common: Common::new(id, label),
429 variant: ButtonVariant::default(),
430 slots: Slots::default(),
431 on_activate: None,
432 scale: 1.,
433 }
434 }
435
436 pub fn filled(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
438 Self::new(id, label).variant(ButtonVariant::Filled)
439 }
440
441 pub fn tonal(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
443 Self::new(id, label).variant(ButtonVariant::FilledTonal)
444 }
445
446 pub fn elevated(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
448 Self::new(id, label).variant(ButtonVariant::Elevated)
449 }
450
451 pub fn outlined(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
453 Self::new(id, label).variant(ButtonVariant::Outlined)
454 }
455
456 pub fn text(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
458 Self::new(id, label).variant(ButtonVariant::Text)
459 }
460
461 common_builders!();
462 appearance_builders!();
463 slot_builders!();
464
465 pub fn variant(mut self, variant: ButtonVariant) -> Self {
466 self.variant = variant;
467 self
468 }
469
470 pub fn loading(mut self, loading: bool) -> Self {
473 self.common.loading = loading;
474 self
475 }
476
477 #[cfg(feature = "feedback")]
478 pub(crate) fn scale(mut self, scale: f32) -> Self {
479 self.scale = scale;
480 self
481 }
482
483 pub fn on_activate(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
484 self.on_activate = Some(Rc::new(handler));
485 self
486 }
487}
488
489fn loading_overlay(
490 id: &ElementId,
491 resting_content: AnyElement,
492 icon_size: f32,
493 tint: gpui::Hsla,
494 window: &mut Window,
495 cx: &mut App,
496) -> AnyElement {
497 div()
498 .relative()
499 .flex()
500 .items_center()
501 .justify_center()
502 .child(div().opacity(0.).child(resting_content))
503 .child(
504 div()
505 .id((id.clone(), "loading-spinner"))
506 .test_support()
507 .absolute()
508 .inset_0()
509 .flex()
510 .items_center()
511 .justify_center()
512 .child(render_loading_spinner(id, icon_size, tint, window, cx)),
513 )
514 .into_any_element()
515}
516
517fn loading_icon_spinner(
518 id: &ElementId,
519 icon_size: f32,
520 tint: gpui::Hsla,
521 window: &mut Window,
522 cx: &mut App,
523) -> AnyElement {
524 div()
525 .id((id.clone(), "loading-spinner"))
526 .test_support()
527 .flex()
528 .items_center()
529 .justify_center()
530 .child(render_loading_spinner(id, icon_size, tint, window, cx))
531 .into_any_element()
532}
533
534impl RenderOnce for Button {
535 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
536 let metrics = self.common.size.metrics();
537 let appearance = button_appearance(self.variant, None, self.common.tone);
538 let style = appearance.style(
539 metrics,
540 self.common.shape.corner(metrics.square, false),
541 58.,
542 );
543 let tint = self
544 .common
545 .appearance
546 .colors
547 .content(self.common.disabled, false)
548 .unwrap_or_else(|| style.foreground(self.common.disabled, window, cx));
549 let resting_content = self.slots.render(
550 self.common.label.clone(),
551 metrics,
552 self.scale,
553 window,
554 tint,
555 cx,
556 );
557 let content = if self.common.loading {
558 loading_overlay(
559 &self.common.id,
560 resting_content,
561 metrics.icon * self.scale,
562 tint,
563 window,
564 cx,
565 )
566 } else {
567 resting_content
568 };
569 self.common.render_scaled(
570 content,
571 style,
572 Action::Invoke(self.on_activate),
573 self.scale,
574 window,
575 cx,
576 )
577 }
578}
579
580#[derive(IntoElement)]
583pub struct ToggleButton {
584 common: Common,
585 variant: ToggleButtonVariant,
586 selected: bool,
587 slots: Slots,
588 on_change: Option<Change>,
589}
590
591impl ToggleButton {
592 pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>, selected: bool) -> Self {
593 Self {
594 common: Common::new(id, label),
595 variant: ToggleButtonVariant::default(),
596 selected,
597 slots: Slots::default(),
598 on_change: None,
599 }
600 }
601
602 pub fn filled(
604 id: impl Into<ElementId>,
605 label: impl Into<SharedString>,
606 selected: bool,
607 ) -> Self {
608 Self::new(id, label, selected).variant(ToggleButtonVariant::Filled)
609 }
610
611 pub fn tonal(id: impl Into<ElementId>, label: impl Into<SharedString>, selected: bool) -> Self {
613 Self::new(id, label, selected).variant(ToggleButtonVariant::FilledTonal)
614 }
615
616 pub fn elevated(
618 id: impl Into<ElementId>,
619 label: impl Into<SharedString>,
620 selected: bool,
621 ) -> Self {
622 Self::new(id, label, selected).variant(ToggleButtonVariant::Elevated)
623 }
624
625 pub fn outlined(
627 id: impl Into<ElementId>,
628 label: impl Into<SharedString>,
629 selected: bool,
630 ) -> Self {
631 Self::new(id, label, selected).variant(ToggleButtonVariant::Outlined)
632 }
633
634 common_builders!();
635 appearance_builders!();
636 slot_builders!();
637
638 pub fn variant(mut self, variant: ToggleButtonVariant) -> Self {
639 self.variant = variant;
640 self
641 }
642
643 pub fn loading(mut self, loading: bool) -> Self {
646 self.common.loading = loading;
647 self
648 }
649
650 pub fn on_change(mut self, handler: impl Fn(bool, &mut Window, &mut App) + 'static) -> Self {
651 self.on_change = Some(Rc::new(handler));
652 self
653 }
654}
655
656impl RenderOnce for ToggleButton {
657 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
658 let metrics = self.common.size.metrics();
659 let variant = match self.variant {
660 ToggleButtonVariant::Elevated => ButtonVariant::Elevated,
661 ToggleButtonVariant::Filled => ButtonVariant::Filled,
662 ToggleButtonVariant::FilledTonal => ButtonVariant::FilledTonal,
663 ToggleButtonVariant::Outlined => ButtonVariant::Outlined,
664 };
665 let mut style = button_appearance(variant, Some(self.selected), self.common.tone).style(
666 metrics,
667 self.common.shape.corner(metrics.square, false),
668 0.,
669 );
670 style.selected_corner = Some(self.common.shape.corner(metrics.square, true));
671 let tint = self
672 .common
673 .appearance
674 .colors
675 .content(self.common.disabled, self.selected)
676 .unwrap_or_else(|| style.foreground(self.common.disabled, window, cx));
677 let resting_content =
678 self.slots
679 .render(self.common.label.clone(), metrics, 1., window, tint, cx);
680 let content = if self.common.loading {
681 loading_overlay(
682 &self.common.id,
683 resting_content,
684 metrics.icon,
685 tint,
686 window,
687 cx,
688 )
689 } else {
690 resting_content
691 };
692 self.common.render(
693 content,
694 style,
695 Action::Toggle {
696 selected: self.selected,
697 on_change: self.on_change,
698 },
699 window,
700 cx,
701 )
702 }
703}
704
705#[derive(IntoElement)]
707pub struct IconButton {
708 common: Common,
709 icon: AnyElement,
710 variant: IconButtonVariant,
711 width: IconButtonWidth,
712 on_activate: Option<Activate>,
713}
714
715impl IconButton {
716 pub fn new(
717 id: impl Into<ElementId>,
718 name: impl Into<SharedString>,
719 icon: impl IntoElement,
720 ) -> Self {
721 Self {
722 common: Common::new(id, name),
723 icon: icon.into_any_element(),
724 variant: IconButtonVariant::default(),
725 width: IconButtonWidth::default(),
726 on_activate: None,
727 }
728 }
729
730 pub fn standard(
732 id: impl Into<ElementId>,
733 name: impl Into<SharedString>,
734 icon: impl IntoElement,
735 ) -> Self {
736 Self::new(id, name, icon).variant(IconButtonVariant::Standard)
737 }
738
739 pub fn filled(
741 id: impl Into<ElementId>,
742 name: impl Into<SharedString>,
743 icon: impl IntoElement,
744 ) -> Self {
745 Self::new(id, name, icon).variant(IconButtonVariant::Filled)
746 }
747
748 pub fn tonal(
750 id: impl Into<ElementId>,
751 name: impl Into<SharedString>,
752 icon: impl IntoElement,
753 ) -> Self {
754 Self::new(id, name, icon).variant(IconButtonVariant::FilledTonal)
755 }
756
757 pub fn outlined(
759 id: impl Into<ElementId>,
760 name: impl Into<SharedString>,
761 icon: impl IntoElement,
762 ) -> Self {
763 Self::new(id, name, icon).variant(IconButtonVariant::Outlined)
764 }
765
766 common_builders!();
767 appearance_builders!();
768
769 pub fn variant(mut self, variant: IconButtonVariant) -> Self {
770 self.variant = variant;
771 self
772 }
773
774 pub fn width(mut self, width: IconButtonWidth) -> Self {
775 self.width = width;
776 self
777 }
778
779 pub fn loading(mut self, loading: bool) -> Self {
782 self.common.loading = loading;
783 self
784 }
785
786 pub fn on_activate(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
787 self.on_activate = Some(Rc::new(handler));
788 self
789 }
790}
791
792impl RenderOnce for IconButton {
793 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
794 let (metrics, width) = self.common.size.icon_metrics(self.width);
795 let mut style = icon_appearance(self.variant, None, self.common.tone).style(
796 metrics,
797 self.common.shape.corner(metrics.square, false),
798 width,
799 );
800 style.corner_motion = (MotionKind::Effects, MotionSpeed::Default);
801 let content = if self.common.loading {
802 let tint = self
803 .common
804 .appearance
805 .colors
806 .content(self.common.disabled, false)
807 .unwrap_or_else(|| style.foreground(self.common.disabled, window, cx));
808 loading_icon_spinner(&self.common.id, metrics.icon, tint, window, cx)
809 } else {
810 icon_slot(self.icon, metrics.icon).into_any_element()
811 };
812 self.common
813 .render(content, style, Action::Invoke(self.on_activate), window, cx)
814 }
815}
816
817#[derive(IntoElement)]
820pub struct IconToggleButton {
821 common: Common,
822 icon: AnyElement,
823 selected_icon: Option<AnyElement>,
824 selected: bool,
825 variant: IconButtonVariant,
826 width: IconButtonWidth,
827 on_change: Option<Change>,
828}
829
830impl IconToggleButton {
831 pub fn new(
832 id: impl Into<ElementId>,
833 name: impl Into<SharedString>,
834 icon: impl IntoElement,
835 selected: bool,
836 ) -> Self {
837 Self {
838 common: Common::new(id, name),
839 icon: icon.into_any_element(),
840 selected_icon: None,
841 selected,
842 variant: IconButtonVariant::default(),
843 width: IconButtonWidth::default(),
844 on_change: None,
845 }
846 }
847
848 pub fn standard(
850 id: impl Into<ElementId>,
851 name: impl Into<SharedString>,
852 icon: impl IntoElement,
853 selected: bool,
854 ) -> Self {
855 Self::new(id, name, icon, selected).variant(IconButtonVariant::Standard)
856 }
857
858 pub fn filled(
860 id: impl Into<ElementId>,
861 name: impl Into<SharedString>,
862 icon: impl IntoElement,
863 selected: bool,
864 ) -> Self {
865 Self::new(id, name, icon, selected).variant(IconButtonVariant::Filled)
866 }
867
868 pub fn tonal(
870 id: impl Into<ElementId>,
871 name: impl Into<SharedString>,
872 icon: impl IntoElement,
873 selected: bool,
874 ) -> Self {
875 Self::new(id, name, icon, selected).variant(IconButtonVariant::FilledTonal)
876 }
877
878 pub fn outlined(
880 id: impl Into<ElementId>,
881 name: impl Into<SharedString>,
882 icon: impl IntoElement,
883 selected: bool,
884 ) -> Self {
885 Self::new(id, name, icon, selected).variant(IconButtonVariant::Outlined)
886 }
887
888 common_builders!();
889 appearance_builders!();
890
891 pub fn selected_icon(mut self, icon: impl IntoElement) -> Self {
892 self.selected_icon = Some(icon.into_any_element());
893 self
894 }
895
896 pub fn variant(mut self, variant: IconButtonVariant) -> Self {
897 self.variant = variant;
898 self
899 }
900
901 pub fn width(mut self, width: IconButtonWidth) -> Self {
902 self.width = width;
903 self
904 }
905
906 pub fn loading(mut self, loading: bool) -> Self {
909 self.common.loading = loading;
910 self
911 }
912
913 pub fn on_change(mut self, handler: impl Fn(bool, &mut Window, &mut App) + 'static) -> Self {
914 self.on_change = Some(Rc::new(handler));
915 self
916 }
917}
918
919impl RenderOnce for IconToggleButton {
920 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
921 let (metrics, width) = self.common.size.icon_metrics(self.width);
922 let mut style = icon_appearance(self.variant, Some(self.selected), self.common.tone).style(
923 metrics,
924 self.common.shape.corner(metrics.square, false),
925 width,
926 );
927 style.selected_corner = Some(self.common.shape.corner(metrics.square, true));
928 style.corner_motion = (MotionKind::Effects, MotionSpeed::Default);
929 let content = if self.common.loading {
930 let tint = self
931 .common
932 .appearance
933 .colors
934 .content(self.common.disabled, self.selected)
935 .unwrap_or_else(|| style.foreground(self.common.disabled, window, cx));
936 loading_icon_spinner(&self.common.id, metrics.icon, tint, window, cx)
937 } else {
938 let icon = if self.selected {
939 self.selected_icon.unwrap_or(self.icon)
940 } else {
941 self.icon
942 };
943 icon_slot(icon, metrics.icon).into_any_element()
944 };
945 self.common.render(
946 content,
947 style,
948 Action::Toggle {
949 selected: self.selected,
950 on_change: self.on_change,
951 },
952 window,
953 cx,
954 )
955 }
956}
957
958#[cfg(test)]
959mod tests {
960 use super::*;
961
962 #[test]
963 fn button_and_icon_button_variant_shorthands_set_expected_variants() {
964 assert_eq!(
965 Button::filled("b1", "Filled").variant,
966 ButtonVariant::Filled
967 );
968 assert_eq!(
969 Button::tonal("b2", "Tonal").variant,
970 ButtonVariant::FilledTonal
971 );
972 assert_eq!(
973 Button::elevated("b3", "Elevated").variant,
974 ButtonVariant::Elevated
975 );
976 assert_eq!(
977 Button::outlined("b4", "Outlined").variant,
978 ButtonVariant::Outlined
979 );
980 assert_eq!(Button::text("b5", "Text").variant, ButtonVariant::Text);
981
982 assert_eq!(
983 ToggleButton::filled("tb1", "Filled", true).variant,
984 ToggleButtonVariant::Filled
985 );
986 assert_eq!(
987 ToggleButton::tonal("tb2", "Tonal", false).variant,
988 ToggleButtonVariant::FilledTonal
989 );
990 assert_eq!(
991 ToggleButton::elevated("tb3", "Elevated", true).variant,
992 ToggleButtonVariant::Elevated
993 );
994 assert_eq!(
995 ToggleButton::outlined("tb4", "Outlined", false).variant,
996 ToggleButtonVariant::Outlined
997 );
998
999 assert_eq!(
1000 IconButton::standard("i1", "Std", div()).variant,
1001 IconButtonVariant::Standard
1002 );
1003 assert_eq!(
1004 IconButton::filled("i2", "Fill", div()).variant,
1005 IconButtonVariant::Filled
1006 );
1007 assert_eq!(
1008 IconButton::tonal("i3", "Tonal", div()).variant,
1009 IconButtonVariant::FilledTonal
1010 );
1011 assert_eq!(
1012 IconButton::outlined("i4", "Out", div()).variant,
1013 IconButtonVariant::Outlined
1014 );
1015
1016 assert_eq!(
1017 IconToggleButton::standard("it1", "Std", div(), false).variant,
1018 IconButtonVariant::Standard
1019 );
1020 assert_eq!(
1021 IconToggleButton::filled("it2", "Fill", div(), true).variant,
1022 IconButtonVariant::Filled
1023 );
1024 assert_eq!(
1025 IconToggleButton::tonal("it3", "Tonal", div(), false).variant,
1026 IconButtonVariant::FilledTonal
1027 );
1028 assert_eq!(
1029 IconToggleButton::outlined("it4", "Out", div(), true).variant,
1030 IconButtonVariant::Outlined
1031 );
1032
1033 let tone: ButtonTone = crate::SemanticTone::Danger;
1034 assert_eq!(tone, ButtonTone::Danger);
1035 }
1036}