dustnet-client 0.4.0

Dustnet client library: transport, viewer, compositor, and AML/WASM runtime
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
//! Panel transitions as `Animation` implementations.
//!
//! A trait-based adapter that keeps both old and new state buffers
//! live and writes the blended result into the scene node's buffer
//! per tick.
//!
//! ## Supported kinds
//!
//! - `Cut`    — scheduled as instantaneous; not modeled as a
//!   `TransitionAdapter` at all (the caller skips scheduling).
//! - `Fade`   — old cells vanish into black, hold, new cells appear.
//! - `Slide*` — boundary sweeps across the region; one layer on each
//!   side.
//! - `DrawDown` — traces the top edge, then reveals complete rows downward.
//! - `DrawRight` — traces the left edge, then reveals complete columns rightward.
//! - `DrawLeft` — the mirror: traces the right edge, then reveals columns leftward.
//! - `DrawOut` — grows a live rectangular frame from the panel centre.
//! - `Dissolve` — per-cell stochastic mask picks old or new.
//!
//! ## Shape-mismatched states
//!
//! When the old and new state buffers differ in position or size,
//! `TransitionAdapter` composes them over the **union** rect: cells
//! exclusive to A source from A, cells exclusive to B source from B,
//! overlapping cells follow the per-kind rule. "State A occupies
//! 10×5 at the top left; state B occupies 3×3 at the bottom right —
//! dissolve, wipe, slide all work."

use crate::compositor::layout::Rect;
use crate::compositor::layout::cell::{Cell, CellBuffer};
use crate::compositor::scene::{NodeId, Scene};
use crate::parser::ast::TransitionKind;

use super::runtime::AnimState;
use super::{AdvanceCtx, AdvanceResult, Animation};

/// Transition tick cadence (~30fps).
pub const TICK_MS: u32 = 33;

/// A transition adapter drives a per-cell blend between two buffers
/// over `duration_ms` into a target scene node's buffer.
pub struct TransitionAdapter {
    id: String,
    target_node: NodeId,
    /// Region the target panel occupies in scene-screen coordinates.
    /// The adapter's output paints into the target node's buffer at
    /// (0, 0); the region is used only for shape-mismatch math.
    target_region: Rect,
    /// Old state's buffer + its rect (in scene-screen coords).
    old_buf: CellBuffer,
    old_rect: Rect,
    /// New state's buffer + its rect (in scene-screen coords).
    new_buf: CellBuffer,
    new_rect: Rect,
    kind: TransitionKind,
    duration_ms: u32,
    elapsed_ms: u32,
    state: AnimState,
    /// Seeded splitmix64 for deterministic dissolve masks. Seeded from
    /// target_region + duration so the same transition looks identical
    /// every time (important for snapshot tests).
    seed: u64,
    /// Density-adaptive easing exponent for dissolve. Computed from the
    /// count of opaque cells in `new_buf`; higher counts get a steeper
    /// curve so the absolute number of cells revealed at the start of
    /// the transition stays small regardless of total content density.
    /// See `compute_dissolve_exponent`. Cached at construction so we
    /// don't redo the count + log per cell per tick.
    dissolve_exponent: f32,
}

/// Choose a power-curve exponent for the dissolve threshold so the
/// "first cell" appears at roughly the same fractional t regardless
/// of how many cells the panel covers.
///
/// Uses `width * height` (the box's full cell capacity) as the
/// density signal — cheap, no buffer scan, and the user-facing
/// character count is bounded by it. Solving `t_first ^ p ≈ 1 / N`
/// for `t_first = 0.1` yields `p = log10(N)`. Clamped to `[2.0, 6.0]`
/// so empty/sparse rects still get a perceptible ease-in and very
/// dense ones don't degenerate into a hard cut at midpoint.
fn compute_dissolve_exponent(rect: Rect) -> f32 {
    let cells = (rect.w as u32).saturating_mul(rect.h as u32);
    if cells == 0 {
        return 2.0;
    }
    (cells as f32).log10().clamp(2.0, 6.0)
}

impl TransitionAdapter {
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        id: String,
        target_node: NodeId,
        target_region: Rect,
        old_buf: CellBuffer,
        old_rect: Rect,
        new_buf: CellBuffer,
        new_rect: Rect,
        kind: TransitionKind,
        duration_ms: u32,
    ) -> Self {
        // Derive a seed from the target region and duration. Deterministic
        // per transition instance; two identical transitions produce the
        // same mask. Not cryptographically strong — just reproducible.
        let seed = ((target_region.x as u64) << 48)
            ^ ((target_region.y as u64) << 32)
            ^ ((target_region.w as u64) << 16)
            ^ (duration_ms as u64);
        let dissolve_exponent = compute_dissolve_exponent(new_rect);
        Self {
            id,
            target_node,
            target_region,
            old_buf,
            old_rect,
            new_buf,
            new_rect,
            kind,
            duration_ms,
            elapsed_ms: 0,
            state: if matches!(kind, TransitionKind::Cut) || duration_ms == 0 {
                AnimState::Finished
            } else {
                AnimState::Running
            },
            seed,
            dissolve_exponent,
        }
    }

    /// The scene node this adapter paints into. Exposed so the
    /// composite walk can suppress the panel's children's chrome
    /// during transitions (the panel buffer is then the sole source
    /// of visible cells in the panel's region).
    pub fn target_node(&self) -> NodeId {
        self.target_node
    }

    /// Progress ∈ [0, 1].
    pub fn t(&self) -> f32 {
        if self.duration_ms == 0 {
            1.0
        } else {
            (self.elapsed_ms as f32 / self.duration_ms as f32).clamp(0.0, 1.0)
        }
    }

    fn cell_from_old(&self, screen_x: u16, screen_y: u16) -> Option<Cell> {
        let local_x = screen_x.checked_sub(self.old_rect.x)?;
        let local_y = screen_y.checked_sub(self.old_rect.y)?;
        if local_x >= self.old_rect.w || local_y >= self.old_rect.h {
            return None;
        }
        self.old_buf.get(local_x, local_y).cloned()
    }

    fn cell_from_new(&self, screen_x: u16, screen_y: u16) -> Option<Cell> {
        let local_x = screen_x.checked_sub(self.new_rect.x)?;
        let local_y = screen_y.checked_sub(self.new_rect.y)?;
        if local_x >= self.new_rect.w || local_y >= self.new_rect.h {
            return None;
        }
        self.new_buf.get(local_x, local_y).cloned()
    }

    /// Grow a temporary version of the new frame from its centre. Border
    /// cells are sampled from the corresponding edge of the completed frame,
    /// so the viewer sees a real rectangle expanding in both axes instead of
    /// an interior crop whose border appears only on the final tick.
    fn draw_out_cell(&self, screen_x: u16, screen_y: u16, old: Option<Cell>) -> Option<Cell> {
        let local_x = screen_x.checked_sub(self.new_rect.x)?;
        let local_y = screen_y.checked_sub(self.new_rect.y)?;
        let w = self.new_rect.w;
        let h = self.new_rect.h;
        if w == 0 || h == 0 || local_x >= w || local_y >= h {
            return old;
        }
        if self.t() >= 1.0 {
            return self.cell_from_new(screen_x, screen_y);
        }

        let revealed_w = ((self.t() * w as f32).ceil() as u16).clamp(1, w);
        let revealed_h = ((self.t() * h as f32).ceil() as u16).clamp(1, h);
        let start_x = w.saturating_sub(revealed_w) / 2;
        let start_y = h.saturating_sub(revealed_h) / 2;
        let end_x = start_x.saturating_add(revealed_w);
        let end_y = start_y.saturating_add(revealed_h);

        if local_x < start_x || local_x >= end_x || local_y < start_y || local_y >= end_y {
            return old;
        }

        let source_x = if local_x == start_x {
            0
        } else if local_x + 1 == end_x {
            w - 1
        } else {
            local_x
        };
        let source_y = if local_y == start_y {
            0
        } else if local_y + 1 == end_y {
            h - 1
        } else {
            local_y
        };
        self.new_buf.get(source_x, source_y).cloned()
    }

    /// Deterministic splitmix64 cell-threshold for dissolve/fade masks.
    /// The threshold is stable per-cell within a transition instance
    /// (derived from cell coords + seed), so the mask is reproducible.
    fn cell_threshold(&self, screen_x: u16, screen_y: u16, salt: u64) -> f32 {
        let mut x = self.seed ^ ((screen_x as u64) << 16) ^ (screen_y as u64) ^ salt;
        x = (x ^ (x >> 30)).wrapping_mul(0xBF58476D1CE4E5B9);
        x = (x ^ (x >> 27)).wrapping_mul(0x94D049BB133111EB);
        x ^= x >> 31;
        ((x % 1000) as f32) / 1000.0
    }

    /// Compose one blended cell at `(screen_x, screen_y)` given the
    /// current progress `t`. Returns `None` when neither old nor new
    /// contributes at this position — the caller leaves the destination
    /// cell untouched (reveals the base layer).
    ///
    /// **Shape-mismatch handling**: cells inside one rect but outside
    /// the other are sourced from the rect that contains them — they
    /// don't get gated on the transition threshold, since the only
    /// "alternative" is "empty," which would just create flicker. Cells
    /// inside *both* rects get the threshold treatment.
    pub fn blend_cell(&self, screen_x: u16, screen_y: u16) -> Option<Cell> {
        let t = self.t();
        let in_old = self.old_rect.contains_point(screen_x, screen_y);
        let in_new = self.new_rect.contains_point(screen_x, screen_y);
        let old = self.cell_from_old(screen_x, screen_y);
        let new = self
            .cell_from_new(screen_x, screen_y)
            .map(|cell| self.mask_unfurl_title(screen_x, screen_y, cell));

        // Shape-mismatch fast paths: cell is only in one rect, so the
        // other state has nothing to say about it.
        if in_old && !in_new {
            return old;
        }
        if in_new && !in_old {
            return new;
        }
        if !in_old && !in_new {
            return None;
        }

        match self.kind {
            TransitionKind::Cut => new.or(old),
            TransitionKind::Fade => {
                let threshold = self.cell_threshold(screen_x, screen_y, 0xFADE);
                if (0.4..=0.6).contains(&t) {
                    // Hold black / base — neither layer wins.
                    None
                } else if t < 0.4 {
                    let progress = t / 0.4;
                    if progress <= threshold { old } else { None }
                } else {
                    let progress = (t - 0.6) / 0.4;
                    if progress > threshold { new } else { None }
                }
            }
            TransitionKind::Dissolve => {
                // Density-adaptive ease-in-out. The exponent `p` was
                // computed from `new_rect`'s w*h (`compute_dissolve_exponent`)
                // so a denser reveal gets a steeper curve — the absolute
                // count of cells visible at any fraction of t stays in a
                // perceptual band regardless of total content density.
                //
                // Standard ease-in-out generalized to power `p`:
                //   t<0.5: f = 2^(p-1) * t^p
                //   t>=0.5: f = 1 - 2^(p-1) * (1-t)^p
                // For p=2 this collapses to the classic 2t² / 1-2(1-t)²;
                // higher p compresses the slow-start phase further.
                //
                // Returns `new` or `old` directly (no `.or()` fallback)
                // so a transparent cell on either side dissolves cleanly
                // through transparency rather than holding the opposite
                // side opaquely — critical for empty→content transitions
                // where the old state is intentionally invisible.
                let p = self.dissolve_exponent;
                let scale = 2.0_f32.powf(p - 1.0);
                let eased_t = if t < 0.5 {
                    scale * t.powf(p)
                } else {
                    1.0 - scale * (1.0 - t).powf(p)
                };
                let threshold = self.cell_threshold(screen_x, screen_y, 0xD155);
                if eased_t > threshold { new } else { old }
            }
            TransitionKind::DrawDown => {
                // Grow the top edge outward from its centre, then unfurl
                // rows from top to bottom. The last revealed row samples the
                // authored bottom edge, so the growing box remains visibly
                // closed throughout the animation.
                const TOP_EDGE_PHASE: f32 = 0.50;
                let local_x = screen_x.saturating_sub(self.target_region.x);
                let local_y = screen_y.saturating_sub(self.target_region.y);
                if t < TOP_EDGE_PHASE {
                    let progress = t / TOP_EDGE_PHASE;
                    let revealed_columns = (progress * self.target_region.w as f32).ceil() as u16;
                    let start = self.target_region.w.saturating_sub(revealed_columns) / 2;
                    let end = start.saturating_add(revealed_columns);
                    if local_y == 0 && local_x >= start && local_x < end {
                        new
                    } else {
                        old
                    }
                } else {
                    let progress = (t - TOP_EDGE_PHASE) / (1.0 - TOP_EDGE_PHASE);
                    let remaining_rows = self.target_region.h.saturating_sub(1);
                    let revealed_rows = 1 + (progress * remaining_rows as f32).ceil() as u16;
                    if local_y >= revealed_rows {
                        old
                    } else if revealed_rows > 1
                        && revealed_rows < self.target_region.h
                        && local_y + 1 == revealed_rows
                    {
                        let new_x = screen_x.saturating_sub(self.new_rect.x);
                        self.new_buf
                            .get(new_x, self.new_buf.height.saturating_sub(1))
                            .cloned()
                    } else {
                        new
                    }
                }
            }
            TransitionKind::DrawRight => {
                // Rotated counterpart to DrawDown: construct the left edge
                // top-to-bottom, then unfurl columns left-to-right. The last
                // revealed column samples the authored right edge so the box
                // remains visibly closed while it grows.
                const LEFT_EDGE_PHASE: f32 = 0.50;
                let local_x = screen_x.saturating_sub(self.target_region.x);
                let local_y = screen_y.saturating_sub(self.target_region.y);
                if t < LEFT_EDGE_PHASE {
                    let progress = t / LEFT_EDGE_PHASE;
                    let revealed_rows = (progress * self.target_region.h as f32).ceil() as u16;
                    if local_x == 0 && local_y < revealed_rows {
                        new
                    } else {
                        old
                    }
                } else {
                    let progress = (t - LEFT_EDGE_PHASE) / (1.0 - LEFT_EDGE_PHASE);
                    let remaining_columns = self.target_region.w.saturating_sub(1);
                    let revealed_columns = 1 + (progress * remaining_columns as f32).ceil() as u16;
                    if local_x >= revealed_columns {
                        old
                    } else if revealed_columns > 1
                        && revealed_columns < self.target_region.w
                        && local_x + 1 == revealed_columns
                    {
                        let new_y = screen_y.saturating_sub(self.new_rect.y);
                        self.new_buf
                            .get(self.new_buf.width.saturating_sub(1), new_y)
                            .cloned()
                    } else {
                        new
                    }
                }
            }
            TransitionKind::DrawLeft => {
                // Mirror of DrawRight: construct the right edge top-to-bottom,
                // then unfurl columns right-to-left. The frontier column samples
                // the authored left edge so the box stays visibly closed while it
                // grows -- the same trick DrawRight plays with the right edge,
                // reflected.
                const RIGHT_EDGE_PHASE: f32 = 0.50;
                let local_x = screen_x.saturating_sub(self.target_region.x);
                let local_y = screen_y.saturating_sub(self.target_region.y);
                let last_x = self.target_region.w.saturating_sub(1);
                if t < RIGHT_EDGE_PHASE {
                    let progress = t / RIGHT_EDGE_PHASE;
                    let revealed_rows = (progress * self.target_region.h as f32).ceil() as u16;
                    if local_x == last_x && local_y < revealed_rows {
                        new
                    } else {
                        old
                    }
                } else {
                    let progress = (t - RIGHT_EDGE_PHASE) / (1.0 - RIGHT_EDGE_PHASE);
                    let remaining_columns = self.target_region.w.saturating_sub(1);
                    let revealed_columns = 1 + (progress * remaining_columns as f32).ceil() as u16;
                    // Counted inward from the right edge, so the frontier is a
                    // column index measured backwards.
                    let frontier = last_x.saturating_sub(revealed_columns.saturating_sub(1));
                    if local_x < frontier {
                        old
                    } else if revealed_columns > 1
                        && revealed_columns < self.target_region.w
                        && local_x == frontier
                    {
                        let new_y = screen_y.saturating_sub(self.new_rect.y);
                        self.new_buf.get(0, new_y).cloned()
                    } else {
                        new
                    }
                }
            }
            TransitionKind::DrawOut => self.draw_out_cell(screen_x, screen_y, old),
            TransitionKind::SlideLeft
            | TransitionKind::SlideRight
            | TransitionKind::SlideUp
            | TransitionKind::SlideDown => {
                // Compute a moving boundary across the union region and
                // decide per cell which side it falls on.
                let target = self.target_region;
                let offset_x = (t * target.w as f32) as u16;
                let offset_y = (t * target.h as f32) as u16;
                let rel_x = screen_x.saturating_sub(target.x);
                let rel_y = screen_y.saturating_sub(target.y);
                let boundary_crossed = match self.kind {
                    TransitionKind::SlideLeft => rel_x + offset_x >= target.w,
                    TransitionKind::SlideRight => rel_x < offset_x,
                    TransitionKind::SlideUp => rel_y + offset_y >= target.h,
                    TransitionKind::SlideDown => rel_y < offset_y,
                    _ => unreachable!(),
                };
                if boundary_crossed {
                    new.or(old)
                } else {
                    old.or(new)
                }
            }
        }
    }

    /// During draw-style unfurls, render a box heading as uninterrupted top
    /// border until the terminal frame. This lets the chrome construct first;
    /// the authored title then appears atomically at t=1.
    fn mask_unfurl_title(&self, screen_x: u16, screen_y: u16, mut cell: Cell) -> Cell {
        if self.t() >= 1.0
            || !matches!(
                self.kind,
                TransitionKind::DrawDown
                    | TransitionKind::DrawRight
                    | TransitionKind::DrawLeft
                    | TransitionKind::DrawOut
            )
            || screen_y != self.new_rect.y
        {
            return cell;
        }

        let Some(local_x) = screen_x.checked_sub(self.new_rect.x) else {
            return cell;
        };
        if local_x == 0 || local_x + 1 >= self.new_buf.width {
            return cell;
        }

        let Some(left) = self.new_buf.get(0, 0) else {
            return cell;
        };
        let Some(right) = self.new_buf.get(self.new_buf.width.saturating_sub(1), 0) else {
            return cell;
        };
        let stroke = match (left.ch, right.ch) {
            ('╭' | '┌', '╮' | '┐') => '─',
            ('╔', '╗') => '═',
            ('┏', '┓') => '━',
            ('+', '+') => '-',
            _ => return cell,
        };

        if cell.ch != stroke {
            cell.ch = stroke;
            cell.grapheme = None;
            cell.style = left.style.clone();
        }
        cell
    }

    /// Paint the current transition frame into the scene node's buffer.
    /// Called by the runtime via `Animation::paint` after each `advance`
    /// that returns `wrote_buffer`.
    pub fn paint_into(&self, scene: &mut Scene) -> Option<NodeId> {
        let target = self.target_region;
        // Try each kind-gated accessor in turn. We can't chain `or_else`
        // because each call borrows `scene` mutably; take turns via early
        // returns.
        let dst = if scene.panel_buffer_mut(self.target_node).is_some() {
            scene.panel_buffer_mut(self.target_node)?
        } else if scene.wasm_buffer_mut(self.target_node).is_some() {
            scene.wasm_buffer_mut(self.target_node)?
        } else if scene.live_buffer_mut(self.target_node).is_some() {
            scene.live_buffer_mut(self.target_node)?
        } else {
            return None;
        };

        // Iterate over the target region in screen coords but write to
        // the destination buffer in its local coords.
        for dy in 0..dst.height.min(target.h) {
            for dx in 0..dst.width.min(target.w) {
                let sx = target.x.saturating_add(dx);
                let sy = target.y.saturating_add(dy);
                if let Some(cell) = self.blend_cell(sx, sy) {
                    dst.set(dx, dy, cell);
                } else {
                    // Neither layer contributes — write a transparent
                    // empty cell so the base layer shows through when
                    // this buffer is composited.
                    dst.set(dx, dy, Cell::empty());
                }
            }
        }
        Some(self.target_node)
    }
}

impl Animation for TransitionAdapter {
    fn id(&self) -> &str {
        &self.id
    }

    fn advance(&mut self, _ctx: &mut AdvanceCtx) -> AdvanceResult {
        if matches!(self.state, AnimState::Finished) {
            return AdvanceResult::none();
        }
        self.elapsed_ms += TICK_MS;
        if self.elapsed_ms >= self.duration_ms {
            self.state = AnimState::Finished;
        }
        AdvanceResult::with_buffer(self.target_node)
    }

    fn finished(&self) -> bool {
        matches!(self.state, AnimState::Finished)
    }

    fn state(&self) -> AnimState {
        self.state
    }

    fn paint(&self, scene: &mut Scene) {
        self.paint_into(scene);
    }

    fn trigger_stop(&mut self) {
        self.state = AnimState::Finished;
        self.elapsed_ms = self.duration_ms;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::compositor::layout::cell::CellStyle;
    use crate::compositor::scene;
    use std::time::Instant;

    fn fill(w: u16, h: u16, ch: char) -> CellBuffer {
        let mut buf = CellBuffer::new(w, h);
        for y in 0..h {
            for x in 0..w {
                buf.put_char(x, y, ch, &CellStyle::default());
            }
        }
        buf
    }

    fn panel_scene(w: u16, h: u16) -> (Scene, NodeId) {
        let doc = {
            let src = r#"[page mode=document]
                [panel id="p" state="a"]
                    [state name="a"][text]A[/text][/state]
                    [state name="b"][text]B[/text][/state]
                [/panel]
            [/page]"#;
            let mut scanner = crate::scanner::Scanner::new(src.as_bytes()).unwrap();
            let tokens = scanner.scan_all().unwrap();
            crate::parser::parse(tokens).document.unwrap()
        };
        let mut scene = scene::build::from_document(&doc);
        let panel = scene.find_by_aml_id("p").unwrap();
        scene.allocate_buffer(panel, w, h);
        (scene, panel)
    }

    #[test]
    fn cut_is_scheduled_as_finished() {
        let (_scene, node) = panel_scene(5, 2);
        let rect = Rect::new(0, 0, 5, 2);
        let t = TransitionAdapter::new(
            "cut".into(),
            node,
            rect,
            fill(5, 2, 'A'),
            rect,
            fill(5, 2, 'B'),
            rect,
            TransitionKind::Cut,
            0,
        );
        assert!(t.finished(), "Cut transitions should be born finished");
    }

    #[test]
    fn zero_duration_finishes_immediately() {
        let (_scene, node) = panel_scene(5, 2);
        let rect = Rect::new(0, 0, 5, 2);
        let t = TransitionAdapter::new(
            "d".into(),
            node,
            rect,
            fill(5, 2, 'A'),
            rect,
            fill(5, 2, 'B'),
            rect,
            TransitionKind::Dissolve,
            0,
        );
        assert!(t.finished());
    }

    #[test]
    fn dissolve_is_deterministic() {
        let (_scene, node) = panel_scene(5, 2);
        let rect = Rect::new(0, 0, 5, 2);
        let a = TransitionAdapter::new(
            "d".into(),
            node,
            rect,
            fill(5, 2, 'A'),
            rect,
            fill(5, 2, 'B'),
            rect,
            TransitionKind::Dissolve,
            100,
        );
        let b = TransitionAdapter::new(
            "d".into(),
            node,
            rect,
            fill(5, 2, 'A'),
            rect,
            fill(5, 2, 'B'),
            rect,
            TransitionKind::Dissolve,
            100,
        );
        // Same seed → same threshold at same cell.
        assert_eq!(
            a.cell_threshold(2, 1, 0xD155),
            b.cell_threshold(2, 1, 0xD155),
        );
    }

    #[test]
    fn slide_left_at_t0_shows_old() {
        let (_scene, node) = panel_scene(5, 2);
        let rect = Rect::new(0, 0, 5, 2);
        let t = TransitionAdapter::new(
            "sl".into(),
            node,
            rect,
            fill(5, 2, 'A'),
            rect,
            fill(5, 2, 'B'),
            rect,
            TransitionKind::SlideLeft,
            100,
        );
        // t=0: old layer wins everywhere.
        assert_eq!(t.blend_cell(2, 1).map(|c| c.ch), Some('A'));
    }

    #[test]
    fn slide_left_at_t1_shows_new() {
        let (mut scene, node) = panel_scene(5, 2);
        let rect = Rect::new(0, 0, 5, 2);
        let mut t = TransitionAdapter::new(
            "sl".into(),
            node,
            rect,
            fill(5, 2, 'A'),
            rect,
            fill(5, 2, 'B'),
            rect,
            TransitionKind::SlideLeft,
            100,
        );
        // Advance past duration.
        t.elapsed_ms = 200;
        t.paint_into(&mut scene);
        // All cells should now show 'B'.
        for y in 0..2 {
            for x in 0..5 {
                assert_eq!(
                    scene.buffer_of(node).unwrap().get(x, y).unwrap().ch,
                    'B',
                    "slide-left at t=1, ({x},{y})",
                );
            }
        }
    }

    #[test]
    fn draw_down_traces_top_edge_before_revealing_rows() {
        let (_scene, node) = panel_scene(10, 4);
        let rect = Rect::new(0, 0, 10, 4);
        let mut next = fill(10, 4, 'B');
        for x in 0..10 {
            next.put_char(x, 0, '─', &CellStyle::default());
            next.put_char(x, 3, '─', &CellStyle::default());
        }
        next.put_char(0, 0, '╭', &CellStyle::default());
        next.put_char(9, 0, '╮', &CellStyle::default());
        next.put_char(0, 3, '╰', &CellStyle::default());
        next.put_char(9, 3, '╯', &CellStyle::default());
        let mut transition = TransitionAdapter::new(
            "draw".into(),
            node,
            rect,
            fill(10, 4, 'A'),
            rect,
            next,
            rect,
            TransitionKind::DrawDown,
            1000,
        );

        transition.elapsed_ms = 200;
        assert_eq!(transition.blend_cell(0, 0).map(|c| c.ch), Some('A'));
        assert_eq!(transition.blend_cell(3, 0).map(|c| c.ch), Some('─'));
        assert_eq!(transition.blend_cell(6, 0).map(|c| c.ch), Some('─'));
        assert_eq!(transition.blend_cell(7, 0).map(|c| c.ch), Some('A'));
        assert_eq!(transition.blend_cell(0, 1).map(|c| c.ch), Some('A'));

        transition.elapsed_ms = 500;
        assert_eq!(transition.blend_cell(0, 0).map(|c| c.ch), Some('╭'));
        assert_eq!(transition.blend_cell(9, 0).map(|c| c.ch), Some('╮'));
        assert_eq!(transition.blend_cell(0, 1).map(|c| c.ch), Some('A'));

        transition.elapsed_ms = 750;
        assert_eq!(transition.blend_cell(0, 2).map(|c| c.ch), Some('╰'));
        assert_eq!(transition.blend_cell(5, 2).map(|c| c.ch), Some('─'));
        assert_eq!(transition.blend_cell(9, 2).map(|c| c.ch), Some('╯'));
        assert_eq!(transition.blend_cell(0, 3).map(|c| c.ch), Some('A'));

        transition.elapsed_ms = 1000;
        assert_eq!(transition.blend_cell(9, 3).map(|c| c.ch), Some('╯'));
    }

    #[test]
    fn draw_right_traces_left_edge_before_revealing_columns() {
        let (_scene, node) = panel_scene(10, 4);
        let rect = Rect::new(0, 0, 10, 4);
        let mut next = fill(10, 4, 'B');
        for y in 0..4 {
            next.put_char(0, y, '│', &CellStyle::default());
            next.put_char(9, y, '│', &CellStyle::default());
        }
        next.put_char(0, 0, '╭', &CellStyle::default());
        next.put_char(0, 3, '╰', &CellStyle::default());
        next.put_char(9, 0, '╮', &CellStyle::default());
        next.put_char(9, 3, '╯', &CellStyle::default());
        let mut transition = TransitionAdapter::new(
            "draw".into(),
            node,
            rect,
            fill(10, 4, 'A'),
            rect,
            next,
            rect,
            TransitionKind::DrawRight,
            1000,
        );

        transition.elapsed_ms = 200;
        assert_eq!(transition.blend_cell(0, 0).map(|c| c.ch), Some('╭'));
        assert_eq!(transition.blend_cell(0, 1).map(|c| c.ch), Some('│'));
        assert_eq!(transition.blend_cell(0, 2).map(|c| c.ch), Some('A'));
        assert_eq!(transition.blend_cell(1, 0).map(|c| c.ch), Some('A'));

        transition.elapsed_ms = 500;
        assert_eq!(transition.blend_cell(0, 0).map(|c| c.ch), Some('╭'));
        assert_eq!(transition.blend_cell(0, 3).map(|c| c.ch), Some('╰'));
        assert_eq!(transition.blend_cell(1, 0).map(|c| c.ch), Some('A'));

        transition.elapsed_ms = 750;
        assert_eq!(transition.blend_cell(5, 0).map(|c| c.ch), Some('╮'));
        assert_eq!(transition.blend_cell(5, 2).map(|c| c.ch), Some('│'));
        assert_eq!(transition.blend_cell(5, 3).map(|c| c.ch), Some('╯'));
        assert_eq!(transition.blend_cell(9, 0).map(|c| c.ch), Some('A'));

        transition.elapsed_ms = 1000;
        assert_eq!(transition.blend_cell(9, 3).map(|c| c.ch), Some('╯'));
    }

    /// `DrawLeft` is exactly `DrawRight` reflected, at every instant.
    ///
    /// The strongest statement of "the opposite of draw-right" available: with
    /// uniform old and new fills the character content carries no left/right
    /// information, so all that is compared is *which* cells have flipped. If
    /// DrawLeft's cell at x matches DrawRight's at w-1-x for every cell at every
    /// sampled time, the two differ only in direction.
    ///
    /// Uniform fills are the point. A bordered box would make the frontier
    /// column's authored-edge sample differ between the two -- left edge versus
    /// right -- and that is content, not geometry.
    #[test]
    fn draw_left_is_draw_right_reflected() {
        const W: u16 = 12;
        const H: u16 = 5;
        let build = |kind| {
            let (_scene, node) = panel_scene(W, H);
            let rect = Rect::new(0, 0, W, H);
            let t = TransitionAdapter::new(
                "draw".into(),
                node,
                rect,
                fill(W, H, 'A'),
                rect,
                fill(W, H, 'B'),
                rect,
                kind,
                1000,
            );
            (_scene, t)
        };
        let (_sr, mut right) = build(TransitionKind::DrawRight);
        let (_sl, mut left) = build(TransitionKind::DrawLeft);

        for ms in [0u32, 100, 250, 400, 500, 600, 750, 900, 1000] {
            right.elapsed_ms = ms;
            left.elapsed_ms = ms;
            for y in 0..H {
                for x in 0..W {
                    let r = right.blend_cell(x, y).map(|c| c.ch);
                    let l = left.blend_cell(W - 1 - x, y).map(|c| c.ch);
                    assert_eq!(
                        r,
                        l,
                        "at {ms}ms, draw-right ({x},{y}) should equal draw-left ({},{y})",
                        W - 1 - x
                    );
                }
            }
        }
    }

    /// The mirror of `draw_right_traces_left_edge_before_revealing_columns`.
    ///
    /// Asserted as the reflection rather than as fresh expectations: the point of
    /// DrawLeft is that it does exactly what DrawRight does, from the other side.
    /// So the right edge is traced first, the frontier moves leftward, and the
    /// column at the frontier shows the authored *left* edge while it travels.
    #[test]
    fn draw_left_traces_right_edge_before_revealing_columns() {
        let (_scene, node) = panel_scene(10, 4);
        let rect = Rect::new(0, 0, 10, 4);
        let mut next = fill(10, 4, 'B');
        for y in 0..4u16 {
            next.put_char(0, y, '│', &CellStyle::default());
            next.put_char(9, y, '│', &CellStyle::default());
        }
        next.put_char(0, 0, '╭', &CellStyle::default());
        next.put_char(0, 3, '╰', &CellStyle::default());
        next.put_char(9, 0, '╮', &CellStyle::default());
        next.put_char(9, 3, '╯', &CellStyle::default());
        let mut transition = TransitionAdapter::new(
            "draw".into(),
            node,
            rect,
            fill(10, 4, 'A'),
            rect,
            next,
            rect,
            TransitionKind::DrawLeft,
            1000,
        );

        // First half traces the right edge downward; the left edge is untouched.
        transition.elapsed_ms = 200;
        assert_eq!(transition.blend_cell(9, 0).map(|c| c.ch), Some('╮'));
        assert_eq!(transition.blend_cell(9, 1).map(|c| c.ch), Some('│'));
        assert_eq!(transition.blend_cell(9, 2).map(|c| c.ch), Some('A'));
        assert_eq!(transition.blend_cell(8, 0).map(|c| c.ch), Some('A'));

        transition.elapsed_ms = 500;
        assert_eq!(transition.blend_cell(9, 0).map(|c| c.ch), Some('╮'));
        assert_eq!(transition.blend_cell(9, 3).map(|c| c.ch), Some('╯'));
        assert_eq!(transition.blend_cell(8, 0).map(|c| c.ch), Some('A'));

        // Second half unfurls leftward, the frontier carrying the left edge.
        transition.elapsed_ms = 750;
        assert_eq!(transition.blend_cell(4, 0).map(|c| c.ch), Some('╭'));
        assert_eq!(transition.blend_cell(4, 2).map(|c| c.ch), Some('│'));
        assert_eq!(transition.blend_cell(4, 3).map(|c| c.ch), Some('╰'));
        assert_eq!(transition.blend_cell(0, 0).map(|c| c.ch), Some('A'));

        transition.elapsed_ms = 1000;
        assert_eq!(transition.blend_cell(0, 0).map(|c| c.ch), Some('╭'));
        assert_eq!(transition.blend_cell(0, 3).map(|c| c.ch), Some('╰'));
    }

    #[test]
    fn draw_out_grows_a_live_frame_from_the_centre() {
        let (_scene, node) = panel_scene(10, 6);
        let rect = Rect::new(0, 0, 10, 6);
        let mut next = fill(10, 6, 'B');
        for x in 1..9 {
            next.put_char(x, 0, '═', &CellStyle::default());
            next.put_char(x, 5, '═', &CellStyle::default());
        }
        for y in 1..5 {
            next.put_char(0, y, '║', &CellStyle::default());
            next.put_char(9, y, '║', &CellStyle::default());
        }
        next.put_char(0, 0, '╔', &CellStyle::default());
        next.put_char(9, 0, '╗', &CellStyle::default());
        next.put_char(0, 5, '╚', &CellStyle::default());
        next.put_char(9, 5, '╝', &CellStyle::default());

        let mut transition = TransitionAdapter::new(
            "draw-out".into(),
            node,
            rect,
            fill(10, 6, 'A'),
            rect,
            next,
            rect,
            TransitionKind::DrawOut,
            1000,
        );
        transition.elapsed_ms = 500;

        // At halfway the completed frame's corners form a smaller 5×3
        // rectangle centred inside the final 10×6 bounds.
        assert_eq!(transition.blend_cell(2, 1).map(|c| c.ch), Some('╔'));
        assert_eq!(transition.blend_cell(6, 1).map(|c| c.ch), Some('╗'));
        assert_eq!(transition.blend_cell(2, 3).map(|c| c.ch), Some('╚'));
        assert_eq!(transition.blend_cell(6, 3).map(|c| c.ch), Some('╝'));
        assert_eq!(transition.blend_cell(4, 2).map(|c| c.ch), Some('B'));
        assert_eq!(transition.blend_cell(1, 1).map(|c| c.ch), Some('A'));

        transition.elapsed_ms = 1000;
        assert_eq!(transition.blend_cell(0, 0).map(|c| c.ch), Some('╔'));
        assert_eq!(transition.blend_cell(9, 5).map(|c| c.ch), Some('╝'));
    }

    #[test]
    fn unfurl_titles_appear_only_after_completion() {
        let rect = Rect::new(0, 0, 9, 3);
        let (_scene, node) = panel_scene(9, 3);
        let old = CellBuffer::new(9, 3);
        let mut new = CellBuffer::new(9, 3);
        new.put_str(0, 0, "╭─ Box ─╮", &CellStyle::default());

        for kind in [
            TransitionKind::DrawDown,
            TransitionKind::DrawRight,
            TransitionKind::DrawLeft,
        ] {
            let mut transition = TransitionAdapter::new(
                "unfurl".into(),
                node,
                rect,
                old.try_clone().unwrap(),
                rect,
                new.try_clone().unwrap(),
                rect,
                kind,
                1000,
            );
            transition.elapsed_ms = 900;
            assert_eq!(
                transition.blend_cell(3, 0).map(|cell| cell.ch),
                Some('─'),
                "{kind:?} must keep the title masked before completion",
            );

            transition.trigger_stop();
            assert_eq!(
                transition.blend_cell(3, 0).map(|cell| cell.ch),
                Some('B'),
                "{kind:?} must reveal the title on its terminal frame",
            );
        }
    }

    /// Shape-mismatched states: State A is 3x2 at (0, 0); State B is 2x2
    /// at (5, 3). Their union is (0, 0) to (7, 5). During dissolve, cells
    /// exclusive to A (x<3, y<2 when B-rect excluded) source from A; cells
    /// exclusive to B source from B; cells in neither leave the base
    /// layer to show through.
    #[test]
    fn shape_mismatched_dissolve_routes_cells_correctly() {
        let (_scene, node) = panel_scene(10, 8);
        let target = Rect::new(0, 0, 10, 8);
        let old_rect = Rect::new(0, 0, 3, 2);
        let new_rect = Rect::new(5, 3, 2, 2);
        let t = TransitionAdapter::new(
            "mix".into(),
            node,
            target,
            fill(3, 2, 'A'),
            old_rect,
            fill(2, 2, 'B'),
            new_rect,
            TransitionKind::Dissolve,
            100,
        );

        // Cell inside A only, outside B: should be 'A'.
        assert_eq!(
            t.blend_cell(1, 1).map(|c| c.ch),
            Some('A'),
            "A-exclusive cell"
        );
        // Cell inside B only, outside A: should be 'B'.
        assert_eq!(
            t.blend_cell(5, 3).map(|c| c.ch),
            Some('B'),
            "B-exclusive cell"
        );
        // Cell outside both rects: None (base layer shows through).
        assert_eq!(t.blend_cell(8, 7), None, "neither-rect cell reveals base");
    }

    /// Advancing a transition through time and calling paint() each tick
    /// writes into the scene node's buffer.
    #[test]
    fn advance_and_paint_updates_scene_buffer() {
        let (mut scene, node) = panel_scene(5, 2);
        let rect = Rect::new(0, 0, 5, 2);
        let mut t = TransitionAdapter::new(
            "d".into(),
            node,
            rect,
            fill(5, 2, 'A'),
            rect,
            fill(5, 2, 'B'),
            rect,
            TransitionKind::Dissolve,
            100,
        );
        let empty: Vec<String> = Vec::new();
        // Advance near the end so B wins most cells.
        for _ in 0..10 {
            let mut ctx = AdvanceCtx::new(Instant::now(), 0, 24, &empty);
            t.advance(&mut ctx);
        }
        t.paint_into(&mut scene);
        let mut a_count = 0;
        let mut b_count = 0;
        for y in 0..2 {
            for x in 0..5 {
                match scene.buffer_of(node).unwrap().get(x, y).unwrap().ch {
                    'A' => a_count += 1,
                    'B' => b_count += 1,
                    _ => {}
                }
            }
        }
        assert_eq!(
            a_count + b_count,
            10,
            "every cell should be A or B after dissolve"
        );
        // Very permissive: with duration=100, 10 ticks * 33ms = 330ms >
        // duration, so we're fully past end — mostly B. But the
        // dissolve mask can leave some A if thresholds are high — at
        // t >= 1.0, `t > threshold` is true for every threshold < 1.0,
        // so all cells go to B.
        assert_eq!(b_count, 10, "at t=1 all cells should be B");
    }

    #[test]
    fn panel_transition_buffers_retain_exact_governed_storage() {
        let governor = crate::resource::ResourceGovernor::new();
        let old = CellBuffer::try_new_governed(
            4,
            2,
            &governor,
            crate::resource::ResourceCategory::CompositorCells,
        )
        .unwrap();
        let new = CellBuffer::try_new_governed(
            4,
            2,
            &governor,
            crate::resource::ResourceCategory::CompositorCells,
        )
        .unwrap();
        let expected = 16 * std::mem::size_of::<Cell>();
        let adapter = TransitionAdapter::new(
            "panel".into(),
            NodeId::default(),
            Rect::new(0, 0, 4, 2),
            old,
            Rect::new(0, 0, 4, 2),
            new,
            Rect::new(0, 0, 4, 2),
            TransitionKind::Fade,
            300,
        );
        assert_eq!(
            governor.used(crate::resource::ResourceCategory::CompositorCells),
            expected
        );
        drop(adapter);
        assert_eq!(
            governor.used(crate::resource::ResourceCategory::CompositorCells),
            0
        );
    }
}