Skip to main content

gpui_material_components/
control_style.rs

1use gpui::{Hsla, Pixels, Rgba, 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/// Control 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 ControlShapes {
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 controls and chips.
30    pub checked: Option<CornerShape>,
31}
32
33/// Per-instance button 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 control variant. A zero width removes its outline.
70#[derive(Clone, Copy, Debug, PartialEq)]
71pub struct ControlBorder {
72    /// Stroke thickness in logical pixels.
73    pub width: Pixels,
74    /// Stroke color.
75    pub color: Hsla,
76}
77
78/// Optional Material elevation levels. Disabled takes precedence, followed by
79/// dragged, pressed, keyboard focus, hover, and rest.
80#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
81pub struct ControlElevations {
82    /// Elevation level while resting.
83    pub resting: Option<Elevation>,
84    /// Elevation level while pressed.
85    pub pressed: Option<Elevation>,
86    /// Elevation level while keyboard focus is visible.
87    pub focused: Option<Elevation>,
88    /// Elevation level while hovered.
89    pub hovered: Option<Elevation>,
90    /// Elevation level while dragged.
91    pub dragged: Option<Elevation>,
92    /// Elevation level while disabled.
93    pub disabled: Option<Elevation>,
94}
95
96impl ControlElevations {
97    pub(crate) fn for_state(self, state: InteractionState) -> Option<Elevation> {
98        if state.disabled {
99            self.disabled
100        } else if state.dragged {
101            self.dragged
102        } else if state.pressed {
103            self.pressed
104        } else if state.focus_visible {
105            self.focused
106        } else if state.hovered {
107            self.hovered
108        } else {
109            self.resting
110        }
111    }
112}
113
114#[derive(Clone, Copy, Default)]
115pub(crate) struct ButtonStyle {
116    pub(crate) colors: ButtonColors,
117    pub(crate) shapes: ControlShapes,
118    pub(crate) border: Option<ControlBorder>,
119    pub(crate) elevations: ControlElevations,
120}
121
122pub(crate) fn finite_length(length: Pixels) -> Pixels {
123    if f32::from(length).is_finite() {
124        length.max(px(0.))
125    } else {
126        px(0.)
127    }
128}
129
130pub(crate) fn mix_color(from: Hsla, to: Hsla, fraction: f32) -> Hsla {
131    if fraction <= 0. {
132        return from;
133    }
134    if fraction >= 1. {
135        return to;
136    }
137    let from = Rgba::from(from);
138    let to = Rgba::from(to);
139    let mix = |a, b| a + (b - a) * fraction;
140    Rgba {
141        r: mix(from.r, to.r),
142        g: mix(from.g, to.g),
143        b: mix(from.b, to.b),
144        a: mix(from.a, to.a),
145    }
146    .into()
147}
148
149#[cfg(test)]
150mod tests {
151    use super::*;
152
153    #[test]
154    fn elevation_overrides_are_independent_and_follow_state_priority() {
155        use Elevation::*;
156        let levels = ControlElevations {
157            resting: Some(Level1),
158            hovered: Some(Level2),
159            focused: Some(Level3),
160            pressed: Some(Level4),
161            dragged: Some(Level5),
162            disabled: Some(Level0),
163        };
164        let mut state = InteractionState::default();
165        assert_eq!(levels.for_state(state), Some(Level1));
166        state.hovered = true;
167        assert_eq!(levels.for_state(state), Some(Level2));
168        state.focus_visible = true;
169        assert_eq!(levels.for_state(state), Some(Level3));
170        state.pressed = true;
171        assert_eq!(levels.for_state(state), Some(Level4));
172        state.dragged = true;
173        assert_eq!(levels.for_state(state), Some(Level5));
174        state.disabled = true;
175        assert_eq!(levels.for_state(state), Some(Level0));
176    }
177}