Skip to main content

gpui_material_components/list/
style.rs

1use gpui::{Hsla, Pixels, px};
2use gpui_material_foundation::InteractionState;
3use gpui_material_theme::{ColorRole, CornerShape, Elevation};
4
5/// Alignment of the slots within one row.
6#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
7pub enum ListItemAlignment {
8    #[default]
9    Auto,
10    Start,
11    Center,
12    End,
13}
14
15/// Logical padding in pixels. Using the builder replaces the adaptive defaults.
16#[derive(Clone, Copy, Debug, PartialEq)]
17pub struct ListItemPadding {
18    pub start: Pixels,
19    pub end: Pixels,
20    pub top: Pixels,
21    pub bottom: Pixels,
22}
23
24impl Default for ListItemPadding {
25    fn default() -> Self {
26        Self {
27            start: px(16.),
28            end: px(16.),
29            top: px(10.),
30            bottom: px(10.),
31        }
32    }
33}
34
35/// Logical shapes for one row. An omitted shape uses the resting or selected
36/// shape, including the list's grouped corners.
37#[derive(Clone, Copy, Debug, Default, PartialEq)]
38pub struct ListItemShapes {
39    pub resting: Option<CornerShape>,
40    pub selected: Option<CornerShape>,
41    pub pressed: Option<CornerShape>,
42    pub focused: Option<CornerShape>,
43    pub hovered: Option<CornerShape>,
44    pub dragged: Option<CornerShape>,
45}
46
47impl ListItemShapes {
48    pub(super) fn resolve(
49        self,
50        resting: CornerShape,
51        selected_shape: CornerShape,
52        selected: bool,
53        state: InteractionState,
54    ) -> CornerShape {
55        let base = if selected {
56            self.selected.unwrap_or(selected_shape)
57        } else {
58            self.resting.unwrap_or(resting)
59        };
60        let active = if state.disabled {
61            None
62        } else if state.dragged {
63            self.dragged
64        } else if state.pressed {
65            self.pressed
66        } else if state.focus_visible {
67            self.focused
68        } else if state.hovered {
69            self.hovered
70        } else {
71            None
72        };
73        active.unwrap_or(base)
74    }
75}
76
77/// Independent colors inherited by the container and each slot in one state.
78#[derive(Clone, Copy, Debug, Default, PartialEq)]
79pub struct ListItemStateColors {
80    pub container: Option<Hsla>,
81    pub content: Option<Hsla>,
82    pub leading: Option<Hsla>,
83    pub trailing: Option<Hsla>,
84    pub overline: Option<Hsla>,
85    pub supporting: Option<Hsla>,
86}
87
88/// Per-row palette. Omitted colors use the current window's theme.
89#[derive(Clone, Copy, Debug, Default, PartialEq)]
90pub struct ListItemColors {
91    pub normal: ListItemStateColors,
92    pub disabled: ListItemStateColors,
93    pub selected: ListItemStateColors,
94    pub dragged: ListItemStateColors,
95}
96
97pub(super) struct RowColors {
98    pub container: Hsla,
99    pub content: Hsla,
100    pub leading: Option<Hsla>,
101    pub trailing: Option<Hsla>,
102    pub overline: Hsla,
103    pub supporting: Hsla,
104}
105
106impl ListItemColors {
107    pub(super) fn resolve(
108        self,
109        selected: bool,
110        selection: f32,
111        state: InteractionState,
112        tone: impl Fn(ColorRole) -> Hsla,
113    ) -> RowColors {
114        let normal_container = self
115            .normal
116            .container
117            .unwrap_or_else(|| tone(ColorRole::Surface));
118        let selected_container = self
119            .selected
120            .container
121            .unwrap_or_else(|| tone(ColorRole::SecondaryContainer));
122        let (colors, content, secondary, side, background) = if state.disabled {
123            let tint = tone(ColorRole::OnSurface).opacity(0.38);
124            let background = self
125                .disabled
126                .container
127                .unwrap_or_else(|| crate::motion::mix(normal_container, tint, selection));
128            (self.disabled, tint, tint, Some(tint), background)
129        } else if state.dragged {
130            let background = self.dragged.container.unwrap_or_else(|| {
131                if selected {
132                    selected_container
133                } else {
134                    tone(ColorRole::SurfaceContainerHighest)
135                }
136            });
137            let content = tone(if selected {
138                ColorRole::OnSecondaryContainer
139            } else {
140                ColorRole::OnSurface
141            });
142            (self.dragged, content, content, Some(content), background)
143        } else {
144            let background = crate::motion::mix(normal_container, selected_container, selection);
145            if selected {
146                let tint = tone(ColorRole::OnSecondaryContainer);
147                (self.selected, tint, tint, None, background)
148            } else {
149                (
150                    self.normal,
151                    tone(ColorRole::OnSurface),
152                    tone(ColorRole::OnSurfaceVariant),
153                    None,
154                    background,
155                )
156            }
157        };
158        RowColors {
159            container: background,
160            content: colors.content.unwrap_or(content),
161            leading: colors.leading.or(side),
162            trailing: colors.trailing.or(side),
163            overline: colors.overline.unwrap_or(secondary),
164            supporting: colors.supporting.unwrap_or(secondary),
165        }
166    }
167}
168
169/// Per-state shadow levels. Rows default to Level0, or Level4 while dragged.
170#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
171pub struct ListItemElevations {
172    pub resting: Option<Elevation>,
173    pub hovered: Option<Elevation>,
174    pub focused: Option<Elevation>,
175    pub pressed: Option<Elevation>,
176    pub dragged: Option<Elevation>,
177    pub disabled: Option<Elevation>,
178}
179
180impl ListItemElevations {
181    pub(super) fn resolve(self, state: InteractionState) -> Elevation {
182        let resting = self.resting.unwrap_or(Elevation::Level0);
183        if state.disabled {
184            self.disabled.unwrap_or(resting)
185        } else if state.dragged {
186            self.dragged.unwrap_or(Elevation::Level4)
187        } else if state.pressed {
188            self.pressed.unwrap_or(resting)
189        } else if state.focus_visible {
190            self.focused.unwrap_or(resting)
191        } else if state.hovered {
192            self.hovered.unwrap_or(resting)
193        } else {
194            resting
195        }
196    }
197}
198
199#[cfg(test)]
200mod tests {
201    use gpui_material_theme::{CornerRole, Direction};
202
203    use super::*;
204
205    #[test]
206    fn selected_and_active_shapes_are_logical_and_disabled_uses_selected_base() {
207        let shape = CornerShape::Radii([4., 8., 12., 16.]);
208        let selected = CornerRole::Large.shape();
209        let styles = ListItemShapes {
210            dragged: Some(shape),
211            ..Default::default()
212        };
213        let mut state = InteractionState::default();
214        state.dragged = true;
215        state.hovered = true;
216        assert_eq!(
217            styles
218                .resolve(CornerRole::None.shape(), selected, true, state)
219                .radii(100., 60., Direction::Rtl),
220            [8., 4., 16., 12.],
221        );
222        state.disabled = true;
223        assert_eq!(
224            styles.resolve(CornerRole::None.shape(), selected, true, state),
225            selected,
226        );
227    }
228
229    #[test]
230    fn all_slot_colors_follow_the_state_and_dragged_elevation_yields_to_disabled() {
231        let palette = |value| ListItemStateColors {
232            container: Some(gpui::rgb(value).into()),
233            content: Some(gpui::rgb(value + 1).into()),
234            leading: Some(gpui::rgb(value + 2).into()),
235            trailing: Some(gpui::rgb(value + 3).into()),
236            overline: Some(gpui::rgb(value + 4).into()),
237            supporting: Some(gpui::rgb(value + 5).into()),
238        };
239        let colors = ListItemColors {
240            normal: palette(0x113300),
241            selected: palette(0x224400),
242            dragged: palette(0x335500),
243            disabled: palette(0x446600),
244        };
245        let elevations = ListItemElevations {
246            dragged: Some(Elevation::Level3),
247            disabled: Some(Elevation::Level1),
248            ..Default::default()
249        };
250        let mut state = InteractionState::default();
251        for (selected, dragged, disabled, expected, level) in [
252            (false, false, false, colors.normal, Elevation::Level0),
253            (true, false, false, colors.selected, Elevation::Level0),
254            (true, true, false, colors.dragged, Elevation::Level3),
255            (true, true, true, colors.disabled, Elevation::Level1),
256        ] {
257            state.disabled = disabled;
258            state.dragged = dragged;
259            let actual = colors.resolve(selected, f32::from(selected), state, |_| {
260                gpui::transparent_black()
261            });
262            let expected_container = expected.container.unwrap();
263            let actual_container = actual.container;
264            assert!(
265                [
266                    actual_container.h - expected_container.h,
267                    actual_container.s - expected_container.s,
268                    actual_container.l - expected_container.l,
269                    actual_container.a - expected_container.a
270                ]
271                .into_iter()
272                .all(|delta| delta.abs() < 1e-6)
273            );
274            assert_eq!(Some(actual.content), expected.content);
275            assert_eq!(actual.leading, expected.leading);
276            assert_eq!(actual.trailing, expected.trailing);
277            assert_eq!(Some(actual.overline), expected.overline);
278            assert_eq!(Some(actual.supporting), expected.supporting);
279            assert_eq!(elevations.resolve(state), level);
280        }
281    }
282}