@c9up/orbit 0.0.0-stage → 0.1.1

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 (105) hide show
  1. package/Cargo.lock +139 -0
  2. package/Cargo.toml +20 -0
  3. package/LICENSE +21 -0
  4. package/README.md +320 -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 +161 -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 +2042 -0
  19. package/crates/orbit-host/src/motion.rs +328 -0
  20. package/crates/orbit-host/src/vector.rs +1173 -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 +77 -0
  26. package/dist/commit.d.ts.map +1 -0
  27. package/dist/commit.js +302 -0
  28. package/dist/commit.js.map +1 -0
  29. package/dist/errors.d.ts +76 -0
  30. package/dist/errors.d.ts.map +1 -0
  31. package/dist/errors.js +89 -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 +20 -0
  36. package/dist/host.js.map +1 -0
  37. package/dist/index.d.ts +31 -0
  38. package/dist/index.d.ts.map +1 -0
  39. package/dist/index.js +31 -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 +67 -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 +33 -0
  54. package/dist/react-native/index.d.ts.map +1 -0
  55. package/dist/react-native/index.js +71 -0
  56. package/dist/react-native/index.js.map +1 -0
  57. package/dist/report.d.ts +13 -0
  58. package/dist/report.d.ts.map +1 -0
  59. package/dist/report.js +22 -0
  60. package/dist/report.js.map +1 -0
  61. package/dist/server.d.ts +109 -0
  62. package/dist/server.d.ts.map +1 -0
  63. package/dist/server.js +290 -0
  64. package/dist/server.js.map +1 -0
  65. package/dist/stack.d.ts +117 -0
  66. package/dist/stack.d.ts.map +1 -0
  67. package/dist/stack.js +299 -0
  68. package/dist/stack.js.map +1 -0
  69. package/dist/styles.d.ts +138 -0
  70. package/dist/styles.d.ts.map +1 -0
  71. package/dist/styles.js +920 -0
  72. package/dist/styles.js.map +1 -0
  73. package/dist/template.d.ts +27 -0
  74. package/dist/template.d.ts.map +1 -0
  75. package/dist/template.js +1436 -0
  76. package/dist/template.js.map +1 -0
  77. package/dist/textDecoder.d.ts +29 -0
  78. package/dist/textDecoder.d.ts.map +1 -0
  79. package/dist/textDecoder.js +149 -0
  80. package/dist/textDecoder.js.map +1 -0
  81. package/dist/url.d.ts +45 -0
  82. package/dist/url.d.ts.map +1 -0
  83. package/dist/url.js +303 -0
  84. package/dist/url.js.map +1 -0
  85. package/package.json +96 -3
  86. package/scripts/build-android.sh +27 -0
  87. package/scripts/test-styles.mjs +31 -0
  88. package/specs/NativeOrbit.ts +34 -0
  89. package/src/color.ts +68 -0
  90. package/src/commit.ts +373 -0
  91. package/src/errors.ts +178 -0
  92. package/src/host.ts +91 -0
  93. package/src/index.ts +64 -0
  94. package/src/motion.ts +88 -0
  95. package/src/node.ts +133 -0
  96. package/src/pages.ts +207 -0
  97. package/src/react-native/index.ts +115 -0
  98. package/src/react-native/react-native.d.ts +39 -0
  99. package/src/report.ts +25 -0
  100. package/src/server.ts +395 -0
  101. package/src/stack.ts +428 -0
  102. package/src/styles.ts +1021 -0
  103. package/src/template.ts +1681 -0
  104. package/src/textDecoder.ts +162 -0
  105. package/src/url.ts +333 -0
package/src/stack.ts ADDED
@@ -0,0 +1,428 @@
1
+ import {
2
+ component,
3
+ createContext,
4
+ inject,
5
+ provide,
6
+ type Signal,
7
+ signal,
8
+ untrack,
9
+ } from "@c9up/aurora/core";
10
+ import { captureContext, type TemplateResult } from "@c9up/aurora/renderer";
11
+ import { afterCommit, treeHost } from "./commit.js";
12
+ import { E_ORBIT_NO_SCREEN, E_ORBIT_STACK_SHOWN } from "./errors.js";
13
+ import { bindMotion, MotionValue } from "./motion.js";
14
+ import { type Child, flatten, NativeNode, Region } from "./node.js";
15
+
16
+ /**
17
+ * Navigation between screens: a stack of aurora components, the top one
18
+ * shown. A covered screen stays built — its views laid out under the top one,
19
+ * its state and its effects kept — and is hidden once covered; a popped one
20
+ * ends. Transitions are motion values, played by the host on the UI thread.
21
+ *
22
+ * const stack = createStack(Home);
23
+ * return html`${stack}`;
24
+ *
25
+ * // in any screen
26
+ * const screen = useScreen();
27
+ * screen.push(Detail, { id: 3 });
28
+ *
29
+ * Nothing here knows URLs: a router or server-driven pages push the screen a
30
+ * URL names on top of it.
31
+ */
32
+
33
+ /** A screen: an aurora component (or any function returning a template). */
34
+ export type ScreenComponent<P> = (props: P) => TemplateResult;
35
+
36
+ export interface Navigator {
37
+ /** Show `screen` on top; the one below keeps its state, covered. */
38
+ push(screen: () => TemplateResult): void;
39
+ push<P>(screen: ScreenComponent<P>, props: P): void;
40
+ /** End the top screen and show the one below. False when only one is left. */
41
+ pop(): boolean;
42
+ /** Swap the top screen for `screen`: the replaced one ends. */
43
+ replace(screen: () => TemplateResult): void;
44
+ replace<P>(screen: ScreenComponent<P>, props: P): void;
45
+ /** Whether `pop` has a screen to go back to (reactive). */
46
+ canGoBack(): boolean;
47
+ }
48
+
49
+ /** What a screen reads with `useScreen()`: its stack, and whether it is shown. */
50
+ export interface Screen extends Navigator {
51
+ /**
52
+ * True while this screen is the one shown: on top of its stack, in a stack
53
+ * that is itself shown (reactive). A covered screen's effects keep running;
54
+ * one that should pause reads this:
55
+ *
56
+ * effect(() => { if (screen.focused()) poll(); });
57
+ */
58
+ focused(): boolean;
59
+ }
60
+
61
+ export interface StackOptions {
62
+ /** `slide` (default): in from the right edge, the covered screen drifting left; `none`: at once. */
63
+ transition?: "slide" | "none";
64
+ /** The transition's length in ms. Default 300. */
65
+ duration?: number;
66
+ /**
67
+ * Painted behind each screen. None by default: a screen paints its own
68
+ * (its root's `bg-…`), and a colour here is one more full-screen layer the
69
+ * GPU fills on every frame. Set it when screens leave areas transparent,
70
+ * or the covered screen shows through them while a transition runs.
71
+ */
72
+ background?: string;
73
+ /**
74
+ * What the back button does when it reaches this stack, in place of
75
+ * `pop()`: true when it went back. Server-driven pages ask photon, whose
76
+ * history knows which page comes back.
77
+ */
78
+ back?: () => boolean;
79
+ }
80
+
81
+ const ScreenContext = createContext<Screen | undefined>(
82
+ "orbit screen",
83
+ undefined,
84
+ );
85
+
86
+ /** The screen this component is part of (in its setup, or in a slot it built). */
87
+ export function useScreen(): Screen {
88
+ const screen = inject(ScreenContext);
89
+ if (screen === undefined) {
90
+ throw new E_ORBIT_NO_SCREEN();
91
+ }
92
+ return screen;
93
+ }
94
+
95
+ /** How far the covered screen drifts left, in widths. */
96
+ const COVERED_SHIFT = 0.3;
97
+
98
+ /** @internal What the renderer gives a stack to build a screen: its children and their disposers. */
99
+ export type ScreenBuilder = (
100
+ result: TemplateResult,
101
+ ) => [Child[], Array<() => void>];
102
+
103
+ interface Entry {
104
+ readonly render: () => TemplateResult;
105
+ readonly top: Signal<boolean>;
106
+ view: NativeNode | null;
107
+ disposers: Array<() => void>;
108
+ x: MotionValue | null;
109
+ visible: MotionValue | null;
110
+ leaving: ReturnType<typeof setTimeout> | null;
111
+ }
112
+
113
+ // Provides the screen, then builds it: what the screen creates injects it.
114
+ const ScreenFrame = component(
115
+ (props: { screen: Screen; render: () => TemplateResult }) => {
116
+ provide(ScreenContext, props.screen);
117
+ return props.render();
118
+ },
119
+ );
120
+
121
+ /** Stacks shown now, in the order they were shown: the back button asks the last first. */
122
+ const shown: Stack[] = [];
123
+
124
+ /**
125
+ * The back button or gesture: pop the innermost shown stack that can.
126
+ * False when none can, and the platform's own back applies (leave the app).
127
+ */
128
+ export function goBack(): boolean {
129
+ for (let i = shown.length - 1; i >= 0; i--) {
130
+ const stack = shown[i];
131
+ if (stack?.isShown() && stack.back()) return true;
132
+ }
133
+ return false;
134
+ }
135
+
136
+ export class Stack implements Navigator {
137
+ readonly #run = captureContext();
138
+ readonly #parent = inject(ScreenContext);
139
+ readonly #transition: "slide" | "none";
140
+ readonly #duration: number;
141
+ readonly #background: string | undefined;
142
+ readonly #back: (() => boolean) | undefined;
143
+ readonly #depth = signal(1);
144
+ #entries: Entry[] = [];
145
+ #leaving: Entry[] = [];
146
+ readonly #region = new Region();
147
+ #container: NativeNode | null = null;
148
+ #build: ScreenBuilder | null = null;
149
+
150
+ constructor(initial: Entry["render"], options: StackOptions) {
151
+ this.#transition = options.transition ?? "slide";
152
+ this.#duration = options.duration ?? 300;
153
+ this.#background = options.background;
154
+ this.#back = options.back;
155
+ this.#entries = [this.#entry(initial)];
156
+ }
157
+
158
+ push(screen: () => TemplateResult): void;
159
+ push<P>(screen: ScreenComponent<P>, props: P): void;
160
+ push<P>(screen: ScreenComponent<P | undefined>, props?: P): void {
161
+ const entry = this.#entry(() => screen(props));
162
+ const below = this.#entries.at(-1);
163
+ this.#entries.push(entry);
164
+ this.#depth(this.#entries.length);
165
+ if (!this.#container) return;
166
+ if (below) this.#cover(below);
167
+ this.#show(entry, true);
168
+ }
169
+
170
+ pop(): boolean {
171
+ if (this.#entries.length < 2) return false;
172
+ const top = this.#entries.pop();
173
+ const below = this.#entries.at(-1);
174
+ this.#depth(this.#entries.length);
175
+ if (!top || !below) return false;
176
+ this.#leave(top, true);
177
+ below.top(true);
178
+ if (this.#container) {
179
+ below.visible?.set(1);
180
+ if (this.#animated()) below.x?.timing(0, { duration: this.#duration });
181
+ else below.x?.set(0);
182
+ }
183
+ return true;
184
+ }
185
+
186
+ replace(screen: () => TemplateResult): void;
187
+ replace<P>(screen: ScreenComponent<P>, props: P): void;
188
+ replace<P>(screen: ScreenComponent<P | undefined>, props?: P): void {
189
+ const entry = this.#entry(() => screen(props));
190
+ const old = this.#entries.pop();
191
+ this.#entries.push(entry);
192
+ if (old) this.#leave(old, false);
193
+ if (this.#container) this.#show(entry, true);
194
+ }
195
+
196
+ /**
197
+ * Swap the top screen for `screen` at once, with no transition: the same
198
+ * place showing something new (a page reloaded with new props).
199
+ */
200
+ replaceInPlace<P>(screen: ScreenComponent<P>, props: P): void {
201
+ const entry = this.#entry(() => screen(props));
202
+ const old = this.#entries.pop();
203
+ this.#entries.push(entry);
204
+ if (old) this.#leave(old, false, false);
205
+ if (this.#container) this.#show(entry, false);
206
+ }
207
+
208
+ /** End every screen and start again from `screen`, at once. */
209
+ reset<P>(screen: ScreenComponent<P>, props: P): void {
210
+ const entry = this.#entry(() => screen(props));
211
+ const old = this.#entries.splice(0);
212
+ this.#entries = [entry];
213
+ this.#depth(1);
214
+ for (const ended of old) this.#leave(ended, false, false);
215
+ if (this.#container) this.#show(entry, false);
216
+ }
217
+
218
+ /** What the back button does here: the `back` option, or `pop()`. */
219
+ back(): boolean {
220
+ return this.#back === undefined ? this.pop() : this.#back();
221
+ }
222
+
223
+ canGoBack(): boolean {
224
+ return this.#depth() > 1;
225
+ }
226
+
227
+ /** Whether this stack is on screen: not inside a covered screen. */
228
+ isShown(): boolean {
229
+ return this.#container !== null && (this.#parent?.focused() ?? true);
230
+ }
231
+
232
+ /**
233
+ * @internal The renderer shows the stack here, once: `build` makes a
234
+ * screen's native children, `ended` takes what ends the stack with the
235
+ * scope that shows it.
236
+ */
237
+ attach(build: ScreenBuilder, ended: (end: () => void) => void): NativeNode {
238
+ if (this.#container) {
239
+ throw new E_ORBIT_STACK_SHOWN();
240
+ }
241
+ this.#build = build;
242
+ this.#container = new NativeNode(
243
+ "RCTView",
244
+ flatten({ flex: 1, overflow: "hidden" }),
245
+ [this.#region],
246
+ );
247
+ for (const entry of this.#entries) this.#show(entry, false);
248
+ for (const entry of this.#entries.slice(0, -1)) {
249
+ entry.visible?.set(0);
250
+ }
251
+ shown.push(this);
252
+ ended(() => this.#end());
253
+ return this.#container;
254
+ }
255
+
256
+ #entry(render: () => TemplateResult): Entry {
257
+ return {
258
+ render,
259
+ top: signal(true),
260
+ view: null,
261
+ disposers: [],
262
+ x: null,
263
+ visible: null,
264
+ leaving: null,
265
+ };
266
+ }
267
+
268
+ #screen(entry: Entry): Screen {
269
+ const parent = this.#parent;
270
+ const stack = this;
271
+ return {
272
+ focused: () => entry.top() && (parent?.focused() ?? true),
273
+ push<P>(screen: ScreenComponent<P | undefined>, props?: P) {
274
+ stack.push(screen, props);
275
+ },
276
+ pop: () => stack.pop(),
277
+ replace<P>(screen: ScreenComponent<P | undefined>, props?: P) {
278
+ stack.replace(screen, props);
279
+ },
280
+ canGoBack: () => stack.canGoBack(),
281
+ };
282
+ }
283
+
284
+ #animated(): boolean {
285
+ return this.#transition === "slide" && this.#width() > 0;
286
+ }
287
+
288
+ /** The stack's width in dp, as last laid out (0 before its first commit). */
289
+ #width(): number {
290
+ const container = this.#container;
291
+ if (!container) return 0;
292
+ const frames: unknown = JSON.parse(
293
+ treeHost().layout(JSON.stringify([container.tag])),
294
+ );
295
+ const frame: unknown = Array.isArray(frames) ? frames[0] : null;
296
+ const width: unknown = Array.isArray(frame) ? frame[2] : 0;
297
+ return typeof width === "number" ? width : 0;
298
+ }
299
+
300
+ /** Build `entry`'s screen and put its view on top; `arrive` slides it in. */
301
+ #show(entry: Entry, arrive: boolean): void {
302
+ const build = this.#build;
303
+ if (!build) return;
304
+ const width = arrive && this.#animated() ? this.#width() : 0;
305
+ const screen = this.#screen(entry);
306
+ const [children, disposers] = untrack(() =>
307
+ build(this.#run(() => ScreenFrame({ screen, render: entry.render }))),
308
+ );
309
+ const view = new NativeNode(
310
+ "RCTView",
311
+ flatten({
312
+ position: "absolute",
313
+ top: 0,
314
+ left: 0,
315
+ right: 0,
316
+ bottom: 0,
317
+ ...(this.#background === undefined
318
+ ? {}
319
+ : { backgroundColor: this.#background }),
320
+ }),
321
+ children,
322
+ );
323
+ entry.view = view;
324
+ entry.disposers = disposers;
325
+ const x = new MotionValue(width);
326
+ const visible = new MotionValue(1);
327
+ entry.x = x;
328
+ entry.visible = visible;
329
+ afterCommit(() => bindMotion(view.tag, { translateX: x, visible }));
330
+ if (width > 0) x.timing(0, { duration: this.#duration });
331
+ this.#layout();
332
+ }
333
+
334
+ /**
335
+ * `entry` is no longer on top: it drifts left, and hides the frame its
336
+ * cover has arrived — at once, a frame later, so the cover is mounted.
337
+ */
338
+ #cover(entry: Entry): void {
339
+ entry.top(false);
340
+ const animated = this.#animated();
341
+ if (animated) {
342
+ const width = this.#width();
343
+ entry.x?.timing(-COVERED_SHIFT * width, { duration: this.#duration });
344
+ }
345
+ entry.visible?.after(animated ? this.#duration : 0, 0);
346
+ }
347
+
348
+ /**
349
+ * `entry` ends now — its effects stop, its unmount hooks run — and its
350
+ * view goes once the transition is over: sliding out to the right when
351
+ * popped, staying under its replacement when replaced.
352
+ */
353
+ #leave(entry: Entry, slide: boolean, animate = true): void {
354
+ entry.top(false);
355
+ for (const end of entry.disposers.splice(0)) end();
356
+ if (!entry.view) return;
357
+ this.#leaving.push(entry);
358
+ const animated = animate && this.#animated();
359
+ if (animated && slide) {
360
+ entry.x?.timing(this.#width(), { duration: this.#duration });
361
+ }
362
+ entry.leaving = setTimeout(
363
+ () => this.#remove(entry),
364
+ animated ? this.#duration : 0,
365
+ );
366
+ }
367
+
368
+ #remove(entry: Entry): void {
369
+ if (entry.leaving !== null) clearTimeout(entry.leaving);
370
+ entry.leaving = null;
371
+ const at = this.#leaving.indexOf(entry);
372
+ if (at >= 0) this.#leaving.splice(at, 1);
373
+ entry.view = null;
374
+ entry.x?.dispose();
375
+ entry.visible?.dispose();
376
+ entry.x = null;
377
+ entry.visible = null;
378
+ this.#layout();
379
+ }
380
+
381
+ /**
382
+ * The views in drawing order: the covered screens, then those leaving
383
+ * (a popped screen slides out over the one it uncovers, a replaced one
384
+ * stays under its replacement), then the top.
385
+ */
386
+ #layout(): void {
387
+ const views = (entries: readonly Entry[]) =>
388
+ entries.flatMap((entry) => (entry.view ? [entry.view] : []));
389
+ const top = this.#entries.at(-1);
390
+ this.#region.set([
391
+ ...views(this.#entries.slice(0, -1)),
392
+ ...views(this.#leaving),
393
+ ...(top ? views([top]) : []),
394
+ ]);
395
+ }
396
+
397
+ #end(): void {
398
+ const at = shown.indexOf(this);
399
+ if (at >= 0) shown.splice(at, 1);
400
+ for (const entry of [...this.#entries, ...this.#leaving]) {
401
+ entry.top(false);
402
+ for (const end of entry.disposers.splice(0)) end();
403
+ if (entry.leaving !== null) clearTimeout(entry.leaving);
404
+ entry.x?.dispose();
405
+ entry.visible?.dispose();
406
+ }
407
+ this.#entries = [];
408
+ this.#leaving = [];
409
+ this.#container = null;
410
+ }
411
+ }
412
+
413
+ /** A stack of screens, `initial` at the bottom; show it with `html\`${stack}\``. */
414
+ export function createStack(
415
+ initial: () => TemplateResult,
416
+ options?: StackOptions,
417
+ ): Stack;
418
+ /** …with the first screen's props. */
419
+ export function createStack<P>(
420
+ initial: ScreenComponent<P>,
421
+ options: StackOptions & { props: P },
422
+ ): Stack;
423
+ export function createStack<P>(
424
+ initial: ScreenComponent<P | undefined>,
425
+ options: StackOptions & { props?: P } = {},
426
+ ): Stack {
427
+ return new Stack(() => initial(options.props), options);
428
+ }