1mod group;
2mod palette;
3mod render;
4mod split;
5
6pub use group::{ChipGroup, ChipGroupItem, ChipGroupSelection};
7
8use std::rc::Rc;
9
10use gpui::{
11 AnyElement, App, ElementId, Entity, FocusHandle, Hsla, IntoElement, Pixels, SharedString,
12 Window, px,
13};
14use gpui_material_foundation::InteractionSource;
15use gpui_material_theme::LogicalEdges;
16
17use crate::SemanticTone;
18use crate::action::{Activate, Change};
19use crate::control_style::{ControlBorder, ControlElevations, ControlShapes, finite_length};
20
21#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
23pub enum ChipVariant {
24 #[default]
26 Assist,
27 Filter,
29 Input,
31 Suggestion,
33}
34
35#[derive(Clone, Copy, Debug, Default, PartialEq)]
37pub struct ChipStateColors {
38 pub container: Option<Hsla>,
40 pub label: Option<Hsla>,
42 pub leading: Option<Hsla>,
44 pub trailing: Option<Hsla>,
46 pub border: Option<Hsla>,
48}
49
50#[derive(Clone, Copy, Debug, Default, PartialEq)]
53pub struct ChipColors {
54 pub enabled: ChipStateColors,
56 pub selected: ChipStateColors,
58 pub disabled: ChipStateColors,
60 pub disabled_selected: ChipStateColors,
62}
63
64impl ChipColors {
65 fn for_state(self, disabled: bool, selected: bool) -> ChipStateColors {
66 match (disabled, selected) {
67 (false, false) => self.enabled,
68 (false, true) => self.selected,
69 (true, false) => self.disabled,
70 (true, true) => ChipStateColors {
71 container: self.disabled_selected.container.or(self.disabled.container),
72 label: self.disabled_selected.label.or(self.disabled.label),
73 leading: self.disabled_selected.leading.or(self.disabled.leading),
74 trailing: self.disabled_selected.trailing.or(self.disabled.trailing),
75 border: self.disabled_selected.border.or(self.disabled.border),
76 },
77 }
78 }
79}
80
81#[derive(IntoElement)]
84pub struct Chip {
85 id: ElementId,
86 label: SharedString,
87 variant: ChipVariant,
88 selected: bool,
89 elevated: bool,
90 disabled: bool,
91 tonal: bool,
92 tone: SemanticTone,
93 dragged: bool,
94 content: Option<AnyElement>,
95 leading: Option<AnyElement>,
96 avatar: Option<AnyElement>,
97 trailing: Option<AnyElement>,
98 shapes: ControlShapes,
99 colors: ChipColors,
100 border: Option<ControlBorder>,
101 elevations: ControlElevations,
102 spacing: Pixels,
103 padding: Option<LogicalEdges>,
104 focus: Option<FocusHandle>,
105 tab_stop: bool,
106 interaction_source: Option<Entity<InteractionSource>>,
107 on_activate: Option<Activate>,
108 on_change: Option<Change>,
109 remove: Option<(SharedString, Activate)>,
110}
111
112impl Chip {
113 pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
115 Self {
116 id: id.into(),
117 label: label.into(),
118 variant: ChipVariant::default(),
119 selected: false,
120 elevated: false,
121 disabled: false,
122 tonal: false,
123 tone: SemanticTone::Default,
124 dragged: false,
125 content: None,
126 leading: None,
127 avatar: None,
128 trailing: None,
129 shapes: ControlShapes::default(),
130 colors: ChipColors::default(),
131 border: None,
132 elevations: ControlElevations::default(),
133 spacing: px(8.),
134 padding: None,
135 focus: None,
136 tab_stop: true,
137 interaction_source: None,
138 on_activate: None,
139 on_change: None,
140 remove: None,
141 }
142 }
143
144 pub fn assist(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
146 Self::new(id, label).variant(ChipVariant::Assist)
147 }
148
149 pub fn filter(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
151 Self::new(id, label).variant(ChipVariant::Filter)
152 }
153
154 pub fn input(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
156 Self::new(id, label).variant(ChipVariant::Input)
157 }
158
159 pub fn suggestion(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
161 Self::new(id, label).variant(ChipVariant::Suggestion)
162 }
163
164 pub fn variant(mut self, variant: ChipVariant) -> Self {
166 self.variant = variant;
167 self
168 }
169
170 pub fn tone(mut self, tone: SemanticTone) -> Self {
173 self.tone = tone;
174 self
175 }
176
177 pub fn selected(mut self, selected: bool) -> Self {
179 self.selected = selected;
180 self
181 }
182
183 pub fn elevated(mut self, elevated: bool) -> Self {
185 self.elevated = elevated;
186 self
187 }
188
189 pub fn disabled(mut self, disabled: bool) -> Self {
191 self.disabled = disabled;
192 self
193 }
194
195 pub fn tonal(mut self, tonal: bool) -> Self {
197 self.tonal = tonal;
198 self
199 }
200
201 pub fn content(mut self, content: impl IntoElement) -> Self {
203 self.content = Some(content.into_any_element());
204 self
205 }
206
207 pub fn leading_icon(mut self, icon: impl IntoElement) -> Self {
209 self.leading = Some(icon.into_any_element());
210 self
211 }
212
213 pub fn avatar(mut self, avatar: impl IntoElement) -> Self {
215 self.avatar = Some(avatar.into_any_element());
216 self
217 }
218
219 pub fn trailing(mut self, trailing: impl IntoElement) -> Self {
222 self.trailing = Some(trailing.into_any_element());
223 self
224 }
225
226 pub fn shapes(mut self, shapes: ControlShapes) -> Self {
228 self.shapes = shapes;
229 self
230 }
231
232 pub fn colors(mut self, colors: ChipColors) -> Self {
234 self.colors = colors;
235 self
236 }
237
238 pub fn border(mut self, border: ControlBorder) -> Self {
240 self.border = Some(border);
241 self
242 }
243
244 pub fn elevations(mut self, elevations: ControlElevations) -> 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 {
258 self.spacing = finite_length(spacing);
259 self
260 }
261
262 pub fn content_padding(mut self, padding: LogicalEdges) -> Self {
264 self.padding = Some(LogicalEdges::new(
265 padding.top,
266 padding.end,
267 padding.bottom,
268 padding.start,
269 ));
270 self
271 }
272
273 pub fn track_focus(mut self, focus: &FocusHandle) -> Self {
275 self.focus = Some(focus.clone());
276 self
277 }
278
279 pub(crate) fn tab_stop(mut self, tab_stop: bool) -> Self {
280 self.tab_stop = tab_stop;
281 self
282 }
283
284 pub fn interaction_source(mut self, source: &Entity<InteractionSource>) -> Self {
286 self.interaction_source = Some(source.clone());
287 self
288 }
289
290 pub fn on_activate(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
292 self.on_activate = Some(Rc::new(handler));
293 self
294 }
295
296 pub fn on_change(mut self, handler: impl Fn(bool, &mut Window, &mut App) + 'static) -> Self {
298 self.on_change = Some(Rc::new(handler));
299 self
300 }
301
302 pub fn on_remove(
306 mut self,
307 name: impl Into<SharedString>,
308 restore: Option<FocusHandle>,
309 handler: impl Fn(&mut Window, &mut App) + 'static,
310 ) -> Self {
311 self.remove = Some((
312 name.into(),
313 Rc::new(move |window, cx| {
314 if let Some(restore) = &restore {
315 restore.focus(window, cx);
316 }
317 handler(window, cx);
318 }),
319 ));
320 self
321 }
322}
323
324#[cfg(test)]
325mod tests {
326 use super::*;
327
328 #[test]
329 fn chip_variant_shorthands_and_tone_configure_expected_fields() {
330 assert_eq!(Chip::assist("c1", "Assist").variant, ChipVariant::Assist);
331 assert_eq!(Chip::filter("c2", "Filter").variant, ChipVariant::Filter);
332 assert_eq!(Chip::input("c3", "Input").variant, ChipVariant::Input);
333 assert_eq!(
334 Chip::suggestion("c4", "Suggest").variant,
335 ChipVariant::Suggestion
336 );
337 let toned = Chip::filter("c5", "Danger").tone(SemanticTone::Danger);
338 assert_eq!(toned.tone, SemanticTone::Danger);
339 }
340}