@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,500 @@
1
+ //! Renderer-neutral kinetic continuation recovered from the existing Mün runtime.
2
+ //!
3
+ //! The legacy JavaScript engine uses exact exponential decay and an exact
4
+ //! damped-spring solution for low-count interaction motion. Keeping those
5
+ //! algorithms here makes drag/timeline/scroll release motion independent from
6
+ //! frame cadence and from any renderer or window-system backend.
7
+
8
+ use std::{error::Error, fmt};
9
+
10
+ const DEFAULT_TIME_CONSTANT: f64 = 0.325;
11
+ const DEFAULT_REST_SPEED: f64 = 5.0;
12
+ const DEFAULT_REST_DELTA: f64 = 0.5;
13
+ const DEFAULT_BOUNCE_RESPONSE: f64 = 0.28;
14
+ const DEFAULT_BOUNCE_DAMPING_RATIO: f64 = 0.82;
15
+ const MIN_TIME_CONSTANT: f64 = 0.016;
16
+ const CRITICAL_EPSILON: f64 = 1e-4;
17
+
18
+ #[derive(Clone, Copy, Debug, PartialEq)]
19
+ pub struct DecaySpec {
20
+ pub velocity: Option<f64>,
21
+ pub time_constant: f64,
22
+ pub power: f64,
23
+ pub rest_speed: f64,
24
+ /// The already-resolved result of the inherited `modifyTarget` hook.
25
+ ///
26
+ /// Source-level callback ownership does not live in the numeric executor.
27
+ /// Gesture/timeline semantics can resolve snapping once, then pass the
28
+ /// resulting scalar here without starting a second animation.
29
+ pub target_override: Option<f64>,
30
+ }
31
+
32
+ impl Default for DecaySpec {
33
+ fn default() -> Self {
34
+ Self {
35
+ velocity: None,
36
+ time_constant: DEFAULT_TIME_CONSTANT,
37
+ power: 1.0,
38
+ rest_speed: DEFAULT_REST_SPEED,
39
+ target_override: None,
40
+ }
41
+ }
42
+ }
43
+
44
+ #[derive(Clone, Copy, Debug, PartialEq)]
45
+ pub struct InertiaSpec {
46
+ pub velocity: Option<f64>,
47
+ pub time_constant: f64,
48
+ pub power: f64,
49
+ pub rest_speed: f64,
50
+ pub rest_delta: f64,
51
+ pub min: f64,
52
+ pub max: f64,
53
+ pub bounce_omega: f64,
54
+ pub bounce_damping_ratio: f64,
55
+ /// The already-resolved result of the inherited `modifyTarget` hook.
56
+ pub target_override: Option<f64>,
57
+ }
58
+
59
+ impl Default for InertiaSpec {
60
+ fn default() -> Self {
61
+ Self {
62
+ velocity: None,
63
+ time_constant: DEFAULT_TIME_CONSTANT,
64
+ power: 0.8,
65
+ rest_speed: DEFAULT_REST_SPEED,
66
+ rest_delta: DEFAULT_REST_DELTA,
67
+ min: f64::NEG_INFINITY,
68
+ max: f64::INFINITY,
69
+ bounce_omega: std::f64::consts::TAU / DEFAULT_BOUNCE_RESPONSE,
70
+ bounce_damping_ratio: DEFAULT_BOUNCE_DAMPING_RATIO,
71
+ target_override: None,
72
+ }
73
+ }
74
+ }
75
+
76
+ #[derive(Clone, Copy, Debug, PartialEq)]
77
+ pub enum KineticSpec {
78
+ Decay(DecaySpec),
79
+ Inertia(InertiaSpec),
80
+ }
81
+
82
+ impl From<DecaySpec> for KineticSpec {
83
+ fn from(value: DecaySpec) -> Self {
84
+ Self::Decay(value)
85
+ }
86
+ }
87
+
88
+ impl From<InertiaSpec> for KineticSpec {
89
+ fn from(value: InertiaSpec) -> Self {
90
+ Self::Inertia(value)
91
+ }
92
+ }
93
+
94
+ #[derive(Clone, Copy, Debug, Eq, PartialEq)]
95
+ pub enum KineticSpecError {
96
+ InvalidBounds,
97
+ }
98
+
99
+ impl fmt::Display for KineticSpecError {
100
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
101
+ match self {
102
+ Self::InvalidBounds => write!(f, "inertia min cannot be greater than max"),
103
+ }
104
+ }
105
+ }
106
+
107
+ impl Error for KineticSpecError {}
108
+
109
+ #[derive(Clone, Copy, Debug, PartialEq)]
110
+ pub struct KineticSample {
111
+ pub position: f64,
112
+ pub velocity: f64,
113
+ }
114
+
115
+ #[derive(Clone, Copy, Debug, Eq, PartialEq)]
116
+ enum KineticMode {
117
+ Decay,
118
+ Spring,
119
+ }
120
+
121
+ #[derive(Clone, Debug)]
122
+ pub(crate) struct KineticChannel {
123
+ kind: KineticKind,
124
+ mode: KineticMode,
125
+ position: f64,
126
+ velocity: f64,
127
+ final_target: f64,
128
+ time_constant: f64,
129
+ rest_speed: f64,
130
+ rest_delta: f64,
131
+ min: f64,
132
+ max: f64,
133
+ bounce_omega: f64,
134
+ bounce_damping_ratio: f64,
135
+ settled: bool,
136
+ }
137
+
138
+ #[derive(Clone, Copy, Debug, Eq, PartialEq)]
139
+ enum KineticKind {
140
+ Decay,
141
+ Inertia,
142
+ }
143
+
144
+ impl KineticSpec {
145
+ fn normalized(self) -> Result<Self, KineticSpecError> {
146
+ match self {
147
+ Self::Decay(spec) => Ok(Self::Decay(DecaySpec {
148
+ velocity: finite_option(spec.velocity),
149
+ time_constant: finite_or(spec.time_constant, DEFAULT_TIME_CONSTANT)
150
+ .max(MIN_TIME_CONSTANT),
151
+ power: finite_or(spec.power, 1.0).max(0.0),
152
+ rest_speed: finite_or(spec.rest_speed, DEFAULT_REST_SPEED).max(0.0),
153
+ target_override: finite_option(spec.target_override),
154
+ })),
155
+ Self::Inertia(spec) => {
156
+ let min = if spec.min.is_nan() {
157
+ f64::NEG_INFINITY
158
+ } else {
159
+ spec.min
160
+ };
161
+ let max = if spec.max.is_nan() {
162
+ f64::INFINITY
163
+ } else {
164
+ spec.max
165
+ };
166
+ if min > max {
167
+ return Err(KineticSpecError::InvalidBounds);
168
+ }
169
+ Ok(Self::Inertia(InertiaSpec {
170
+ velocity: finite_option(spec.velocity),
171
+ time_constant: finite_or(spec.time_constant, DEFAULT_TIME_CONSTANT)
172
+ .max(MIN_TIME_CONSTANT),
173
+ power: finite_or(spec.power, 0.8).max(0.0),
174
+ rest_speed: finite_or(spec.rest_speed, DEFAULT_REST_SPEED).max(0.0),
175
+ rest_delta: finite_or(spec.rest_delta, DEFAULT_REST_DELTA).max(0.0),
176
+ min,
177
+ max,
178
+ bounce_omega: finite_or(
179
+ spec.bounce_omega,
180
+ std::f64::consts::TAU / DEFAULT_BOUNCE_RESPONSE,
181
+ )
182
+ .max(0.0),
183
+ bounce_damping_ratio: finite_or(
184
+ spec.bounce_damping_ratio,
185
+ DEFAULT_BOUNCE_DAMPING_RATIO,
186
+ )
187
+ .max(0.0),
188
+ target_override: finite_option(spec.target_override),
189
+ }))
190
+ }
191
+ }
192
+ }
193
+
194
+ fn configured_velocity(self) -> Option<f64> {
195
+ match self {
196
+ Self::Decay(spec) => spec.velocity,
197
+ Self::Inertia(spec) => spec.velocity,
198
+ }
199
+ }
200
+
201
+ fn time_constant(self) -> f64 {
202
+ match self {
203
+ Self::Decay(spec) => spec.time_constant,
204
+ Self::Inertia(spec) => spec.time_constant,
205
+ }
206
+ }
207
+
208
+ fn power(self) -> f64 {
209
+ match self {
210
+ Self::Decay(spec) => spec.power,
211
+ Self::Inertia(spec) => spec.power,
212
+ }
213
+ }
214
+
215
+ fn target_override(self) -> Option<f64> {
216
+ match self {
217
+ Self::Decay(spec) => spec.target_override,
218
+ Self::Inertia(spec) => spec.target_override,
219
+ }
220
+ }
221
+ }
222
+
223
+ impl KineticChannel {
224
+ pub(crate) fn new(
225
+ current: f32,
226
+ inherited_velocity: f32,
227
+ requested_spec: &KineticSpec,
228
+ ) -> Result<Self, KineticSpecError> {
229
+ let spec = requested_spec.normalized()?;
230
+ let from = current as f64;
231
+ let source_velocity = spec
232
+ .configured_velocity()
233
+ .unwrap_or(inherited_velocity as f64);
234
+ let projected_target = project_decay_target(
235
+ from,
236
+ source_velocity,
237
+ spec.time_constant(),
238
+ spec.power(),
239
+ spec.target_override(),
240
+ );
241
+ let initial_velocity = if spec.time_constant() > 0.0 {
242
+ (projected_target - from) / spec.time_constant()
243
+ } else {
244
+ source_velocity * spec.power()
245
+ };
246
+
247
+ let channel = match spec {
248
+ KineticSpec::Decay(spec) => Self {
249
+ kind: KineticKind::Decay,
250
+ mode: KineticMode::Decay,
251
+ position: from,
252
+ velocity: initial_velocity,
253
+ final_target: projected_target,
254
+ time_constant: spec.time_constant,
255
+ rest_speed: spec.rest_speed,
256
+ rest_delta: DEFAULT_REST_DELTA,
257
+ min: f64::NEG_INFINITY,
258
+ max: f64::INFINITY,
259
+ bounce_omega: 0.0,
260
+ bounce_damping_ratio: 1.0,
261
+ settled: false,
262
+ },
263
+ KineticSpec::Inertia(spec) => {
264
+ let outside = nearest_bound(from, spec.min, spec.max);
265
+ let final_target = clamp_to_bounds(projected_target, spec.min, spec.max);
266
+ Self {
267
+ kind: KineticKind::Inertia,
268
+ mode: if outside.is_some() {
269
+ KineticMode::Spring
270
+ } else {
271
+ KineticMode::Decay
272
+ },
273
+ position: from,
274
+ velocity: initial_velocity,
275
+ final_target: outside.unwrap_or(final_target),
276
+ time_constant: spec.time_constant,
277
+ rest_speed: spec.rest_speed,
278
+ rest_delta: spec.rest_delta,
279
+ min: spec.min,
280
+ max: spec.max,
281
+ bounce_omega: spec.bounce_omega,
282
+ bounce_damping_ratio: spec.bounce_damping_ratio,
283
+ settled: false,
284
+ }
285
+ }
286
+ };
287
+
288
+ Ok(channel)
289
+ }
290
+
291
+ pub(crate) fn position(&self) -> f32 {
292
+ self.position as f32
293
+ }
294
+
295
+ pub(crate) fn velocity(&self) -> f32 {
296
+ self.velocity as f32
297
+ }
298
+
299
+ pub(crate) fn settled(&self) -> bool {
300
+ self.settled
301
+ }
302
+
303
+ pub(crate) fn snap(&mut self, target: f32) {
304
+ self.position = target as f64;
305
+ self.velocity = 0.0;
306
+ self.final_target = target as f64;
307
+ self.settled = true;
308
+ }
309
+
310
+ pub(crate) fn step(&mut self, dt_seconds: f32) {
311
+ if self.settled || !dt_seconds.is_finite() || dt_seconds <= 0.0 {
312
+ return;
313
+ }
314
+
315
+ let dt = dt_seconds as f64;
316
+ if self.mode == KineticMode::Decay {
317
+ let next = step_decay(self.position, self.velocity, dt, self.time_constant);
318
+ self.position = next.position;
319
+ self.velocity = next.velocity;
320
+
321
+ if self.kind == KineticKind::Inertia {
322
+ if let Some(crossed) = nearest_bound(self.position, self.min, self.max) {
323
+ self.mode = KineticMode::Spring;
324
+ self.final_target = crossed;
325
+ }
326
+ }
327
+
328
+ if self.mode == KineticMode::Decay && self.velocity.abs() <= self.rest_speed {
329
+ self.position = self.final_target;
330
+ self.velocity = 0.0;
331
+ self.settled = true;
332
+ return;
333
+ }
334
+ }
335
+
336
+ // The inherited engine enters bounce mode in the same frame a decay
337
+ // crosses a bound, carrying the post-decay position and velocity into
338
+ // the exact damped-spring solution.
339
+ if self.mode == KineticMode::Spring {
340
+ let next = step_damped_spring(
341
+ self.position,
342
+ self.velocity,
343
+ self.final_target,
344
+ self.bounce_omega,
345
+ self.bounce_damping_ratio,
346
+ dt,
347
+ );
348
+ self.position = next.position;
349
+ self.velocity = next.velocity;
350
+
351
+ if (self.final_target - self.position).abs() <= self.rest_delta
352
+ && self.velocity.abs() <= self.rest_speed
353
+ {
354
+ self.position = self.final_target;
355
+ self.velocity = 0.0;
356
+ self.settled = true;
357
+ }
358
+ }
359
+ }
360
+ }
361
+
362
+ fn finite_or(value: f64, fallback: f64) -> f64 {
363
+ if value.is_finite() { value } else { fallback }
364
+ }
365
+
366
+ fn finite_option(value: Option<f64>) -> Option<f64> {
367
+ value.filter(|value| value.is_finite())
368
+ }
369
+
370
+ /// Projects the inherited decay destination once. `target_override` is the
371
+ /// already-resolved result of the source runtime's optional `modifyTarget`
372
+ /// hook; non-finite overrides fall back to the ordinary projection.
373
+ pub fn project_decay_target(
374
+ value: f64,
375
+ velocity: f64,
376
+ time_constant: f64,
377
+ power: f64,
378
+ target_override: Option<f64>,
379
+ ) -> f64 {
380
+ let base = value + velocity * time_constant * power;
381
+ target_override
382
+ .filter(|target| target.is_finite())
383
+ .unwrap_or(base)
384
+ }
385
+
386
+ pub fn nearest_bound(value: f64, min: f64, max: f64) -> Option<f64> {
387
+ if value < min {
388
+ Some(min)
389
+ } else if value > max {
390
+ Some(max)
391
+ } else {
392
+ None
393
+ }
394
+ }
395
+
396
+ pub fn clamp_to_bounds(value: f64, min: f64, max: f64) -> f64 {
397
+ if !min.is_finite() && !max.is_finite() {
398
+ value
399
+ } else {
400
+ value.clamp(min, max)
401
+ }
402
+ }
403
+
404
+ /// Exact exponential integration for `dv/dt = -v/tau`.
405
+ pub fn step_decay(
406
+ position: f64,
407
+ velocity: f64,
408
+ dt_seconds: f64,
409
+ time_constant: f64,
410
+ ) -> KineticSample {
411
+ if !(dt_seconds > 0.0) || !(time_constant > 0.0) {
412
+ return KineticSample { position, velocity };
413
+ }
414
+ let attenuation = (-dt_seconds / time_constant).exp();
415
+ KineticSample {
416
+ position: position + velocity * time_constant * (1.0 - attenuation),
417
+ velocity: velocity * attenuation,
418
+ }
419
+ }
420
+
421
+ /// Exact solution of `y'' + 2*zeta*omega*y' + omega^2*y = 0` around target.
422
+ pub fn step_damped_spring(
423
+ position: f64,
424
+ velocity: f64,
425
+ target: f64,
426
+ omega: f64,
427
+ damping_ratio: f64,
428
+ dt_seconds: f64,
429
+ ) -> KineticSample {
430
+ if !(dt_seconds > 0.0) || !(omega > 0.0) {
431
+ return KineticSample { position, velocity };
432
+ }
433
+
434
+ let y0 = position - target;
435
+ let v0 = velocity;
436
+ let zeta = damping_ratio.max(0.0);
437
+
438
+ if zeta < 1.0 - CRITICAL_EPSILON {
439
+ let alpha = zeta * omega;
440
+ let wd = omega * (1.0 - zeta * zeta).sqrt();
441
+ let exp = (-alpha * dt_seconds).exp();
442
+ let sin = (wd * dt_seconds).sin();
443
+ let cos = (wd * dt_seconds).cos();
444
+ let b = (v0 + alpha * y0) / wd;
445
+ let y = exp * (y0 * cos + b * sin);
446
+ let velocity = exp * (-alpha * (y0 * cos + b * sin) + (-y0 * wd * sin + b * wd * cos));
447
+ return KineticSample {
448
+ position: target + y,
449
+ velocity,
450
+ };
451
+ }
452
+
453
+ if zeta > 1.0 + CRITICAL_EPSILON {
454
+ let root = (zeta * zeta - 1.0).sqrt();
455
+ let r1 = -omega * (zeta - root);
456
+ let r2 = -omega * (zeta + root);
457
+ let denominator = r1 - r2;
458
+ let c1 = (v0 - r2 * y0) / denominator;
459
+ let c2 = y0 - c1;
460
+ let e1 = (r1 * dt_seconds).exp();
461
+ let e2 = (r2 * dt_seconds).exp();
462
+ let y = c1 * e1 + c2 * e2;
463
+ let velocity = c1 * r1 * e1 + c2 * r2 * e2;
464
+ return KineticSample {
465
+ position: target + y,
466
+ velocity,
467
+ };
468
+ }
469
+
470
+ let exp = (-omega * dt_seconds).exp();
471
+ let b = v0 + omega * y0;
472
+ let y = exp * (y0 + b * dt_seconds);
473
+ let velocity = exp * (b - omega * (y0 + b * dt_seconds));
474
+ KineticSample {
475
+ position: target + y,
476
+ velocity,
477
+ }
478
+ }
479
+
480
+ #[cfg(test)]
481
+ mod tests {
482
+ use super::*;
483
+
484
+ #[test]
485
+ fn exact_damped_spring_is_stable_through_a_long_frame() {
486
+ let one = step_damped_spring(180.0, 2500.0, 100.0, 25.0, 0.78, 0.2);
487
+ let mut many = KineticSample {
488
+ position: 180.0,
489
+ velocity: 2500.0,
490
+ };
491
+ for _ in 0..20 {
492
+ many = step_damped_spring(many.position, many.velocity, 100.0, 25.0, 0.78, 0.01);
493
+ }
494
+
495
+ assert!(one.position.is_finite());
496
+ assert!(one.velocity.is_finite());
497
+ assert!((one.position - many.position).abs() < 1e-9);
498
+ assert!((one.velocity - many.velocity).abs() < 1e-8);
499
+ }
500
+ }
@@ -0,0 +1,173 @@
1
+ //! Renderer-neutral intrinsic measurement boundary for native layout.
2
+ //!
3
+ //! Taffy remains an adapter below Mün semantics. Exact glyph/control metrics may
4
+ //! come from a native backend, but those backends expose only semantic intrinsic
5
+ //! sizes here rather than leaking shaping or renderer objects into the runtime.
6
+
7
+ #[derive(Clone, Copy, Debug, Default, PartialEq)]
8
+ pub struct IntrinsicSize {
9
+ pub width: f32,
10
+ pub height: f32,
11
+ }
12
+
13
+ impl IntrinsicSize {
14
+ pub const fn new(width: f32, height: f32) -> Self {
15
+ Self { width, height }
16
+ }
17
+ }
18
+
19
+ /// Shaped geometry of one single-line text run, exposed by a text backend.
20
+ ///
21
+ /// `carets` holds one `(scalar offset, x)` stop for every extended grapheme
22
+ /// boundary of the shaped text, sorted by offset, with `x` in unscaled logical
23
+ /// points from the run's origin. The runtime derives caret, selection, preedit
24
+ /// and pointer-to-offset mapping from these stops; backends never own editing state.
25
+ #[derive(Clone, Debug, Default, PartialEq)]
26
+ pub struct TextLineLayout {
27
+ pub carets: Vec<(usize, f32)>,
28
+ pub width: f32,
29
+ pub line_height: f32,
30
+ }
31
+
32
+ impl TextLineLayout {
33
+ /// Caret x for a scalar offset, using the nearest stop at or before it.
34
+ pub fn x_for_offset(&self, offset: usize) -> f32 {
35
+ self.carets
36
+ .iter()
37
+ .rev()
38
+ .find(|(stop, _)| *stop <= offset)
39
+ .or(self.carets.first())
40
+ .map_or(0.0, |(_, x)| *x)
41
+ }
42
+
43
+ /// The grapheme boundary whose caret position is visually nearest to `x`.
44
+ pub fn offset_for_x(&self, x: f32) -> usize {
45
+ self.carets
46
+ .iter()
47
+ .min_by(|(_, a), (_, b)| (a - x).abs().total_cmp(&(b - x).abs()))
48
+ .map_or(0, |(offset, _)| *offset)
49
+ }
50
+
51
+ /// Visual horizontal extent covering every boundary inside `range`.
52
+ pub fn span(&self, range: std::ops::Range<usize>) -> Option<(f32, f32)> {
53
+ if range.is_empty() {
54
+ return None;
55
+ }
56
+ let xs = self
57
+ .carets
58
+ .iter()
59
+ .filter(|(offset, _)| range.contains(offset) || *offset == range.end)
60
+ .map(|(_, x)| *x);
61
+ let (min, max) = xs.fold((f32::INFINITY, f32::NEG_INFINITY), |(lo, hi), x| {
62
+ (lo.min(x), hi.max(x))
63
+ });
64
+ (min.is_finite() && max > min).then_some((min, max))
65
+ }
66
+ }
67
+
68
+ /// Supplies backend-derived intrinsic sizes for semantic leaf content.
69
+ ///
70
+ /// Measurements are intentionally unconstrained content sizes. Explicit Mün
71
+ /// width/height constraints and container alignment remain authoritative in
72
+ /// the Taffy adapter.
73
+ pub trait IntrinsicMeasurer {
74
+ fn measure_text(&self, text: &str) -> IntrinsicSize;
75
+ fn measure_action(&self, label: &str) -> IntrinsicSize;
76
+ fn measure_panel(&self) -> IntrinsicSize;
77
+
78
+ fn measure_text_field(&self, value: &str, placeholder: Option<&str>) -> IntrinsicSize {
79
+ let text = if value.is_empty() {
80
+ placeholder.unwrap_or("")
81
+ } else {
82
+ value
83
+ };
84
+ let measured = self.measure_action(text);
85
+ IntrinsicSize::new(measured.width.max(180.0), measured.height)
86
+ }
87
+
88
+ /// Single-line shaped geometry for editing presentation and pointer mapping.
89
+ ///
90
+ /// The default is a deterministic headless model (uniform advance per
91
+ /// grapheme cluster) for tests and IR tooling without a font backend. Native
92
+ /// renderers must override it with real shaping data.
93
+ fn text_line(&self, text: &str, font_size: f32) -> TextLineLayout {
94
+ let advance = font_size * 0.6;
95
+ let carets = crate::text_edit::grapheme_boundaries(text)
96
+ .into_iter()
97
+ .enumerate()
98
+ .map(|(index, offset)| (offset, index as f32 * advance))
99
+ .collect::<Vec<_>>();
100
+ TextLineLayout {
101
+ width: carets.last().map_or(0.0, |(_, x)| *x),
102
+ carets,
103
+ line_height: font_size * 1.25,
104
+ }
105
+ }
106
+
107
+ /// Checkbox-style toggle: a 14pt box, a 6pt gap and the label line.
108
+ fn measure_toggle(&self, label: &str) -> IntrinsicSize {
109
+ let line = self.text_line(label, 16.0);
110
+ IntrinsicSize::new(
111
+ (line.width + 20.0).ceil(),
112
+ line.line_height.max(22.0).ceil(),
113
+ )
114
+ }
115
+
116
+ /// Linear progress: an optional label line above a 6pt track.
117
+ fn measure_progress(&self, label: Option<&str>) -> IntrinsicSize {
118
+ let label = label.map_or(0.0, |label| self.text_line(label, 16.0).line_height + 4.0);
119
+ IntrinsicSize::new(160.0, (label + 6.0).ceil())
120
+ }
121
+
122
+ fn measure_radio_group(&self, labels: &[String]) -> IntrinsicSize {
123
+ let width = labels
124
+ .iter()
125
+ .map(|label| self.measure_action(label).width)
126
+ .fold(120.0, f32::max);
127
+ IntrinsicSize::new(width, (labels.len().max(1) as f32) * 30.0)
128
+ }
129
+ }
130
+
131
+ /// Compatibility fallback used when no native backend measurer is supplied.
132
+ ///
133
+ /// These preserve the pre-boundary runtime metrics exactly. Native backends may
134
+ /// replace them with glyph/control measurements through the runtime measurement API.
135
+ #[derive(Clone, Copy, Debug, Default)]
136
+ pub struct FallbackIntrinsicMeasurer;
137
+
138
+ impl IntrinsicMeasurer for FallbackIntrinsicMeasurer {
139
+ fn measure_text(&self, _text: &str) -> IntrinsicSize {
140
+ IntrinsicSize::new(240.0, 32.0)
141
+ }
142
+
143
+ fn measure_action(&self, label: &str) -> IntrinsicSize {
144
+ IntrinsicSize::new((label.chars().count() as f32 * 9.0 + 34.0).max(92.0), 38.0)
145
+ }
146
+
147
+ fn measure_panel(&self) -> IntrinsicSize {
148
+ IntrinsicSize::new(160.0, 96.0)
149
+ }
150
+ }
151
+
152
+ #[cfg(test)]
153
+ mod tests {
154
+ use super::*;
155
+
156
+ #[test]
157
+ fn fallback_metrics_preserve_existing_runtime_baselines() {
158
+ let measurer = FallbackIntrinsicMeasurer;
159
+ assert_eq!(
160
+ measurer.measure_text("arbitrary"),
161
+ IntrinsicSize::new(240.0, 32.0)
162
+ );
163
+ assert_eq!(
164
+ measurer.measure_action("Go"),
165
+ IntrinsicSize::new(92.0, 38.0)
166
+ );
167
+ assert_eq!(
168
+ measurer.measure_action("Long action label"),
169
+ IntrinsicSize::new(187.0, 38.0)
170
+ );
171
+ assert_eq!(measurer.measure_panel(), IntrinsicSize::new(160.0, 96.0));
172
+ }
173
+ }