@mun-ui/ui 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (158) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +104 -2
  3. package/bin/compile.mjs +38 -0
  4. package/bin/dev-protocol.mjs +94 -0
  5. package/bin/formatter.mjs +109 -0
  6. package/bin/hot-reload.mjs +38 -0
  7. package/bin/mun.mjs +521 -0
  8. package/bin/native.mjs +133 -0
  9. package/bin/pnpm-workspace.mjs +58 -0
  10. package/bin/program-patch.mjs +104 -0
  11. package/bin/project.mjs +322 -0
  12. package/bin/watch.mjs +316 -0
  13. package/bin/workflow.mjs +261 -0
  14. package/dist/astro.d.ts +3 -0
  15. package/dist/astro.d.ts.map +1 -0
  16. package/dist/astro.js +3 -0
  17. package/dist/astro.js.map +1 -0
  18. package/dist/closures.d.ts +2 -0
  19. package/dist/closures.d.ts.map +1 -0
  20. package/dist/closures.js +2 -0
  21. package/dist/closures.js.map +1 -0
  22. package/dist/compiler/index.d.ts +2 -0
  23. package/dist/compiler/index.d.ts.map +1 -0
  24. package/dist/compiler/index.js +2 -0
  25. package/dist/compiler/index.js.map +1 -0
  26. package/dist/core.d.ts +3 -0
  27. package/dist/core.d.ts.map +1 -0
  28. package/dist/core.js +3 -0
  29. package/dist/core.js.map +1 -0
  30. package/dist/experimental.d.ts +2 -0
  31. package/dist/experimental.d.ts.map +1 -0
  32. package/dist/experimental.js +2 -0
  33. package/dist/experimental.js.map +1 -0
  34. package/dist/index.d.ts +3 -0
  35. package/dist/index.d.ts.map +1 -0
  36. package/dist/index.js +3 -0
  37. package/dist/index.js.map +1 -0
  38. package/dist/jsx-dev-runtime.d.ts +2 -0
  39. package/dist/jsx-dev-runtime.d.ts.map +1 -0
  40. package/dist/jsx-dev-runtime.js +2 -0
  41. package/dist/jsx-dev-runtime.js.map +1 -0
  42. package/dist/jsx-runtime.d.ts +2 -0
  43. package/dist/jsx-runtime.d.ts.map +1 -0
  44. package/dist/jsx-runtime.js +2 -0
  45. package/dist/jsx-runtime.js.map +1 -0
  46. package/dist/legacy.d.ts +3 -0
  47. package/dist/legacy.d.ts.map +1 -0
  48. package/dist/legacy.js +3 -0
  49. package/dist/legacy.js.map +1 -0
  50. package/dist/mun.d.ts +5 -0
  51. package/dist/mun.d.ts.map +1 -0
  52. package/dist/mun.js +4 -0
  53. package/dist/mun.js.map +1 -0
  54. package/dist/progressive-blur/index.d.ts +8 -0
  55. package/dist/progressive-blur/index.d.ts.map +1 -0
  56. package/dist/progressive-blur/index.js +8 -0
  57. package/dist/progressive-blur/index.js.map +1 -0
  58. package/dist/progressive-blur/plan.d.ts +3 -0
  59. package/dist/progressive-blur/plan.d.ts.map +1 -0
  60. package/dist/progressive-blur/plan.js +3 -0
  61. package/dist/progressive-blur/plan.js.map +1 -0
  62. package/dist/react-types.d.ts +2 -0
  63. package/dist/react-types.d.ts.map +1 -0
  64. package/dist/react-types.js +2 -0
  65. package/dist/react-types.js.map +1 -0
  66. package/dist/rendering-quality.d.ts +4 -0
  67. package/dist/rendering-quality.d.ts.map +1 -0
  68. package/dist/rendering-quality.js +4 -0
  69. package/dist/rendering-quality.js.map +1 -0
  70. package/dist/state.d.ts +2 -0
  71. package/dist/state.d.ts.map +1 -0
  72. package/dist/state.js +2 -0
  73. package/dist/state.js.map +1 -0
  74. package/dist/types.d.ts +2 -0
  75. package/dist/types.d.ts.map +1 -0
  76. package/dist/types.js +2 -0
  77. package/dist/types.js.map +1 -0
  78. package/dist/vite.d.ts +2 -0
  79. package/dist/vite.d.ts.map +1 -0
  80. package/dist/vite.js +2 -0
  81. package/dist/vite.js.map +1 -0
  82. package/editors/install.mjs +102 -0
  83. package/editors/lsp/mun-lsp.mjs +123 -0
  84. package/editors/lsp/service.mjs +306 -0
  85. package/editors/lsp/source.mjs +58 -0
  86. package/editors/vscode/client.mjs +40 -0
  87. package/editors/vscode/dev-client.mjs +83 -0
  88. package/editors/vscode/discovery.mjs +46 -0
  89. package/editors/vscode/export.mjs +42 -0
  90. package/editors/vscode/extension.cjs +226 -0
  91. package/editors/vscode/language-configuration.json +15 -0
  92. package/editors/vscode/package.json +119 -0
  93. package/editors/vscode/protocol.mjs +29 -0
  94. package/editors/vscode/syntaxes/mun.tmLanguage.json +130 -0
  95. package/native/Cargo.lock +3581 -0
  96. package/native/Cargo.toml +21 -0
  97. package/native/README.md +21 -0
  98. package/native/mun-native/Cargo.toml +23 -0
  99. package/native/mun-native/src/accessibility.rs +738 -0
  100. package/native/mun-native/src/dev.rs +604 -0
  101. package/native/mun-native/src/lib.rs +2676 -0
  102. package/native/mun-native/src/main.rs +204 -0
  103. package/native/mun-native/src/offscreen.rs +393 -0
  104. package/native/mun-native/src/rect.wgsl +63 -0
  105. package/native/mun-native/src/text.rs +310 -0
  106. package/native/mun-native/tests/offscreen_rendering.rs +161 -0
  107. package/native/mun-native/tests/stress.rs +408 -0
  108. package/native/mun-native/tests/window_lifecycle.rs +183 -0
  109. package/native/mun-runtime/Cargo.toml +13 -0
  110. package/native/mun-runtime/src/accessibility.rs +193 -0
  111. package/native/mun-runtime/src/collection.rs +1073 -0
  112. package/native/mun-runtime/src/gesture.rs +1070 -0
  113. package/native/mun-runtime/src/input.rs +325 -0
  114. package/native/mun-runtime/src/ir.rs +782 -0
  115. package/native/mun-runtime/src/kinetics.rs +500 -0
  116. package/native/mun-runtime/src/layout.rs +173 -0
  117. package/native/mun-runtime/src/layout_cache.rs +209 -0
  118. package/native/mun-runtime/src/lib.rs +49 -0
  119. package/native/mun-runtime/src/motion.rs +1162 -0
  120. package/native/mun-runtime/src/retained.rs +574 -0
  121. package/native/mun-runtime/src/runtime.rs +7078 -0
  122. package/native/mun-runtime/src/scene.rs +530 -0
  123. package/native/mun-runtime/src/scroll.rs +521 -0
  124. package/native/mun-runtime/src/scroll_view.rs +220 -0
  125. package/native/mun-runtime/src/text_edit.rs +670 -0
  126. package/native/mun-runtime/src/timeline.rs +1534 -0
  127. package/native/mun-runtime/src/validate.rs +677 -0
  128. package/native/mun-runtime/tests/accessibility_semantics.rs +168 -0
  129. package/native/mun-runtime/tests/compiler_contract.rs +110 -0
  130. package/native/mun-runtime/tests/flexible_frames.rs +47 -0
  131. package/native/mun-runtime/tests/hot_update.rs +367 -0
  132. package/native/mun-runtime/tests/incremental_runtime.rs +323 -0
  133. package/native/mun-runtime/tests/ir_validation.rs +167 -0
  134. package/native/mun-runtime/tests/keyed_collections.rs +251 -0
  135. package/native/mun-runtime/tests/native_controls_style.rs +491 -0
  136. package/native/mun-runtime/tests/native_demo.rs +285 -0
  137. package/native/mun-runtime/tests/native_layout_style.rs +175 -0
  138. package/native/mun-runtime/tests/native_settings_pane.rs +152 -0
  139. package/native/mun-runtime/tests/scroll_view.rs +403 -0
  140. package/native/mun-runtime/tests/text_editing.rs +491 -0
  141. package/package.json +210 -3
  142. package/schemas/README.md +19 -0
  143. package/schemas/semantic-ui-ir-v1.schema.json +1972 -0
  144. package/templates/native/App.mun +15 -0
  145. package/templates/native/gitignore +3 -0
  146. package/templates/native/mun.toml +8 -0
  147. package/templates/project/gitignore +5 -0
  148. package/templates/project/index.html +13 -0
  149. package/templates/project/package.json +21 -0
  150. package/templates/project/src/App.mun +13 -0
  151. package/templates/project/src/index.css +21 -0
  152. package/templates/project/src/main.ts +5 -0
  153. package/templates/project/tsconfig.json +20 -0
  154. package/templates/project/vite.config.ts +6 -0
  155. package/templates/vite/src/App.css +134 -0
  156. package/templates/vite/src/App.tsx +91 -0
  157. package/templates/vite/src/index.css +23 -0
  158. package/templates/vite/vite.config.ts +10 -0
@@ -0,0 +1,1162 @@
1
+ use std::collections::{HashMap, HashSet};
2
+
3
+ #[path = "kinetics.rs"]
4
+ pub mod kinetics;
5
+
6
+ pub use kinetics::{DecaySpec, InertiaSpec, KineticSample, KineticSpec, KineticSpecError};
7
+
8
+ use self::kinetics::KineticChannel;
9
+ use crate::{
10
+ ir::{MotionExecutionPlan, MotionProperty},
11
+ timeline::{TimelineClock, TimelineDirection},
12
+ };
13
+
14
+ const MAX_STEP_SECONDS: f32 = 1.0 / 240.0;
15
+ const MAX_SUBSTEPS: usize = 32;
16
+ const POSITION_EPSILON: f32 = 0.001;
17
+ const VELOCITY_EPSILON: f32 = 0.01;
18
+
19
+ #[derive(Clone, Debug, Eq, Hash, PartialEq)]
20
+ pub struct MotionChannelKey {
21
+ pub node_id: String,
22
+ pub property: MotionProperty,
23
+ }
24
+
25
+ #[derive(Clone, Debug)]
26
+ struct SpringChannel {
27
+ position: f32,
28
+ velocity: f32,
29
+ origin: f32,
30
+ forward_target: f32,
31
+ target: f32,
32
+ cycle: u64,
33
+ iterations: Option<u64>,
34
+ autoreverses: bool,
35
+ omega: f32,
36
+ damping_ratio: f32,
37
+ delay_remaining: f32,
38
+ blend_from_omega: f32,
39
+ blend_from_damping_ratio: f32,
40
+ blend_to_omega: f32,
41
+ blend_to_damping_ratio: f32,
42
+ blend_elapsed: f32,
43
+ blend_duration: f32,
44
+ settled: bool,
45
+ }
46
+
47
+ #[derive(Clone, Debug)]
48
+ struct TimingChannel {
49
+ position: f32,
50
+ velocity: f32,
51
+ from: f32,
52
+ target: f32,
53
+ duration: f32,
54
+ curve: [f32; 4],
55
+ clock: TimelineClock,
56
+ iterations: Option<u64>,
57
+ autoreverses: bool,
58
+ delay_remaining: f32,
59
+ settled: bool,
60
+ }
61
+
62
+ #[derive(Clone, Debug)]
63
+ enum MotionChannel {
64
+ Spring(SpringChannel),
65
+ Timing(TimingChannel),
66
+ Kinetic(KineticChannel),
67
+ }
68
+
69
+ impl MotionChannel {
70
+ fn position(&self) -> f32 {
71
+ match self {
72
+ Self::Spring(channel) => channel.position,
73
+ Self::Timing(channel) => channel.position,
74
+ Self::Kinetic(channel) => channel.position(),
75
+ }
76
+ }
77
+
78
+ fn velocity(&self) -> f32 {
79
+ match self {
80
+ Self::Spring(channel) => channel.velocity,
81
+ Self::Timing(channel) => channel.velocity,
82
+ Self::Kinetic(channel) => channel.velocity(),
83
+ }
84
+ }
85
+
86
+ fn settled(&self) -> bool {
87
+ match self {
88
+ Self::Spring(channel) => channel.settled,
89
+ Self::Timing(channel) => channel.settled,
90
+ Self::Kinetic(channel) => channel.settled(),
91
+ }
92
+ }
93
+
94
+ fn stop_after_current_cycle(&mut self) {
95
+ match self {
96
+ Self::Spring(channel) => {
97
+ channel.iterations = Some(channel.cycle.saturating_add(1));
98
+ }
99
+ Self::Timing(channel) => channel.clock.stop_after_current_iteration(),
100
+ Self::Kinetic(_) => {}
101
+ }
102
+ }
103
+ }
104
+
105
+ #[derive(Clone, Debug)]
106
+ struct PendingRetarget {
107
+ target: f32,
108
+ plan: MotionExecutionPlan,
109
+ remaining: f32,
110
+ }
111
+
112
+ /// Native counterpart to the existing Mün runtime's renderer-neutral per-property motion scheduler.
113
+ ///
114
+ /// Spring retargeting deliberately preserves velocity. Timing channels use the
115
+ /// same cubic-bezier solving algorithm as the existing Mün runtime's `core/bezier.js` and retarget
116
+ /// from the current presentation value, keeping channel ownership independent
117
+ /// per node/property rather than falling back to timer-driven widget effects.
118
+ #[derive(Default, Debug)]
119
+ pub struct MotionScheduler {
120
+ channels: Vec<MotionChannel>,
121
+ indices: HashMap<MotionChannelKey, usize>,
122
+ pending: HashMap<usize, PendingRetarget>,
123
+ }
124
+
125
+ impl MotionScheduler {
126
+ /// Start inherited decay/inertia continuation for one renderer-neutral
127
+ /// node/property channel. When a live channel already owns the property,
128
+ /// its current presentation value and velocity become the kinetic handoff
129
+ /// unless the kinetic spec supplies an explicit release velocity.
130
+ pub fn animate_velocity(
131
+ &mut self,
132
+ key: MotionChannelKey,
133
+ current: f32,
134
+ spec: &KineticSpec,
135
+ ) -> Result<(), KineticSpecError> {
136
+ if let Some(index) = self.indices.get(&key).copied() {
137
+ let presentation = self.channels[index].position();
138
+ let inherited_velocity = self.channels[index].velocity();
139
+ let channel = KineticChannel::new(presentation, inherited_velocity, spec)?;
140
+ self.pending.remove(&index);
141
+ self.channels[index] = MotionChannel::Kinetic(channel);
142
+ return Ok(());
143
+ }
144
+
145
+ let index = self.channels.len();
146
+ let channel = KineticChannel::new(current, 0.0, spec)?;
147
+ self.channels.push(MotionChannel::Kinetic(channel));
148
+ self.indices.insert(key, index);
149
+ Ok(())
150
+ }
151
+
152
+ pub fn retarget(
153
+ &mut self,
154
+ key: MotionChannelKey,
155
+ current: f32,
156
+ target: f32,
157
+ plan: &MotionExecutionPlan,
158
+ ) {
159
+ if let Some(index) = self.indices.get(&key).copied() {
160
+ let delay = plan_delay_seconds(plan);
161
+ if delay > 0.0 {
162
+ // Match the existing Mün runtime's delayed-retarget generation semantics: the
163
+ // current control keeps running, but no future repeat cycles
164
+ // are started before the delayed handoff.
165
+ self.channels[index].stop_after_current_cycle();
166
+ self.pending.insert(
167
+ index,
168
+ PendingRetarget {
169
+ target,
170
+ plan: plan.clone(),
171
+ remaining: delay,
172
+ },
173
+ );
174
+ return;
175
+ }
176
+ self.pending.remove(&index);
177
+ self.retarget_index(index, target, plan);
178
+ return;
179
+ }
180
+
181
+ let index = self.channels.len();
182
+ self.channels
183
+ .push(channel_from_plan(current, target, plan, 0.0, true));
184
+ self.indices.insert(key, index);
185
+ }
186
+
187
+ fn retarget_index(&mut self, index: usize, target: f32, plan: &MotionExecutionPlan) {
188
+ let presentation = self.channels[index].position();
189
+ let velocity = self.channels[index].velocity();
190
+ match (plan, &mut self.channels[index]) {
191
+ (
192
+ MotionExecutionPlan::Spring {
193
+ omega,
194
+ damping_ratio,
195
+ blend_duration,
196
+ repeat_count,
197
+ autoreverses,
198
+ ..
199
+ },
200
+ MotionChannel::Spring(channel),
201
+ ) => {
202
+ channel.origin = presentation;
203
+ channel.forward_target = target;
204
+ channel.target = target;
205
+ channel.cycle = 0;
206
+ channel.iterations = repeat_iterations(repeat_count);
207
+ channel.autoreverses = *autoreverses;
208
+ channel.delay_remaining = 0.0;
209
+ let blend_duration = (*blend_duration).max(0.0);
210
+ if blend_duration > 0.0
211
+ && (channel.omega != *omega || channel.damping_ratio != *damping_ratio)
212
+ {
213
+ channel.blend_from_omega = channel.omega;
214
+ channel.blend_from_damping_ratio = channel.damping_ratio;
215
+ channel.blend_to_omega = *omega;
216
+ channel.blend_to_damping_ratio = *damping_ratio;
217
+ channel.blend_elapsed = 0.0;
218
+ channel.blend_duration = blend_duration;
219
+ } else {
220
+ channel.omega = *omega;
221
+ channel.damping_ratio = *damping_ratio;
222
+ channel.blend_from_omega = *omega;
223
+ channel.blend_from_damping_ratio = *damping_ratio;
224
+ channel.blend_to_omega = *omega;
225
+ channel.blend_to_damping_ratio = *damping_ratio;
226
+ channel.blend_elapsed = 0.0;
227
+ channel.blend_duration = 0.0;
228
+ }
229
+ // Deliberately preserve the live channel velocity.
230
+ channel.velocity = velocity;
231
+ channel.settled = false;
232
+ }
233
+ (
234
+ MotionExecutionPlan::Timing {
235
+ duration,
236
+ curve,
237
+ repeat_count,
238
+ autoreverses,
239
+ ..
240
+ },
241
+ MotionChannel::Timing(channel),
242
+ ) => {
243
+ let duration = duration.max(0.0);
244
+ let iterations = repeat_iterations(repeat_count);
245
+ channel.from = presentation;
246
+ channel.position = presentation;
247
+ channel.target = target;
248
+ channel.duration = duration;
249
+ channel.curve = *curve;
250
+ channel.clock = timeline_clock(duration, iterations, *autoreverses);
251
+ channel.iterations = iterations;
252
+ channel.autoreverses = *autoreverses;
253
+ channel.delay_remaining = 0.0;
254
+ channel.velocity = 0.0;
255
+ channel.settled = false;
256
+ }
257
+ _ => {
258
+ self.channels[index] =
259
+ channel_from_plan(presentation, target, plan, velocity, false);
260
+ }
261
+ }
262
+ }
263
+
264
+ pub fn snap(&mut self, key: &MotionChannelKey, target: f32) {
265
+ let Some(index) = self.indices.get(key).copied() else {
266
+ return;
267
+ };
268
+ self.pending.remove(&index);
269
+ match &mut self.channels[index] {
270
+ MotionChannel::Spring(channel) => {
271
+ channel.position = target;
272
+ channel.velocity = 0.0;
273
+ channel.origin = target;
274
+ channel.forward_target = target;
275
+ channel.target = target;
276
+ channel.cycle = 0;
277
+ channel.iterations = Some(1);
278
+ channel.delay_remaining = 0.0;
279
+ channel.blend_elapsed = 0.0;
280
+ channel.blend_duration = 0.0;
281
+ channel.settled = true;
282
+ }
283
+ MotionChannel::Timing(channel) => {
284
+ channel.position = target;
285
+ channel.velocity = 0.0;
286
+ channel.from = target;
287
+ channel.target = target;
288
+ channel.clock = timeline_clock(channel.duration, Some(1), false);
289
+ channel.clock.seek_elapsed(channel.duration as f64);
290
+ channel.iterations = Some(1);
291
+ channel.autoreverses = false;
292
+ channel.delay_remaining = 0.0;
293
+ channel.settled = true;
294
+ }
295
+ MotionChannel::Kinetic(channel) => channel.snap(target),
296
+ }
297
+ }
298
+
299
+ /// Remove all renderer-neutral channels owned by semantic node identities.
300
+ ///
301
+ /// Retained identity replacement uses this to prevent presentation velocity,
302
+ /// delayed handoffs, or settled values from leaking into a new runtime
303
+ /// instance that happens to reuse the same structural node ID.
304
+ pub fn remove_nodes<'a>(&mut self, node_ids: impl IntoIterator<Item = &'a str>) -> usize {
305
+ let node_ids = node_ids.into_iter().collect::<HashSet<_>>();
306
+ if node_ids.is_empty() || self.channels.is_empty() {
307
+ return 0;
308
+ }
309
+
310
+ let old_channels = std::mem::take(&mut self.channels);
311
+ let old_indices = std::mem::take(&mut self.indices);
312
+ let old_pending = std::mem::take(&mut self.pending);
313
+ let mut keep = vec![true; old_channels.len()];
314
+ let mut retained_keys = Vec::with_capacity(old_indices.len());
315
+
316
+ for (key, old_index) in old_indices {
317
+ if node_ids.contains(key.node_id.as_str()) {
318
+ keep[old_index] = false;
319
+ } else {
320
+ retained_keys.push((key, old_index));
321
+ }
322
+ }
323
+
324
+ let removed = keep.iter().filter(|keep| !**keep).count();
325
+ let mut remap = vec![None; old_channels.len()];
326
+ for (old_index, channel) in old_channels.into_iter().enumerate() {
327
+ if keep[old_index] {
328
+ let new_index = self.channels.len();
329
+ remap[old_index] = Some(new_index);
330
+ self.channels.push(channel);
331
+ }
332
+ }
333
+
334
+ for (key, old_index) in retained_keys {
335
+ if let Some(new_index) = remap[old_index] {
336
+ self.indices.insert(key, new_index);
337
+ }
338
+ }
339
+ for (old_index, pending) in old_pending {
340
+ if let Some(new_index) = remap.get(old_index).and_then(|index| *index) {
341
+ self.pending.insert(new_index, pending);
342
+ }
343
+ }
344
+
345
+ removed
346
+ }
347
+
348
+ pub fn step(&mut self, dt_seconds: f32) -> bool {
349
+ if self.channels.is_empty() || dt_seconds <= 0.0 {
350
+ return false;
351
+ }
352
+
353
+ for index in 0..self.channels.len() {
354
+ let mut handoff: Option<(f32, MotionExecutionPlan, f32)> = None;
355
+ if let Some(pending) = self.pending.get_mut(&index) {
356
+ let before_handoff = dt_seconds.min(pending.remaining);
357
+ if before_handoff > 0.0 {
358
+ step_channel(&mut self.channels[index], before_handoff);
359
+ pending.remaining = (pending.remaining - before_handoff).max(0.0);
360
+ }
361
+ if pending.remaining <= f32::EPSILON {
362
+ handoff = Some((
363
+ pending.target,
364
+ pending.plan.clone(),
365
+ dt_seconds - before_handoff,
366
+ ));
367
+ }
368
+ } else {
369
+ step_channel(&mut self.channels[index], dt_seconds);
370
+ }
371
+
372
+ if let Some((target, plan, remaining)) = handoff {
373
+ self.pending.remove(&index);
374
+ self.retarget_index(index, target, &plan);
375
+ if remaining > 0.0 {
376
+ step_channel(&mut self.channels[index], remaining);
377
+ }
378
+ }
379
+ }
380
+
381
+ self.is_active()
382
+ }
383
+
384
+ pub fn value(&self, key: &MotionChannelKey) -> Option<f32> {
385
+ self.indices
386
+ .get(key)
387
+ .and_then(|index| self.channels.get(*index))
388
+ .map(MotionChannel::position)
389
+ }
390
+
391
+ pub fn velocity(&self, key: &MotionChannelKey) -> Option<f32> {
392
+ self.indices
393
+ .get(key)
394
+ .and_then(|index| self.channels.get(*index))
395
+ .map(MotionChannel::velocity)
396
+ }
397
+
398
+ pub fn is_key_active(&self, key: &MotionChannelKey) -> bool {
399
+ let Some(index) = self.indices.get(key).copied() else {
400
+ return false;
401
+ };
402
+ self.pending.contains_key(&index)
403
+ || self
404
+ .channels
405
+ .get(index)
406
+ .map(|channel| !channel.settled())
407
+ .unwrap_or(false)
408
+ }
409
+
410
+ pub fn is_active(&self) -> bool {
411
+ !self.pending.is_empty() || self.channels.iter().any(|channel| !channel.settled())
412
+ }
413
+ }
414
+
415
+ fn channel_from_plan(
416
+ current: f32,
417
+ target: f32,
418
+ plan: &MotionExecutionPlan,
419
+ initial_velocity: f32,
420
+ honor_delay: bool,
421
+ ) -> MotionChannel {
422
+ match plan {
423
+ MotionExecutionPlan::Spring {
424
+ omega,
425
+ damping_ratio,
426
+ delay_ms,
427
+ repeat_count,
428
+ autoreverses,
429
+ ..
430
+ } => MotionChannel::Spring(SpringChannel {
431
+ position: current,
432
+ velocity: initial_velocity,
433
+ origin: current,
434
+ forward_target: target,
435
+ target,
436
+ cycle: 0,
437
+ iterations: repeat_iterations(repeat_count),
438
+ autoreverses: *autoreverses,
439
+ omega: *omega,
440
+ damping_ratio: *damping_ratio,
441
+ delay_remaining: if honor_delay {
442
+ (*delay_ms / 1000.0).max(0.0)
443
+ } else {
444
+ 0.0
445
+ },
446
+ blend_from_omega: *omega,
447
+ blend_from_damping_ratio: *damping_ratio,
448
+ blend_to_omega: *omega,
449
+ blend_to_damping_ratio: *damping_ratio,
450
+ blend_elapsed: 0.0,
451
+ blend_duration: 0.0,
452
+ settled: false,
453
+ }),
454
+ MotionExecutionPlan::Timing {
455
+ duration,
456
+ curve,
457
+ delay_ms,
458
+ repeat_count,
459
+ autoreverses,
460
+ ..
461
+ } => {
462
+ let duration = duration.max(0.0);
463
+ let iterations = repeat_iterations(repeat_count);
464
+ MotionChannel::Timing(TimingChannel {
465
+ position: current,
466
+ velocity: 0.0,
467
+ from: current,
468
+ target,
469
+ duration,
470
+ curve: *curve,
471
+ clock: timeline_clock(duration, iterations, *autoreverses),
472
+ iterations,
473
+ autoreverses: *autoreverses,
474
+ delay_remaining: if honor_delay {
475
+ (*delay_ms / 1000.0).max(0.0)
476
+ } else {
477
+ 0.0
478
+ },
479
+ settled: false,
480
+ })
481
+ }
482
+ }
483
+ }
484
+
485
+ fn plan_delay_seconds(plan: &MotionExecutionPlan) -> f32 {
486
+ match plan {
487
+ MotionExecutionPlan::Spring { delay_ms, .. }
488
+ | MotionExecutionPlan::Timing { delay_ms, .. } => (*delay_ms / 1000.0).max(0.0),
489
+ }
490
+ }
491
+
492
+ fn repeat_iterations(value: &serde_json::Value) -> Option<u64> {
493
+ if value.as_str() == Some("infinite") {
494
+ None
495
+ } else {
496
+ Some(value.as_u64().unwrap_or(1).max(1))
497
+ }
498
+ }
499
+
500
+ fn timeline_clock(duration: f32, iterations: Option<u64>, autoreverses: bool) -> TimelineClock {
501
+ TimelineClock::new(
502
+ duration as f64,
503
+ iterations,
504
+ if autoreverses {
505
+ TimelineDirection::Alternate
506
+ } else {
507
+ TimelineDirection::Normal
508
+ },
509
+ )
510
+ }
511
+
512
+ fn step_channel(channel: &mut MotionChannel, dt_seconds: f32) {
513
+ match channel {
514
+ MotionChannel::Spring(channel) => step_spring(channel, dt_seconds),
515
+ MotionChannel::Timing(channel) => step_timing(channel, dt_seconds),
516
+ MotionChannel::Kinetic(channel) => channel.step(dt_seconds),
517
+ }
518
+ }
519
+
520
+ fn consume_delay(delay_remaining: &mut f32, dt_seconds: f32) -> f32 {
521
+ if *delay_remaining <= 0.0 {
522
+ return dt_seconds;
523
+ }
524
+ let active = (dt_seconds - *delay_remaining).max(0.0);
525
+ *delay_remaining = (*delay_remaining - dt_seconds).max(0.0);
526
+ active
527
+ }
528
+
529
+ fn step_spring(channel: &mut SpringChannel, dt_seconds: f32) {
530
+ if channel.settled {
531
+ return;
532
+ }
533
+ let dt_seconds = consume_delay(&mut channel.delay_remaining, dt_seconds);
534
+ if dt_seconds <= 0.0 {
535
+ return;
536
+ }
537
+
538
+ if channel.blend_duration > 0.0 {
539
+ channel.blend_elapsed = (channel.blend_elapsed + dt_seconds).min(channel.blend_duration);
540
+ let progress = channel.blend_elapsed / channel.blend_duration;
541
+ channel.omega = channel.blend_from_omega
542
+ + (channel.blend_to_omega - channel.blend_from_omega) * progress;
543
+ channel.damping_ratio = channel.blend_from_damping_ratio
544
+ + (channel.blend_to_damping_ratio - channel.blend_from_damping_ratio) * progress;
545
+ if progress >= 1.0 {
546
+ channel.omega = channel.blend_to_omega;
547
+ channel.damping_ratio = channel.blend_to_damping_ratio;
548
+ channel.blend_duration = 0.0;
549
+ channel.blend_elapsed = 0.0;
550
+ }
551
+ }
552
+
553
+ let mut steps = 1usize;
554
+ while dt_seconds / steps as f32 > MAX_STEP_SECONDS && steps < MAX_SUBSTEPS {
555
+ steps += 1;
556
+ }
557
+ let h = dt_seconds / steps as f32;
558
+
559
+ for _ in 0..steps {
560
+ let acceleration = channel.omega * channel.omega * (channel.target - channel.position)
561
+ - 2.0 * channel.damping_ratio * channel.omega * channel.velocity;
562
+ channel.velocity += acceleration * h;
563
+ channel.position += channel.velocity * h;
564
+
565
+ if (channel.target - channel.position).abs() <= POSITION_EPSILON
566
+ && channel.velocity.abs() <= VELOCITY_EPSILON
567
+ {
568
+ channel.position = channel.target;
569
+ channel.velocity = 0.0;
570
+ if !advance_spring_cycle(channel) {
571
+ channel.settled = true;
572
+ }
573
+ break;
574
+ }
575
+ }
576
+ }
577
+
578
+ fn advance_spring_cycle(channel: &mut SpringChannel) -> bool {
579
+ let next = channel.cycle.saturating_add(1);
580
+ if channel
581
+ .iterations
582
+ .is_some_and(|iterations| next >= iterations)
583
+ {
584
+ return false;
585
+ }
586
+ channel.cycle = next;
587
+ channel.settled = false;
588
+ if channel.autoreverses {
589
+ channel.target = if next % 2 == 1 {
590
+ channel.origin
591
+ } else {
592
+ channel.forward_target
593
+ };
594
+ } else {
595
+ // the existing Mün runtime's non-autoreversing repeat restarts from the origin between
596
+ // cycles instead of reversing the current channel.
597
+ channel.position = channel.origin;
598
+ channel.target = channel.forward_target;
599
+ }
600
+ channel.velocity = 0.0;
601
+ true
602
+ }
603
+
604
+ fn step_timing(channel: &mut TimingChannel, dt_seconds: f32) {
605
+ if channel.settled {
606
+ return;
607
+ }
608
+ let dt_seconds = consume_delay(&mut channel.delay_remaining, dt_seconds);
609
+ if dt_seconds <= 0.0 {
610
+ return;
611
+ }
612
+ if channel.duration <= f32::EPSILON {
613
+ let finishes_at_origin = channel.autoreverses
614
+ && channel
615
+ .iterations
616
+ .is_some_and(|iterations| iterations > 1 && iterations % 2 == 0);
617
+ channel.position = if finishes_at_origin {
618
+ channel.from
619
+ } else {
620
+ channel.target
621
+ };
622
+ channel.velocity = 0.0;
623
+ channel.settled = true;
624
+ return;
625
+ }
626
+
627
+ let step = channel.clock.step(dt_seconds as f64);
628
+ let progress = (step.mapping.local_time / channel.duration as f64) as f32;
629
+ let eased = evaluate_bezier(channel.curve, progress);
630
+ let delta = channel.target - channel.from;
631
+ channel.position = channel.from + delta * eased;
632
+ if step.finished {
633
+ channel.velocity = 0.0;
634
+ channel.settled = true;
635
+ } else {
636
+ let derivative = evaluate_bezier_derivative(channel.curve, progress);
637
+ channel.velocity = delta * derivative / channel.duration
638
+ * step.mapping.sign as f32
639
+ * channel.clock.playback_rate() as f32;
640
+ }
641
+ }
642
+
643
+ fn sample_curve(a1: f32, a2: f32, t: f32) -> f32 {
644
+ let inv = 1.0 - t;
645
+ 3.0 * inv * inv * t * a1 + 3.0 * inv * t * t * a2 + t * t * t
646
+ }
647
+
648
+ fn sample_derivative(a1: f32, a2: f32, t: f32) -> f32 {
649
+ 3.0 * (1.0 - t) * (1.0 - t) * a1 + 6.0 * (1.0 - t) * t * (a2 - a1) + 3.0 * t * t * (1.0 - a2)
650
+ }
651
+
652
+ /// Port of the existing Mün runtime's renderer-neutral cubic-bezier evaluator.
653
+ fn evaluate_bezier(curve: [f32; 4], progress: f32) -> f32 {
654
+ let [x1, y1, x2, y2] = curve;
655
+ let x = progress.clamp(0.0, 1.0);
656
+ if x1 == y1 && x2 == y2 {
657
+ return x;
658
+ }
659
+
660
+ let mut t = x;
661
+ for _ in 0..5 {
662
+ let estimate = sample_curve(x1, x2, t) - x;
663
+ let derivative = sample_derivative(x1, x2, t);
664
+ if derivative.abs() < 1e-7 {
665
+ break;
666
+ }
667
+ t = (t - estimate / derivative).clamp(0.0, 1.0);
668
+ }
669
+
670
+ let mut low = 0.0;
671
+ let mut high = 1.0;
672
+ for _ in 0..8 {
673
+ let estimate = sample_curve(x1, x2, t);
674
+ if (estimate - x).abs() < 1e-6 {
675
+ break;
676
+ }
677
+ if estimate < x {
678
+ low = t;
679
+ } else {
680
+ high = t;
681
+ }
682
+ t = (low + high) * 0.5;
683
+ }
684
+
685
+ sample_curve(y1, y2, t)
686
+ }
687
+
688
+ fn evaluate_bezier_derivative(curve: [f32; 4], progress: f32) -> f32 {
689
+ let [x1, y1, x2, y2] = curve;
690
+ let x = progress.clamp(0.0, 1.0);
691
+ if x1 == y1 && x2 == y2 {
692
+ return 1.0;
693
+ }
694
+ let mut t = x;
695
+ for _ in 0..5 {
696
+ let estimate = sample_curve(x1, x2, t) - x;
697
+ let derivative = sample_derivative(x1, x2, t);
698
+ if derivative.abs() < 1e-7 {
699
+ break;
700
+ }
701
+ t = (t - estimate / derivative).clamp(0.0, 1.0);
702
+ }
703
+ let mut low = 0.0;
704
+ let mut high = 1.0;
705
+ for _ in 0..8 {
706
+ let estimate = sample_curve(x1, x2, t);
707
+ if (estimate - x).abs() < 1e-6 {
708
+ break;
709
+ }
710
+ if estimate < x {
711
+ low = t;
712
+ } else {
713
+ high = t;
714
+ }
715
+ t = (low + high) * 0.5;
716
+ }
717
+ let dx = sample_derivative(x1, x2, t);
718
+ if dx.abs() < 1e-7 {
719
+ 0.0
720
+ } else {
721
+ sample_derivative(y1, y2, t) / dx
722
+ }
723
+ }
724
+
725
+ #[cfg(test)]
726
+ mod tests {
727
+ use super::*;
728
+ use serde_json::json;
729
+
730
+ fn spring() -> MotionExecutionPlan {
731
+ MotionExecutionPlan::Spring {
732
+ omega: std::f32::consts::TAU / 0.48,
733
+ damping_ratio: 0.82,
734
+ blend_duration: 0.0,
735
+ delay_ms: 0.0,
736
+ repeat_count: json!(1),
737
+ autoreverses: true,
738
+ }
739
+ }
740
+
741
+ fn timing() -> MotionExecutionPlan {
742
+ MotionExecutionPlan::Timing {
743
+ duration: 0.4,
744
+ curve: [0.42, 0.0, 0.58, 1.0],
745
+ delay_ms: 0.0,
746
+ repeat_count: json!(1),
747
+ autoreverses: true,
748
+ }
749
+ }
750
+
751
+ #[test]
752
+ fn spring_retarget_preserves_velocity() {
753
+ let key = MotionChannelKey {
754
+ node_id: "panel".into(),
755
+ property: MotionProperty::Width,
756
+ };
757
+ let mut scheduler = MotionScheduler::default();
758
+ scheduler.retarget(key.clone(), 160.0, 320.0, &spring());
759
+ scheduler.step(0.08);
760
+ let velocity_before = scheduler.velocity(&key).unwrap();
761
+ assert!(velocity_before.abs() > 1.0);
762
+
763
+ scheduler.retarget(
764
+ key.clone(),
765
+ scheduler.value(&key).unwrap(),
766
+ 160.0,
767
+ &spring(),
768
+ );
769
+ assert_eq!(scheduler.velocity(&key).unwrap(), velocity_before);
770
+ }
771
+
772
+ #[test]
773
+ fn removing_replaced_node_channels_compacts_indices_and_preserves_pending_handoffs() {
774
+ let replaced = MotionChannelKey {
775
+ node_id: "replaced".into(),
776
+ property: MotionProperty::Width,
777
+ };
778
+ let stable = MotionChannelKey {
779
+ node_id: "stable".into(),
780
+ property: MotionProperty::Opacity,
781
+ };
782
+ let mut scheduler = MotionScheduler::default();
783
+
784
+ scheduler.retarget(replaced.clone(), 0.0, 10.0, &timing());
785
+ scheduler.retarget(stable.clone(), 0.0, 5.0, &timing());
786
+
787
+ let mut delayed = timing();
788
+ if let MotionExecutionPlan::Timing { delay_ms, .. } = &mut delayed {
789
+ *delay_ms = 100.0;
790
+ }
791
+ scheduler.retarget(stable.clone(), 0.0, 20.0, &delayed);
792
+ assert!(scheduler.pending.contains_key(&1));
793
+
794
+ let removed = scheduler.remove_nodes(["replaced"]);
795
+
796
+ assert_eq!(removed, 1);
797
+ assert!(scheduler.value(&replaced).is_none());
798
+ assert!(scheduler.velocity(&replaced).is_none());
799
+ assert!(!scheduler.is_key_active(&replaced));
800
+ assert_eq!(scheduler.indices.get(&stable), Some(&0));
801
+ assert!(scheduler.pending.contains_key(&0));
802
+ assert_eq!(scheduler.pending.len(), 1);
803
+ assert!(scheduler.value(&stable).is_some());
804
+
805
+ scheduler.step(0.05);
806
+ assert!(scheduler.pending.contains_key(&0));
807
+ scheduler.step(0.06);
808
+ assert!(!scheduler.pending.contains_key(&0));
809
+ assert!(scheduler.value(&stable).is_some());
810
+ }
811
+
812
+ #[test]
813
+ fn delay_holds_presentation_until_the_delay_expires() {
814
+ let key = MotionChannelKey {
815
+ node_id: "panel".into(),
816
+ property: MotionProperty::Width,
817
+ };
818
+ let mut plan = timing();
819
+ if let MotionExecutionPlan::Timing { delay_ms, .. } = &mut plan {
820
+ *delay_ms = 100.0;
821
+ }
822
+ let mut scheduler = MotionScheduler::default();
823
+ scheduler.retarget(key.clone(), 160.0, 320.0, &plan);
824
+
825
+ scheduler.step(0.05);
826
+ assert!((scheduler.value(&key).unwrap() - 160.0).abs() < 1e-5);
827
+ scheduler.step(0.05);
828
+ assert!((scheduler.value(&key).unwrap() - 160.0).abs() < 1e-5);
829
+ scheduler.step(0.05);
830
+ assert!(scheduler.value(&key).unwrap() > 160.0);
831
+ }
832
+
833
+ #[test]
834
+ fn spring_retarget_blends_coefficients_without_losing_velocity() {
835
+ let key = MotionChannelKey {
836
+ node_id: "panel".into(),
837
+ property: MotionProperty::Width,
838
+ };
839
+ let mut scheduler = MotionScheduler::default();
840
+ let initial = spring();
841
+ scheduler.retarget(key.clone(), 160.0, 320.0, &initial);
842
+ scheduler.step(0.05);
843
+ let velocity_before = scheduler.velocity(&key).unwrap();
844
+
845
+ let mut retarget = spring();
846
+ if let MotionExecutionPlan::Spring {
847
+ omega,
848
+ damping_ratio,
849
+ blend_duration,
850
+ ..
851
+ } = &mut retarget
852
+ {
853
+ *omega *= 1.8;
854
+ *damping_ratio = 0.64;
855
+ *blend_duration = 0.2;
856
+ }
857
+ scheduler.retarget(
858
+ key.clone(),
859
+ scheduler.value(&key).unwrap(),
860
+ 180.0,
861
+ &retarget,
862
+ );
863
+ assert_eq!(scheduler.velocity(&key).unwrap(), velocity_before);
864
+
865
+ let index = scheduler.indices[&key];
866
+ let MotionChannel::Spring(channel) = &scheduler.channels[index] else {
867
+ panic!("expected spring channel");
868
+ };
869
+ let old_omega = channel.omega;
870
+ let target_omega = channel.blend_to_omega;
871
+ assert_ne!(old_omega, target_omega);
872
+
873
+ scheduler.step(0.1);
874
+ let MotionChannel::Spring(channel) = &scheduler.channels[index] else {
875
+ panic!("expected spring channel");
876
+ };
877
+ assert!(channel.omega > old_omega.min(target_omega));
878
+ assert!(channel.omega < old_omega.max(target_omega));
879
+ }
880
+
881
+ #[test]
882
+ fn delayed_live_retarget_keeps_previous_motion_running_until_handoff() {
883
+ let key = MotionChannelKey {
884
+ node_id: "panel".into(),
885
+ property: MotionProperty::Width,
886
+ };
887
+ let initial = MotionExecutionPlan::Timing {
888
+ duration: 1.0,
889
+ curve: [0.0, 0.0, 1.0, 1.0],
890
+ delay_ms: 0.0,
891
+ repeat_count: json!(1),
892
+ autoreverses: true,
893
+ };
894
+ let delayed = MotionExecutionPlan::Timing {
895
+ duration: 0.4,
896
+ curve: [0.0, 0.0, 1.0, 1.0],
897
+ delay_ms: 200.0,
898
+ repeat_count: json!(1),
899
+ autoreverses: true,
900
+ };
901
+ let mut scheduler = MotionScheduler::default();
902
+ scheduler.retarget(key.clone(), 0.0, 100.0, &initial);
903
+ scheduler.step(0.1);
904
+ let at_retarget = scheduler.value(&key).unwrap();
905
+ assert!((at_retarget - 10.0).abs() < 0.01);
906
+
907
+ scheduler.retarget(key.clone(), at_retarget, 25.0, &delayed);
908
+ scheduler.step(0.1);
909
+ let during_delay = scheduler.value(&key).unwrap();
910
+ assert!((during_delay - 20.0).abs() < 0.01);
911
+ scheduler.step(0.1);
912
+ let at_handoff = scheduler.value(&key).unwrap();
913
+ assert!((at_handoff - 30.0).abs() < 0.02);
914
+ scheduler.step(0.1);
915
+ let after_handoff = scheduler.value(&key).unwrap();
916
+ assert!(after_handoff < at_handoff && after_handoff > 25.0);
917
+ }
918
+
919
+ #[test]
920
+ fn timing_repeat_count_and_autoreverse_match_legacy_iteration_semantics() {
921
+ let key = MotionChannelKey {
922
+ node_id: "panel".into(),
923
+ property: MotionProperty::Width,
924
+ };
925
+ let plan = MotionExecutionPlan::Timing {
926
+ duration: 0.1,
927
+ curve: [0.0, 0.0, 1.0, 1.0],
928
+ delay_ms: 0.0,
929
+ repeat_count: json!(2),
930
+ autoreverses: true,
931
+ };
932
+ let mut scheduler = MotionScheduler::default();
933
+ scheduler.retarget(key.clone(), 0.0, 10.0, &plan);
934
+ scheduler.step(0.1);
935
+ assert!((scheduler.value(&key).unwrap() - 10.0).abs() < 1e-5);
936
+ assert!(scheduler.is_active());
937
+ scheduler.step(0.05);
938
+ assert!((scheduler.value(&key).unwrap() - 5.0).abs() < 0.01);
939
+ scheduler.step(0.05);
940
+ assert!(scheduler.value(&key).unwrap().abs() < 1e-5);
941
+ assert!(!scheduler.is_active());
942
+ }
943
+
944
+ #[test]
945
+ fn timing_repeat_without_autoreverse_restarts_from_origin() {
946
+ let key = MotionChannelKey {
947
+ node_id: "panel".into(),
948
+ property: MotionProperty::Width,
949
+ };
950
+ let plan = MotionExecutionPlan::Timing {
951
+ duration: 0.1,
952
+ curve: [0.0, 0.0, 1.0, 1.0],
953
+ delay_ms: 0.0,
954
+ repeat_count: json!(2),
955
+ autoreverses: false,
956
+ };
957
+ let mut scheduler = MotionScheduler::default();
958
+ scheduler.retarget(key.clone(), 0.0, 10.0, &plan);
959
+ scheduler.step(0.1);
960
+ assert!(scheduler.value(&key).unwrap().abs() < 1e-5);
961
+ scheduler.step(0.1);
962
+ assert!((scheduler.value(&key).unwrap() - 10.0).abs() < 1e-5);
963
+ assert!(!scheduler.is_active());
964
+ }
965
+
966
+ #[test]
967
+ fn spring_repeat_autoreverse_finishes_at_origin_on_even_iteration_count() {
968
+ let key = MotionChannelKey {
969
+ node_id: "panel".into(),
970
+ property: MotionProperty::Width,
971
+ };
972
+ let plan = MotionExecutionPlan::Spring {
973
+ omega: 30.0,
974
+ damping_ratio: 1.0,
975
+ blend_duration: 0.0,
976
+ delay_ms: 0.0,
977
+ repeat_count: json!(2),
978
+ autoreverses: true,
979
+ };
980
+ let mut scheduler = MotionScheduler::default();
981
+ scheduler.retarget(key.clone(), 0.0, 10.0, &plan);
982
+ for _ in 0..1000 {
983
+ scheduler.step(1.0 / 120.0);
984
+ if !scheduler.is_active() {
985
+ break;
986
+ }
987
+ }
988
+ assert!(scheduler.value(&key).unwrap().abs() < 0.01);
989
+ assert!(!scheduler.is_active());
990
+ }
991
+
992
+ #[test]
993
+ fn timing_repeat_forever_remains_active_without_growing_repeat_work() {
994
+ let key = MotionChannelKey {
995
+ node_id: "panel".into(),
996
+ property: MotionProperty::Width,
997
+ };
998
+ let plan = MotionExecutionPlan::Timing {
999
+ duration: 0.1,
1000
+ curve: [0.0, 0.0, 1.0, 1.0],
1001
+ delay_ms: 0.0,
1002
+ repeat_count: json!("infinite"),
1003
+ autoreverses: true,
1004
+ };
1005
+ let mut scheduler = MotionScheduler::default();
1006
+ scheduler.retarget(key.clone(), 0.0, 10.0, &plan);
1007
+
1008
+ // Cross 10,000 complete iterations in one scheduler step. The timeline
1009
+ // clock maps the elapsed time directly instead of looping per repeat.
1010
+ scheduler.step(1_000.05);
1011
+ let value = scheduler.value(&key).unwrap();
1012
+ assert!(value > 0.0 && value < 10.0);
1013
+ assert!(scheduler.is_active());
1014
+ }
1015
+
1016
+ #[test]
1017
+ fn kinetic_decay_is_frame_rate_independent_and_settles_at_projected_target() {
1018
+ let key = MotionChannelKey {
1019
+ node_id: "scroll".into(),
1020
+ property: MotionProperty::TranslationX,
1021
+ };
1022
+ let spec = KineticSpec::Decay(DecaySpec {
1023
+ velocity: Some(1000.0),
1024
+ time_constant: 0.3,
1025
+ rest_speed: 0.001,
1026
+ ..DecaySpec::default()
1027
+ });
1028
+ let mut sixty = MotionScheduler::default();
1029
+ let mut one_forty_four = MotionScheduler::default();
1030
+ sixty
1031
+ .animate_velocity(key.clone(), 0.0, &spec)
1032
+ .expect("valid decay");
1033
+ one_forty_four
1034
+ .animate_velocity(key.clone(), 0.0, &spec)
1035
+ .expect("valid decay");
1036
+
1037
+ for _ in 0..60 {
1038
+ sixty.step(1.0 / 60.0);
1039
+ }
1040
+ for _ in 0..144 {
1041
+ one_forty_four.step(1.0 / 144.0);
1042
+ }
1043
+ assert!((sixty.value(&key).unwrap() - one_forty_four.value(&key).unwrap()).abs() < 0.001);
1044
+ assert!(
1045
+ (sixty.velocity(&key).unwrap() - one_forty_four.velocity(&key).unwrap()).abs() < 0.01
1046
+ );
1047
+
1048
+ for _ in 0..2400 {
1049
+ if !sixty.step(1.0 / 120.0) {
1050
+ break;
1051
+ }
1052
+ }
1053
+ assert!((sixty.value(&key).unwrap() - 300.0).abs() < 1e-4);
1054
+ assert_eq!(sixty.velocity(&key).unwrap(), 0.0);
1055
+ assert!(!sixty.is_key_active(&key));
1056
+ }
1057
+
1058
+ #[test]
1059
+ fn kinetic_inertia_carries_release_velocity_into_bounded_bounce() {
1060
+ let key = MotionChannelKey {
1061
+ node_id: "sheet".into(),
1062
+ property: MotionProperty::TranslationY,
1063
+ };
1064
+ let spec = KineticSpec::Inertia(InertiaSpec {
1065
+ velocity: Some(1200.0),
1066
+ min: 0.0,
1067
+ max: 100.0,
1068
+ bounce_omega: std::f64::consts::TAU / 0.25,
1069
+ bounce_damping_ratio: 0.78,
1070
+ ..InertiaSpec::default()
1071
+ });
1072
+ let mut scheduler = MotionScheduler::default();
1073
+ scheduler
1074
+ .animate_velocity(key.clone(), 50.0, &spec)
1075
+ .expect("valid inertia");
1076
+
1077
+ scheduler.step(1.0 / 60.0);
1078
+ assert!(scheduler.value(&key).unwrap() > 50.0);
1079
+ assert!(scheduler.velocity(&key).unwrap() > 0.0);
1080
+
1081
+ for _ in 0..1200 {
1082
+ if !scheduler.step(1.0 / 120.0) {
1083
+ break;
1084
+ }
1085
+ }
1086
+ assert_eq!(scheduler.value(&key).unwrap(), 100.0);
1087
+ assert_eq!(scheduler.velocity(&key).unwrap(), 0.0);
1088
+ assert!(!scheduler.is_key_active(&key));
1089
+ }
1090
+
1091
+ #[test]
1092
+ fn kinetic_target_override_snaps_without_starting_a_second_animation() {
1093
+ let key = MotionChannelKey {
1094
+ node_id: "scrubber".into(),
1095
+ property: MotionProperty::TranslationX,
1096
+ };
1097
+ let spec = KineticSpec::Inertia(InertiaSpec {
1098
+ velocity: Some(800.0),
1099
+ min: 0.0,
1100
+ max: 500.0,
1101
+ rest_speed: 0.01,
1102
+ target_override: Some(200.0),
1103
+ ..InertiaSpec::default()
1104
+ });
1105
+ let mut scheduler = MotionScheduler::default();
1106
+ scheduler
1107
+ .animate_velocity(key.clone(), 40.0, &spec)
1108
+ .expect("valid inertia");
1109
+
1110
+ for _ in 0..1600 {
1111
+ if !scheduler.step(1.0 / 120.0) {
1112
+ break;
1113
+ }
1114
+ }
1115
+ assert_eq!(scheduler.value(&key).unwrap(), 200.0);
1116
+ assert!(!scheduler.is_key_active(&key));
1117
+ }
1118
+
1119
+ #[test]
1120
+ fn kinetic_handoff_inherits_live_channel_velocity_when_unspecified() {
1121
+ let key = MotionChannelKey {
1122
+ node_id: "drag".into(),
1123
+ property: MotionProperty::TranslationX,
1124
+ };
1125
+ let mut scheduler = MotionScheduler::default();
1126
+ scheduler.retarget(key.clone(), 0.0, 200.0, &spring());
1127
+ scheduler.step(0.05);
1128
+ let presentation = scheduler.value(&key).unwrap();
1129
+ let release_velocity = scheduler.velocity(&key).unwrap();
1130
+ assert!(release_velocity.abs() > 1.0);
1131
+
1132
+ let spec = KineticSpec::Decay(DecaySpec {
1133
+ velocity: None,
1134
+ time_constant: 0.3,
1135
+ ..DecaySpec::default()
1136
+ });
1137
+ scheduler
1138
+ .animate_velocity(key.clone(), presentation, &spec)
1139
+ .expect("valid decay");
1140
+ assert!((scheduler.value(&key).unwrap() - presentation).abs() < 1e-5);
1141
+ assert!((scheduler.velocity(&key).unwrap() - release_velocity).abs() < 1e-4);
1142
+ }
1143
+
1144
+ #[test]
1145
+ fn timing_uses_existing_bezier_route_and_retargets_from_presentation_value() {
1146
+ let key = MotionChannelKey {
1147
+ node_id: "panel".into(),
1148
+ property: MotionProperty::Width,
1149
+ };
1150
+ let mut scheduler = MotionScheduler::default();
1151
+ scheduler.retarget(key.clone(), 160.0, 320.0, &timing());
1152
+ scheduler.step(0.1);
1153
+ let presentation = scheduler.value(&key).unwrap();
1154
+ assert!(presentation > 160.0 && presentation < 320.0);
1155
+
1156
+ scheduler.retarget(key.clone(), presentation, 200.0, &timing());
1157
+ assert!((scheduler.value(&key).unwrap() - presentation).abs() < 1e-5);
1158
+ scheduler.step(0.4);
1159
+ assert!((scheduler.value(&key).unwrap() - 200.0).abs() < 1e-5);
1160
+ assert!(!scheduler.is_active());
1161
+ }
1162
+ }