1mod group;
2mod render;
3
4pub use group::{ChipGroup, ChipGroupItem, ChipGroupSelection};
5
6use std::rc::Rc;
7
8use gpui::{
9 AnyElement, App, ElementId, Entity, FocusHandle, Hsla, IntoElement, Pixels, SharedString,
10 Window, px,
11};
12use gpui_material_foundation::InteractionSource;
13use gpui_material_theme::Elevation;
14
15use crate::SemanticTone;
16use crate::action::{Activate, Change};
17use crate::button_style::{
18 ButtonBorder, ButtonElevations, ButtonPadding, ButtonShapes, finite_length,
19};
20
21#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
22pub enum ChipVariant {
23 #[default]
24 Assist,
25 Filter,
26 Input,
27 Suggestion,
28}
29
30#[derive(Clone, Copy, Debug, Default, PartialEq)]
32pub struct ChipStateColors {
33 pub container: Option<Hsla>,
34 pub label: Option<Hsla>,
35 pub leading: Option<Hsla>,
36 pub trailing: Option<Hsla>,
37 pub border: Option<Hsla>,
38}
39
40#[derive(Clone, Copy, Debug, Default, PartialEq)]
43pub struct ChipColors {
44 pub enabled: ChipStateColors,
45 pub selected: ChipStateColors,
46 pub disabled: ChipStateColors,
47 pub disabled_selected: ChipStateColors,
48}
49
50impl ChipColors {
51 fn for_state(self, disabled: bool, selected: bool) -> ChipStateColors {
52 match (disabled, selected) {
53 (false, false) => self.enabled,
54 (false, true) => self.selected,
55 (true, false) => self.disabled,
56 (true, true) => ChipStateColors {
57 container: self.disabled_selected.container.or(self.disabled.container),
58 label: self.disabled_selected.label.or(self.disabled.label),
59 leading: self.disabled_selected.leading.or(self.disabled.leading),
60 trailing: self.disabled_selected.trailing.or(self.disabled.trailing),
61 border: self.disabled_selected.border.or(self.disabled.border),
62 },
63 }
64 }
65}
66
67#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
70pub struct ChipElevations {
71 pub resting: Option<Elevation>,
72 pub pressed: Option<Elevation>,
73 pub focused: Option<Elevation>,
74 pub hovered: Option<Elevation>,
75 pub dragged: Option<Elevation>,
76 pub disabled: Option<Elevation>,
77}
78
79impl ChipElevations {
80 fn button(self) -> ButtonElevations {
81 ButtonElevations {
82 resting: self.resting,
83 pressed: self.pressed,
84 focused: self.focused,
85 hovered: self.hovered,
86 disabled: self.disabled,
87 }
88 }
89}
90
91#[derive(IntoElement)]
94pub struct Chip {
95 id: ElementId,
96 label: SharedString,
97 variant: ChipVariant,
98 selected: bool,
99 elevated: bool,
100 disabled: bool,
101 tonal: bool,
102 tone: SemanticTone,
103 dragged: bool,
104 content: Option<AnyElement>,
105 leading: Option<AnyElement>,
106 avatar: Option<AnyElement>,
107 trailing: Option<AnyElement>,
108 shapes: ButtonShapes,
109 colors: ChipColors,
110 border: Option<ButtonBorder>,
111 elevations: ChipElevations,
112 spacing: Pixels,
113 padding: Option<ButtonPadding>,
114 focus: Option<FocusHandle>,
115 tab_stop: bool,
116 interaction_source: Option<Entity<InteractionSource>>,
117 on_activate: Option<Activate>,
118 on_change: Option<Change>,
119 remove: Option<(SharedString, Activate)>,
120}
121
122impl Chip {
123 pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
124 Self {
125 id: id.into(),
126 label: label.into(),
127 variant: ChipVariant::default(),
128 selected: false,
129 elevated: false,
130 disabled: false,
131 tonal: false,
132 tone: SemanticTone::Default,
133 dragged: false,
134 content: None,
135 leading: None,
136 avatar: None,
137 trailing: None,
138 shapes: ButtonShapes::default(),
139 colors: ChipColors::default(),
140 border: None,
141 elevations: ChipElevations::default(),
142 spacing: px(8.),
143 padding: None,
144 focus: None,
145 tab_stop: true,
146 interaction_source: None,
147 on_activate: None,
148 on_change: None,
149 remove: None,
150 }
151 }
152
153 pub fn assist(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
155 Self::new(id, label).variant(ChipVariant::Assist)
156 }
157
158 pub fn filter(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
160 Self::new(id, label).variant(ChipVariant::Filter)
161 }
162
163 pub fn input(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
165 Self::new(id, label).variant(ChipVariant::Input)
166 }
167
168 pub fn suggestion(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
170 Self::new(id, label).variant(ChipVariant::Suggestion)
171 }
172
173 pub fn variant(mut self, variant: ChipVariant) -> Self {
174 self.variant = variant;
175 self
176 }
177
178 pub fn tone(mut self, tone: SemanticTone) -> Self {
181 self.tone = tone;
182 self
183 }
184
185 pub fn selected(mut self, selected: bool) -> Self {
186 self.selected = selected;
187 self
188 }
189
190 pub fn elevated(mut self, elevated: bool) -> Self {
191 self.elevated = elevated;
192 self
193 }
194
195 pub fn disabled(mut self, disabled: bool) -> Self {
196 self.disabled = disabled;
197 self
198 }
199
200 pub fn tonal(mut self, tonal: bool) -> Self {
202 self.tonal = tonal;
203 self
204 }
205
206 pub fn content(mut self, content: impl IntoElement) -> Self {
208 self.content = Some(content.into_any_element());
209 self
210 }
211
212 pub fn leading_icon(mut self, icon: impl IntoElement) -> Self {
213 self.leading = Some(icon.into_any_element());
214 self
215 }
216
217 pub fn avatar(mut self, avatar: impl IntoElement) -> Self {
219 self.avatar = Some(avatar.into_any_element());
220 self
221 }
222
223 pub fn trailing(mut self, trailing: impl IntoElement) -> Self {
226 self.trailing = Some(trailing.into_any_element());
227 self
228 }
229
230 pub fn shapes(mut self, shapes: ButtonShapes) -> Self {
231 self.shapes = shapes;
232 self
233 }
234
235 pub fn colors(mut self, colors: ChipColors) -> Self {
236 self.colors = colors;
237 self
238 }
239
240 pub fn border(mut self, border: ButtonBorder) -> Self {
241 self.border = Some(border);
242 self
243 }
244
245 pub fn elevations(mut self, elevations: ChipElevations) -> Self {
246 self.elevations = elevations;
247 self
248 }
249
250 pub fn dragged(mut self, dragged: bool) -> Self {
252 self.dragged = dragged;
253 self
254 }
255
256 pub fn spacing(mut self, spacing: Pixels) -> Self {
257 self.spacing = finite_length(spacing);
258 self
259 }
260
261 pub fn content_padding(mut self, padding: ButtonPadding) -> Self {
262 self.padding = Some(ButtonPadding {
263 top: finite_length(padding.top),
264 end: finite_length(padding.end),
265 bottom: finite_length(padding.bottom),
266 start: finite_length(padding.start),
267 });
268 self
269 }
270
271 pub fn track_focus(mut self, focus: &FocusHandle) -> Self {
273 self.focus = Some(focus.clone());
274 self
275 }
276
277 pub(crate) fn tab_stop(mut self, tab_stop: bool) -> Self {
278 self.tab_stop = tab_stop;
279 self
280 }
281
282 pub fn interaction_source(mut self, source: &Entity<InteractionSource>) -> Self {
284 self.interaction_source = Some(source.clone());
285 self
286 }
287
288 pub fn on_activate(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
289 self.on_activate = Some(Rc::new(handler));
290 self
291 }
292
293 pub fn on_change(mut self, handler: impl Fn(bool, &mut Window, &mut App) + 'static) -> Self {
294 self.on_change = Some(Rc::new(handler));
295 self
296 }
297
298 pub fn on_remove(
302 mut self,
303 name: impl Into<SharedString>,
304 restore: Option<FocusHandle>,
305 handler: impl Fn(&mut Window, &mut App) + 'static,
306 ) -> Self {
307 self.remove = Some((
308 name.into(),
309 Rc::new(move |window, cx| {
310 if let Some(restore) = &restore {
311 restore.focus(window, cx);
312 }
313 handler(window, cx);
314 }),
315 ));
316 self
317 }
318}
319
320#[cfg(test)]
321mod tests {
322 use super::*;
323
324 #[test]
325 fn chip_variant_shorthands_and_tone_configure_expected_fields() {
326 assert_eq!(Chip::assist("c1", "Assist").variant, ChipVariant::Assist);
327 assert_eq!(Chip::filter("c2", "Filter").variant, ChipVariant::Filter);
328 assert_eq!(Chip::input("c3", "Input").variant, ChipVariant::Input);
329 assert_eq!(
330 Chip::suggestion("c4", "Suggest").variant,
331 ChipVariant::Suggestion
332 );
333 let toned = Chip::filter("c5", "Danger").tone(SemanticTone::Danger);
334 assert_eq!(toned.tone, SemanticTone::Danger);
335 }
336}