1use std::rc::Rc;
2
3use gpui::{
4 AnyElement, App, ElementId, FocusHandle, IntoElement, ParentElement, Pixels, RenderOnce,
5 SharedString, Styled, Window, div, px,
6};
7use gpui_material_foundation::window_material_theme;
8use gpui_material_theme::{
9 ColorRole, CornerRole, Direction, Elevation, MotionKind, MotionSpeed, TypeRole,
10 TypographyEmphasis,
11};
12
13use crate::action::{
14 Action, ActionControl, ActionLayout, ActionStyle, Activate, Change, DisabledStyle,
15 SegmentShape, icon_slot,
16};
17
18#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
20pub enum ControlSize {
21 ExtraSmall,
22 #[default]
23 Small,
24 Medium,
25 Large,
26 ExtraLarge,
27}
28
29#[derive(Clone, Copy)]
30struct Metrics {
31 height: f32,
32 icon: f32,
33 gap: f32,
34 padding: f32,
35 outline: f32,
36 square: CornerRole,
37 pressed: CornerRole,
38 label: TypeRole,
39}
40
41impl ControlSize {
42 fn metrics(self) -> Metrics {
44 use CornerRole::*;
45 use TypeRole::*;
46 let (height, icon, gap, padding, outline, square, pressed, label) = match self {
47 Self::ExtraSmall => (32., 20., 8., 16., 1., Medium, Small, LabelLarge),
48 Self::Small => (40., 20., 8., 16., 1., Medium, Small, LabelLarge),
49 Self::Medium => (56., 24., 8., 24., 1., Large, Medium, TitleMedium),
50 Self::Large => (96., 32., 12., 48., 2., ExtraLarge, Large, HeadlineSmall),
51 Self::ExtraLarge => (136., 40., 16., 64., 3., ExtraLarge, Large, HeadlineLarge),
52 };
53 Metrics {
54 height,
55 icon,
56 gap,
57 padding,
58 outline,
59 square,
60 pressed,
61 label,
62 }
63 }
64
65 fn icon_metrics(self, width: IconButtonWidth) -> (Metrics, f32) {
66 let mut metrics = self.metrics();
68 let (icon, narrow, uniform, wide) = match self {
69 Self::ExtraSmall => (20., 4., 6., 10.),
70 Self::Small => (24., 4., 8., 14.),
71 Self::Medium => (24., 12., 16., 24.),
72 Self::Large => (32., 16., 32., 48.),
73 Self::ExtraLarge => (40., 32., 48., 72.),
74 };
75 metrics.icon = icon;
76 let space = match width {
77 IconButtonWidth::Narrow => narrow,
78 IconButtonWidth::Uniform => uniform,
79 IconButtonWidth::Wide => wide,
80 };
81 (metrics, icon + 2. * space)
82 }
83}
84
85#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
86pub enum ButtonShape {
87 #[default]
88 Round,
89 Square,
90}
91
92impl ButtonShape {
93 fn corner(self, square: CornerRole, invert: bool) -> CornerRole {
94 if (self == Self::Round) != invert {
95 CornerRole::Full
96 } else {
97 square
98 }
99 }
100}
101
102#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
103pub enum ButtonVariant {
104 Elevated,
105 #[default]
106 Filled,
107 FilledTonal,
108 Outlined,
109 Text,
110}
111
112#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
113pub enum ToggleButtonVariant {
114 Elevated,
115 #[default]
116 Filled,
117 FilledTonal,
118 Outlined,
119}
120
121#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
122pub enum IconButtonVariant {
123 #[default]
124 Standard,
125 Filled,
126 FilledTonal,
127 Outlined,
128}
129
130#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
131pub enum IconButtonWidth {
132 Narrow,
133 #[default]
134 Uniform,
135 Wide,
136}
137
138struct Common {
139 id: ElementId,
140 label: SharedString,
141 disabled: bool,
142 focus: Option<FocusHandle>,
143 size: ControlSize,
144 shape: ButtonShape,
145}
146
147impl Common {
148 fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
149 Self {
150 id: id.into(),
151 label: label.into(),
152 disabled: false,
153 focus: None,
154 size: ControlSize::default(),
155 shape: ButtonShape::default(),
156 }
157 }
158
159 fn render(
160 self,
161 content: AnyElement,
162 style: ActionStyle,
163 action: Action,
164 window: &mut Window,
165 cx: &mut App,
166 ) -> AnyElement {
167 self.render_scaled(content, style, action, 1., window, cx)
168 }
169
170 fn render_scaled(
171 self,
172 content: AnyElement,
173 style: ActionStyle,
174 action: Action,
175 scale: f32,
176 window: &mut Window,
177 cx: &mut App,
178 ) -> AnyElement {
179 ActionControl {
180 id: self.id,
181 label: self.label,
182 focus: self.focus,
183 disabled: self.disabled,
184 action,
185 style,
186 content,
187 }
188 .render_scaled(scale, window, cx)
189 }
190}
191
192macro_rules! common_builders {
194 () => {
195 pub fn size(mut self, size: ControlSize) -> Self {
197 self.common.size = size;
198 self
199 }
200
201 pub fn shape(mut self, shape: ButtonShape) -> Self {
203 self.common.shape = shape;
204 self
205 }
206
207 pub fn disabled(mut self, disabled: bool) -> Self {
209 self.common.disabled = disabled;
210 self
211 }
212
213 pub fn track_focus(mut self, focus: &FocusHandle) -> Self {
215 self.common.focus = Some(focus.clone());
216 self
217 }
218 };
219}
220
221#[derive(Default)]
222struct Slots {
223 leading: Option<AnyElement>,
224 trailing: Option<AnyElement>,
225}
226
227impl Slots {
228 fn render(
229 self,
230 label: SharedString,
231 metrics: Metrics,
232 scale: f32,
233 window: &Window,
234 tint: gpui::Hsla,
235 cx: &App,
236 ) -> AnyElement {
237 let direction = window_material_theme(window, cx).layout.direction;
238 let typography = metrics.label.style(TypographyEmphasis::Baseline);
239 let mut row = div()
240 .flex()
241 .items_center()
242 .justify_center()
243 .min_w_0()
244 .max_w_full()
245 .gap(px(metrics.gap * scale))
246 .px(px(metrics
247 .padding
248 .min(f32::from(window.viewport_size().width) * 0.125)
249 * scale))
250 .py(px(4. * scale))
251 .text_size(px(typography.size * scale))
252 .line_height(px(typography.line_height * scale));
253 if direction == Direction::Rtl {
254 row = row.flex_row_reverse();
255 }
256 row.children(
257 self.leading
258 .map(|icon| icon_slot(icon, metrics.icon * scale)),
259 )
260 .child(crate::text::single_line(
261 label,
262 typography,
263 scale,
264 tint,
265 (None, 1., 1.),
266 window,
267 cx,
268 ))
269 .children(
270 self.trailing
271 .map(|icon| icon_slot(icon, metrics.icon * scale)),
272 )
273 .into_any_element()
274 }
275}
276
277macro_rules! slot_builders {
278 () => {
279 pub fn leading_icon(mut self, icon: impl IntoElement) -> Self {
281 self.slots.leading = Some(icon.into_any_element());
282 self
283 }
284
285 pub fn trailing_icon(mut self, icon: impl IntoElement) -> Self {
287 self.slots.trailing = Some(icon.into_any_element());
288 self
289 }
290 };
291}
292
293#[derive(IntoElement)]
296pub struct Button {
297 common: Common,
298 variant: ButtonVariant,
299 slots: Slots,
300 on_activate: Option<Activate>,
301 scale: f32,
302}
303
304impl Button {
305 pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
306 Self {
307 common: Common::new(id, label),
308 variant: ButtonVariant::default(),
309 slots: Slots::default(),
310 on_activate: None,
311 scale: 1.,
312 }
313 }
314
315 common_builders!();
316 slot_builders!();
317
318 pub fn variant(mut self, variant: ButtonVariant) -> Self {
319 self.variant = variant;
320 self
321 }
322
323 pub(crate) fn scale(mut self, scale: f32) -> Self {
324 self.scale = scale;
325 self
326 }
327
328 pub fn on_activate(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
329 self.on_activate = Some(Rc::new(handler));
330 self
331 }
332}
333
334impl RenderOnce for Button {
335 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
336 let metrics = self.common.size.metrics();
337 let appearance = button_appearance(self.variant, None);
338 let style = appearance.style(
339 metrics,
340 self.common.shape.corner(metrics.square, false),
341 58.,
342 );
343 let content = self.slots.render(
344 self.common.label.clone(),
345 metrics,
346 self.scale,
347 window,
348 style.foreground(self.common.disabled, window, cx),
349 cx,
350 );
351 self.common.render_scaled(
352 content,
353 style,
354 Action::Invoke(self.on_activate),
355 self.scale,
356 window,
357 cx,
358 )
359 }
360}
361
362#[derive(IntoElement)]
365pub struct ToggleButton {
366 common: Common,
367 variant: ToggleButtonVariant,
368 selected: bool,
369 slots: Slots,
370 on_change: Option<Change>,
371}
372
373impl ToggleButton {
374 pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>, selected: bool) -> Self {
375 Self {
376 common: Common::new(id, label),
377 variant: ToggleButtonVariant::default(),
378 selected,
379 slots: Slots::default(),
380 on_change: None,
381 }
382 }
383
384 common_builders!();
385 slot_builders!();
386
387 pub fn variant(mut self, variant: ToggleButtonVariant) -> Self {
388 self.variant = variant;
389 self
390 }
391
392 pub fn on_change(mut self, handler: impl Fn(bool, &mut Window, &mut App) + 'static) -> Self {
393 self.on_change = Some(Rc::new(handler));
394 self
395 }
396}
397
398impl RenderOnce for ToggleButton {
399 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
400 let metrics = self.common.size.metrics();
401 let variant = match self.variant {
402 ToggleButtonVariant::Elevated => ButtonVariant::Elevated,
403 ToggleButtonVariant::Filled => ButtonVariant::Filled,
404 ToggleButtonVariant::FilledTonal => ButtonVariant::FilledTonal,
405 ToggleButtonVariant::Outlined => ButtonVariant::Outlined,
406 };
407 let mut style = button_appearance(variant, Some(self.selected)).style(
408 metrics,
409 self.common.shape.corner(metrics.square, false),
410 0.,
411 );
412 style.selected_corner = Some(self.common.shape.corner(metrics.square, true));
413 let content = self.slots.render(
414 self.common.label.clone(),
415 metrics,
416 1.,
417 window,
418 style.foreground(self.common.disabled, window, cx),
419 cx,
420 );
421 self.common.render(
422 content,
423 style,
424 Action::Toggle {
425 selected: self.selected,
426 on_change: self.on_change,
427 },
428 window,
429 cx,
430 )
431 }
432}
433
434#[derive(IntoElement)]
436pub struct IconButton {
437 common: Common,
438 icon: AnyElement,
439 variant: IconButtonVariant,
440 width: IconButtonWidth,
441 on_activate: Option<Activate>,
442}
443
444impl IconButton {
445 pub fn new(
446 id: impl Into<ElementId>,
447 name: impl Into<SharedString>,
448 icon: impl IntoElement,
449 ) -> Self {
450 Self {
451 common: Common::new(id, name),
452 icon: icon.into_any_element(),
453 variant: IconButtonVariant::default(),
454 width: IconButtonWidth::default(),
455 on_activate: None,
456 }
457 }
458
459 common_builders!();
460
461 pub fn variant(mut self, variant: IconButtonVariant) -> Self {
462 self.variant = variant;
463 self
464 }
465
466 pub fn width(mut self, width: IconButtonWidth) -> Self {
467 self.width = width;
468 self
469 }
470
471 pub fn on_activate(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
472 self.on_activate = Some(Rc::new(handler));
473 self
474 }
475}
476
477impl RenderOnce for IconButton {
478 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
479 let (metrics, width) = self.common.size.icon_metrics(self.width);
480 let mut style = icon_appearance(self.variant, None).style(
481 metrics,
482 self.common.shape.corner(metrics.square, false),
483 width,
484 );
485 style.corner_motion = (MotionKind::Effects, MotionSpeed::Default);
486 self.common.render(
487 icon_slot(self.icon, metrics.icon).into_any_element(),
488 style,
489 Action::Invoke(self.on_activate),
490 window,
491 cx,
492 )
493 }
494}
495
496#[derive(IntoElement)]
499pub struct IconToggleButton {
500 common: Common,
501 icon: AnyElement,
502 selected_icon: Option<AnyElement>,
503 selected: bool,
504 variant: IconButtonVariant,
505 width: IconButtonWidth,
506 on_change: Option<Change>,
507}
508
509impl IconToggleButton {
510 pub fn new(
511 id: impl Into<ElementId>,
512 name: impl Into<SharedString>,
513 icon: impl IntoElement,
514 selected: bool,
515 ) -> Self {
516 Self {
517 common: Common::new(id, name),
518 icon: icon.into_any_element(),
519 selected_icon: None,
520 selected,
521 variant: IconButtonVariant::default(),
522 width: IconButtonWidth::default(),
523 on_change: None,
524 }
525 }
526
527 common_builders!();
528
529 pub fn selected_icon(mut self, icon: impl IntoElement) -> Self {
530 self.selected_icon = Some(icon.into_any_element());
531 self
532 }
533
534 pub fn variant(mut self, variant: IconButtonVariant) -> Self {
535 self.variant = variant;
536 self
537 }
538
539 pub fn width(mut self, width: IconButtonWidth) -> Self {
540 self.width = width;
541 self
542 }
543
544 pub fn on_change(mut self, handler: impl Fn(bool, &mut Window, &mut App) + 'static) -> Self {
545 self.on_change = Some(Rc::new(handler));
546 self
547 }
548}
549
550impl RenderOnce for IconToggleButton {
551 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
552 let (metrics, width) = self.common.size.icon_metrics(self.width);
553 let mut style = icon_appearance(self.variant, Some(self.selected)).style(
554 metrics,
555 self.common.shape.corner(metrics.square, false),
556 width,
557 );
558 style.selected_corner = Some(self.common.shape.corner(metrics.square, true));
559 style.corner_motion = (MotionKind::Effects, MotionSpeed::Default);
560 let icon = if self.selected {
561 self.selected_icon.unwrap_or(self.icon)
562 } else {
563 self.icon
564 };
565 self.common.render(
566 icon_slot(icon, metrics.icon).into_any_element(),
567 style,
568 Action::Toggle {
569 selected: self.selected,
570 on_change: self.on_change,
571 },
572 window,
573 cx,
574 )
575 }
576}
577
578#[doc(hidden)]
580pub mod composition {
581 use super::*;
582
583 #[derive(Clone, Copy)]
585 pub enum Segment {
586 Only,
587 Leading,
588 Middle,
589 Trailing,
590 SplitLeading,
591 SplitTrailing,
592 }
593
594 #[derive(IntoElement)]
596 pub struct ButtonPart {
597 common: Common,
598 variant: ButtonVariant,
599 selected: Option<bool>,
600 on_activate: Option<Activate>,
601 segment: Option<Segment>,
602 icon: Option<AnyElement>,
603 width: Pixels,
604 tab_stop: bool,
605 semantic_only: bool,
606 }
607
608 impl ButtonPart {
609 pub fn new(
610 id: impl Into<ElementId>,
611 label: impl Into<SharedString>,
612 width: Pixels,
613 ) -> Self {
614 Self {
615 common: Common::new(id, label),
616 variant: ButtonVariant::default(),
617 selected: None,
618 on_activate: None,
619 segment: None,
620 icon: None,
621 width,
622 tab_stop: true,
623 semantic_only: false,
624 }
625 }
626
627 common_builders!();
628
629 pub fn variant(mut self, variant: ButtonVariant) -> Self {
630 self.variant = variant;
631 self
632 }
633
634 pub fn segment(mut self, segment: Option<Segment>) -> Self {
635 self.segment = segment;
636 self
637 }
638
639 pub fn selected(mut self, selected: Option<bool>) -> Self {
640 self.selected = selected;
641 self
642 }
643
644 pub fn icon(mut self, icon: impl IntoElement) -> Self {
645 self.icon = Some(icon.into_any_element());
646 self
647 }
648
649 pub fn tab_stop(mut self, tab_stop: bool) -> Self {
650 self.tab_stop = tab_stop;
651 self
652 }
653
654 pub fn on_activate(mut self, callback: impl Fn(&mut Window, &mut App) + 'static) -> Self {
655 self.on_activate = Some(Rc::new(callback));
656 self
657 }
658
659 pub fn on_accessible_activate(
660 mut self,
661 callback: impl Fn(&mut Window, &mut App) + 'static,
662 ) -> Self {
663 self.on_activate = Some(Rc::new(callback));
664 self.semantic_only = true;
665 self
666 }
667 }
668
669 impl RenderOnce for ButtonPart {
670 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
671 let mut metrics = self.common.size.metrics();
672 let segment = self.segment.map(|segment| {
673 use CornerRole::*;
674 let (inner, pressed) = match segment {
675 Segment::SplitLeading | Segment::SplitTrailing => match self.common.size {
676 ControlSize::ExtraSmall => (ExtraSmall, Small),
677 ControlSize::Small | ControlSize::Medium => (ExtraSmall, Medium),
678 ControlSize::Large => (Small, LargeIncreased),
679 ControlSize::ExtraLarge => (Medium, LargeIncreased),
680 },
681 _ => (Small, ExtraSmall),
682 };
683 SegmentShape {
684 leading: matches!(
685 segment,
686 Segment::Only | Segment::Leading | Segment::SplitLeading
687 ),
688 trailing: matches!(
689 segment,
690 Segment::Only | Segment::Trailing | Segment::SplitTrailing
691 ),
692 inner,
693 pressed,
694 }
695 });
696 let mut style =
697 button_appearance(self.variant, self.selected).style(metrics, CornerRole::Full, 0.);
698 if self.selected.is_some() {
699 style.selected_corner = Some(metrics.square);
700 }
701 if matches!(self.segment, Some(Segment::SplitTrailing)) {
702 metrics.icon = split_trailing_size(self.common.size).0;
703 }
704 let content = if let Some(icon) = self.icon {
705 icon_slot(icon, metrics.icon).into_any_element()
706 } else {
707 let tint = style.foreground(self.common.disabled, window, cx);
708 let typography = metrics.label.style(TypographyEmphasis::Baseline);
709 let padding = metrics
710 .padding
711 .min((f32::from(self.width) - 16.).max(0.) / 2.);
712 let label_width = (self.width - px(2. * padding)).max(px(0.));
713 div()
714 .flex()
715 .min_w_0()
716 .max_w_full()
717 .w(self.width)
718 .justify_center()
719 .px(px(padding))
720 .child(crate::text::single_line(
721 self.common.label.clone(),
722 typography,
723 1.,
724 tint,
725 (Some(label_width), 1., 1.),
726 window,
727 cx,
728 ))
729 .into_any_element()
730 };
731 let action = if let Some(selected) = self.selected {
732 Action::Toggle {
733 selected,
734 on_change: self.on_activate.map(|callback| {
735 Rc::new(move |_, window: &mut Window, cx: &mut App| callback(window, cx))
736 as Change
737 }),
738 }
739 } else {
740 Action::Invoke(self.on_activate)
741 };
742 ActionControl {
743 id: self.common.id,
744 label: self.common.label,
745 focus: self.common.focus,
746 disabled: self.common.disabled,
747 action,
748 style,
749 content,
750 }
751 .render_layout(
752 1.,
753 ActionLayout {
754 segment,
755 width: Some(self.width),
756 tab_stop: self.tab_stop,
757 semantic_only: self.semantic_only,
758 },
759 window,
760 cx,
761 )
762 }
763 }
764
765 pub fn label_width(
766 size: ControlSize,
767 label: SharedString,
768 window: &Window,
769 cx: &App,
770 ) -> Pixels {
771 let metrics = size.metrics();
772 let theme = window_material_theme(window, cx);
773 let style = metrics.label.style(TypographyEmphasis::Baseline);
774 let width = gpui_material_foundation::shape_text(
775 if label.contains(['\r', '\n']) {
776 label.replace(['\r', '\n'], " ").into()
777 } else {
778 label
779 },
780 style,
781 &theme.typography,
782 1.,
783 gpui::transparent_black(),
784 window,
785 cx,
786 )
787 .map_or(px(0.), |text| text.line().width.ceil());
788 (width + px(metrics.padding * 2.)).max(px(theme.layout.density.interactive_size(1.)))
789 }
790
791 pub fn split_trailing_size(size: ControlSize) -> (f32, f32) {
792 match size {
794 ControlSize::ExtraSmall | ControlSize::Small => (22., 48.),
795 ControlSize::Medium => (26., 56.),
796 ControlSize::Large => (38., 96.),
797 ControlSize::ExtraLarge => (50., 136.),
798 }
799 }
800}
801
802struct Appearance {
803 background: Option<ColorRole>,
804 foreground: ColorRole,
805 outlined: bool,
806 disabled: DisabledStyle,
807 elevation: Elevation,
808 hover_elevation: Elevation,
809}
810
811impl Appearance {
812 fn style(self, metrics: Metrics, corner: CornerRole, min_width: f32) -> ActionStyle {
813 ActionStyle {
814 background: self.background,
815 background_color: None,
816 foreground: self.foreground,
817 disabled: self.disabled,
818 height: metrics.height,
819 min_width,
820 corner,
821 pressed_corner: metrics.pressed,
822 selected_corner: None,
823 corner_motion: (MotionKind::Spatial, MotionSpeed::Fast),
824 outline: if self.outlined { metrics.outline } else { 0. },
825 elevation: self.elevation,
826 hover_elevation: self.hover_elevation,
827 }
828 }
829}
830
831fn button_appearance(variant: ButtonVariant, selected: Option<bool>) -> Appearance {
832 use ColorRole::*;
833 use Elevation::*;
834 let (background, foreground, outlined, elevation, hover_elevation) = match (variant, selected) {
835 (ButtonVariant::Elevated, Some(true)) => (Some(Primary), OnPrimary, false, Level1, Level2),
836 (ButtonVariant::Elevated, _) => (Some(SurfaceContainerLow), Primary, false, Level1, Level2),
837 (ButtonVariant::Filled, Some(false)) => (
838 Some(SurfaceContainer),
839 OnSurfaceVariant,
840 false,
841 Level0,
842 Level1,
843 ),
844 (ButtonVariant::Filled, _) => (Some(Primary), OnPrimary, false, Level0, Level1),
845 (ButtonVariant::FilledTonal, Some(true)) => {
846 (Some(Secondary), OnSecondary, false, Level0, Level1)
847 }
848 (ButtonVariant::FilledTonal, _) => (
849 Some(SecondaryContainer),
850 OnSecondaryContainer,
851 false,
852 Level0,
853 Level1,
854 ),
855 (ButtonVariant::Outlined, Some(true)) => (
856 Some(InverseSurface),
857 InverseOnSurface,
858 false,
859 Level0,
860 Level0,
861 ),
862 (ButtonVariant::Outlined, _) => (None, OnSurfaceVariant, true, Level0, Level0),
863 (ButtonVariant::Text, _) => (None, Primary, false, Level0, Level0),
865 };
866 Appearance {
867 background,
868 foreground,
869 outlined,
870 disabled: DisabledStyle {
871 foreground: if variant == ButtonVariant::FilledTonal && selected.is_none() {
874 OnSurface
875 } else {
876 OnSurfaceVariant
877 },
878 background: match (variant, selected) {
879 (ButtonVariant::Outlined, Some(_)) => Some((OutlineVariant, 0.10)),
880 (ButtonVariant::Outlined | ButtonVariant::Text, _) => None,
881 (ButtonVariant::FilledTonal, None) => Some((OnSurface, 0.12)),
882 _ => Some((OnSurface, 0.10)),
883 },
884 outline: (OutlineVariant, 0.10),
885 },
886 elevation,
887 hover_elevation,
888 }
889}
890
891fn icon_appearance(variant: IconButtonVariant, selected: Option<bool>) -> Appearance {
892 let mut appearance = match variant {
893 IconButtonVariant::Standard => button_appearance(ButtonVariant::Text, selected),
894 IconButtonVariant::Filled => button_appearance(ButtonVariant::Filled, selected),
895 IconButtonVariant::FilledTonal => button_appearance(ButtonVariant::FilledTonal, selected),
896 IconButtonVariant::Outlined => button_appearance(ButtonVariant::Outlined, selected),
897 };
898 if variant == IconButtonVariant::Standard {
899 appearance.foreground = if selected == Some(true) {
900 ColorRole::Primary
901 } else {
902 ColorRole::OnSurfaceVariant
903 };
904 }
905 appearance.disabled = DisabledStyle {
906 foreground: ColorRole::OnSurface,
907 background: match variant {
909 IconButtonVariant::Filled | IconButtonVariant::FilledTonal => {
910 Some((ColorRole::OnSurface, 0.10))
911 }
912 IconButtonVariant::Standard | IconButtonVariant::Outlined => None,
913 },
914 outline: (ColorRole::OutlineVariant, 0.38),
915 };
916 appearance.hover_elevation = Elevation::Level0;
917 appearance
918}
919
920#[cfg(test)]
921mod tests {
922 use super::*;
923
924 #[test]
925 fn text_action_and_standard_icon_keep_distinct_compose_colors() {
926 assert_eq!(
929 button_appearance(ButtonVariant::Text, None).foreground,
930 ColorRole::Primary
931 );
932 for selected in [None, Some(false)] {
933 assert_eq!(
934 icon_appearance(IconButtonVariant::Standard, selected).foreground,
935 ColorRole::OnSurfaceVariant
936 );
937 }
938 assert_eq!(
939 icon_appearance(IconButtonVariant::Standard, Some(true)).foreground,
940 ColorRole::Primary
941 );
942 }
943
944 #[test]
945 fn disabled_tokens_are_not_inferred_from_enabled_selection_colors() {
946 use ColorRole::*;
947 for selected in [Some(false), Some(true)] {
948 let outlined = button_appearance(ButtonVariant::Outlined, selected);
949 assert_eq!(outlined.disabled.background, Some((OutlineVariant, 0.10)));
950 assert_eq!(outlined.disabled.foreground, OnSurfaceVariant);
951 assert_eq!(outlined.disabled.outline, (OutlineVariant, 0.10));
952 let icon = icon_appearance(IconButtonVariant::Outlined, selected);
953 assert_eq!(icon.disabled.background, None);
954 assert_eq!(icon.disabled.foreground, OnSurface);
955 assert_eq!(icon.disabled.outline, (OutlineVariant, 0.38));
956 }
957 assert_eq!(
958 button_appearance(ButtonVariant::Outlined, None)
959 .disabled
960 .background,
961 None
962 );
963 assert_eq!(
964 button_appearance(ButtonVariant::Filled, None)
965 .disabled
966 .background,
967 Some((OnSurface, 0.10))
968 );
969 assert_eq!(
972 button_appearance(ButtonVariant::FilledTonal, None)
973 .disabled
974 .background,
975 Some((OnSurface, 0.12))
976 );
977 assert_eq!(
978 button_appearance(ButtonVariant::FilledTonal, Some(false))
979 .disabled
980 .background,
981 Some((OnSurface, 0.10))
982 );
983 assert_eq!(
984 icon_appearance(IconButtonVariant::FilledTonal, None)
985 .disabled
986 .background,
987 Some((OnSurface, 0.10))
988 );
989 }
990}