Skip to main content

gpui_material_components/
button_style.rs

1use gpui::{Hsla, Pixels, px};
2use gpui_material_foundation::InteractionState;
3use gpui_material_theme::{CornerShape, Elevation};
4
5/// Material Expressive sizes, from extra small to extra large.
6#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
7pub enum ControlSize {
8    /// Extra-small control tier (32 dp height).
9    ExtraSmall,
10    /// Small control tier (40 dp height, default).
11    #[default]
12    Small,
13    /// Medium control tier (56 dp height).
14    Medium,
15    /// Large control tier (96 dp height).
16    Large,
17    /// Extra-large control tier (136 dp height).
18    ExtraLarge,
19}
20
21/// Button corner overrides in logical top-start, top-end, bottom-end,
22/// bottom-start order. Unspecified states keep their size and shape presets.
23#[derive(Clone, Copy, Debug, Default, PartialEq)]
24pub struct ButtonShapes {
25    /// Corner shape while resting and unselected.
26    pub resting: Option<CornerShape>,
27    /// Corner shape while pressed.
28    pub pressed: Option<CornerShape>,
29    /// Corner shape while checked/selected on toggle buttons.
30    pub checked: Option<CornerShape>,
31}
32
33/// Per-instance pigments. Each omitted field keeps the variant's color in
34/// that state. Checked colors apply to enabled toggles.
35#[derive(Clone, Copy, Debug, Default, PartialEq)]
36pub struct ButtonColors {
37    /// Enabled container background color.
38    pub container: Option<Hsla>,
39    /// Enabled foreground label and icon color.
40    pub content: Option<Hsla>,
41    /// Disabled container background color.
42    pub disabled_container: Option<Hsla>,
43    /// Disabled foreground label and icon color.
44    pub disabled_content: Option<Hsla>,
45    /// Enabled container background color when a toggle button is selected.
46    pub checked_container: Option<Hsla>,
47    /// Enabled foreground label and icon color when a toggle button is selected.
48    pub checked_content: Option<Hsla>,
49}
50
51impl ButtonColors {
52    pub(crate) fn container(self, disabled: bool, checked: bool) -> Option<Hsla> {
53        match (disabled, checked) {
54            (true, _) => self.disabled_container,
55            (false, true) => self.checked_container,
56            (false, false) => self.container,
57        }
58    }
59
60    pub(crate) fn content(self, disabled: bool, checked: bool) -> Option<Hsla> {
61        match (disabled, checked) {
62            (true, _) => self.disabled_content,
63            (false, true) => self.checked_content,
64            (false, false) => self.content,
65        }
66    }
67}
68
69/// A stroke for any button variant. A zero width removes its outline.
70#[derive(Clone, Copy, Debug, PartialEq)]
71pub struct ButtonBorder {
72    /// Stroke thickness in logical pixels.
73    pub width: Pixels,
74    /// Stroke color.
75    pub color: Hsla,
76}
77
78/// Insets around the label or custom content, including leading and trailing icons.
79#[derive(Clone, Copy, Debug, Default, PartialEq)]
80pub struct ButtonPadding {
81    /// Top padding in logical pixels.
82    pub top: Pixels,
83    /// Trailing-edge padding (right in LTR, left in RTL).
84    pub end: Pixels,
85    /// Bottom padding in logical pixels.
86    pub bottom: Pixels,
87    /// Leading-edge padding (left in LTR, right in RTL).
88    pub start: Pixels,
89}
90
91/// Optional Material elevation levels. Disabled takes precedence, followed by
92/// pressed, keyboard focus, hover and rest.
93#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
94pub struct ButtonElevations {
95    /// Elevation level while resting.
96    pub resting: Option<Elevation>,
97    /// Elevation level while pressed.
98    pub pressed: Option<Elevation>,
99    /// Elevation level while keyboard focus is visible.
100    pub focused: Option<Elevation>,
101    /// Elevation level while hovered.
102    pub hovered: Option<Elevation>,
103    /// Elevation level while disabled.
104    pub disabled: Option<Elevation>,
105}
106
107impl ButtonElevations {
108    pub(crate) fn for_state(self, state: InteractionState) -> Option<Elevation> {
109        if state.disabled {
110            self.disabled
111        } else if state.pressed {
112            self.pressed
113        } else if state.focus_visible {
114            self.focused
115        } else if state.hovered {
116            self.hovered
117        } else {
118            self.resting
119        }
120    }
121}
122
123#[derive(Clone, Copy, Default)]
124pub(crate) struct ButtonStyle {
125    pub(crate) colors: ButtonColors,
126    pub(crate) shapes: ButtonShapes,
127    pub(crate) border: Option<ButtonBorder>,
128    pub(crate) elevations: ButtonElevations,
129}
130
131pub(crate) fn finite_length(length: Pixels) -> Pixels {
132    if f32::from(length).is_finite() {
133        length.max(px(0.))
134    } else {
135        px(0.)
136    }
137}
138
139#[cfg(test)]
140mod tests {
141    use super::*;
142
143    #[test]
144    fn elevation_overrides_are_independent_and_follow_state_priority() {
145        use Elevation::*;
146        let levels = ButtonElevations {
147            resting: Some(Level1),
148            hovered: Some(Level2),
149            focused: Some(Level3),
150            pressed: Some(Level4),
151            disabled: Some(Level5),
152        };
153        let mut state = InteractionState::default();
154        assert_eq!(levels.for_state(state), Some(Level1));
155        state.hovered = true;
156        assert_eq!(levels.for_state(state), Some(Level2));
157        state.focus_visible = true;
158        assert_eq!(levels.for_state(state), Some(Level3));
159        state.pressed = true;
160        assert_eq!(levels.for_state(state), Some(Level4));
161        state.disabled = true;
162        assert_eq!(levels.for_state(state), Some(Level5));
163        let resting_only = ButtonElevations {
164            resting: Some(Level2),
165            ..Default::default()
166        };
167        assert_eq!(resting_only.for_state(state), None);
168    }
169}