1use crate::*;
2
3#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
5pub enum ChipKind {
6 Assist,
7 #[default]
8 Filter,
9 Input,
10 Suggestion,
11}
12
13#[derive(Clone, Copy, Debug, PartialEq, Eq)]
15pub struct ChipOptions {
16 pub kind: ChipKind,
17 pub selected: bool,
18 pub elevated: bool,
19 pub show_close: bool,
20 pub enabled: bool,
21}
22impl Default for ChipOptions {
23 fn default() -> Self {
24 Self {
25 kind: ChipKind::Filter,
26 selected: false,
27 elevated: false,
28 show_close: false,
29 enabled: true,
30 }
31 }
32}
33
34impl Frame<'_> {
35 pub fn chip(&mut self, id: u64, label: &str, selected: bool, enabled: bool) -> Response {
36 use crate::timing::{Easing, TweenSpec};
37 let row = self.allocate(48.0);
38 let b = Rect {
39 y: row.y + 8.0,
40 height: 32.0,
41 ..row
42 };
43 let r = self.interact(id, b, enabled);
44 let t = self.ui.theme;
45 let target = f32::from(selected);
46 let spec = TweenSpec::new(0.150, Easing::Cubic([0.4, 0.0, 0.2, 1.0]));
49 let slide = self.ui.motion.css_tween(id, 3, target, target, spec, spec);
50 let stroke_spec = TweenSpec {
51 delay: 0.045,
52 ..spec
53 };
54 let stroke = self
55 .ui
56 .motion
57 .css_tween(id, 4, target, target, stroke_spec, stroke_spec);
58 let color = if selected {
59 t.on_secondary
60 } else {
61 t.on_surface_variant
62 };
63 if selected {
64 self.rect(
65 b,
66 8.0,
67 if r.enabled {
68 t.secondary
69 } else {
70 t.text.opacity(0.12)
71 },
72 );
73 } else {
74 self.rect(
75 b,
76 8.0,
77 if !r.enabled {
78 t.text.opacity(0.12)
79 } else if r.focused {
80 t.on_surface_variant
81 } else {
82 t.outline
83 },
84 );
85 self.rect(b.inset(1.0), 7.0, t.background);
86 }
87 let overlay_target = if !r.enabled {
88 0.0
89 } else if r.focused {
90 0.12
91 } else if r.hovered {
92 0.08
93 } else {
94 0.0
95 };
96 let overlay_spec = TweenSpec::new(0.150, Easing::Linear);
97 let overlay =
98 self.ui
99 .motion
100 .css_tween(id, 0, overlay_target, 0.0, overlay_spec, overlay_spec);
101 if r.enabled && overlay > 0.0 {
102 self.rect(b, 8.0, color.opacity(overlay));
103 }
104 self.ink(r, 8.0, color);
105 if selected && stroke > 0.0 {
106 let left = b.x - 9.0 + 17.0 * slide;
109 let point = |x: f32, y: f32| {
110 [
111 left + (x + 2.0) * (2.0 / 3.0),
112 b.y + 6.0 + (y + 3.0) * (2.0 / 3.0),
113 ]
114 };
115 let p = [point(1.73, 12.91), point(8.1, 19.28), point(22.79, 4.59)];
116 let length = [6.37_f32 * 2.0_f32.sqrt(), 14.69_f32 * 2.0_f32.sqrt()];
117 let mut remaining = stroke * (length[0] + length[1]);
118 let ink = if r.enabled {
119 color
120 } else {
121 t.text.opacity(0.38)
122 };
123 self.commands
124 .push(DrawCommand::PushClip(self.clip.intersect(Rect {
125 x: b.x,
126 y: b.y + 4.0,
127 width: 12.0 + 24.0 * slide,
128 height: 24.0,
129 })));
130 for i in 0..2 {
131 let amount = (remaining / length[i]).clamp(0.0, 1.0);
132 if amount > 0.0 {
133 let end = [
134 p[i][0] + (p[i + 1][0] - p[i][0]) * amount,
135 p[i][1] + (p[i + 1][1] - p[i][1]) * amount,
136 ];
137 self.paint_line(p[i], end, 4.0 / 3.0, ink);
138 }
139 remaining -= length[i];
140 }
141 self.commands.push(DrawCommand::PopClip);
142 }
143 self.styled_text(
144 b,
145 label,
146 if r.enabled {
147 color
148 } else {
149 t.text.opacity(0.38)
150 },
151 TextStyle {
152 align: 0,
153 padding: 12.0 + 24.0 * slide,
154 ..TextStyle::LABEL
155 },
156 );
157 self.focus_ring(r);
158 self.semantic(r, Role::Button, label, selected, None, None, None);
159 r
160 }
161 pub fn chip_with_options(
165 &mut self,
166 id: u64,
167 label: &str,
168 options: ChipOptions,
169 ) -> (Response, bool) {
170 use crate::timing::{Easing, TweenSpec};
171 let row = self.allocate(48.0);
172 let b = Rect {
173 y: row.y + 8.0,
174 height: 32.0,
175 ..row
176 };
177 let effective_selected = options.selected && options.kind != ChipKind::Assist;
178 let show_check = options.kind == ChipKind::Filter && effective_selected;
179 let close_w = if options.show_close {
180 32.0_f32.min(b.width)
181 } else {
182 0.0
183 };
184 let close_hit = Rect {
185 x: (b.x + b.width - close_w).max(b.x),
186 y: b.y,
187 width: close_w,
188 height: b.height,
189 };
190 let body_hit = Rect {
191 width: (b.width - close_w).max(0.0),
192 ..b
193 };
194 let mut r = self.interact(id, body_hit, options.enabled);
195 r.bounds = b;
196 let close_response = if options.show_close {
197 let close_id = id.wrapping_mul(0x9e37_79b9_7f4a_7c15) ^ 0x4348_4950_5f43_4c53;
198 Some(self.interact(close_id, close_hit, options.enabled))
199 } else {
200 None
201 };
202 let t = self.ui.theme;
203 let check_target = f32::from(show_check);
204 let spec = TweenSpec::new(0.150, Easing::Cubic([0.4, 0.0, 0.2, 1.0]));
205 let slide = if options.kind == ChipKind::Filter {
206 self.ui
207 .motion
208 .css_tween(id, 3, check_target, check_target, spec, spec)
209 } else {
210 0.0
211 };
212 let stroke_spec = TweenSpec {
213 delay: 0.045,
214 ..spec
215 };
216 let stroke = if options.kind == ChipKind::Filter {
217 self.ui
218 .motion
219 .css_tween(id, 4, check_target, check_target, stroke_spec, stroke_spec)
220 } else {
221 0.0
222 };
223 let foreground = if !r.enabled {
224 t.text.opacity(0.38)
225 } else if effective_selected {
226 t.on_secondary_container
227 } else if options.kind == ChipKind::Assist {
228 t.text
229 } else {
230 t.on_surface_variant
231 };
232 if options.elevated {
233 if r.enabled {
234 self.elevation(b, 8.0, if r.hovered { 2 } else { 1 });
235 }
236 self.rect(
237 b,
238 8.0,
239 if !r.enabled {
240 t.text.opacity(0.12)
241 } else if effective_selected {
242 t.secondary_container
243 } else {
244 t.surface_low
245 },
246 );
247 } else if effective_selected {
248 self.rect(
249 b,
250 8.0,
251 if r.enabled {
252 t.secondary_container
253 } else {
254 t.text.opacity(0.12)
255 },
256 );
257 } else {
258 self.rect(
259 b,
260 8.0,
261 if !r.enabled {
262 t.text.opacity(0.12)
263 } else if r.focused {
264 t.on_surface_variant
265 } else {
266 t.outline
267 },
268 );
269 self.rect(b.inset(1.0), 7.0, t.background);
270 }
271 let overlay_target = if !r.enabled {
272 0.0
273 } else if r.focused {
274 0.12
275 } else if r.hovered {
276 0.08
277 } else {
278 0.0
279 };
280 let overlay_spec = TweenSpec::new(0.150, Easing::Linear);
281 let overlay =
282 self.ui
283 .motion
284 .css_tween(id, 0, overlay_target, 0.0, overlay_spec, overlay_spec);
285 if r.enabled && overlay > 0.0 {
286 self.rect(b, 8.0, foreground.opacity(overlay));
287 }
288 self.ink(r, 8.0, foreground);
289 if show_check && stroke > 0.0 {
290 let left = b.x - 9.0 + 17.0 * slide;
291 let point = |x: f32, y: f32| {
292 [
293 left + (x + 2.0) * (2.0 / 3.0),
294 b.y + 6.0 + (y + 3.0) * (2.0 / 3.0),
295 ]
296 };
297 let p = [point(1.73, 12.91), point(8.1, 19.28), point(22.79, 4.59)];
298 let length = [6.37_f32 * 2.0_f32.sqrt(), 14.69_f32 * 2.0_f32.sqrt()];
299 let mut remaining = stroke * (length[0] + length[1]);
300 self.commands
301 .push(DrawCommand::PushClip(self.clip.intersect(Rect {
302 x: b.x,
303 y: b.y + 4.0,
304 width: 12.0 + 24.0 * slide,
305 height: 24.0,
306 })));
307 for i in 0..2 {
308 let amount = (remaining / length[i]).clamp(0.0, 1.0);
309 if amount > 0.0 {
310 let end = [
311 p[i][0] + (p[i + 1][0] - p[i][0]) * amount,
312 p[i][1] + (p[i + 1][1] - p[i][1]) * amount,
313 ];
314 self.paint_line(p[i], end, 4.0 / 3.0, foreground);
315 }
316 remaining -= length[i];
317 }
318 self.commands.push(DrawCommand::PopClip);
319 }
320 let text_rect = Rect {
321 width: (b.width - if options.show_close { 28.0 } else { 0.0 }).max(0.0),
322 ..b
323 };
324 self.styled_text(
325 text_rect,
326 label,
327 foreground,
328 TextStyle {
329 align: 0,
330 padding: 12.0 + 24.0 * slide,
331 ..TextStyle::LABEL
332 },
333 );
334 let mut close_clicked = false;
335 if let Some(cr) = close_response {
336 close_clicked = cr.clicked;
337 let cx = close_hit.x + close_hit.width * 0.5;
338 let cy = b.y + b.height * 0.5;
339 let circle = Rect {
340 x: cx - 12.0,
341 y: cy - 12.0,
342 width: 24.0,
343 height: 24.0,
344 };
345 if cr.enabled {
346 let alpha = if cr.focused && self.ui.keyboard_focus {
347 0.12
348 } else if cr.hovered {
349 0.08
350 } else {
351 0.0
352 };
353 if alpha > 0.0 {
354 self.rect(circle, 12.0, foreground.opacity(alpha));
355 }
356 }
357 self.ink(Response { bounds: circle, ..cr }, 12.0, foreground);
358 self.paint_line([cx - 4.5, cy - 4.5], [cx + 4.5, cy + 4.5], 1.75, foreground);
359 self.paint_line([cx + 4.5, cy - 4.5], [cx - 4.5, cy + 4.5], 1.75, foreground);
360 let remove_label = format!("Remove {label}");
361 self.semantic(cr, Role::Button, &remove_label, false, None, None, None);
362 }
363 self.focus_ring(r);
364 self.semantic(r, Role::Button, label, effective_selected, None, None, None);
365 (r, close_clicked)
366 }
367}
368
369#[cfg(test)]
370#[path = "chip.spec.rs"]
371mod tests;