1use arc_motion::{CubicBezier, is_reduced_motion, motion_now};
8use arc_tokens::{ActiveArcTheme as _, CubicBezierSpec, hex};
9use gpui_kit::{
10 App, Bounds, Corners, Edges, ElementId, Hsla, IntoElement, ParentElement, Pixels, RenderOnce,
11 Role, SharedString, Styled, TestSupportExt as _, Window, base::StyledExt as _, canvas, div,
12 point, prelude::*, px, quad, size,
13};
14use std::cell::RefCell;
15use std::rc::Rc;
16
17const EASE_IN_OUT: CubicBezierSpec = CubicBezierSpec::new(0.77, 0.0, 0.175, 1.0);
19const EASE_OUT: CubicBezierSpec = CubicBezierSpec::new(0.23, 1.0, 0.32, 1.0);
20const EASE_IN_OUT_SOLVER: CubicBezier = CubicBezier::from_spec(EASE_IN_OUT);
21const EASE_OUT_SOLVER: CubicBezier = CubicBezier::from_spec(EASE_OUT);
22
23#[derive(Clone, Copy, Debug, PartialEq)]
28pub struct LatticeLit(pub f32);
29impl LatticeLit {
30 pub const DEFAULT: LatticeLit = LatticeLit(0.62);
31 pub fn fractions(self) -> (f32, f32, f32) {
35 let lit = self.0.clamp(0.05, 1.0);
36 let (r, p) = match lit {
37 v if (v - 0.62).abs() < 0.005 => (18.0, 42.0),
38 v if (v - 0.45).abs() < 0.005 => (13.0, 31.0),
39 v if (v - 0.35).abs() < 0.005 => (10.0, 24.0),
40 v if (v - 0.25).abs() < 0.005 => (7.0, 17.0),
41 v => (v * 18.0 / 0.62, v * 42.0 / 0.62),
43 };
44 (r / 100.0, p / 100.0, lit)
45 }
46}
47
48#[derive(Clone, Copy, Debug, Eq, PartialEq)]
50pub enum LatticePattern {
51 Arrow,
52 Dots,
53 Ripple,
54 Spiral,
55 Orbit,
56 Snake,
57 Sweep,
58 Spin,
59 Rain,
60 Pulse,
61}
62
63impl LatticePattern {
64 fn resolve(self, grid: usize) -> ResolvedPattern {
66 let n = grid * grid;
67 let (raw, loop_steps, scale, lit) = match (self, grid) {
68 (Self::Arrow, 3) => (
69 &[1, 2, 3, 0, 1, 2, 1, 2, 3][..],
70 7.2,
71 1.0,
72 LatticeLit::DEFAULT,
73 ),
74 (Self::Dots, 3) => (
75 &[0, 1, 2, 0, 1, 2, 0, 1, 2][..],
76 3.0,
77 2.4,
78 LatticeLit::DEFAULT,
79 ),
80 (Self::Ripple, 3) => (
81 &[2, 1, 2, 1, 0, 1, 2, 1, 2][..],
82 4.8,
83 1.5,
84 LatticeLit::DEFAULT,
85 ),
86 (Self::Spiral, 3) => (&[0, 1, 2, 7, 8, 3, 6, 5, 4][..], 9.0, 1.2, LatticeLit(0.35)),
87 (Self::Orbit, 3) => (
88 &[0, 1, 2, 7, USIZE_HOLE, 3, 6, 5, 4][..],
89 8.0,
90 1.2,
91 LatticeLit::DEFAULT,
92 ),
93 (Self::Orbit, _) => (
94 &[
95 0, 1, 2, 3, 11, USIZE_HOLE, USIZE_HOLE, 4, 10, USIZE_HOLE, USIZE_HOLE, 5, 9, 8,
96 7, 6,
97 ][..],
98 6.0,
99 1.2,
100 LatticeLit(0.45),
101 ),
102 (Self::Snake, 3) => (&[0, 1, 2, 5, 4, 3, 6, 7, 8][..], 9.0, 1.0, LatticeLit(0.35)),
103 (Self::Snake, _) => (
104 &[0, 1, 2, 3, 7, 6, 5, 4, 8, 9, 10, 11, 15, 14, 13, 12][..],
105 16.0,
106 1.0,
107 LatticeLit(0.25),
108 ),
109 (Self::Sweep, 4) => (
110 &[0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5, 3, 4, 5, 6][..],
111 5.0,
112 1.0,
113 LatticeLit(0.45),
114 ),
115 (Self::Spin, 4) => (
116 &[0, 0, 1, 1, 0, 0, 1, 1, 3, 3, 2, 2, 3, 3, 2, 2][..],
117 4.0,
118 1.6,
119 LatticeLit(0.35),
120 ),
121 (Self::Rain, 4) => (
122 &[0, 2, 1, 3, 1, 3, 2, 4, 2, 4, 3, 5, 3, 5, 4, 6][..],
123 4.0,
124 1.2,
125 LatticeLit(0.35),
126 ),
127 (Self::Pulse, 4) => (
128 &[2, 1, 1, 2, 1, 0, 0, 1, 1, 0, 0, 1, 2, 1, 1, 2][..],
129 2.4,
130 2.5,
131 LatticeLit(0.45),
132 ),
133 (_, _) => (
135 &[0, 1, 2, 7, USIZE_HOLE, 3, 6, 5, 4][..],
136 8.0,
137 1.2,
138 LatticeLit::DEFAULT,
139 ),
140 };
141 let mut cells = Vec::with_capacity(n);
142 for i in 0..n {
143 let v = raw.get(i).copied().unwrap_or(USIZE_HOLE);
144 cells.push(if v == USIZE_HOLE {
145 None
146 } else {
147 Some(v as f32)
148 });
149 }
150 ResolvedPattern {
151 cells,
152 loop_steps,
153 scale,
154 lit,
155 }
156 }
157}
158
159const USIZE_HOLE: usize = usize::MAX;
161
162pub struct ResolvedPattern {
164 pub cells: Vec<Option<f32>>,
165 pub loop_steps: f32,
166 pub scale: f32,
167 pub lit: LatticeLit,
168}
169
170#[derive(Clone, Debug, PartialEq)]
172pub struct LatticeCells {
173 pub cells: Vec<Option<f32>>,
174 pub loop_steps: Option<f32>,
175 pub scale: Option<f32>,
176 pub lit: Option<LatticeLit>,
177}
178
179impl LatticeCells {
180 pub fn resolve(self, grid: usize) -> ResolvedPattern {
182 let n = grid * grid;
183 let cells: Vec<Option<f32>> = (0..n)
184 .map(|i| self.cells.get(i).copied().unwrap_or(None))
185 .collect();
186 let max = cells.iter().flatten().fold(0.0f32, |a, v| a.max(*v));
187 ResolvedPattern {
188 cells,
189 loop_steps: self.loop_steps.unwrap_or(max + 4.2),
190 scale: self.scale.unwrap_or(1.0),
191 lit: self.lit.unwrap_or(LatticeLit::DEFAULT),
192 }
193 }
194}
195
196#[derive(Clone, Debug, PartialEq)]
198pub enum LatticeWave {
199 Named(LatticePattern),
200 Custom(LatticeCells),
201}
202
203impl LatticeWave {
204 fn resolve(self, grid: usize) -> ResolvedPattern {
205 match self {
206 Self::Named(p) => p.resolve(grid),
207 Self::Custom(c) => c.resolve(grid),
208 }
209 }
210}
211
212impl From<LatticePattern> for LatticeWave {
213 fn from(p: LatticePattern) -> Self {
214 Self::Named(p)
215 }
216}
217
218impl From<LatticeCells> for LatticeWave {
219 fn from(c: LatticeCells) -> Self {
220 Self::Custom(c)
221 }
222}
223
224#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
226pub enum LatticeStatus {
227 #[default]
228 Working,
229 Done,
230 Error,
231}
232
233#[derive(Clone)]
235struct LatticeState {
236 anchor: f64,
238 frozen_ds: u32,
240 last_status: LatticeStatus,
241}
242
243pub fn lattice_cell_opacity(phase: f32, cycle: f32, lit: LatticeLit, idle: f32) -> f32 {
246 if cycle <= 0.0 {
247 return idle;
248 }
249 let (rise, peak_end, settle) = lit.fractions();
250 let rise = rise * cycle;
251 let peak_end = peak_end * cycle;
252 let settle = settle * cycle;
253 let p = if phase <= 0.0 { 0.0 } else { phase };
254 if p < rise {
255 let t = p / rise.max(1e-6);
256 idle + (1.0 - idle) * EASE_IN_OUT_SOLVER.sample(t)
257 } else if p < peak_end {
258 1.0
259 } else if p < settle {
260 let t = (p - peak_end) / (settle - peak_end).max(1e-6);
261 1.0 - (1.0 - idle) * EASE_IN_OUT_SOLVER.sample(t)
262 } else {
263 idle
264 }
265}
266
267pub fn lattice_format_ds(ds: u32) -> String {
269 if ds < 600 {
270 format!("{:.1}s", ds as f32 / 10.0)
271 } else {
272 format!("{}m {:.1}s", ds / 600, (ds % 600) as f32 / 10.0)
273 }
274}
275
276pub fn lattice_spoken_ds(ds: u32) -> String {
278 if ds < 600 {
279 format!("{:.1} seconds", ds as f32 / 10.0)
280 } else {
281 format!(
282 "{} minutes {:.1} seconds",
283 ds / 600,
284 (ds % 600) as f32 / 10.0
285 )
286 }
287}
288
289pub fn lattice_mark_cells(grid: usize, status: LatticeStatus) -> &'static [usize] {
291 match (grid, status) {
292 (3, LatticeStatus::Done) => &[2, 3, 5, 7],
293 (3, _) => &[0, 2, 4, 6, 8],
294 (_, LatticeStatus::Done) => &[7, 8, 10, 13],
295 (_, _) => &[0, 3, 5, 6, 9, 10, 12, 15],
296 }
297}
298
299#[derive(IntoElement)]
301pub struct LatticeLoader {
302 id: ElementId,
303 label: SharedString,
304 done_label: SharedString,
305 error_label: SharedString,
306 status: LatticeStatus,
307 wave: LatticeWave,
308 grid: usize,
309 shape_round: bool,
310 color: Option<Hsla>,
311 done_color: Hsla,
312 error_color: Hsla,
313 cell_size: f32,
314 gap: f32,
315 font_size: f32,
316 step_ms: f32,
317 idle_opacity: f32,
318 glow: bool,
319 glow_color: Option<Hsla>,
320 show_timer: bool,
321 elapsed_secs: Option<f64>,
323}
324
325impl LatticeLoader {
326 pub fn new(id: impl Into<ElementId>) -> Self {
327 Self {
328 id: id.into(),
329 label: "Thinking".into(),
330 done_label: "Done in".into(),
331 error_label: "Failed after".into(),
332 status: LatticeStatus::Working,
333 wave: LatticeWave::Named(LatticePattern::Orbit),
334 grid: 3,
335 shape_round: true,
336 color: None,
337 done_color: hex(0x22c55e, 1.0),
338 error_color: hex(0xef4444, 1.0),
339 cell_size: 6.0,
340 gap: 2.0,
341 font_size: 14.0,
342 step_ms: 90.0,
343 idle_opacity: 0.15,
344 glow: false,
345 glow_color: None,
346 show_timer: true,
347 elapsed_secs: None,
348 }
349 }
350 pub fn label(mut self, v: impl Into<SharedString>) -> Self {
351 self.label = v.into();
352 self
353 }
354 pub fn done_label(mut self, v: impl Into<SharedString>) -> Self {
355 self.done_label = v.into();
356 self
357 }
358 pub fn error_label(mut self, v: impl Into<SharedString>) -> Self {
359 self.error_label = v.into();
360 self
361 }
362 pub fn status(mut self, v: LatticeStatus) -> Self {
363 self.status = v;
364 self
365 }
366 pub fn wave(mut self, v: impl Into<LatticeWave>) -> Self {
367 self.wave = v.into();
368 self
369 }
370 pub fn grid(mut self, v: usize) -> Self {
372 self.grid = if v == 4 { 4 } else { 3 };
373 self
374 }
375 pub fn shape_round(mut self, v: bool) -> Self {
377 self.shape_round = v;
378 self
379 }
380 pub fn color(mut self, v: Hsla) -> Self {
382 self.color = Some(v);
383 self
384 }
385 pub fn done_color(mut self, v: Hsla) -> Self {
386 self.done_color = v;
387 self
388 }
389 pub fn error_color(mut self, v: Hsla) -> Self {
390 self.error_color = v;
391 self
392 }
393 pub fn cell_size(mut self, v: f32) -> Self {
394 self.cell_size = v.max(1.0);
395 self
396 }
397 pub fn gap(mut self, v: f32) -> Self {
398 self.gap = v.max(0.0);
399 self
400 }
401 pub fn font_size(mut self, v: f32) -> Self {
402 self.font_size = v.max(6.0);
403 self
404 }
405 pub fn step_ms(mut self, v: f32) -> Self {
407 self.step_ms = v.max(1.0);
408 self
409 }
410 pub fn idle_opacity(mut self, v: f32) -> Self {
411 self.idle_opacity = v.clamp(0.0, 1.0);
412 self
413 }
414 pub fn glow(mut self, v: bool) -> Self {
415 self.glow = v;
416 self
417 }
418 pub fn glow_color(mut self, v: Hsla) -> Self {
420 self.glow_color = Some(v);
421 self
422 }
423 pub fn show_timer(mut self, v: bool) -> Self {
424 self.show_timer = v;
425 self
426 }
427 pub fn elapsed_secs(mut self, v: f64) -> Self {
429 self.elapsed_secs = Some(v);
430 self
431 }
432}
433
434impl RenderOnce for LatticeLoader {
435 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
436 let theme = cx.arc_theme();
437 let colors = theme.colors;
438 let reduced = is_reduced_motion(cx);
439 let now = motion_now(cx);
440
441 let grid = if self.grid == 4 { 4 } else { 3 };
442 let n = grid;
443 let pat = self.wave.clone().resolve(grid);
444 let d = (self.step_ms * pat.scale).max(1.0) / 1000.0; let cycle = (pat.loop_steps * d).max(0.05); let lit = LatticeLit(pat.lit.0.clamp(0.05, 1.0));
447 let ink = self.color.unwrap_or(colors.foreground);
448 let mark_color = match self.status {
449 LatticeStatus::Error => self.error_color,
450 _ => self.done_color,
451 };
452 let glow_ink = self.glow_color.unwrap_or(ink);
453 let glow_mark = self.glow_color.unwrap_or(mark_color);
454
455 let state_cell = window
457 .use_keyed_state(
458 ElementId::NamedChild(self.id.clone().into(), "lattice-state".into()),
459 cx,
460 |_, _| {
461 Rc::new(RefCell::new(LatticeState {
462 anchor: now,
463 frozen_ds: 0,
464 last_status: LatticeStatus::Working,
465 }))
466 },
467 )
468 .read(cx)
469 .clone();
470 let ds = {
471 let mut st = state_cell.borrow_mut();
472 if st.last_status != self.status {
473 match self.status {
474 LatticeStatus::Working => {
475 st.anchor = now;
477 st.frozen_ds = 0;
478 }
479 _ => {
480 let live = ((now - st.anchor).max(0.0) * 10.0) as u32;
482 st.frozen_ds = live;
483 }
484 }
485 st.last_status = self.status;
486 }
487 match (self.status, self.elapsed_secs) {
488 (_, Some(e)) => (e.max(0.0) * 10.0) as u32,
489 (LatticeStatus::Working, None) => ((now - st.anchor).max(0.0) * 10.0) as u32,
490 (_, None) => st.frozen_ds,
491 }
492 };
493 let timer_text = lattice_format_ds(ds);
494 let working = self.status == LatticeStatus::Working;
495 let needs_anim_frame = working && self.elapsed_secs.is_none();
496
497 let wave_progress = if working {
499 (now % cycle as f64) as f32
500 } else {
501 0.0
502 };
503
504 let layer_progress = if working { 0.0f32 } else { 1.0f32 };
506
507 let grid_px_w = n as f32 * self.cell_size + (n.saturating_sub(1)) as f32 * self.gap;
508 let grid_px_h = grid_px_w;
509
510 let cells: Vec<(usize, Option<f32>)> =
511 pat.cells.iter().enumerate().map(|(i, v)| (i, *v)).collect();
512 let mark_set: Vec<usize> = lattice_mark_cells(grid, self.status).to_vec();
513 let status = self.status;
514
515 let lattice_canvas = canvas(
518 move |bounds, _, _| bounds,
519 move |bounds: Bounds<Pixels>, _, window, _| {
520 let cell = self.cell_size.max(1.0);
521 let gap = self.gap.max(0.0);
522 let radius = if self.shape_round {
523 cell * 0.5
524 } else {
525 (cell * 0.25).max(1.0)
526 };
527 let mark_alpha = layer_progress;
529 let run_alpha = 1.0 - layer_progress;
530 let origin_x = f32::from(bounds.origin.x);
531 let origin_y = f32::from(bounds.origin.y);
532
533 if run_alpha > 0.001 {
535 for (i, unit) in &cells {
536 let Some(unit) = unit else { continue };
537 let col = (*i % n) as f32;
538 let row = (*i / n) as f32;
539 let x = origin_x + col * (cell + gap);
540 let y = origin_y + row * (cell + gap);
541 let delay = unit * d;
542 let phase = if reduced {
543 (now % 1.4) as f32 / 1.4 * cycle
545 } else {
546 (wave_progress - delay).rem_euclid(cycle)
547 };
548 let mut alpha = lattice_cell_opacity(phase, cycle, lit, self.idle_opacity);
549 if reduced {
550 alpha *= 0.7;
551 }
552 let mut c = ink;
553 c.a *= alpha * run_alpha;
554 window.paint_quad(quad(
555 Bounds::new(point(px(x), px(y)), size(px(cell), px(cell))),
556 Corners::all(px(radius)),
557 c,
558 Edges::all(px(0.0)),
559 Hsla::default(),
560 gpui_kit::BorderStyle::default(),
561 ));
562 if self.glow && alpha > 0.05 {
563 let halo = cell * 1.2;
566 for k in 1..=3usize {
567 let spread = halo * (k as f32) / 3.0;
568 let mut hc = glow_ink;
569 hc.a *= alpha * run_alpha * 0.10 / k as f32;
570 let b = Bounds::new(
571 point(px(x - spread + cell * 0.5), px(y - spread + cell * 0.5)),
572 size(px(cell + spread * 2.0), px(cell + spread * 2.0)),
573 );
574 window.paint_quad(quad(
575 b,
576 Corners::all(px((cell + spread * 2.0) * 0.5)),
577 hc,
578 Edges::all(px(0.0)),
579 Hsla::default(),
580 gpui_kit::BorderStyle::default(),
581 ));
582 }
583 }
584 }
585 for (i, unit) in &cells {
587 if unit.is_some() {
588 continue;
589 }
590 let col = (*i % n) as f32;
591 let row = (*i / n) as f32;
592 let mut c = ink;
593 c.a *= self.idle_opacity * 0.47 * run_alpha;
594 window.paint_quad(quad(
595 Bounds::new(
596 point(
597 px(origin_x + col * (cell + gap)),
598 px(origin_y + row * (cell + gap)),
599 ),
600 size(px(cell), px(cell)),
601 ),
602 Corners::all(px(radius)),
603 c,
604 Edges::all(px(0.0)),
605 Hsla::default(),
606 gpui_kit::BorderStyle::default(),
607 ));
608 }
609 }
610
611 if mark_alpha > 0.001 {
613 let scale = 0.9 + 0.1 * ease_out_sample(mark_alpha);
614 let cx_pt = origin_x + grid_px_w * 0.5;
615 let cy_pt = origin_y + grid_px_h * 0.5;
616 for i in &mark_set {
617 let col = (*i % n) as f32;
618 let row = (*i / n) as f32;
619 let base_x = origin_x + col * (cell + gap);
620 let base_y = origin_y + row * (cell + gap);
621 let cx_cell = base_x + cell * 0.5;
623 let cy_cell = base_y + cell * 0.5;
624 let sx = cx_pt + (cx_cell - cx_pt) * scale;
625 let sy = cy_pt + (cy_cell - cy_pt) * scale;
626 let s = cell * scale;
627 let mut c = mark_color;
628 c.a *= mark_alpha;
629 window.paint_quad(quad(
630 Bounds::new(
631 point(px(sx - s * 0.5), px(sy - s * 0.5)),
632 size(px(s), px(s)),
633 ),
634 Corners::all(px(if self.shape_round {
635 s * 0.5
636 } else {
637 (s * 0.25).max(1.0)
638 })),
639 c,
640 Edges::all(px(0.0)),
641 Hsla::default(),
642 gpui_kit::BorderStyle::default(),
643 ));
644 if self.glow {
645 let halo = cell * 1.2;
646 for k in 1..=2usize {
647 let spread = halo * (k as f32) / 2.0;
648 let mut hc = glow_mark;
649 hc.a *= mark_alpha * 0.12 / k as f32;
650 window.paint_quad(quad(
651 Bounds::new(
652 point(
653 px(sx - s * 0.5 - spread + s * 0.5),
654 px(sy - s * 0.5 - spread + s * 0.5),
655 ),
656 size(px(s + spread * 2.0), px(s + spread * 2.0)),
657 ),
658 Corners::all(px((s + spread * 2.0) * 0.5)),
659 hc,
660 Edges::all(px(0.0)),
661 Hsla::default(),
662 gpui_kit::BorderStyle::default(),
663 ));
664 }
665 }
666 }
667 }
668 },
669 )
670 .w(px(grid_px_w))
671 .h(px(grid_px_h));
672
673 let active = status;
675 let text_items = [
676 (LatticeStatus::Working, self.label.clone()),
677 (LatticeStatus::Done, self.done_label.clone()),
678 (LatticeStatus::Error, self.error_label.clone()),
679 ];
680 let active_label = text_items
683 .iter()
684 .find(|(s, _)| *s == active)
685 .map(|(_, t)| t.clone())
686 .unwrap_or_else(|| self.label.clone());
687
688 let timer_el = if self.show_timer {
689 Some(
690 div()
691 .text_size(px(self.font_size * 0.875))
692 .text_color(ink.opacity(0.6))
693 .child(SharedString::from(timer_text)),
694 )
695 } else {
696 None
697 };
698
699 let announce = if working {
700 format!("{}, in progress", self.label)
701 } else if status == LatticeStatus::Done {
702 format!(
703 "{}{}",
704 self.done_label,
705 if self.show_timer {
706 format!(" {}", lattice_spoken_ds(ds))
707 } else {
708 String::new()
709 }
710 )
711 } else {
712 format!(
713 "{}{}",
714 self.error_label,
715 if self.show_timer {
716 format!(" {}", lattice_spoken_ds(ds))
717 } else {
718 String::new()
719 }
720 )
721 };
722
723 if needs_anim_frame {
724 window.request_animation_frame();
725 }
726
727 div()
728 .id(self.id.clone())
729 .test_support()
730 .role(Role::Status)
731 .aria_label(SharedString::from(announce))
732 .flex_none()
733 .flex()
734 .items_center()
735 .gap(px(self.font_size * 0.625))
736 .child(lattice_canvas)
737 .child(
738 div()
739 .text_size(px(self.font_size))
740 .font_medium()
741 .text_color(ink)
742 .child(active_label),
743 )
744 .children(timer_el)
745 }
746}
747
748fn ease_out_sample(t: f32) -> f32 {
750 EASE_OUT_SOLVER.sample(t)
751}
752
753#[cfg(test)]
754mod tests {
755 use super::*;
756
757 fn close(a: f32, b: f32) -> bool {
758 (a - b).abs() < 1e-3
759 }
760
761 #[test]
762 fn lattice_keyframes_track_source_curve_shape() {
763 let lit = LatticeLit(0.62);
765 let idle = 0.15;
766 let (rise, peak_end, settle) = lit.fractions();
767 assert!(close(lattice_cell_opacity(0.0, 1.0, lit, idle), idle));
768 let mid = lattice_cell_opacity(rise * 0.5, 1.0, lit, idle);
770 assert!(mid > idle && mid < 1.0, "mid={mid}");
771 assert!(close(
773 lattice_cell_opacity((rise + peak_end) * 0.5, 1.0, lit, idle),
774 1.0
775 ));
776 let tail = lattice_cell_opacity((peak_end + settle) * 0.5, 1.0, lit, idle);
778 assert!(tail < 1.0 && tail > idle, "tail={tail}");
779 assert!(close(lattice_cell_opacity(settle, 1.0, lit, idle), idle));
780 assert!(close(
781 lattice_cell_opacity(settle + 0.1, 1.0, lit, idle),
782 idle
783 ));
784 }
785
786 #[test]
787 fn lattice_lit_variants_compress_the_wave() {
788 let lit = LatticeLit(0.35);
790 let idle = 0.15;
791 let (rise, peak_end, settle) = lit.fractions();
792 assert!(close(
793 lattice_cell_opacity((rise + peak_end) * 0.5, 1.0, lit, idle),
794 1.0
795 ));
796 assert!(close(lattice_cell_opacity(settle, 1.0, lit, idle), idle));
797 let (r45, p45, s45) = LatticeLit(0.45).fractions();
799 assert!(close(r45, 0.13) && close(p45, 0.31) && close(s45, 0.45));
800 let (r25, p25, s25) = LatticeLit(0.25).fractions();
801 assert!(close(r25, 0.07) && close(p25, 0.17) && close(s25, 0.25));
802 assert!(close(
804 lattice_cell_opacity(2.0 * 0.62, 2.0, LatticeLit(0.62), idle),
805 idle
806 ));
807 }
808
809 #[test]
810 fn lattice_timer_formats_like_source() {
811 assert_eq!(lattice_format_ds(0), "0.0s");
812 assert_eq!(lattice_format_ds(41), "4.1s");
813 assert_eq!(lattice_format_ds(599), "59.9s");
814 assert_eq!(lattice_format_ds(600), "1m 0.0s");
815 assert_eq!(lattice_format_ds(641), "1m 4.1s");
816 assert_eq!(lattice_spoken_ds(41), "4.1 seconds");
817 assert_eq!(lattice_spoken_ds(641), "1 minutes 4.1 seconds");
818 }
819
820 #[test]
821 fn lattice_marks_match_source_cells() {
822 assert_eq!(lattice_mark_cells(3, LatticeStatus::Done), &[2, 3, 5, 7]);
823 assert_eq!(
824 lattice_mark_cells(3, LatticeStatus::Error),
825 &[0, 2, 4, 6, 8]
826 );
827 assert_eq!(lattice_mark_cells(4, LatticeStatus::Done), &[7, 8, 10, 13]);
828 assert_eq!(
829 lattice_mark_cells(4, LatticeStatus::Error),
830 &[0, 3, 5, 6, 9, 10, 12, 15]
831 );
832 }
833
834 #[test]
835 fn lattice_patterns_resolve_with_holes_and_cycles() {
836 let o3 = LatticePattern::Orbit.resolve(3);
838 assert_eq!(o3.cells[4], None);
839 assert_eq!(o3.cells.len(), 9);
840 assert!(close(o3.loop_steps, 8.0));
841 assert!(close(o3.scale, 1.2));
842 assert!(close(o3.lit.0, 0.62));
843 let o4 = LatticePattern::Orbit.resolve(4);
844 assert_eq!(o4.cells[5], None);
845 assert_eq!(o4.cells[6], None);
846 assert!(close(o4.lit.0, 0.45));
847 let s4 = LatticePattern::Snake.resolve(4);
849 assert!(close(s4.lit.0, 0.25));
850 assert!(close(s4.loop_steps, 16.0));
851 let p4 = LatticePattern::Pulse.resolve(4);
852 assert!(close(p4.scale, 2.5));
853 let sp = LatticePattern::Spin.resolve(4);
855 assert_eq!(sp.cells[0], Some(0.0));
856 assert_eq!(sp.cells[1], Some(0.0));
857 assert_eq!(sp.cells[2], Some(1.0));
858 }
859
860 #[test]
861 fn lattice_custom_cells_follow_source_defaults() {
862 let c = LatticeCells {
864 cells: vec![Some(0.0), None, Some(3.0)],
865 loop_steps: None,
866 scale: None,
867 lit: None,
868 };
869 let r = c.resolve(2);
870 assert_eq!(r.cells.len(), 4); assert_eq!(r.cells[1], None);
872 assert_eq!(r.cells[3], None);
873 assert!(close(r.loop_steps, 7.2)); assert!(close(r.scale, 1.0));
875 assert!(close(r.lit.0, 0.62));
876 let c2 = LatticeCells {
878 cells: vec![Some(1.0)],
879 loop_steps: Some(5.0),
880 scale: Some(2.0),
881 lit: Some(LatticeLit(0.4)),
882 };
883 let r2 = c2.resolve(3);
884 assert!(close(r2.loop_steps, 5.0));
885 assert!(close(r2.scale, 2.0));
886 assert!(close(r2.lit.0, 0.4));
887 }
888
889 #[test]
890 fn lattice_cycle_and_delay_follow_step_scale() {
891 let p = LatticePattern::Dots.resolve(3);
893 assert!(close(p.loop_steps, 3.0));
894 assert!(close(p.scale, 2.4));
895 let step_ms = 90.0;
896 let d = step_ms * p.scale / 1000.0;
897 let cycle = p.loop_steps * d;
898 assert!(close(cycle, 3.0 * 90.0 * 2.4 / 1000.0));
899 }
900}