gpui_material_components/
chip.rs1mod render;
2
3use std::rc::Rc;
4
5use gpui::{
6 AnyElement, App, ElementId, Entity, FocusHandle, Hsla, IntoElement, Pixels, SharedString,
7 Window, px,
8};
9use gpui_material_foundation::InteractionSource;
10use gpui_material_theme::Elevation;
11
12use crate::action::{Activate, Change};
13use crate::button_style::{
14 ButtonBorder, ButtonElevations, ButtonPadding, ButtonShapes, finite_length,
15};
16
17#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
18pub enum ChipVariant {
19 #[default]
20 Assist,
21 Filter,
22 Input,
23 Suggestion,
24}
25
26#[derive(Clone, Copy, Debug, Default, PartialEq)]
28pub struct ChipStateColors {
29 pub container: Option<Hsla>,
30 pub label: Option<Hsla>,
31 pub leading: Option<Hsla>,
32 pub trailing: Option<Hsla>,
33 pub border: Option<Hsla>,
34}
35
36#[derive(Clone, Copy, Debug, Default, PartialEq)]
39pub struct ChipColors {
40 pub enabled: ChipStateColors,
41 pub selected: ChipStateColors,
42 pub disabled: ChipStateColors,
43 pub disabled_selected: ChipStateColors,
44}
45
46impl ChipColors {
47 fn for_state(self, disabled: bool, selected: bool) -> ChipStateColors {
48 match (disabled, selected) {
49 (false, false) => self.enabled,
50 (false, true) => self.selected,
51 (true, false) => self.disabled,
52 (true, true) => ChipStateColors {
53 container: self.disabled_selected.container.or(self.disabled.container),
54 label: self.disabled_selected.label.or(self.disabled.label),
55 leading: self.disabled_selected.leading.or(self.disabled.leading),
56 trailing: self.disabled_selected.trailing.or(self.disabled.trailing),
57 border: self.disabled_selected.border.or(self.disabled.border),
58 },
59 }
60 }
61}
62
63#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
66pub struct ChipElevations {
67 pub resting: Option<Elevation>,
68 pub pressed: Option<Elevation>,
69 pub focused: Option<Elevation>,
70 pub hovered: Option<Elevation>,
71 pub dragged: Option<Elevation>,
72 pub disabled: Option<Elevation>,
73}
74
75impl ChipElevations {
76 fn button(self) -> ButtonElevations {
77 ButtonElevations {
78 resting: self.resting,
79 pressed: self.pressed,
80 focused: self.focused,
81 hovered: self.hovered,
82 disabled: self.disabled,
83 }
84 }
85}
86
87#[derive(IntoElement)]
90pub struct Chip {
91 id: ElementId,
92 label: SharedString,
93 variant: ChipVariant,
94 selected: bool,
95 elevated: bool,
96 disabled: bool,
97 tonal: bool,
98 dragged: bool,
99 content: Option<AnyElement>,
100 leading: Option<AnyElement>,
101 avatar: Option<AnyElement>,
102 trailing: Option<AnyElement>,
103 shapes: ButtonShapes,
104 colors: ChipColors,
105 border: Option<ButtonBorder>,
106 elevations: ChipElevations,
107 spacing: Pixels,
108 padding: Option<ButtonPadding>,
109 interaction_source: Option<Entity<InteractionSource>>,
110 on_activate: Option<Activate>,
111 on_change: Option<Change>,
112 remove: Option<(SharedString, Activate)>,
113}
114
115impl Chip {
116 pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
117 Self {
118 id: id.into(),
119 label: label.into(),
120 variant: ChipVariant::default(),
121 selected: false,
122 elevated: false,
123 disabled: false,
124 tonal: false,
125 dragged: false,
126 content: None,
127 leading: None,
128 avatar: None,
129 trailing: None,
130 shapes: ButtonShapes::default(),
131 colors: ChipColors::default(),
132 border: None,
133 elevations: ChipElevations::default(),
134 spacing: px(8.),
135 padding: None,
136 interaction_source: None,
137 on_activate: None,
138 on_change: None,
139 remove: None,
140 }
141 }
142
143 pub fn variant(mut self, variant: ChipVariant) -> Self {
144 self.variant = variant;
145 self
146 }
147
148 pub fn selected(mut self, selected: bool) -> Self {
149 self.selected = selected;
150 self
151 }
152
153 pub fn elevated(mut self, elevated: bool) -> Self {
154 self.elevated = elevated;
155 self
156 }
157
158 pub fn disabled(mut self, disabled: bool) -> Self {
159 self.disabled = disabled;
160 self
161 }
162
163 pub fn tonal(mut self, tonal: bool) -> Self {
165 self.tonal = tonal;
166 self
167 }
168
169 pub fn content(mut self, content: impl IntoElement) -> Self {
171 self.content = Some(content.into_any_element());
172 self
173 }
174
175 pub fn leading_icon(mut self, icon: impl IntoElement) -> Self {
176 self.leading = Some(icon.into_any_element());
177 self
178 }
179
180 pub fn avatar(mut self, avatar: impl IntoElement) -> Self {
182 self.avatar = Some(avatar.into_any_element());
183 self
184 }
185
186 pub fn trailing(mut self, trailing: impl IntoElement) -> Self {
189 self.trailing = Some(trailing.into_any_element());
190 self
191 }
192
193 pub fn shapes(mut self, shapes: ButtonShapes) -> Self {
194 self.shapes = shapes;
195 self
196 }
197
198 pub fn colors(mut self, colors: ChipColors) -> Self {
199 self.colors = colors;
200 self
201 }
202
203 pub fn border(mut self, border: ButtonBorder) -> Self {
204 self.border = Some(border);
205 self
206 }
207
208 pub fn elevations(mut self, elevations: ChipElevations) -> Self {
209 self.elevations = elevations;
210 self
211 }
212
213 pub fn dragged(mut self, dragged: bool) -> Self {
215 self.dragged = dragged;
216 self
217 }
218
219 pub fn spacing(mut self, spacing: Pixels) -> Self {
220 self.spacing = finite_length(spacing);
221 self
222 }
223
224 pub fn content_padding(mut self, padding: ButtonPadding) -> Self {
225 self.padding = Some(ButtonPadding {
226 top: finite_length(padding.top),
227 end: finite_length(padding.end),
228 bottom: finite_length(padding.bottom),
229 start: finite_length(padding.start),
230 });
231 self
232 }
233
234 pub fn interaction_source(mut self, source: &Entity<InteractionSource>) -> Self {
236 self.interaction_source = Some(source.clone());
237 self
238 }
239
240 pub fn on_activate(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
241 self.on_activate = Some(Rc::new(handler));
242 self
243 }
244
245 pub fn on_change(mut self, handler: impl Fn(bool, &mut Window, &mut App) + 'static) -> Self {
246 self.on_change = Some(Rc::new(handler));
247 self
248 }
249
250 pub fn on_remove(
254 mut self,
255 name: impl Into<SharedString>,
256 restore: Option<FocusHandle>,
257 handler: impl Fn(&mut Window, &mut App) + 'static,
258 ) -> Self {
259 self.remove = Some((
260 name.into(),
261 Rc::new(move |window, cx| {
262 if let Some(restore) = &restore {
263 restore.focus(window, cx);
264 }
265 handler(window, cx);
266 }),
267 ));
268 self
269 }
270}