Skip to main content

gpui_material_components/button/
types.rs

1use gpui::{AnyElement, App, ElementId, Entity, FocusHandle, SharedString, Window};
2use gpui_material_foundation::InteractionSource;
3use gpui_material_theme::{CornerRole, TypeRole};
4
5use crate::SemanticTone;
6use crate::action::{Action, ActionControl, ActionLayout, ActionStyle};
7use crate::control_style::{ButtonStyle, ControlSize};
8
9#[derive(Clone, Copy)]
10pub(super) struct Metrics {
11    pub(super) height: f32,
12    pub(super) icon: f32,
13    pub(super) gap: f32,
14    pub(super) padding: f32,
15    pub(super) outline: f32,
16    pub(super) square: CornerRole,
17    pub(super) pressed: CornerRole,
18    pub(super) label: TypeRole,
19}
20
21impl ControlSize {
22    // Button{XSmall,Small,Medium,Large,XLarge}Tokens in Compose Material 3 1.5.0-rc01.
23    pub(super) fn metrics(self) -> Metrics {
24        use CornerRole::*;
25        use TypeRole::*;
26        let (height, icon, gap, padding, outline, square, pressed, label) = match self {
27            Self::ExtraSmall => (32., 20., 8., 16., 1., Medium, Small, LabelLarge),
28            Self::Small => (40., 20., 8., 16., 1., Medium, Small, LabelLarge),
29            Self::Medium => (56., 24., 8., 24., 1., Large, Medium, TitleMedium),
30            Self::Large => (96., 32., 12., 48., 2., ExtraLarge, Large, HeadlineSmall),
31            Self::ExtraLarge => (136., 40., 16., 64., 3., ExtraLarge, Large, HeadlineLarge),
32        };
33        Metrics {
34            height,
35            icon,
36            gap,
37            padding,
38            outline,
39            square,
40            pressed,
41            label,
42        }
43    }
44
45    pub(super) fn icon_metrics(self, width: IconButtonWidth) -> (Metrics, f32) {
46        // Expressive icon buttons have their own icon and horizontal space.
47        let mut metrics = self.metrics();
48        let (icon, narrow, uniform, wide) = match self {
49            Self::ExtraSmall => (20., 4., 6., 10.),
50            Self::Small => (24., 4., 8., 14.),
51            Self::Medium => (24., 12., 16., 24.),
52            Self::Large => (32., 16., 32., 48.),
53            Self::ExtraLarge => (40., 32., 48., 72.),
54        };
55        metrics.icon = icon;
56        let space = match width {
57            IconButtonWidth::Narrow => narrow,
58            IconButtonWidth::Uniform => uniform,
59            IconButtonWidth::Wide => wide,
60        };
61        (metrics, icon + 2. * space)
62    }
63}
64
65/// Resting corner shape preset for Material buttons.
66#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
67pub enum ButtonShape {
68    /// Pill-shaped full corner radius (default).
69    #[default]
70    Round,
71    /// Size-dependent rounded-square corner radius.
72    Square,
73}
74
75impl ButtonShape {
76    pub(super) fn corner(self, square: CornerRole, invert: bool) -> CornerRole {
77        if (self == Self::Round) != invert {
78            CornerRole::Full
79        } else {
80            square
81        }
82    }
83}
84
85/// Visual emphasis style for [`super::Button`].
86#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
87pub enum ButtonVariant {
88    /// Raised low-emphasis surface with drop shadow.
89    Elevated,
90    /// High-emphasis solid filled container (default).
91    #[default]
92    Filled,
93    /// Medium-emphasis tonal container fill.
94    FilledTonal,
95    /// Medium-emphasis outlined container with transparent fill.
96    Outlined,
97    /// Low-emphasis borderless text action.
98    Text,
99}
100
101/// Visual emphasis style for [`super::ToggleButton`].
102#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
103pub enum ToggleButtonVariant {
104    /// Raised surface with drop shadow.
105    Elevated,
106    /// High-emphasis solid filled toggle (default).
107    #[default]
108    Filled,
109    /// Medium-emphasis tonal container toggle.
110    FilledTonal,
111    /// Medium-emphasis outlined toggle.
112    Outlined,
113}
114
115/// Visual emphasis style for [`super::IconButton`] and [`super::IconToggleButton`].
116#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
117pub enum IconButtonVariant {
118    /// Borderless, transparent icon button (default).
119    #[default]
120    Standard,
121    /// High-emphasis solid filled icon button.
122    Filled,
123    /// Medium-emphasis tonal container icon button.
124    FilledTonal,
125    /// Medium-emphasis outlined icon button.
126    Outlined,
127}
128
129/// Horizontal width preset for Expressive icon buttons.
130#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
131pub enum IconButtonWidth {
132    /// Compact horizontal padding.
133    Narrow,
134    /// Balanced square-like horizontal padding (default).
135    #[default]
136    Uniform,
137    /// Expanded pill-like horizontal padding.
138    Wide,
139}
140
141pub(super) struct Common {
142    pub(super) id: ElementId,
143    pub(super) label: SharedString,
144    pub(super) disabled: bool,
145    pub(super) loading: bool,
146    pub(super) focus: Option<FocusHandle>,
147    pub(super) interaction_source: Option<Entity<InteractionSource>>,
148    pub(super) appearance: ButtonStyle,
149    pub(super) size: ControlSize,
150    pub(super) shape: ButtonShape,
151    pub(super) tone: SemanticTone,
152}
153
154impl Common {
155    pub(super) 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: SemanticTone::default(),
167        }
168    }
169
170    pub(super) 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    pub(super) 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}