@c9up/orbit 0.0.0-stage → 0.1.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 (95) hide show
  1. package/Cargo.lock +146 -0
  2. package/Cargo.toml +16 -0
  3. package/LICENSE +21 -0
  4. package/README.md +314 -2
  5. package/android/orbit.cmake +31 -0
  6. package/android/src/main/cpp/NativeOrbit.cpp +309 -0
  7. package/android/src/main/cpp/NativeOrbit.h +35 -0
  8. package/android/src/main/cpp/orbit_host.h +34 -0
  9. package/android/src/main/java/com/c9up/orbit/OrbitEffects.kt +286 -0
  10. package/android/src/main/java/com/c9up/orbit/OrbitHost.kt +258 -0
  11. package/android/src/main/java/com/c9up/orbit/OrbitImage.kt +346 -0
  12. package/android/src/main/java/com/c9up/orbit/OrbitInput.kt +145 -0
  13. package/android/src/main/java/com/c9up/orbit/OrbitPress.kt +110 -0
  14. package/android/src/main/java/com/c9up/orbit/OrbitVector.kt +115 -0
  15. package/android/src/main/java/com/c9up/orbit/OrbitViews.kt +362 -0
  16. package/crates/orbit-host/Cargo.toml +14 -0
  17. package/crates/orbit-host/src/gesture.rs +561 -0
  18. package/crates/orbit-host/src/lib.rs +1608 -0
  19. package/crates/orbit-host/src/motion.rs +328 -0
  20. package/crates/orbit-host/src/vector.rs +917 -0
  21. package/dist/color.d.ts +10 -0
  22. package/dist/color.d.ts.map +1 -0
  23. package/dist/color.js +62 -0
  24. package/dist/color.js.map +1 -0
  25. package/dist/commit.d.ts +45 -0
  26. package/dist/commit.d.ts.map +1 -0
  27. package/dist/commit.js +172 -0
  28. package/dist/commit.js.map +1 -0
  29. package/dist/errors.d.ts +42 -0
  30. package/dist/errors.d.ts.map +1 -0
  31. package/dist/errors.js +15 -0
  32. package/dist/errors.js.map +1 -0
  33. package/dist/host.d.ts +49 -0
  34. package/dist/host.d.ts.map +1 -0
  35. package/dist/host.js +18 -0
  36. package/dist/host.js.map +1 -0
  37. package/dist/index.d.ts +29 -0
  38. package/dist/index.d.ts.map +1 -0
  39. package/dist/index.js +29 -0
  40. package/dist/index.js.map +1 -0
  41. package/dist/motion.d.ts +18 -0
  42. package/dist/motion.d.ts.map +1 -0
  43. package/dist/motion.js +66 -0
  44. package/dist/motion.js.map +1 -0
  45. package/dist/node.d.ts +46 -0
  46. package/dist/node.d.ts.map +1 -0
  47. package/dist/node.js +110 -0
  48. package/dist/node.js.map +1 -0
  49. package/dist/pages.d.ts +56 -0
  50. package/dist/pages.d.ts.map +1 -0
  51. package/dist/pages.js +157 -0
  52. package/dist/pages.js.map +1 -0
  53. package/dist/react-native/index.d.ts +25 -0
  54. package/dist/react-native/index.d.ts.map +1 -0
  55. package/dist/react-native/index.js +43 -0
  56. package/dist/react-native/index.js.map +1 -0
  57. package/dist/server.d.ts +85 -0
  58. package/dist/server.d.ts.map +1 -0
  59. package/dist/server.js +282 -0
  60. package/dist/server.js.map +1 -0
  61. package/dist/stack.d.ts +117 -0
  62. package/dist/stack.d.ts.map +1 -0
  63. package/dist/stack.js +299 -0
  64. package/dist/stack.js.map +1 -0
  65. package/dist/styles.d.ts +138 -0
  66. package/dist/styles.d.ts.map +1 -0
  67. package/dist/styles.js +920 -0
  68. package/dist/styles.js.map +1 -0
  69. package/dist/template.d.ts +27 -0
  70. package/dist/template.d.ts.map +1 -0
  71. package/dist/template.js +1392 -0
  72. package/dist/template.js.map +1 -0
  73. package/dist/url.d.ts +45 -0
  74. package/dist/url.d.ts.map +1 -0
  75. package/dist/url.js +302 -0
  76. package/dist/url.js.map +1 -0
  77. package/package.json +90 -3
  78. package/scripts/build-android.sh +27 -0
  79. package/scripts/test-styles.mjs +31 -0
  80. package/specs/NativeOrbit.ts +34 -0
  81. package/src/color.ts +73 -0
  82. package/src/commit.ts +206 -0
  83. package/src/errors.ts +48 -0
  84. package/src/host.ts +92 -0
  85. package/src/index.ts +60 -0
  86. package/src/motion.ts +86 -0
  87. package/src/node.ts +133 -0
  88. package/src/pages.ts +210 -0
  89. package/src/react-native/index.ts +76 -0
  90. package/src/react-native/react-native.d.ts +31 -0
  91. package/src/server.ts +359 -0
  92. package/src/stack.ts +434 -0
  93. package/src/styles.ts +1024 -0
  94. package/src/template.ts +1657 -0
  95. package/src/url.ts +326 -0
@@ -0,0 +1,328 @@
1
+ //! UI-thread animation for the Rust host.
2
+ //!
3
+ //! JS declares values, interpolations, animations and bindings once; the
4
+ //! platform calls `tick` on every vsync from the UI thread, with the current
5
+ //! scroll offsets, and applies what comes back. Nothing here waits for JS, so a
6
+ //! blocked JS thread does not stop an animation or a scroll-driven value.
7
+
8
+ use std::collections::HashMap;
9
+ use std::sync::Mutex;
10
+
11
+ pub const PROP_OPACITY: u8 = 0;
12
+ pub const PROP_TRANSLATE_X: u8 = 1;
13
+ pub const PROP_TRANSLATE_Y: u8 = 2;
14
+ pub const PROP_SCALE: u8 = 3;
15
+ /// Shown above 0.5, hidden (laid out, untouchable, not drawn) at or below:
16
+ /// what hides a covered screen the frame its cover finishes arriving.
17
+ pub const PROP_VISIBLE: u8 = 4;
18
+
19
+ enum Node {
20
+ Value(f64),
21
+ Interpolation {
22
+ input: i32,
23
+ from: Vec<f64>,
24
+ to: Vec<f64>,
25
+ },
26
+ }
27
+
28
+ enum Anim {
29
+ Frames {
30
+ frames: Vec<f64>,
31
+ from: f64,
32
+ to: f64,
33
+ start_ns: Option<i64>,
34
+ looping: bool,
35
+ },
36
+ Spring {
37
+ to: f64,
38
+ stiffness: f64,
39
+ damping: f64,
40
+ mass: f64,
41
+ velocity: f64,
42
+ last_ns: Option<i64>,
43
+ },
44
+ }
45
+
46
+ #[derive(Default)]
47
+ pub struct Motion {
48
+ nodes: HashMap<i32, Node>,
49
+ anims: HashMap<i32, Anim>,
50
+ bindings: Vec<(i32, u8, i32)>,
51
+ scroll: HashMap<i32, i32>,
52
+ last: HashMap<(i32, u8), f32>,
53
+ }
54
+
55
+ pub static MOTION: Mutex<Option<Motion>> = Mutex::new(None);
56
+
57
+ pub fn with<R>(f: impl FnOnce(&mut Motion) -> R) -> R {
58
+ let mut guard = MOTION.lock().unwrap_or_else(|p| p.into_inner());
59
+ f(guard.get_or_insert_with(Motion::default))
60
+ }
61
+
62
+ const FRAME_NS: f64 = 1e9 / 60.0;
63
+
64
+ impl Motion {
65
+ pub fn value(&mut self, tag: i32, initial: f64) {
66
+ self.nodes.insert(tag, Node::Value(initial));
67
+ }
68
+
69
+ pub fn interpolation(&mut self, tag: i32, input: i32, from: Vec<f64>, to: Vec<f64>) {
70
+ self.nodes
71
+ .insert(tag, Node::Interpolation { input, from, to });
72
+ }
73
+
74
+ pub fn set(&mut self, tag: i32, v: f64) {
75
+ self.anims.remove(&tag);
76
+ self.nodes.insert(tag, Node::Value(v));
77
+ }
78
+
79
+ pub fn stop(&mut self, tag: i32) {
80
+ self.anims.remove(&tag);
81
+ }
82
+
83
+ pub fn frames(&mut self, tag: i32, frames: Vec<f64>, to: f64, looping: bool) {
84
+ let from = self.read(tag);
85
+ self.anims.insert(
86
+ tag,
87
+ Anim::Frames {
88
+ frames,
89
+ from,
90
+ to,
91
+ start_ns: None,
92
+ looping,
93
+ },
94
+ );
95
+ }
96
+
97
+ pub fn spring(&mut self, tag: i32, to: f64, stiffness: f64, damping: f64, mass: f64) {
98
+ self.anims.insert(
99
+ tag,
100
+ Anim::Spring {
101
+ to,
102
+ stiffness,
103
+ damping,
104
+ mass: mass.max(1e-3),
105
+ velocity: 0.0,
106
+ last_ns: None,
107
+ },
108
+ );
109
+ }
110
+
111
+ /// Forget a value or an interpolation, its animation and what it drives.
112
+ pub fn dispose(&mut self, tag: i32) {
113
+ self.nodes.remove(&tag);
114
+ self.anims.remove(&tag);
115
+ self.bindings.retain(|(_, _, node)| *node != tag);
116
+ self.scroll.retain(|_, value| *value != tag);
117
+ }
118
+
119
+ /// A view the host deleted: nothing drives it any more.
120
+ pub fn forget_view(&mut self, view: i32) {
121
+ self.bindings.retain(|(v, _, _)| *v != view);
122
+ self.last.retain(|(v, _), _| *v != view);
123
+ self.scroll.remove(&view);
124
+ }
125
+
126
+ pub fn bind(&mut self, view: i32, prop: u8, node: i32) {
127
+ self.bindings
128
+ .retain(|(v, p, _)| !(*v == view && *p == prop));
129
+ self.bindings.push((view, prop, node));
130
+ }
131
+
132
+ pub fn bind_scroll(&mut self, scroll_view: i32, value: i32) {
133
+ self.scroll.insert(scroll_view, value);
134
+ }
135
+
136
+ fn read(&self, tag: i32) -> f64 {
137
+ match self.nodes.get(&tag) {
138
+ Some(Node::Value(v)) => *v,
139
+ Some(Node::Interpolation { input, from, to }) => {
140
+ interpolate(self.read(*input), from, to)
141
+ }
142
+ None => 0.0,
143
+ }
144
+ }
145
+
146
+ /// Advance to `now_ns`; returns (view, prop, value) records for what changed.
147
+ pub fn tick(&mut self, now_ns: i64, scrolls: &[(i32, f32)], out: &mut Vec<u8>) -> bool {
148
+ for (view, y) in scrolls {
149
+ if let Some(value) = self.scroll.get(view) {
150
+ self.nodes.insert(*value, Node::Value(f64::from(*y)));
151
+ }
152
+ }
153
+ let mut finished = Vec::new();
154
+ for (tag, anim) in self.anims.iter_mut() {
155
+ let next = match anim {
156
+ Anim::Frames {
157
+ frames,
158
+ from,
159
+ to,
160
+ start_ns,
161
+ looping,
162
+ } => {
163
+ let start = *start_ns.get_or_insert(now_ns);
164
+ let mut index = ((now_ns - start) as f64 / FRAME_NS) as usize;
165
+ if index >= frames.len() {
166
+ if *looping && !frames.is_empty() {
167
+ index %= frames.len();
168
+ } else {
169
+ finished.push(*tag);
170
+ index = frames.len().saturating_sub(1);
171
+ }
172
+ }
173
+ let f = frames.get(index).copied().unwrap_or(1.0);
174
+ *from + f * (*to - *from)
175
+ }
176
+ Anim::Spring {
177
+ to,
178
+ stiffness,
179
+ damping,
180
+ mass,
181
+ velocity,
182
+ last_ns,
183
+ } => {
184
+ let x = match self.nodes.get(tag) {
185
+ Some(Node::Value(v)) => *v,
186
+ _ => 0.0,
187
+ };
188
+ let dt = last_ns
189
+ .map(|l| ((now_ns - l) as f64 / 1e9).clamp(0.0, 0.064))
190
+ .unwrap_or(0.0);
191
+ *last_ns = Some(now_ns);
192
+ // Semi-implicit Euler in 1 ms steps: stable for stiff springs.
193
+ let (mut x, mut v) = (x, *velocity);
194
+ let steps = (dt * 1000.0).ceil() as usize;
195
+ let h = if steps > 0 { dt / steps as f64 } else { 0.0 };
196
+ for _ in 0..steps {
197
+ let a = (-*stiffness * (x - *to) - *damping * v) / *mass;
198
+ v += a * h;
199
+ x += v * h;
200
+ }
201
+ *velocity = v;
202
+ if v.abs() < 1e-3 && (x - *to).abs() < 1e-3 {
203
+ finished.push(*tag);
204
+ x = *to;
205
+ }
206
+ x
207
+ }
208
+ };
209
+ self.nodes.insert(*tag, Node::Value(next));
210
+ }
211
+ for tag in finished {
212
+ self.anims.remove(&tag);
213
+ }
214
+ for &(view, prop, node) in &self.bindings {
215
+ let v = self.read(node) as f32;
216
+ if self.last.get(&(view, prop)) != Some(&v) {
217
+ self.last.insert((view, prop), v);
218
+ out.extend_from_slice(&view.to_le_bytes());
219
+ out.push(prop);
220
+ out.extend_from_slice(&v.to_le_bytes());
221
+ }
222
+ }
223
+ !self.anims.is_empty()
224
+ }
225
+ }
226
+
227
+ fn interpolate(x: f64, from: &[f64], to: &[f64]) -> f64 {
228
+ if from.len() < 2 || to.len() != from.len() {
229
+ return to.first().copied().unwrap_or(x);
230
+ }
231
+ if x <= from[0] {
232
+ return to[0];
233
+ }
234
+ for i in 1..from.len() {
235
+ if x <= from[i] {
236
+ let t = (x - from[i - 1]) / (from[i] - from[i - 1]);
237
+ return to[i - 1] + t * (to[i] - to[i - 1]);
238
+ }
239
+ }
240
+ to[to.len() - 1]
241
+ }
242
+
243
+ #[cfg(test)]
244
+ mod tests {
245
+ use super::*;
246
+
247
+ fn records(buf: &[u8]) -> Vec<(i32, u8, f32)> {
248
+ buf.chunks(9)
249
+ .map(|c| {
250
+ (
251
+ i32::from_le_bytes([c[0], c[1], c[2], c[3]]),
252
+ c[4],
253
+ f32::from_le_bytes([c[5], c[6], c[7], c[8]]),
254
+ )
255
+ })
256
+ .collect()
257
+ }
258
+
259
+ #[test]
260
+ fn frames_loop_and_scroll_drive_bindings_without_js() {
261
+ let mut m = Motion::default();
262
+ m.value(1, 0.0);
263
+ m.frames(1, vec![0.0, 0.5, 1.0], 300.0, true);
264
+ m.bind(10, PROP_TRANSLATE_X, 1);
265
+ m.value(2, 0.0);
266
+ m.interpolation(3, 2, vec![0.0, 200.0], vec![1.0, 0.2]);
267
+ m.bind(11, PROP_OPACITY, 3);
268
+ m.bind_scroll(20, 2);
269
+ let frame = FRAME_NS as i64;
270
+ let mut out = Vec::new();
271
+ m.tick(0, &[(20, 0.0)], &mut out);
272
+ out.clear();
273
+ m.tick(frame + 1, &[(20, 100.0)], &mut out);
274
+ let r = records(&out);
275
+ assert!(r.contains(&(10, PROP_TRANSLATE_X, 150.0)), "{r:?}");
276
+ assert!(
277
+ r.iter()
278
+ .any(|&(v, p, x)| v == 11 && p == PROP_OPACITY && (x - 0.6).abs() < 1e-4),
279
+ "{r:?}"
280
+ );
281
+ out.clear();
282
+ // Past the end, a looping animation starts over.
283
+ // 3.5 frames in: index 3 of a 3-frame loop is frame 0 again.
284
+ m.tick(3 * frame + frame / 2, &[(20, 100.0)], &mut out);
285
+ assert_eq!(records(&out), vec![(10, PROP_TRANSLATE_X, 0.0)]);
286
+ }
287
+
288
+ #[test]
289
+ fn a_disposed_value_and_a_deleted_view_leave_nothing_behind() {
290
+ let mut m = Motion::default();
291
+ m.value(1, 0.0);
292
+ m.frames(1, vec![0.0, 1.0], 100.0, true);
293
+ m.bind(10, PROP_TRANSLATE_X, 1);
294
+ m.value(2, 1.0);
295
+ m.bind(11, PROP_VISIBLE, 2);
296
+ m.bind_scroll(20, 2);
297
+ let mut out = Vec::new();
298
+ m.tick(0, &[], &mut out);
299
+ m.forget_view(11);
300
+ m.forget_view(20);
301
+ m.dispose(1);
302
+ assert!(m.bindings.is_empty() && m.scroll.is_empty() && m.anims.is_empty());
303
+ assert!(!m.last.contains_key(&(11, PROP_VISIBLE)));
304
+ assert!(!m.nodes.contains_key(&1));
305
+ out.clear();
306
+ assert!(!m.tick(FRAME_NS as i64, &[(20, 5.0)], &mut out));
307
+ assert!(out.is_empty());
308
+ }
309
+
310
+ #[test]
311
+ fn a_spring_settles_on_its_target() {
312
+ let mut m = Motion::default();
313
+ m.value(1, 0.8);
314
+ m.spring(1, 1.0, 260.0, 7.0, 1.0);
315
+ let mut t = 0;
316
+ let mut active = true;
317
+ for _ in 0..600 {
318
+ let mut out = Vec::new();
319
+ active = m.tick(t, &[], &mut out);
320
+ t += FRAME_NS as i64;
321
+ if !active {
322
+ break;
323
+ }
324
+ }
325
+ assert!(!active);
326
+ assert!((m.read(1) - 1.0).abs() < 1e-9);
327
+ }
328
+ }