open-gpui-motion 0.2.0

Renderer-neutral motion primitives for Open GPUI.
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
//! Renderer-neutral motion controller contracts.

use crate::spring::{MotionModel, MotionScalarSample};
use crate::value::MotionValue;
use crate::{
    MotionPolicyInput, MotionPolicyReport, MotionRunState, MotionSpec, validate_motion_policy,
};
use std::time::{Duration, Instant};

/// Renderer-neutral frame demand returned by motion controllers.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MotionFrameDemand {
    /// No more animation frames are required.
    Idle,
    /// At least one track is active and the adapter should request another frame for the reason.
    NeedsFrame(MotionFrameReason),
}

/// Minimal reason vocabulary for adapter-owned frame requests.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MotionFrameReason {
    /// The adapter should sample motion and render the updated presentation state.
    UpdateRender,
}

impl MotionFrameDemand {
    /// Returns whether another frame should be requested by the adapter.
    pub const fn needs_frame(self) -> bool {
        matches!(self, Self::NeedsFrame(_))
    }

    /// Returns the reason another frame is needed.
    pub const fn reason(self) -> Option<MotionFrameReason> {
        match self {
            Self::Idle => None,
            Self::NeedsFrame(reason) => Some(reason),
        }
    }

    fn from_active(active: bool) -> Self {
        if active {
            Self::NeedsFrame(MotionFrameReason::UpdateRender)
        } else {
            Self::Idle
        }
    }

    /// Combines two frame demands into one adapter-owned frame request.
    pub const fn combine(self, other: Self) -> Self {
        match (self, other) {
            (Self::NeedsFrame(reason), _) | (_, Self::NeedsFrame(reason)) => {
                Self::NeedsFrame(reason)
            }
            (Self::Idle, Self::Idle) => Self::Idle,
        }
    }

    /// Combines many frame demands into one adapter-owned frame request.
    pub fn combine_all(demands: impl IntoIterator<Item = Self>) -> Self {
        demands.into_iter().fold(Self::Idle, Self::combine)
    }
}

/// Adapter clock sample mapped into deterministic controller elapsed time.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MotionClockSample {
    elapsed: Duration,
    delta: Duration,
    clamped: bool,
}

impl MotionClockSample {
    /// A zero elapsed-time clock sample.
    pub const ZERO: Self = Self {
        elapsed: Duration::ZERO,
        delta: Duration::ZERO,
        clamped: false,
    };

    /// Creates a clock sample from previous and requested elapsed times.
    ///
    /// Non-monotonic elapsed time is clamped to the previous elapsed time. This keeps controller
    /// sampling deterministic and avoids negative deltas for adapters whose frame clock moves
    /// backwards or is restored from stale state.
    pub fn from_elapsed(previous_elapsed: Duration, requested_elapsed: Duration) -> Self {
        if requested_elapsed < previous_elapsed {
            Self {
                elapsed: previous_elapsed,
                delta: Duration::ZERO,
                clamped: true,
            }
        } else {
            Self {
                elapsed: requested_elapsed,
                delta: requested_elapsed - previous_elapsed,
                clamped: false,
            }
        }
    }

    /// Creates a clock sample from a start instant and current adapter instant.
    pub fn from_instant(started_at: Instant, now: Instant) -> Self {
        Self::from_elapsed(Duration::ZERO, now.saturating_duration_since(started_at))
    }

    /// Creates a clock sample from adapter instants and clamps non-monotonic elapsed time.
    pub fn from_instants(started_at: Instant, previous_now: Instant, now: Instant) -> Self {
        Self::from_elapsed(
            previous_now.saturating_duration_since(started_at),
            now.saturating_duration_since(started_at),
        )
    }

    /// Returns clamped controller elapsed time.
    pub const fn elapsed(self) -> Duration {
        self.elapsed
    }

    /// Returns elapsed-time delta since the previous sample.
    pub const fn delta(self) -> Duration {
        self.delta
    }

    /// Returns whether requested elapsed time was clamped.
    pub const fn clamped(self) -> bool {
        self.clamped
    }
}

/// Policy-resolved execution state for a motion run before adapter sampling begins.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MotionExecutionState {
    /// The run should publish its final semantic state without requesting frames.
    Immediate,
    /// The run may sample over time and request adapter-owned frames.
    Scheduled,
}

impl MotionExecutionState {
    /// Returns whether the run completes immediately.
    pub const fn is_immediate(self) -> bool {
        matches!(self, Self::Immediate)
    }

    /// Returns whether the run should be sampled over time.
    pub const fn is_scheduled(self) -> bool {
        matches!(self, Self::Scheduled)
    }
}

/// Renderer-neutral policy result used to start motion from a single owner.
#[derive(Debug, Clone, PartialEq)]
pub struct MotionExecutionPlan {
    model: MotionModel,
    policy_report: MotionPolicyReport,
    state: MotionExecutionState,
}

impl MotionExecutionPlan {
    /// Resolves a requested model through motion policy, falling back to immediate motion on
    /// policy failure.
    pub fn resolve(input: MotionPolicyInput) -> Self {
        let requested_model = input.model();
        let policy_report = validate_motion_policy(input);
        let model = if policy_report.is_ok() {
            requested_model
        } else {
            MotionModel::timeline(MotionSpec::immediate())
        };
        let state = if model.is_immediate() {
            MotionExecutionState::Immediate
        } else {
            MotionExecutionState::Scheduled
        };
        Self {
            model,
            policy_report,
            state,
        }
    }

    /// Returns the model that should execute after policy resolution.
    pub const fn model(&self) -> MotionModel {
        self.model
    }

    /// Returns the policy report produced for the requested model.
    pub const fn policy_report(&self) -> &MotionPolicyReport {
        &self.policy_report
    }

    /// Returns the policy-resolved execution state.
    pub const fn state(&self) -> MotionExecutionState {
        self.state
    }

    /// Returns whether the run should complete immediately.
    pub const fn is_immediate(&self) -> bool {
        self.state.is_immediate()
    }

    /// Consumes the plan and returns its parts.
    pub fn into_parts(self) -> (MotionModel, MotionPolicyReport, MotionExecutionState) {
        (self.model, self.policy_report, self.state)
    }
}

/// One scalar motion track sampled by deterministic elapsed time.
#[derive(Debug, Clone, PartialEq)]
pub struct MotionScalarTrack {
    model: MotionModel,
    value: MotionValue,
    target: f32,
    initial_velocity: f32,
    started_at: Duration,
    cancelled_at: Option<Duration>,
    finished_at: Option<Duration>,
}

impl MotionScalarTrack {
    /// Starts a scalar track at the provided controller time.
    pub fn start(
        model: MotionModel,
        from: f32,
        target: f32,
        initial_velocity: f32,
        started_at: Duration,
    ) -> Self {
        Self {
            model,
            value: MotionValue::new(from),
            target,
            initial_velocity,
            started_at,
            cancelled_at: None,
            finished_at: None,
        }
    }

    /// Creates an immediate scalar track at a fixed value.
    pub fn immediate(value: f32, started_at: Duration) -> Self {
        Self::start(
            MotionModel::timeline(MotionSpec::immediate()),
            value,
            value,
            0.0,
            started_at,
        )
    }

    /// Returns the motion model.
    pub const fn model(&self) -> MotionModel {
        self.model
    }

    /// Returns the source value.
    pub const fn from(&self) -> f32 {
        self.value.current()
    }

    /// Returns the target value.
    pub const fn target(&self) -> f32 {
        self.target
    }

    /// Returns the initial velocity.
    pub const fn initial_velocity(&self) -> f32 {
        self.initial_velocity
    }

    /// Returns the controller time at which the track started.
    pub const fn started_at(&self) -> Duration {
        self.started_at
    }

    /// Returns the controller time at which the track was cancelled.
    pub const fn cancelled_at(&self) -> Option<Duration> {
        self.cancelled_at
    }

    /// Returns the controller time at which the track was explicitly finished.
    pub const fn finished_at(&self) -> Option<Duration> {
        self.finished_at
    }

    /// Cancels the track at the provided controller time.
    pub fn cancel_at(&mut self, cancelled_at: Duration) {
        if self.finished_at.is_none() {
            self.cancelled_at = Some(cancelled_at);
        }
    }

    /// Finishes the track at the provided controller time and publishes the target value.
    pub fn finish_at(&mut self, finished_at: Duration) {
        self.finished_at = Some(finished_at);
        self.cancelled_at = None;
    }

    /// Retargets the track from its sampled value and velocity.
    pub fn retarget(&self, model: MotionModel, target: f32, now: Duration) -> Self {
        let sample = self.sample_at(now);
        Self::start(model, sample.value(), target, sample.velocity(), now)
    }

    /// Samples the track at the provided controller time.
    pub fn sample_at(&self, now: Duration) -> MotionScalarSample {
        if let Some(finished_at) = self.finished_at {
            return MotionScalarSample::new(
                MotionRunState::Completed,
                finished_at.saturating_sub(self.started_at),
                self.target,
                0.0,
                self.target,
            );
        }

        let effective_now = self.cancelled_at.unwrap_or(now);
        let elapsed = effective_now.saturating_sub(self.started_at);
        let mut sample = self.model.sample_scalar_elapsed(
            self.value.current(),
            self.target,
            self.initial_velocity,
            elapsed,
        );
        if self.cancelled_at.is_some() && sample.state().is_active() {
            sample = MotionScalarSample::new(
                MotionRunState::Cancelled,
                sample.elapsed(),
                sample.value(),
                sample.velocity(),
                sample.target(),
            );
        }
        sample
    }

    /// Returns whether this track needs another adapter-owned frame.
    pub fn frame_demand_at(&self, now: Duration) -> MotionFrameDemand {
        MotionFrameDemand::from_active(self.sample_at(now).is_active())
    }
}

/// Sample from a policy-resolved scalar execution.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MotionScalarExecutionSample {
    sample: MotionScalarSample,
    complete: bool,
    frame_demand: MotionFrameDemand,
}

impl MotionScalarExecutionSample {
    const fn new(
        sample: MotionScalarSample,
        complete: bool,
        frame_demand: MotionFrameDemand,
    ) -> Self {
        Self {
            sample,
            complete,
            frame_demand,
        }
    }

    /// Returns the underlying scalar sample.
    pub const fn scalar_sample(self) -> MotionScalarSample {
        self.sample
    }

    /// Returns the sampled scalar value.
    pub const fn value(self) -> f32 {
        self.sample.value()
    }

    /// Returns whether the run has reached the semantic completion state.
    pub const fn complete(self) -> bool {
        self.complete
    }

    /// Returns whether the adapter should request another frame.
    pub const fn frame_demand(self) -> MotionFrameDemand {
        self.frame_demand
    }
}

/// Sample from a normalized 0..1 progress execution.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MotionProgressSample {
    sample: MotionScalarExecutionSample,
}

impl MotionProgressSample {
    const fn new(sample: MotionScalarExecutionSample) -> Self {
        Self { sample }
    }

    /// Returns the underlying scalar execution sample.
    pub const fn scalar_sample(self) -> MotionScalarExecutionSample {
        self.sample
    }

    /// Returns the clamped normalized progress.
    pub fn progress(self) -> f32 {
        self.sample.value().clamp(0.0, 1.0)
    }

    /// Returns whether the progress run reached its semantic completion state.
    pub const fn complete(self) -> bool {
        self.sample.complete()
    }

    /// Returns whether the adapter should request another frame.
    pub const fn frame_demand(self) -> MotionFrameDemand {
        self.sample.frame_demand()
    }
}

/// A single scalar track plus its policy-resolved execution metadata.
#[derive(Debug, Clone, PartialEq)]
pub struct MotionScalarExecution {
    plan: MotionExecutionPlan,
    track: MotionScalarTrack,
}

impl MotionScalarExecution {
    /// Starts a scalar execution from an already resolved motion plan.
    pub fn start(
        plan: MotionExecutionPlan,
        from: f32,
        target: f32,
        initial_velocity: f32,
        started_at: Duration,
    ) -> Self {
        let track =
            MotionScalarTrack::start(plan.model(), from, target, initial_velocity, started_at);
        Self { plan, track }
    }

    /// Resolves policy input and starts a scalar execution.
    pub fn start_resolved(
        input: MotionPolicyInput,
        from: f32,
        target: f32,
        initial_velocity: f32,
        started_at: Duration,
    ) -> Self {
        Self::start(
            MotionExecutionPlan::resolve(input),
            from,
            target,
            initial_velocity,
            started_at,
        )
    }

    /// Returns the resolved execution plan.
    pub const fn plan(&self) -> &MotionExecutionPlan {
        &self.plan
    }

    /// Returns the underlying scalar track.
    pub const fn track(&self) -> &MotionScalarTrack {
        &self.track
    }

    /// Returns the model that should execute after policy resolution.
    pub const fn model(&self) -> MotionModel {
        self.plan.model()
    }

    /// Returns the policy report produced for the requested model.
    pub const fn policy_report(&self) -> &MotionPolicyReport {
        self.plan.policy_report()
    }

    /// Returns the policy-resolved execution state.
    pub const fn state(&self) -> MotionExecutionState {
        self.plan.state()
    }

    /// Samples the execution at deterministic elapsed time.
    pub fn sample_at(&self, now: Duration) -> MotionScalarExecutionSample {
        let sample = self.track.sample_at(now);
        let complete = self.plan.is_immediate() || sample.reached_final_state();
        let frame_demand = if complete {
            MotionFrameDemand::Idle
        } else {
            MotionFrameDemand::from_active(sample.is_active())
        };
        MotionScalarExecutionSample::new(sample, complete, frame_demand)
    }

    /// Samples the execution at a deterministic adapter clock sample.
    pub fn sample_clock(&self, clock: MotionClockSample) -> MotionScalarExecutionSample {
        self.sample_at(clock.elapsed())
    }

    /// Samples the execution from adapter instants while keeping deterministic elapsed-time
    /// semantics in the controller layer.
    pub fn sample_since(&self, started_at: Instant, now: Instant) -> MotionScalarExecutionSample {
        self.sample_at(now.saturating_duration_since(started_at))
    }
}

/// A policy-resolved normalized 0..1 progress run.
///
/// This is the renderer-neutral lifecycle primitive for adapters that need one progress value to
/// drive their own layout, geometry, or paint projection. It deliberately does not schedule frames
/// itself; callers translate the returned [`MotionFrameDemand`] through their adapter.
#[derive(Debug, Clone, PartialEq)]
pub struct MotionProgressExecution {
    execution: MotionScalarExecution,
    started_at: Instant,
}

impl MotionProgressExecution {
    /// Starts a normalized progress run from an already resolved motion plan.
    pub fn start(plan: MotionExecutionPlan, started_at: Instant) -> Self {
        Self {
            execution: MotionScalarExecution::start(plan, 0.0, 1.0, 0.0, Duration::ZERO),
            started_at,
        }
    }

    /// Resolves policy input and starts a normalized progress run.
    pub fn start_resolved(input: MotionPolicyInput, started_at: Instant) -> Self {
        Self::start(MotionExecutionPlan::resolve(input), started_at)
    }

    /// Returns the underlying scalar execution.
    pub const fn scalar_execution(&self) -> &MotionScalarExecution {
        &self.execution
    }

    /// Returns the resolved execution plan.
    pub const fn plan(&self) -> &MotionExecutionPlan {
        self.execution.plan()
    }

    /// Returns the model that should execute after policy resolution.
    pub const fn model(&self) -> MotionModel {
        self.execution.model()
    }

    /// Returns the policy report produced for the requested model.
    pub const fn policy_report(&self) -> &MotionPolicyReport {
        self.execution.policy_report()
    }

    /// Returns the policy-resolved execution state.
    pub const fn state(&self) -> MotionExecutionState {
        self.execution.state()
    }

    /// Returns the adapter instant at which this progress run started.
    pub const fn started_at(&self) -> Instant {
        self.started_at
    }

    /// Samples the progress run at deterministic elapsed time.
    pub fn sample_at(&self, now: Duration) -> MotionProgressSample {
        MotionProgressSample::new(self.execution.sample_at(now))
    }

    /// Samples the progress run at a deterministic adapter clock sample.
    pub fn sample_clock(&self, clock: MotionClockSample) -> MotionProgressSample {
        MotionProgressSample::new(self.execution.sample_clock(clock))
    }

    /// Samples the progress run from adapter instants while keeping deterministic elapsed-time
    /// semantics in the controller layer.
    pub fn sample_since(&self, now: Instant) -> MotionProgressSample {
        MotionProgressSample::new(self.execution.sample_since(self.started_at, now))
    }
}

/// A sampled keyed scalar motion track.
#[derive(Debug, Clone, PartialEq)]
pub struct MotionScalarTrackSample<K> {
    key: K,
    sample: MotionScalarSample,
}

impl<K> MotionScalarTrackSample<K> {
    /// Creates a keyed track sample.
    pub const fn new(key: K, sample: MotionScalarSample) -> Self {
        Self { key, sample }
    }

    /// Returns the track key.
    pub const fn key(&self) -> &K {
        &self.key
    }

    /// Returns the scalar motion sample.
    pub const fn sample(&self) -> MotionScalarSample {
        self.sample
    }
}

/// A grouped controller sample.
#[derive(Debug, Clone, PartialEq)]
pub struct MotionScalarControllerSample<K> {
    tracks: Vec<MotionScalarTrackSample<K>>,
    frame_demand: MotionFrameDemand,
}

impl<K> MotionScalarControllerSample<K> {
    /// Creates a grouped sample.
    pub fn new(tracks: Vec<MotionScalarTrackSample<K>>, frame_demand: MotionFrameDemand) -> Self {
        Self {
            tracks,
            frame_demand,
        }
    }

    /// Returns sampled tracks in controller order.
    pub fn tracks(&self) -> &[MotionScalarTrackSample<K>] {
        &self.tracks
    }

    /// Returns the grouped frame demand.
    pub const fn frame_demand(&self) -> MotionFrameDemand {
        self.frame_demand
    }

    /// Returns whether all grouped tracks are terminal for adapter frame scheduling.
    pub const fn complete(&self) -> bool {
        !self.frame_demand.needs_frame()
    }
}

impl<K: PartialEq> MotionScalarControllerSample<K> {
    /// Returns the sample for a key.
    pub fn track(&self, key: &K) -> Option<&MotionScalarTrackSample<K>> {
        self.tracks.iter().find(|track| track.key() == key)
    }
}

/// A small keyed scalar motion controller.
#[derive(Debug, Clone, PartialEq)]
pub struct MotionScalarController<K> {
    tracks: Vec<(K, MotionScalarTrack)>,
}

impl<K> Default for MotionScalarController<K> {
    fn default() -> Self {
        Self::new()
    }
}

impl<K> MotionScalarController<K> {
    /// Creates an empty scalar motion controller.
    pub const fn new() -> Self {
        Self { tracks: Vec::new() }
    }

    /// Returns all registered tracks in insertion order.
    pub fn tracks(&self) -> &[(K, MotionScalarTrack)] {
        &self.tracks
    }
}

impl<K: PartialEq> MotionScalarController<K> {
    /// Starts or replaces a keyed scalar track.
    pub fn start(
        &mut self,
        key: K,
        model: MotionModel,
        from: f32,
        target: f32,
        initial_velocity: f32,
        started_at: Duration,
    ) {
        let track = MotionScalarTrack::start(model, from, target, initial_velocity, started_at);
        if let Some((_, existing)) = self
            .tracks
            .iter_mut()
            .find(|(track_key, _)| track_key == &key)
        {
            *existing = track;
        } else {
            self.tracks.push((key, track));
        }
    }

    /// Sets or replaces a keyed scalar track with an immediate fixed value.
    pub fn set_immediate(&mut self, key: K, value: f32, now: Duration) {
        let track = MotionScalarTrack::immediate(value, now);
        if let Some((_, existing)) = self
            .tracks
            .iter_mut()
            .find(|(track_key, _)| track_key == &key)
        {
            *existing = track;
        } else {
            self.tracks.push((key, track));
        }
    }

    /// Retargets an existing keyed track from its sampled value and velocity.
    pub fn retarget(&mut self, key: K, model: MotionModel, target: f32, now: Duration) {
        if let Some((_, existing)) = self
            .tracks
            .iter_mut()
            .find(|(track_key, _)| track_key == &key)
        {
            *existing = existing.retarget(model, target, now);
        } else {
            self.start(key, model, target, target, 0.0, now);
        }
    }

    /// Cancels an existing keyed track at the provided controller time.
    pub fn cancel(&mut self, key: &K, now: Duration) {
        if let Some((_, existing)) = self
            .tracks
            .iter_mut()
            .find(|(track_key, _)| track_key == key)
        {
            existing.cancel_at(now);
        }
    }

    /// Finishes an existing keyed track at the provided controller time.
    pub fn finish(&mut self, key: &K, now: Duration) {
        if let Some((_, existing)) = self
            .tracks
            .iter_mut()
            .find(|(track_key, _)| track_key == key)
        {
            existing.finish_at(now);
        }
    }

    /// Returns the frame demand at the provided controller time.
    pub fn frame_demand_at(&self, now: Duration) -> MotionFrameDemand {
        MotionFrameDemand::combine_all(
            self.tracks
                .iter()
                .map(|(_, track)| track.frame_demand_at(now)),
        )
    }
}

impl<K> MotionScalarController<K> {
    /// Removes terminal tracks and returns the number of pruned entries.
    pub fn prune_terminal_at(&mut self, now: Duration) -> usize {
        let before = self.tracks.len();
        self.tracks
            .retain(|(_, track)| track.frame_demand_at(now).needs_frame());
        before - self.tracks.len()
    }
}

impl<K: Clone> MotionScalarController<K> {
    /// Samples all tracks at the provided controller time.
    pub fn sample_at(&self, now: Duration) -> MotionScalarControllerSample<K> {
        let tracks = self
            .tracks
            .iter()
            .map(|(key, track)| MotionScalarTrackSample::new(key.clone(), track.sample_at(now)))
            .collect::<Vec<_>>();
        let frame_demand = MotionFrameDemand::combine_all(
            tracks
                .iter()
                .map(|track| MotionFrameDemand::from_active(track.sample().is_active())),
        );
        MotionScalarControllerSample::new(tracks, frame_demand)
    }

    /// Samples all tracks at a deterministic adapter clock sample.
    pub fn sample_clock(&self, clock: MotionClockSample) -> MotionScalarControllerSample<K> {
        self.sample_at(clock.elapsed())
    }

    /// Samples all tracks from adapter instants while keeping deterministic elapsed-time
    /// semantics in the controller layer.
    pub fn sample_since(
        &self,
        started_at: Instant,
        now: Instant,
    ) -> MotionScalarControllerSample<K> {
        self.sample_at(now.saturating_duration_since(started_at))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        MotionDuration, MotionEasing, MotionModel, MotionPolicyContext, MotionPreference,
        MotionRunState, MotionSpec, MotionSpringSpec,
    };
    use std::time::Duration;

    #[test]
    fn grouped_tracks_report_frame_demand_until_every_track_completes() {
        let mut controller = MotionScalarController::new();
        let model = MotionModel::timeline(MotionSpec::new(
            MotionPreference::Animated,
            MotionDuration::Custom(Duration::from_millis(100)),
            MotionEasing::Linear,
        ));

        controller.start("left", model, 0.0, 1.0, 0.0, Duration::ZERO);
        controller.start("right", model, 1.0, 0.0, 0.0, Duration::ZERO);

        let active = controller.sample_at(Duration::from_millis(50));
        assert!(active.frame_demand().needs_frame());
        assert_eq!(
            active.frame_demand().reason(),
            Some(MotionFrameReason::UpdateRender)
        );
        assert_eq!(active.tracks().len(), 2);
        assert!(
            active
                .tracks()
                .iter()
                .all(|track| track.sample().is_active())
        );

        let complete = controller.sample_at(Duration::from_millis(120));
        assert!(!complete.frame_demand().needs_frame());
        assert!(
            complete
                .tracks()
                .iter()
                .all(|track| track.sample().reached_final_state())
        );
        assert!(complete.complete());
    }

    #[test]
    fn frame_demand_combines_idle_and_active_with_stable_reason() {
        let active = MotionFrameDemand::NeedsFrame(MotionFrameReason::UpdateRender);

        assert_eq!(MotionFrameDemand::Idle.combine(active), active);
        assert_eq!(active.combine(MotionFrameDemand::Idle), active);
        assert_eq!(
            MotionFrameDemand::combine_all([
                MotionFrameDemand::Idle,
                active,
                MotionFrameDemand::Idle,
            ]),
            active
        );
    }

    #[test]
    fn clock_sample_clamps_non_monotonic_elapsed_time() {
        let sample =
            MotionClockSample::from_elapsed(Duration::from_millis(40), Duration::from_millis(15));

        assert_eq!(sample.elapsed(), Duration::from_millis(40));
        assert_eq!(sample.delta(), Duration::ZERO);
        assert!(sample.clamped());
    }

    #[test]
    fn finishing_track_jumps_to_target_and_stops_frame_demand() {
        let mut track = MotionScalarTrack::start(
            MotionModel::timeline(MotionSpec::new(
                MotionPreference::Animated,
                MotionDuration::Custom(Duration::from_millis(200)),
                MotionEasing::Linear,
            )),
            0.0,
            1.0,
            0.0,
            Duration::ZERO,
        );

        track.finish_at(Duration::from_millis(40));
        let sample = track.sample_at(Duration::from_millis(50));

        assert_eq!(sample.state(), MotionRunState::Completed);
        assert_eq!(sample.value(), 1.0);
        assert!(sample.reached_final_state());
        assert!(
            !track
                .frame_demand_at(Duration::from_millis(50))
                .needs_frame()
        );
    }

    #[test]
    fn controller_prunes_terminal_tracks_after_cancel_and_finish() {
        let mut controller = MotionScalarController::new();
        let model = MotionModel::timeline(MotionSpec::new(
            MotionPreference::Animated,
            MotionDuration::Custom(Duration::from_millis(200)),
            MotionEasing::Linear,
        ));

        controller.start("cancelled", model, 0.0, 1.0, 0.0, Duration::ZERO);
        controller.start("finished", model, 0.0, 1.0, 0.0, Duration::ZERO);
        controller.start("active", model, 0.0, 1.0, 0.0, Duration::ZERO);
        controller.cancel(&"cancelled", Duration::from_millis(40));
        controller.finish(&"finished", Duration::from_millis(40));

        assert_eq!(controller.prune_terminal_at(Duration::from_millis(50)), 2);
        assert_eq!(controller.tracks().len(), 1);
        assert_eq!(controller.tracks()[0].0, "active");
        assert!(
            controller
                .frame_demand_at(Duration::from_millis(50))
                .needs_frame()
        );
    }

    #[test]
    fn retarget_preserves_sampled_value_and_velocity_for_one_track() {
        let mut controller = MotionScalarController::new();
        let model = MotionModel::spring(MotionSpringSpec::layout(MotionPreference::Animated));

        controller.start("pane", model, 0.0, 1.0, 0.0, Duration::ZERO);
        let before = controller.sample_at(Duration::from_millis(80));
        let sampled = before.track(&"pane").expect("pane track").sample();

        controller.retarget("pane", model, 2.0, Duration::from_millis(80));
        let after = controller.sample_at(Duration::from_millis(80));
        let retargeted = after.track(&"pane").expect("pane track").sample();

        assert_eq!(retargeted.value(), sampled.value());
        assert_eq!(retargeted.velocity(), sampled.velocity());
        assert_eq!(retargeted.target(), 2.0);
        assert!(after.frame_demand().needs_frame());
        assert_eq!(
            after.frame_demand().reason(),
            Some(MotionFrameReason::UpdateRender)
        );
    }

    #[test]
    fn cancelling_track_is_terminal_without_reaching_final_state() {
        let mut track = MotionScalarTrack::start(
            MotionModel::timeline(MotionSpec::new(
                MotionPreference::Animated,
                MotionDuration::Custom(Duration::from_millis(200)),
                MotionEasing::Linear,
            )),
            0.0,
            1.0,
            0.0,
            Duration::ZERO,
        );

        track.cancel_at(Duration::from_millis(40));
        let sample = track.sample_at(Duration::from_millis(100));

        assert_eq!(sample.state(), MotionRunState::Cancelled);
        assert!(!sample.reached_final_state());
        assert!(
            !track
                .frame_demand_at(Duration::from_millis(100))
                .needs_frame()
        );
    }

    #[test]
    fn immediate_track_never_requests_a_frame() {
        let track = MotionScalarTrack::immediate(0.75, Duration::from_millis(10));

        let sample = track.sample_at(Duration::from_millis(10));
        assert_eq!(sample.state(), MotionRunState::Immediate);
        assert_eq!(sample.value(), 0.75);
        assert!(
            !track
                .frame_demand_at(Duration::from_millis(10))
                .needs_frame()
        );
    }

    #[test]
    fn execution_plan_downgrades_policy_failures_to_immediate_motion() {
        let requested_model = MotionModel::timeline(MotionSpec::new(
            MotionPreference::Animated,
            MotionDuration::Custom(Duration::from_millis(900)),
            MotionEasing::Linear,
        ));
        let plan = MotionExecutionPlan::resolve(
            MotionPolicyInput::new(MotionPolicyContext::CommittedLayout, requested_model)
                .with_spatial_motion(true)
                .with_reduced_motion_final_state(true),
        );

        assert!(!plan.policy_report().is_ok());
        assert!(plan.is_immediate());
        assert!(plan.model().is_immediate());
    }

    #[test]
    fn scalar_execution_reports_completion_and_frame_demand_from_one_sample() {
        let execution = MotionScalarExecution::start_resolved(
            MotionPolicyInput::new(
                MotionPolicyContext::CommittedLayout,
                MotionModel::timeline(MotionSpec::new(
                    MotionPreference::Animated,
                    MotionDuration::Custom(Duration::from_millis(100)),
                    MotionEasing::Linear,
                )),
            )
            .with_spatial_motion(true)
            .with_reduced_motion_final_state(true),
            0.0,
            1.0,
            0.0,
            Duration::ZERO,
        );

        let midpoint = execution.sample_at(Duration::from_millis(50));
        assert_eq!(midpoint.value(), 0.5);
        assert!(!midpoint.complete());
        assert!(midpoint.frame_demand().needs_frame());

        let complete = execution.sample_at(Duration::from_millis(120));
        assert_eq!(complete.value(), 1.0);
        assert!(complete.complete());
        assert!(!complete.frame_demand().needs_frame());
    }

    #[test]
    fn reduced_motion_execution_publishes_final_value_without_frame_demand() {
        let execution = MotionScalarExecution::start_resolved(
            MotionPolicyInput::new(
                MotionPolicyContext::CommittedLayout,
                MotionModel::timeline(MotionSpec::committed_layout(MotionPreference::Reduced)),
            )
            .with_reduced_motion_final_state(true),
            0.0,
            1.0,
            0.0,
            Duration::ZERO,
        );

        let sample = execution.sample_at(Duration::ZERO);

        assert_eq!(sample.scalar_sample().state(), MotionRunState::Immediate);
        assert_eq!(sample.value(), 1.0);
        assert!(sample.complete());
        assert!(!sample.frame_demand().needs_frame());
    }

    #[test]
    fn progress_execution_samples_normalized_lifecycle() {
        let started_at = Instant::now();
        let progress = MotionProgressExecution::start_resolved(
            MotionPolicyInput::new(
                MotionPolicyContext::CommittedLayout,
                MotionModel::timeline(MotionSpec::new(
                    MotionPreference::Animated,
                    MotionDuration::Custom(Duration::from_millis(100)),
                    MotionEasing::Linear,
                )),
            )
            .with_spatial_motion(true)
            .with_reduced_motion_final_state(true),
            started_at,
        );

        let midpoint = progress.sample_since(started_at + Duration::from_millis(50));
        assert_eq!(midpoint.progress(), 0.5);
        assert!(midpoint.frame_demand().needs_frame());

        let complete = progress.sample_at(Duration::from_millis(120));
        assert_eq!(complete.progress(), 1.0);
        assert!(complete.complete());
        assert!(!complete.frame_demand().needs_frame());
    }
}