1const BASE: u32 = 0xF0000;
6
7#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
8pub enum Icon {
9 Files = 0,
10 Search = 1,
11 Git = 2,
12 Settings = 3,
13 Folder = 4,
14 FolderOpen = 5,
15 File = 6,
16 ChevronRight = 7,
17 ChevronDown = 8,
18 Close = 9,
19 ReplaceAll = 10,
20 Globe = 11,
21 Check = 12,
22 Agents = 13,
23 Outline = 14,
24 ListBullet = 30,
26 ListNumber = 15,
27 ListTask = 16,
28 Quote = 17,
29 Code = 18,
30 Link = 19,
31 Table = 20,
32 Picture = 21,
33 Sigma = 22,
34 Rule = 23,
35 Comment = 24,
36 Export = 25,
37 Play = 26,
38 Cite = 27,
39 More = 28,
40 Diagram = 29,
41 Track = 31,
42 PlayAll = 32,
43 Stop = 33,
44 Restart = 34,
45 AddCell = 35,
46 AddText = 36,
47 Terminal = 37,
48 Diff = 38,
49 Replace = 39,
50 ArrowUp = 40,
51 ArrowDown = 41,
52 Maximize = 42,
54 Minimize = 43,
55}
56
57#[derive(Clone, Copy, Debug, PartialEq, Eq)]
59pub struct Piece {
60 pub icon: Icon,
61 pub width: u8,
62 pub height: u8,
63 pub column: u8,
64 pub row: u8,
65}
66
67impl Piece {
68 pub fn symbol(self) -> char {
69 let code = (self.icon as u32) << 8
70 | u32::from(self.width) << 6
71 | u32::from(self.height) << 4
72 | u32::from(self.column) << 2
73 | u32::from(self.row);
74 char::from_u32(BASE + code).expect("the private plane holds every code")
75 }
76
77 pub fn decode(symbol: &str) -> Option<Self> {
78 let mut chars = symbol.chars();
79 let code = (chars.next()? as u32).checked_sub(BASE)?;
80 if chars.next().is_some() || code >= 1 << 12 {
81 return None;
82 }
83 let icon = match code >> 8 {
84 0 => Icon::Files,
85 1 => Icon::Search,
86 2 => Icon::Git,
87 13 => Icon::Agents,
88 14 => Icon::Outline,
89 _ => Icon::Settings,
90 };
91 Some(Self {
92 icon,
93 width: (code >> 6 & 3) as u8,
94 height: (code >> 4 & 3) as u8,
95 column: (code >> 2 & 3) as u8,
96 row: (code & 3) as u8,
97 })
98 }
99
100 pub fn fallback(self) -> &'static str {
102 const ART: [[[&str; 3]; 2]; 6] = [
103 [["▛", "▀", "▜"], ["▙", "▄", "▟"]],
104 [["▞", "▀", "▚"], ["▚", "▄", "▟"]],
105 [["●", "─", "●"], ["●", "─", "╯"]],
106 [["▚", "▀", "▞"], ["▞", "▄", "▚"]],
107 [["▗", "▄", "▖"], ["▐", "█", "▌"]],
108 [["▪", "─", "─"], [" ", "▪", "─"]],
109 ];
110 let art = match self.icon {
111 Icon::Agents => 4,
112 Icon::Outline => 5,
113 other => (other as usize).min(3),
114 };
115 if self.width == 1 || self.height == 1 {
116 if self.column > 0 {
117 return " ";
118 }
119 return match self.icon {
120 Icon::Files => "▤",
121 Icon::Search => "⌕",
122 Icon::Git => "⎇",
123 Icon::Agents => "◉",
124 Icon::Outline => "≡",
125 _ => "⚙",
126 };
127 }
128 ART[art][self.row.min(1) as usize][self.column.min(2) as usize]
129 }
130}
131
132type Point = (f32, f32);
133
134fn segment(p: Point, a: Point, b: Point) -> f32 {
135 let (dx, dy) = (b.0 - a.0, b.1 - a.1);
136 let t = (((p.0 - a.0) * dx + (p.1 - a.1) * dy) / (dx * dx + dy * dy)).clamp(0.0, 1.0);
137 ((p.0 - a.0 - t * dx).powi(2) + (p.1 - a.1 - t * dy).powi(2)).sqrt()
138}
139
140fn path(p: Point, points: &[Point]) -> f32 {
142 points
143 .windows(2)
144 .map(|pair| segment(p, pair[0], pair[1]))
145 .fold(f32::MAX, f32::min)
146}
147
148fn circle(p: Point, c: Point) -> f32 {
149 ((p.0 - c.0).powi(2) + (p.1 - c.1).powi(2)).sqrt()
150}
151
152fn ring(p: Point, c: Point, radius: f32) -> f32 {
154 (circle(p, c) - radius).abs()
155}
156
157fn rounded(p: Point, min: Point, max: Point, radius: f32) -> f32 {
159 let (cx, cy) = ((min.0 + max.0) / 2.0, (min.1 + max.1) / 2.0);
160 let (qx, qy) = (
161 (p.0 - cx).abs() - (max.0 - min.0) / 2.0 + radius,
162 (p.1 - cy).abs() - (max.1 - min.1) / 2.0 + radius,
163 );
164 let outside = (qx.max(0.0).powi(2) + qy.max(0.0).powi(2)).sqrt();
165 (outside + qx.max(qy).min(0.0) - radius).abs()
166}
167
168fn replace(p: Point) -> f32 {
171 rounded(p, (2.0, 14.0), (10.0, 22.0), 2.0)
172 .min(path(
173 p,
174 &[(6.0, 10.0), (6.0, 5.0), (6.9, 2.9), (9.0, 2.0), (10.0, 2.0)],
175 ))
176 .min(path(p, &[(3.0, 7.0), (6.0, 10.0), (9.0, 7.0)]))
177 .min(path(p, &[(14.0, 4.0), (14.6, 2.6), (16.0, 2.0)]))
178 .min(path(p, &[(16.0, 10.0), (14.6, 9.4), (14.0, 8.0)]))
179 .min(path(p, &[(20.0, 2.0), (21.4, 2.6), (22.0, 4.0)]))
180 .min(path(p, &[(22.0, 8.0), (21.4, 9.4), (20.0, 10.0)]))
181}
182
183fn half_stroke(icon: Icon) -> f32 {
186 match icon {
187 Icon::Files
188 | Icon::Search
189 | Icon::Git
190 | Icon::Settings
191 | Icon::Outline
192 | Icon::File
193 | Icon::Folder
194 | Icon::FolderOpen
195 | Icon::ChevronRight
196 | Icon::ChevronDown
197 | Icon::Close
198 | Icon::ArrowUp
199 | Icon::ArrowDown
200 | Icon::Replace
201 | Icon::Maximize
202 | Icon::Minimize
203 | Icon::ReplaceAll => 0.9,
204 Icon::Agents => 0.8,
205 Icon::Check => 1.7,
206 _ => 1.15,
207 }
208}
209
210pub fn inked(icon: Icon, u: f32, v: f32) -> bool {
213 let p = (u * 24.0, v * 24.0);
214 let distance = match icon {
215 Icon::Files => path(
217 p,
218 &[
219 (9.0, 18.0),
220 (7.6, 17.4),
221 (7.0, 16.0),
222 (7.0, 4.0),
223 (7.6, 2.6),
224 (9.0, 2.0),
225 (16.0, 2.0),
226 (20.0, 6.0),
227 (20.0, 16.0),
228 (19.4, 17.4),
229 (18.0, 18.0),
230 (9.0, 18.0),
231 ],
232 )
233 .min(path(
234 p,
235 &[
236 (20.0, 7.0),
237 (17.0, 7.0),
238 (15.6, 6.4),
239 (15.0, 5.0),
240 (15.0, 2.0),
241 ],
242 ))
243 .min(path(
244 p,
245 &[
246 (3.0, 7.6),
247 (3.0, 20.4),
248 (3.5, 21.5),
249 (4.6, 22.0),
250 (14.4, 22.0),
251 ],
252 )),
253 Icon::Search => ring(p, (11.0, 11.0), 7.0).min(segment(p, (16.65, 16.65), (20.0, 20.0))),
254 Icon::Git => {
255 let joint = (9.0, 9.0);
256 let quarter = p.0 >= joint.0 && p.1 >= joint.1;
257 let curve = if quarter {
258 ring(p, joint, 9.0)
259 } else {
260 f32::MAX
261 };
262 ring(p, (6.0, 18.0), 3.0)
263 .min(ring(p, (18.0, 6.0), 3.0))
264 .min(segment(p, (6.0, 3.0), (6.0, 15.0)))
265 .min(curve)
266 }
267 Icon::Settings => segment(p, (20.0, 7.0), (11.0, 7.0))
269 .min(segment(p, (14.0, 17.0), (5.0, 17.0)))
270 .min(ring(p, (17.0, 17.0), 3.0))
271 .min(ring(p, (7.0, 7.0), 3.0)),
272 Icon::Agents => rounded(p, (4.0, 8.5), (20.0, 20.5), 3.0)
273 .min(path(p, &[(12.0, 8.5), (12.0, 3.5), (8.5, 3.5)]))
274 .min(segment(p, (9.0, 13.0), (9.0, 15.5)))
275 .min(segment(p, (15.0, 13.0), (15.0, 15.5)))
276 .min(segment(p, (1.5, 14.5), (4.0, 14.5)))
277 .min(segment(p, (20.0, 14.5), (22.5, 14.5))),
278 Icon::File => path(
280 p,
281 &[
282 (15.0, 2.0),
283 (6.0, 2.0),
284 (4.6, 2.6),
285 (4.0, 4.0),
286 (4.0, 20.0),
287 (4.6, 21.4),
288 (6.0, 22.0),
289 (18.0, 22.0),
290 (19.4, 21.4),
291 (20.0, 20.0),
292 (20.0, 7.0),
293 (15.0, 2.0),
294 ],
295 )
296 .min(path(
297 p,
298 &[
299 (14.0, 2.0),
300 (14.0, 6.0),
301 (14.6, 7.4),
302 (16.0, 8.0),
303 (20.0, 8.0),
304 ],
305 )),
306 Icon::Folder => path(
307 p,
308 &[
309 (20.0, 20.0),
310 (21.4, 19.4),
311 (22.0, 18.0),
312 (22.0, 8.0),
313 (21.4, 6.6),
314 (20.0, 6.0),
315 (12.1, 6.0),
316 (10.4, 5.1),
317 (9.6, 3.9),
318 (7.9, 3.0),
319 (4.0, 3.0),
320 (2.6, 3.6),
321 (2.0, 5.0),
322 (2.0, 18.0),
323 (2.6, 19.4),
324 (4.0, 20.0),
325 (20.0, 20.0),
326 ],
327 ),
328 Icon::FolderOpen => path(
329 p,
330 &[
331 (6.0, 14.0),
332 (7.5, 11.1),
333 (9.2, 10.0),
334 (20.0, 10.0),
335 (21.9, 12.5),
336 (20.4, 18.5),
337 (18.5, 20.0),
338 (4.0, 20.0),
339 (2.6, 19.4),
340 (2.0, 18.0),
341 (2.0, 5.0),
342 (2.6, 3.6),
343 (4.0, 3.0),
344 (7.9, 3.0),
345 (9.6, 3.9),
346 (10.4, 5.1),
347 (12.1, 6.0),
348 (18.0, 6.0),
349 (19.4, 6.6),
350 (20.0, 8.0),
351 (20.0, 10.0),
352 ],
353 ),
354 Icon::Outline => segment(p, (21.0, 6.0), (8.0, 6.0))
356 .min(segment(p, (21.0, 12.0), (13.0, 12.0)))
357 .min(segment(p, (21.0, 18.0), (13.0, 18.0)))
358 .min(path(
359 p,
360 &[
361 (3.0, 6.0),
362 (3.0, 10.0),
363 (3.6, 11.4),
364 (5.0, 12.0),
365 (8.0, 12.0),
366 ],
367 ))
368 .min(path(
369 p,
370 &[
371 (3.0, 10.0),
372 (3.0, 16.0),
373 (3.6, 17.4),
374 (5.0, 18.0),
375 (8.0, 18.0),
376 ],
377 )),
378 Icon::ListBullet => (circle(p, (5.0, 6.5)) - 0.4)
379 .min(circle(p, (5.0, 12.0)) - 0.4)
380 .min(circle(p, (5.0, 17.5)) - 0.4)
381 .min(segment(p, (9.5, 6.5), (20.5, 6.5)))
382 .min(segment(p, (9.5, 12.0), (20.5, 12.0)))
383 .min(segment(p, (9.5, 17.5), (20.5, 17.5))),
384 Icon::ListNumber => path(p, &[(3.8, 5.2), (5.4, 4.0), (5.4, 9.5)])
385 .min(path(
386 p,
387 &[
388 (3.6, 15.0),
389 (5.0, 13.8),
390 (6.8, 14.8),
391 (3.6, 19.8),
392 (7.2, 19.8),
393 ],
394 ))
395 .min(segment(p, (10.5, 6.8), (20.5, 6.8)))
396 .min(segment(p, (10.5, 16.8), (20.5, 16.8))),
397 Icon::ListTask => rounded(p, (4.0, 4.0), (20.0, 20.0), 3.5)
398 .min(path(p, &[(8.0, 12.2), (11.0, 15.2), (16.2, 9.0)])),
399 Icon::Quote => segment(p, (4.5, 5.0), (4.5, 19.0))
400 .min(segment(p, (9.5, 7.0), (20.5, 7.0)))
401 .min(segment(p, (9.5, 12.0), (20.5, 12.0)))
402 .min(segment(p, (9.5, 17.0), (16.5, 17.0))),
403 Icon::Code => path(p, &[(8.5, 6.5), (3.0, 12.0), (8.5, 17.5)])
404 .min(path(p, &[(15.5, 6.5), (21.0, 12.0), (15.5, 17.5)])),
405 Icon::Link => rounded(p, (2.5, 8.0), (13.5, 16.0), 4.0).min(rounded(
406 p,
407 (10.5, 8.0),
408 (21.5, 16.0),
409 4.0,
410 )),
411 Icon::Table => rounded(p, (3.0, 4.5), (21.0, 19.5), 2.5)
412 .min(segment(p, (3.0, 9.5), (21.0, 9.5)))
413 .min(segment(p, (3.0, 14.5), (21.0, 14.5)))
414 .min(segment(p, (12.0, 4.5), (12.0, 19.5))),
415 Icon::Track => path(
417 p,
418 &[
419 (4.0, 16.5),
420 (15.5, 5.0),
421 (19.0, 8.5),
422 (7.5, 20.0),
423 (4.0, 20.0),
424 (4.0, 16.5),
425 ],
426 )
427 .min(path(p, &[(13.0, 20.5), (21.0, 20.5)])),
428 Icon::Diagram => rounded(p, (2.5, 3.5), (10.0, 9.5), 1.5)
429 .min(rounded(p, (14.0, 3.5), (21.5, 9.5), 1.5))
430 .min(rounded(p, (14.0, 14.5), (21.5, 20.5), 1.5))
431 .min(path(p, &[(10.0, 6.5), (14.0, 6.5)]))
432 .min(path(p, &[(6.2, 9.5), (6.2, 17.5), (14.0, 17.5)])),
433 Icon::Picture => rounded(p, (3.0, 4.5), (21.0, 19.5), 2.5)
434 .min(ring(p, (8.5, 9.8), 1.5))
435 .min(path(
436 p,
437 &[
438 (3.5, 18.0),
439 (9.5, 12.8),
440 (13.5, 16.2),
441 (16.2, 13.8),
442 (20.5, 17.5),
443 ],
444 )),
445 Icon::Sigma => path(
446 p,
447 &[
448 (18.0, 7.5),
449 (18.0, 4.5),
450 (5.5, 4.5),
451 (12.5, 12.0),
452 (5.5, 19.5),
453 (18.0, 19.5),
454 (18.0, 16.5),
455 ],
456 ),
457 Icon::Rule => segment(p, (3.0, 12.0), (21.0, 12.0))
458 .min(segment(p, (8.0, 6.5), (16.0, 6.5)))
459 .min(segment(p, (8.0, 17.5), (16.0, 17.5))),
460 Icon::Comment => rounded(p, (3.0, 4.0), (21.0, 16.5), 3.5)
461 .min(path(p, &[(8.0, 16.5), (8.0, 20.5), (12.5, 16.5)])),
462 Icon::Export => path(p, &[(12.0, 3.5), (12.0, 15.0)])
463 .min(path(p, &[(7.0, 10.5), (12.0, 15.5), (17.0, 10.5)]))
464 .min(segment(p, (4.0, 20.0), (20.0, 20.0))),
465 Icon::Play => path(p, &[(7.0, 4.5), (19.5, 12.0), (7.0, 19.5), (7.0, 4.5)]),
466 Icon::PlayAll => path(p, &[(3.0, 5.5), (12.0, 12.0), (3.0, 18.5), (3.0, 5.5)]).min(path(
467 p,
468 &[(12.0, 5.5), (21.0, 12.0), (12.0, 18.5), (12.0, 5.5)],
469 )),
470 Icon::Stop => rounded(p, (6.0, 6.0), (18.0, 18.0), 1.5),
471 Icon::Restart => {
472 let angle = (p.1 - 12.0).atan2(p.0 - 12.0);
474 let gap = angle > -1.1 && angle < 0.1;
475 let arc = if gap {
476 f32::MAX
477 } else {
478 ring(p, (12.0, 12.0), 7.5)
479 };
480 arc.min(path(p, &[(19.5, 4.0), (19.5, 10.0), (13.8, 10.0)]))
481 }
482 Icon::AddCell => rounded(p, (3.0, 6.0), (21.0, 18.0), 2.0)
483 .min(segment(p, (12.0, 9.0), (12.0, 15.0)))
484 .min(segment(p, (9.0, 12.0), (15.0, 12.0))),
485 Icon::AddText => segment(p, (3.0, 6.0), (21.0, 6.0))
486 .min(segment(p, (3.0, 11.0), (13.0, 11.0)))
487 .min(segment(p, (3.0, 16.0), (10.0, 16.0)))
488 .min(segment(p, (18.0, 13.0), (18.0, 21.0)))
489 .min(segment(p, (14.0, 17.0), (22.0, 17.0))),
490 Icon::Cite => path(p, &[(9.0, 4.5), (5.5, 4.5), (5.5, 19.5), (9.0, 19.5)])
491 .min(path(
492 p,
493 &[(15.0, 4.5), (18.5, 4.5), (18.5, 19.5), (15.0, 19.5)],
494 ))
495 .min(segment(p, (12.0, 9.0), (12.0, 15.0))),
496 Icon::More => (circle(p, (5.0, 12.0)) - 0.5)
497 .min(circle(p, (12.0, 12.0)) - 0.5)
498 .min(circle(p, (19.0, 12.0)) - 0.5),
499 Icon::ChevronRight => path(p, &[(9.5, 6.5), (15.0, 12.0), (9.5, 17.5)]),
500 Icon::ChevronDown => path(p, &[(6.5, 9.5), (12.0, 15.0), (17.5, 9.5)]),
501 Icon::Close => {
502 segment(p, (6.5, 6.5), (17.5, 17.5)).min(segment(p, (17.5, 6.5), (6.5, 17.5)))
503 }
504 Icon::Replace => replace(p),
505 Icon::ArrowUp => path(p, &[(5.0, 12.0), (12.0, 5.0), (19.0, 12.0)]).min(segment(
506 p,
507 (12.0, 5.0),
508 (12.0, 19.0),
509 )),
510 Icon::ArrowDown => path(p, &[(19.0, 12.0), (12.0, 19.0), (5.0, 12.0)]).min(segment(
511 p,
512 (12.0, 5.0),
513 (12.0, 19.0),
514 )),
515 Icon::ReplaceAll => replace(p)
516 .min(path(
517 p,
518 &[
519 (14.0, 14.0),
520 (15.4, 14.6),
521 (16.0, 16.0),
522 (16.0, 20.0),
523 (15.4, 21.4),
524 (14.0, 22.0),
525 ],
526 ))
527 .min(path(
528 p,
529 &[
530 (20.0, 14.0),
531 (21.4, 14.6),
532 (22.0, 16.0),
533 (22.0, 20.0),
534 (21.4, 21.4),
535 (20.0, 22.0),
536 ],
537 )),
538 Icon::Check => path(p, &[(4.5, 12.5), (9.5, 17.5), (19.5, 6.5)]),
539 Icon::Maximize => path(p, &[(15.0, 3.0), (21.0, 3.0), (21.0, 9.0)])
540 .min(path(p, &[(9.0, 21.0), (3.0, 21.0), (3.0, 15.0)]))
541 .min(segment(p, (21.0, 3.0), (14.0, 10.0)))
542 .min(segment(p, (3.0, 21.0), (10.0, 14.0))),
543 Icon::Minimize => path(p, &[(4.0, 14.0), (10.0, 14.0), (10.0, 20.0)])
544 .min(path(p, &[(20.0, 10.0), (14.0, 10.0), (14.0, 4.0)]))
545 .min(segment(p, (14.0, 10.0), (21.0, 3.0)))
546 .min(segment(p, (3.0, 21.0), (10.0, 14.0))),
547 Icon::Terminal => path(p, &[(3.5, 6.0), (10.0, 12.0), (3.5, 18.0)]).min(segment(
548 p,
549 (12.5, 18.0),
550 (20.5, 18.0),
551 )),
552 Icon::Diff => rounded(p, (3.0, 3.0), (21.0, 21.0), 4.0)
553 .min(segment(p, (12.0, 6.5), (12.0, 12.5)))
554 .min(segment(p, (9.0, 9.5), (15.0, 9.5)))
555 .min(segment(p, (9.0, 16.5), (15.0, 16.5))),
556 Icon::Globe => {
557 let (x, y) = ((p.0 - 12.0) / 4.5, (p.1 - 12.0) / 9.5);
558 let meridian = ((x * x + y * y).sqrt() - 1.0).abs() * 4.5;
559 ring(p, (12.0, 12.0), 9.5)
560 .min(meridian)
561 .min(segment(p, (2.5, 12.0), (21.5, 12.0)))
562 }
563 };
564 distance < half_stroke(icon)
565}
566
567#[cfg(test)]
568mod tests {
569 use super::*;
570
571 #[test]
572 fn pieces_survive_the_grid() {
573 for icon in [Icon::Files, Icon::Search, Icon::Git, Icon::Settings] {
574 let piece = Piece {
575 icon,
576 width: 3,
577 height: 2,
578 column: 2,
579 row: 1,
580 };
581 let text = piece.symbol().to_string();
582 assert_eq!(Piece::decode(&text), Some(piece));
583 }
584 assert_eq!(Piece::decode("a"), None);
585 }
586
587 #[test]
588 fn every_icon_has_ink() {
589 for icon in [
590 Icon::Files,
591 Icon::Search,
592 Icon::Git,
593 Icon::Settings,
594 Icon::Agents,
595 Icon::Folder,
596 Icon::File,
597 Icon::Maximize,
598 Icon::Minimize,
599 ] {
600 let dots = (0..50)
601 .flat_map(|y| (0..50).map(move |x| (x, y)))
602 .filter(|(x, y)| inked(icon, *x as f32 / 50.0, *y as f32 / 50.0))
603 .count();
604 assert!(dots > 150, "{icon:?} has {dots}");
605 }
606 }
607}