1use gpui::{Hsla, Pixels, px};
2use gpui_material_foundation::InteractionState;
3use gpui_material_theme::{ColorRole, CornerShape, Elevation};
4
5use crate::SemanticTone;
6
7#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
9pub enum ListItemAlignment {
10 #[default]
11 Auto,
12 Start,
13 Center,
14 End,
15}
16
17#[derive(Clone, Copy, Debug, PartialEq)]
19pub struct ListItemPadding {
20 pub start: Pixels,
21 pub end: Pixels,
22 pub top: Pixels,
23 pub bottom: Pixels,
24}
25
26impl Default for ListItemPadding {
27 fn default() -> Self {
28 Self {
29 start: px(16.),
30 end: px(16.),
31 top: px(10.),
32 bottom: px(10.),
33 }
34 }
35}
36
37#[derive(Clone, Copy, Debug, Default, PartialEq)]
40pub struct ListItemShapes {
41 pub resting: Option<CornerShape>,
42 pub selected: Option<CornerShape>,
43 pub pressed: Option<CornerShape>,
44 pub focused: Option<CornerShape>,
45 pub hovered: Option<CornerShape>,
46 pub dragged: Option<CornerShape>,
47}
48
49impl ListItemShapes {
50 pub(super) fn resolve(
51 self,
52 resting: CornerShape,
53 selected_shape: CornerShape,
54 selected: bool,
55 state: InteractionState,
56 ) -> CornerShape {
57 let base = if selected {
58 self.selected.unwrap_or(selected_shape)
59 } else {
60 self.resting.unwrap_or(resting)
61 };
62 let active = if state.disabled {
63 None
64 } else if state.dragged {
65 self.dragged
66 } else if state.pressed {
67 self.pressed
68 } else if state.focus_visible {
69 self.focused
70 } else if state.hovered {
71 self.hovered
72 } else {
73 None
74 };
75 active.unwrap_or(base)
76 }
77}
78
79#[derive(Clone, Copy, Debug, Default, PartialEq)]
81pub struct ListItemStateColors {
82 pub container: Option<Hsla>,
83 pub content: Option<Hsla>,
84 pub leading: Option<Hsla>,
85 pub trailing: Option<Hsla>,
86 pub overline: Option<Hsla>,
87 pub supporting: Option<Hsla>,
88}
89
90#[derive(Clone, Copy, Debug, Default, PartialEq)]
92pub struct ListItemColors {
93 pub normal: ListItemStateColors,
94 pub disabled: ListItemStateColors,
95 pub selected: ListItemStateColors,
96 pub dragged: ListItemStateColors,
97}
98
99pub(super) struct RowColors {
100 pub container: Hsla,
101 pub content: Hsla,
102 pub leading: Option<Hsla>,
103 pub trailing: Option<Hsla>,
104 pub overline: Hsla,
105 pub supporting: Hsla,
106}
107
108impl ListItemColors {
109 pub(super) fn resolve(
110 self,
111 selected: bool,
112 selection: f32,
113 state: InteractionState,
114 semantic_tone: SemanticTone,
115 tone: impl Fn(ColorRole) -> Hsla,
116 ) -> RowColors {
117 let normal_container = self
118 .normal
119 .container
120 .unwrap_or_else(|| tone(ColorRole::Surface));
121 let selected_container = self.selected.container.unwrap_or_else(|| {
122 tone(
123 semantic_tone
124 .container_role()
125 .unwrap_or(ColorRole::SecondaryContainer),
126 )
127 });
128 let (colors, content, secondary, side, background) = if state.disabled {
129 let tint = tone(ColorRole::OnSurface).opacity(0.38);
130 let background = self
131 .disabled
132 .container
133 .unwrap_or_else(|| crate::motion::mix(normal_container, tint, selection));
134 (self.disabled, tint, tint, Some(tint), background)
135 } else if state.dragged {
136 let background = self.dragged.container.unwrap_or_else(|| {
137 if selected {
138 selected_container
139 } else {
140 tone(ColorRole::SurfaceContainerHighest)
141 }
142 });
143 let content = tone(if selected {
144 semantic_tone
145 .on_container_role()
146 .unwrap_or(ColorRole::OnSecondaryContainer)
147 } else {
148 semantic_tone.role().unwrap_or(ColorRole::OnSurface)
149 });
150 (self.dragged, content, content, Some(content), background)
151 } else {
152 let background = crate::motion::mix(normal_container, selected_container, selection);
153 if selected {
154 let tint = tone(
155 semantic_tone
156 .on_container_role()
157 .unwrap_or(ColorRole::OnSecondaryContainer),
158 );
159 let side = semantic_tone.on_container_role().map(&tone);
160 (self.selected, tint, tint, side, background)
161 } else {
162 let accent = semantic_tone.role().map(&tone);
163 (
164 self.normal,
165 accent.unwrap_or_else(|| tone(ColorRole::OnSurface)),
166 tone(ColorRole::OnSurfaceVariant),
167 accent,
168 background,
169 )
170 }
171 };
172 RowColors {
173 container: background,
174 content: colors.content.unwrap_or(content),
175 leading: colors.leading.or(side),
176 trailing: colors.trailing.or(side),
177 overline: colors.overline.unwrap_or(secondary),
178 supporting: colors.supporting.unwrap_or(secondary),
179 }
180 }
181}
182
183#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
185pub struct ListItemElevations {
186 pub resting: Option<Elevation>,
187 pub hovered: Option<Elevation>,
188 pub focused: Option<Elevation>,
189 pub pressed: Option<Elevation>,
190 pub dragged: Option<Elevation>,
191 pub disabled: Option<Elevation>,
192}
193
194impl ListItemElevations {
195 pub(super) fn resolve(self, state: InteractionState) -> Elevation {
196 let resting = self.resting.unwrap_or(Elevation::Level0);
197 if state.disabled {
198 self.disabled.unwrap_or(resting)
199 } else if state.dragged {
200 self.dragged.unwrap_or(Elevation::Level4)
201 } else if state.pressed {
202 self.pressed.unwrap_or(resting)
203 } else if state.focus_visible {
204 self.focused.unwrap_or(resting)
205 } else if state.hovered {
206 self.hovered.unwrap_or(resting)
207 } else {
208 resting
209 }
210 }
211}
212
213#[cfg(test)]
214mod tests {
215 use gpui_material_theme::{CornerRole, Direction};
216
217 use super::*;
218
219 #[test]
220 fn selected_and_active_shapes_are_logical_and_disabled_uses_selected_base() {
221 let shape = CornerShape::Radii([4., 8., 12., 16.]);
222 let selected = CornerRole::Large.shape();
223 let styles = ListItemShapes {
224 dragged: Some(shape),
225 ..Default::default()
226 };
227 let mut state = InteractionState::default();
228 state.dragged = true;
229 state.hovered = true;
230 assert_eq!(
231 styles
232 .resolve(CornerRole::None.shape(), selected, true, state)
233 .radii(100., 60., Direction::Rtl),
234 [8., 4., 16., 12.],
235 );
236 state.disabled = true;
237 assert_eq!(
238 styles.resolve(CornerRole::None.shape(), selected, true, state),
239 selected,
240 );
241 }
242
243 #[test]
244 fn all_slot_colors_follow_the_state_and_dragged_elevation_yields_to_disabled() {
245 let palette = |value| ListItemStateColors {
246 container: Some(gpui::rgb(value).into()),
247 content: Some(gpui::rgb(value + 1).into()),
248 leading: Some(gpui::rgb(value + 2).into()),
249 trailing: Some(gpui::rgb(value + 3).into()),
250 overline: Some(gpui::rgb(value + 4).into()),
251 supporting: Some(gpui::rgb(value + 5).into()),
252 };
253 let colors = ListItemColors {
254 normal: palette(0x113300),
255 selected: palette(0x224400),
256 dragged: palette(0x335500),
257 disabled: palette(0x446600),
258 };
259 let elevations = ListItemElevations {
260 dragged: Some(Elevation::Level3),
261 disabled: Some(Elevation::Level1),
262 ..Default::default()
263 };
264 let mut state = InteractionState::default();
265 for (selected, dragged, disabled, expected, level) in [
266 (false, false, false, colors.normal, Elevation::Level0),
267 (true, false, false, colors.selected, Elevation::Level0),
268 (true, true, false, colors.dragged, Elevation::Level3),
269 (true, true, true, colors.disabled, Elevation::Level1),
270 ] {
271 state.disabled = disabled;
272 state.dragged = dragged;
273 let actual = colors.resolve(
274 selected,
275 f32::from(selected),
276 state,
277 SemanticTone::Default,
278 |_| gpui::transparent_black(),
279 );
280 let expected_container = expected.container.unwrap();
281 let actual_container = actual.container;
282 assert!(
283 [
284 actual_container.h - expected_container.h,
285 actual_container.s - expected_container.s,
286 actual_container.l - expected_container.l,
287 actual_container.a - expected_container.a
288 ]
289 .into_iter()
290 .all(|delta| delta.abs() < 1e-6)
291 );
292 assert_eq!(Some(actual.content), expected.content);
293 assert_eq!(actual.leading, expected.leading);
294 assert_eq!(actual.trailing, expected.trailing);
295 assert_eq!(Some(actual.overline), expected.overline);
296 assert_eq!(Some(actual.supporting), expected.supporting);
297 assert_eq!(elevations.resolve(state), level);
298 }
299 }
300
301 #[test]
302 fn semantic_tone_tints_enabled_and_selected_roles_without_overriding_disabled_or_explicit_colors()
303 {
304 let role_color = |role: ColorRole| -> Hsla {
305 match role {
306 ColorRole::Error => gpui::rgb(0xB3261E).into(),
307 ColorRole::ErrorContainer => gpui::rgb(0xF9DEDC).into(),
308 ColorRole::OnErrorContainer => gpui::rgb(0x410E0B).into(),
309 ColorRole::OnSurface => gpui::rgb(0x1C1B1F).into(),
310 ColorRole::OnSurfaceVariant => gpui::rgb(0x49454F).into(),
311 _ => gpui::rgb(0xFFFFFF).into(),
312 }
313 };
314 let state = InteractionState::default();
315 let normal =
316 ListItemColors::default().resolve(false, 0., state, SemanticTone::Danger, role_color);
317 assert_eq!(normal.content, role_color(ColorRole::Error));
318 assert_eq!(normal.leading, Some(role_color(ColorRole::Error)));
319 assert_eq!(normal.trailing, Some(role_color(ColorRole::Error)));
320
321 let selected =
322 ListItemColors::default().resolve(true, 1., state, SemanticTone::Danger, role_color);
323 assert_eq!(selected.content, role_color(ColorRole::OnErrorContainer));
324 assert_eq!(
325 selected.leading,
326 Some(role_color(ColorRole::OnErrorContainer))
327 );
328 assert_eq!(
329 selected.trailing,
330 Some(role_color(ColorRole::OnErrorContainer))
331 );
332
333 let mut disabled_state = InteractionState::default();
334 disabled_state.disabled = true;
335 let disabled = ListItemColors::default().resolve(
336 false,
337 0.,
338 disabled_state,
339 SemanticTone::Danger,
340 role_color,
341 );
342 assert_eq!(
343 disabled.content,
344 role_color(ColorRole::OnSurface).opacity(0.38)
345 );
346
347 let custom: Hsla = gpui::rgb(0x123456).into();
348 let overridden = ListItemColors {
349 normal: ListItemStateColors {
350 content: Some(custom),
351 leading: Some(custom),
352 ..Default::default()
353 },
354 ..Default::default()
355 }
356 .resolve(false, 0., state, SemanticTone::Danger, role_color);
357 assert_eq!(overridden.content, custom);
358 assert_eq!(overridden.leading, Some(custom));
359 assert_eq!(overridden.trailing, Some(role_color(ColorRole::Error)));
360 }
361}