@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.
- package/Cargo.lock +146 -0
- package/Cargo.toml +16 -0
- package/LICENSE +21 -0
- package/README.md +314 -2
- package/android/orbit.cmake +31 -0
- package/android/src/main/cpp/NativeOrbit.cpp +309 -0
- package/android/src/main/cpp/NativeOrbit.h +35 -0
- package/android/src/main/cpp/orbit_host.h +34 -0
- package/android/src/main/java/com/c9up/orbit/OrbitEffects.kt +286 -0
- package/android/src/main/java/com/c9up/orbit/OrbitHost.kt +258 -0
- package/android/src/main/java/com/c9up/orbit/OrbitImage.kt +346 -0
- package/android/src/main/java/com/c9up/orbit/OrbitInput.kt +145 -0
- package/android/src/main/java/com/c9up/orbit/OrbitPress.kt +110 -0
- package/android/src/main/java/com/c9up/orbit/OrbitVector.kt +115 -0
- package/android/src/main/java/com/c9up/orbit/OrbitViews.kt +362 -0
- package/crates/orbit-host/Cargo.toml +14 -0
- package/crates/orbit-host/src/gesture.rs +561 -0
- package/crates/orbit-host/src/lib.rs +1608 -0
- package/crates/orbit-host/src/motion.rs +328 -0
- package/crates/orbit-host/src/vector.rs +917 -0
- package/dist/color.d.ts +10 -0
- package/dist/color.d.ts.map +1 -0
- package/dist/color.js +62 -0
- package/dist/color.js.map +1 -0
- package/dist/commit.d.ts +45 -0
- package/dist/commit.d.ts.map +1 -0
- package/dist/commit.js +172 -0
- package/dist/commit.js.map +1 -0
- package/dist/errors.d.ts +42 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +15 -0
- package/dist/errors.js.map +1 -0
- package/dist/host.d.ts +49 -0
- package/dist/host.d.ts.map +1 -0
- package/dist/host.js +18 -0
- package/dist/host.js.map +1 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +29 -0
- package/dist/index.js.map +1 -0
- package/dist/motion.d.ts +18 -0
- package/dist/motion.d.ts.map +1 -0
- package/dist/motion.js +66 -0
- package/dist/motion.js.map +1 -0
- package/dist/node.d.ts +46 -0
- package/dist/node.d.ts.map +1 -0
- package/dist/node.js +110 -0
- package/dist/node.js.map +1 -0
- package/dist/pages.d.ts +56 -0
- package/dist/pages.d.ts.map +1 -0
- package/dist/pages.js +157 -0
- package/dist/pages.js.map +1 -0
- package/dist/react-native/index.d.ts +25 -0
- package/dist/react-native/index.d.ts.map +1 -0
- package/dist/react-native/index.js +43 -0
- package/dist/react-native/index.js.map +1 -0
- package/dist/server.d.ts +85 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +282 -0
- package/dist/server.js.map +1 -0
- package/dist/stack.d.ts +117 -0
- package/dist/stack.d.ts.map +1 -0
- package/dist/stack.js +299 -0
- package/dist/stack.js.map +1 -0
- package/dist/styles.d.ts +138 -0
- package/dist/styles.d.ts.map +1 -0
- package/dist/styles.js +920 -0
- package/dist/styles.js.map +1 -0
- package/dist/template.d.ts +27 -0
- package/dist/template.d.ts.map +1 -0
- package/dist/template.js +1392 -0
- package/dist/template.js.map +1 -0
- package/dist/url.d.ts +45 -0
- package/dist/url.d.ts.map +1 -0
- package/dist/url.js +302 -0
- package/dist/url.js.map +1 -0
- package/package.json +90 -3
- package/scripts/build-android.sh +27 -0
- package/scripts/test-styles.mjs +31 -0
- package/specs/NativeOrbit.ts +34 -0
- package/src/color.ts +73 -0
- package/src/commit.ts +206 -0
- package/src/errors.ts +48 -0
- package/src/host.ts +92 -0
- package/src/index.ts +60 -0
- package/src/motion.ts +86 -0
- package/src/node.ts +133 -0
- package/src/pages.ts +210 -0
- package/src/react-native/index.ts +76 -0
- package/src/react-native/react-native.d.ts +31 -0
- package/src/server.ts +359 -0
- package/src/stack.ts +434 -0
- package/src/styles.ts +1024 -0
- package/src/template.ts +1657 -0
- package/src/url.ts +326 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type {TurboModule} from 'react-native';
|
|
2
|
+
import {TurboModuleRegistry} from 'react-native';
|
|
3
|
+
|
|
4
|
+
/** orbit's Rust native host (orbit-host), over JSI. Mirrors `OrbitHost` in src/host.ts. */
|
|
5
|
+
export interface Spec extends TurboModule {
|
|
6
|
+
/** The root's size in dp. */
|
|
7
|
+
readonly setViewport: (width: number, height: number) => void;
|
|
8
|
+
/** Commit a JSON batch of tree ops; returns the host's JSON report. */
|
|
9
|
+
readonly commit: (ops: string) => string;
|
|
10
|
+
/** An image node's bytes (a Uint8Array or ArrayBuffer), held in memory until the node goes. */
|
|
11
|
+
readonly imageBytes: (tag: number, bytes: Object) => void;
|
|
12
|
+
/** Where nodes were laid out: a JSON array of tags in, of [x, y, w, h] (dp) or null out. */
|
|
13
|
+
readonly layout: (tags: string) => string;
|
|
14
|
+
/** Scroll a scroll view by `by` dp, or to its end. */
|
|
15
|
+
readonly scroll: (tag: number, by: number, toEnd: boolean) => void;
|
|
16
|
+
/** Touches on host views, as React Native's touch event types, in dp. */
|
|
17
|
+
/** Touches (dp), text-field events with their text in `data`, `topViewport` on tag 0 (window resized). */
|
|
18
|
+
readonly setEventHandler: (handler: (tag: number, type: string, x: number, y: number, data: string) => void) => void;
|
|
19
|
+
|
|
20
|
+
// UI-thread animation, driven by orbit-host on every vsync (no JS per frame).
|
|
21
|
+
readonly motionValue: (tag: number, initial: number) => void;
|
|
22
|
+
readonly motionInterpolate: (tag: number, input: number, from: ReadonlyArray<number>, to: ReadonlyArray<number>) => void;
|
|
23
|
+
readonly motionSet: (tag: number, value: number) => void;
|
|
24
|
+
readonly motionStop: (tag: number) => void;
|
|
25
|
+
readonly motionFrames: (tag: number, frames: ReadonlyArray<number>, to: number, loop: boolean) => void;
|
|
26
|
+
readonly motionSpring: (tag: number, to: number, stiffness: number, damping: number, mass: number) => void;
|
|
27
|
+
/** Forget a motion value or interpolation, its animation and its bindings. */
|
|
28
|
+
readonly motionDispose: (tag: number) => void;
|
|
29
|
+
/** prop: 0 opacity, 1 translateX, 2 translateY, 3 scale, 4 visible (> 0.5). */
|
|
30
|
+
readonly motionBind: (view: number, prop: number, node: number) => void;
|
|
31
|
+
readonly motionBindScroll: (scrollView: number, value: number) => void;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export default TurboModuleRegistry.getEnforcing<Spec>('NativeOrbit');
|
package/src/color.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { OrbitError } from "./errors.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A CSS colour as the 32-bit ARGB integer the native host reads (signed, as
|
|
5
|
+
* Android's `Color` int), without React Native's `processColor`: the core of
|
|
6
|
+
* orbit runs and is tested without React Native.
|
|
7
|
+
*
|
|
8
|
+
* Reads `#rgb`, `#rgba`, `#rrggbb`, `#rrggbbaa`, `rgb()` / `rgba()` with
|
|
9
|
+
* comma or space syntax, and `transparent`.
|
|
10
|
+
*/
|
|
11
|
+
export function colorToArgb(css: string): number {
|
|
12
|
+
const value = css.trim().toLowerCase();
|
|
13
|
+
if (value === "transparent") return 0;
|
|
14
|
+
if (value.startsWith("#")) return fromHex(value.slice(1), css);
|
|
15
|
+
const fn = /^rgba?\(\s*([^)]+)\)$/.exec(value);
|
|
16
|
+
if (fn) return fromRgb(fn[1] ?? "", css);
|
|
17
|
+
throw new OrbitError(
|
|
18
|
+
"E_ORBIT_INVALID_COLOR",
|
|
19
|
+
`[orbit] cannot read the colour "${css}"`,
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function pack(r: number, g: number, b: number, a: number): number {
|
|
24
|
+
const byte = (n: number) => Math.max(0, Math.min(255, Math.round(n)));
|
|
25
|
+
return (byte(a) << 24) | (byte(r) << 16) | (byte(g) << 8) | byte(b) | 0;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function fromHex(hex: string, css: string): number {
|
|
29
|
+
if (!/^[0-9a-f]+$/.test(hex)) throw invalid(css);
|
|
30
|
+
const at = (i: number, len: number) =>
|
|
31
|
+
Number.parseInt(
|
|
32
|
+
len === 1 ? hex.charAt(i).repeat(2) : hex.slice(i, i + 2),
|
|
33
|
+
16,
|
|
34
|
+
);
|
|
35
|
+
switch (hex.length) {
|
|
36
|
+
case 3:
|
|
37
|
+
return pack(at(0, 1), at(1, 1), at(2, 1), 255);
|
|
38
|
+
case 4:
|
|
39
|
+
return pack(at(0, 1), at(1, 1), at(2, 1), at(3, 1));
|
|
40
|
+
case 6:
|
|
41
|
+
return pack(at(0, 2), at(2, 2), at(4, 2), 255);
|
|
42
|
+
case 8:
|
|
43
|
+
return pack(at(0, 2), at(2, 2), at(4, 2), at(6, 2));
|
|
44
|
+
default:
|
|
45
|
+
throw invalid(css);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function fromRgb(args: string, css: string): number {
|
|
50
|
+
const parts = args.split(/[\s,/]+/).filter(Boolean);
|
|
51
|
+
if (parts.length !== 3 && parts.length !== 4) throw invalid(css);
|
|
52
|
+
const channel = (p: string) =>
|
|
53
|
+
p.endsWith("%") ? (Number.parseFloat(p) * 255) / 100 : Number.parseFloat(p);
|
|
54
|
+
const alpha = (p: string | undefined) => {
|
|
55
|
+
if (p === undefined) return 255;
|
|
56
|
+
return (
|
|
57
|
+
(p.endsWith("%") ? Number.parseFloat(p) / 100 : Number.parseFloat(p)) *
|
|
58
|
+
255
|
|
59
|
+
);
|
|
60
|
+
};
|
|
61
|
+
const [r, g, b] = parts.map(channel);
|
|
62
|
+
const a = alpha(parts[3]);
|
|
63
|
+
if ([r, g, b, a].some((n) => n === undefined || Number.isNaN(n)))
|
|
64
|
+
throw invalid(css);
|
|
65
|
+
return pack(r ?? 0, g ?? 0, b ?? 0, a);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function invalid(css: string): OrbitError {
|
|
69
|
+
return new OrbitError(
|
|
70
|
+
"E_ORBIT_INVALID_COLOR",
|
|
71
|
+
`[orbit] cannot read the colour "${css}"`,
|
|
72
|
+
);
|
|
73
|
+
}
|
package/src/commit.ts
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { OrbitError } from "./errors.js";
|
|
2
|
+
import { currentHost, type OrbitHost } from "./host.js";
|
|
3
|
+
import { type NativeNode, setDirtyListener } from "./node.js";
|
|
4
|
+
import { viewportSize } from "./styles.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Committing the JS tree to the Rust host: one JSON batch per commit over JSI
|
|
8
|
+
* (create, props patch, children, root). Layout, diffing and mounting happen in
|
|
9
|
+
* Rust and on the platform side. Commits are batched to a microtask, so many
|
|
10
|
+
* signal writes in one turn cost one commit.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export interface OrbitReport {
|
|
14
|
+
deleted: number[];
|
|
15
|
+
parseUs: number;
|
|
16
|
+
layoutUs: number;
|
|
17
|
+
diffUs: number;
|
|
18
|
+
measures: number;
|
|
19
|
+
nodes: number;
|
|
20
|
+
opsBytes: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function isReport(value: unknown): value is OrbitReport {
|
|
24
|
+
return (
|
|
25
|
+
typeof value === "object" &&
|
|
26
|
+
value !== null &&
|
|
27
|
+
Array.isArray(Reflect.get(value, "deleted"))
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const created = new WeakSet<NativeNode>();
|
|
32
|
+
const byTag = new Map<number, NativeNode>();
|
|
33
|
+
const lastChildren = new WeakMap<NativeNode, number[]>();
|
|
34
|
+
const afterCommitHooks: Array<() => void> = [];
|
|
35
|
+
const commitListeners = new Set<() => void>();
|
|
36
|
+
let mountedHost: OrbitHost | null = null;
|
|
37
|
+
|
|
38
|
+
/** Run `listener` after every commit; returns what stops it. */
|
|
39
|
+
export function onEveryCommit(listener: () => void): () => void {
|
|
40
|
+
commitListeners.add(listener);
|
|
41
|
+
return () => commitListeners.delete(listener);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The host the mounted tree commits to (layout reads, scrolling). */
|
|
45
|
+
export function treeHost(): OrbitHost {
|
|
46
|
+
return mountedHost ?? currentHost();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Run `hook` once the next commit has reached the host (aurora's onMount timing). */
|
|
50
|
+
export function afterCommit(hook: () => void): void {
|
|
51
|
+
afterCommitHooks.push(hook);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function sameTags(
|
|
55
|
+
a: readonly number[] | undefined,
|
|
56
|
+
b: readonly number[],
|
|
57
|
+
): boolean {
|
|
58
|
+
if (!a || a.length !== b.length) return false;
|
|
59
|
+
for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
type Bytes = Array<[number, Uint8Array]>;
|
|
64
|
+
|
|
65
|
+
// Only nodes the commit reaches hand their bytes over: the host drops them
|
|
66
|
+
// with the node, so bytes of a node it never saw would stay behind.
|
|
67
|
+
function takeBytes(node: NativeNode, bytes: Bytes): void {
|
|
68
|
+
if (node.pendingBytes) {
|
|
69
|
+
bytes.push([node.tag, node.pendingBytes]);
|
|
70
|
+
node.pendingBytes = null;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function visit(node: NativeNode, ops: unknown[], bytes: Bytes): void {
|
|
75
|
+
takeBytes(node, bytes);
|
|
76
|
+
if (!created.has(node)) {
|
|
77
|
+
ops.push(["c", node.tag, node.viewName, node.props]);
|
|
78
|
+
created.add(node);
|
|
79
|
+
byTag.set(node.tag, node);
|
|
80
|
+
node.pendingProps = null;
|
|
81
|
+
const kids = node.flatChildren();
|
|
82
|
+
for (const kid of kids) visit(kid, ops, bytes);
|
|
83
|
+
const tags = kids.map((kid) => kid.tag);
|
|
84
|
+
if (tags.length > 0) ops.push(["k", node.tag, tags]);
|
|
85
|
+
lastChildren.set(node, tags);
|
|
86
|
+
node.childrenDirty = false;
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (node.pendingProps) {
|
|
90
|
+
ops.push(["u", node.tag, node.pendingProps]);
|
|
91
|
+
node.pendingProps = null;
|
|
92
|
+
}
|
|
93
|
+
if (node.childrenDirty) {
|
|
94
|
+
const kids = node.flatChildren();
|
|
95
|
+
for (const kid of kids) visit(kid, ops, bytes);
|
|
96
|
+
const tags = kids.map((kid) => kid.tag);
|
|
97
|
+
if (!sameTags(lastChildren.get(node), tags)) {
|
|
98
|
+
ops.push(["k", node.tag, tags]);
|
|
99
|
+
lastChildren.set(node, tags);
|
|
100
|
+
}
|
|
101
|
+
node.childrenDirty = false;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export class Root {
|
|
106
|
+
readonly #host: OrbitHost;
|
|
107
|
+
readonly #tree: NativeNode;
|
|
108
|
+
#scheduled = false;
|
|
109
|
+
#rootSent = false;
|
|
110
|
+
/** Every commit's report, newest last (development and benchmarks). */
|
|
111
|
+
readonly reports: OrbitReport[] = [];
|
|
112
|
+
|
|
113
|
+
constructor(host: OrbitHost, tree: NativeNode) {
|
|
114
|
+
this.#host = host;
|
|
115
|
+
this.#tree = tree;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
schedule(): void {
|
|
119
|
+
if (this.#scheduled) return;
|
|
120
|
+
this.#scheduled = true;
|
|
121
|
+
void Promise.resolve().then(() => {
|
|
122
|
+
// A synchronous flush() may already have committed this batch.
|
|
123
|
+
if (this.#scheduled) this.commit();
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Commit what changed now, instead of at the end of the turn. */
|
|
128
|
+
flush(): OrbitReport {
|
|
129
|
+
return this.commit();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
commit(): OrbitReport {
|
|
133
|
+
this.#scheduled = false;
|
|
134
|
+
const ops: unknown[] = [];
|
|
135
|
+
const bytes: Bytes = [];
|
|
136
|
+
visit(this.#tree, ops, bytes);
|
|
137
|
+
if (!this.#rootSent) {
|
|
138
|
+
ops.push(["r", this.#tree.tag]);
|
|
139
|
+
this.#rootSent = true;
|
|
140
|
+
}
|
|
141
|
+
// Before the ops that show them: the host reads them when it mounts.
|
|
142
|
+
for (const [tag, data] of bytes) this.#host.imageBytes(tag, data);
|
|
143
|
+
const answer: unknown = JSON.parse(this.#host.commit(JSON.stringify(ops)));
|
|
144
|
+
if (!isReport(answer)) {
|
|
145
|
+
throw new OrbitError(
|
|
146
|
+
"E_ORBIT_COMMIT_FAILED",
|
|
147
|
+
`[orbit] the host refused a commit: ${JSON.stringify(answer)}`,
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
for (const tag of answer.deleted) {
|
|
151
|
+
const node = byTag.get(tag);
|
|
152
|
+
if (!node) continue;
|
|
153
|
+
byTag.delete(tag);
|
|
154
|
+
created.delete(node);
|
|
155
|
+
lastChildren.delete(node);
|
|
156
|
+
}
|
|
157
|
+
this.reports.push(answer);
|
|
158
|
+
for (const hook of afterCommitHooks.splice(0)) hook();
|
|
159
|
+
for (const listener of [...commitListeners]) listener();
|
|
160
|
+
return answer;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export interface MountOptions {
|
|
165
|
+
/** The root's size in dp (React Native: `Dimensions.get("window")`). */
|
|
166
|
+
viewport: { width: number; height: number };
|
|
167
|
+
/** Defaults to the host given to `useHost`. */
|
|
168
|
+
host?: OrbitHost;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** Mount a native tree; returns its root (`flush()` commits synchronously). */
|
|
172
|
+
export function mountTree(tree: NativeNode, options: MountOptions): Root {
|
|
173
|
+
const host = options.host ?? currentHost();
|
|
174
|
+
const root = new Root(host, tree);
|
|
175
|
+
mountedHost = host;
|
|
176
|
+
setDirtyListener(() => root.schedule());
|
|
177
|
+
host.setViewport(options.viewport.width, options.viewport.height);
|
|
178
|
+
host.setEventHandler((tag, type, x, y, data) => {
|
|
179
|
+
if (type === "topScroll") {
|
|
180
|
+
for (const listener of byTag.get(tag)?.scrollListeners ?? []) listener(y);
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
if (type === "topViewport") {
|
|
184
|
+
// The window resized (the keyboard came or went): lay out again.
|
|
185
|
+
host.setViewport(x, y);
|
|
186
|
+
viewportSize({ width: x, height: y });
|
|
187
|
+
root.commit();
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
const payload = {
|
|
191
|
+
pageX: x,
|
|
192
|
+
pageY: y,
|
|
193
|
+
locationX: x,
|
|
194
|
+
locationY: y,
|
|
195
|
+
text: data,
|
|
196
|
+
};
|
|
197
|
+
for (let node = byTag.get(tag) ?? null; node; node = node.parent) {
|
|
198
|
+
if (node.onEvent) {
|
|
199
|
+
node.onEvent(type, payload);
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
root.commit();
|
|
205
|
+
return root;
|
|
206
|
+
}
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Orbit's errors.
|
|
3
|
+
*
|
|
4
|
+
* Every failure carries a stable `code` (`E_ORBIT_<REASON>`), so a caller
|
|
5
|
+
* branches on what went wrong rather than on a message.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export type OrbitErrorCode =
|
|
9
|
+
/** `mount` ran before `useHost` gave orbit a native host. */
|
|
10
|
+
| "E_ORBIT_NO_HOST"
|
|
11
|
+
/** The native host refused a commit; the message carries its report. */
|
|
12
|
+
| "E_ORBIT_COMMIT_FAILED"
|
|
13
|
+
/** An element with no native mapping yet (`<select>`, `<canvas>`, or `<text>` / a gradient inside an `<svg>`). */
|
|
14
|
+
| "E_ORBIT_UNSUPPORTED_ELEMENT"
|
|
15
|
+
/** A block element inside text: native text holds only inline runs. */
|
|
16
|
+
| "E_ORBIT_BLOCK_IN_TEXT"
|
|
17
|
+
/** `motion=${…}` was not an object of motion values. */
|
|
18
|
+
| "E_ORBIT_INVALID_MOTION"
|
|
19
|
+
/** `scroll-y=${…}` was not a motion value. */
|
|
20
|
+
| "E_ORBIT_INVALID_SCROLL_BINDING"
|
|
21
|
+
/** A colour orbit cannot read (named colours other than `transparent`, `hsl()`…). */
|
|
22
|
+
| "E_ORBIT_INVALID_COLOR"
|
|
23
|
+
/** `<img src>` over plain http: images are fetched over https only. */
|
|
24
|
+
| "E_ORBIT_INSECURE_IMAGE_URL"
|
|
25
|
+
/** `<img src>` with a scheme that is not an image source (`data:`, `javascript:`…). */
|
|
26
|
+
| "E_ORBIT_IMAGE_SOURCE"
|
|
27
|
+
/** Two elements of an `each` list share a key: a key names one row. */
|
|
28
|
+
| "E_ORBIT_DUPLICATE_KEY"
|
|
29
|
+
/** `useScreen()` in a component no screen of a stack built. */
|
|
30
|
+
| "E_ORBIT_NO_SCREEN"
|
|
31
|
+
/** One stack put in two places: a stack is shown once. */
|
|
32
|
+
| "E_ORBIT_STACK_SHOWN"
|
|
33
|
+
/** `useStyles` got something other than a table `ream css:compile` wrote. */
|
|
34
|
+
| "E_ORBIT_STYLE_TABLE"
|
|
35
|
+
/** `serverPages({ server })` over plain http to a host that is not this machine or the emulator's. */
|
|
36
|
+
| "E_ORBIT_INSECURE_SERVER"
|
|
37
|
+
/** The server named a page the app does not have (`serverPages({ pages })`). */
|
|
38
|
+
| "E_ORBIT_UNKNOWN_PAGE";
|
|
39
|
+
|
|
40
|
+
export class OrbitError extends Error {
|
|
41
|
+
readonly code: OrbitErrorCode;
|
|
42
|
+
|
|
43
|
+
constructor(code: OrbitErrorCode, message: string, options?: ErrorOptions) {
|
|
44
|
+
super(message, options);
|
|
45
|
+
this.name = new.target.name;
|
|
46
|
+
this.code = code;
|
|
47
|
+
}
|
|
48
|
+
}
|
package/src/host.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { OrbitError } from "./errors.js";
|
|
2
|
+
import { useNativeMotion } from "./motion.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* What orbit needs from the native side: the Rust host behind JSI (the
|
|
6
|
+
* `NativeOrbit` TurboModule, see `specs/NativeOrbit.ts`), or a fake in tests.
|
|
7
|
+
* Structural on purpose: orbit's core imports nothing from React Native.
|
|
8
|
+
*/
|
|
9
|
+
export interface OrbitHost {
|
|
10
|
+
/** The root's size in dp. */
|
|
11
|
+
setViewport(width: number, height: number): void;
|
|
12
|
+
/** Apply a JSON batch of tree operations; returns the host's JSON report. */
|
|
13
|
+
commit(ops: string): string;
|
|
14
|
+
/**
|
|
15
|
+
* The bytes an image node shows (`<img src=${bytes}>`): copied into the
|
|
16
|
+
* host's memory, never to disk, and dropped (zeroed) with the node.
|
|
17
|
+
*/
|
|
18
|
+
imageBytes(tag: number, bytes: Uint8Array): void;
|
|
19
|
+
/**
|
|
20
|
+
* Where nodes were laid out: a JSON array of tags in, a JSON array of
|
|
21
|
+
* `[x, y, width, height]` (dp, the root's coordinates, scroll offsets left
|
|
22
|
+
* out) or null per tag out.
|
|
23
|
+
*/
|
|
24
|
+
layout(tags: string): string;
|
|
25
|
+
/** Scroll a scroll view by `by` dp, or to its end. */
|
|
26
|
+
scroll(tag: number, by: number, toEnd: boolean): void;
|
|
27
|
+
/**
|
|
28
|
+
* Events from host views: touches (React Native's touch event types, in dp),
|
|
29
|
+
* text fields (`topChange`, `topFocus`, `topBlur`, `topSubmit`, with the
|
|
30
|
+
* text in `data`), `topScroll` from a scroll view with its offset in `y`
|
|
31
|
+
* (dp), and `topViewport` on tag 0 when the window resized (the keyboard),
|
|
32
|
+
* with `x`/`y` the new size in dp.
|
|
33
|
+
*/
|
|
34
|
+
setEventHandler(
|
|
35
|
+
handler: (
|
|
36
|
+
tag: number,
|
|
37
|
+
type: string,
|
|
38
|
+
x: number,
|
|
39
|
+
y: number,
|
|
40
|
+
data: string,
|
|
41
|
+
) => void,
|
|
42
|
+
): void;
|
|
43
|
+
motionValue(tag: number, initial: number): void;
|
|
44
|
+
motionInterpolate(
|
|
45
|
+
tag: number,
|
|
46
|
+
input: number,
|
|
47
|
+
from: readonly number[],
|
|
48
|
+
to: readonly number[],
|
|
49
|
+
): void;
|
|
50
|
+
motionSet(tag: number, value: number): void;
|
|
51
|
+
motionStop(tag: number): void;
|
|
52
|
+
motionFrames(
|
|
53
|
+
tag: number,
|
|
54
|
+
frames: readonly number[],
|
|
55
|
+
to: number,
|
|
56
|
+
loop: boolean,
|
|
57
|
+
): void;
|
|
58
|
+
motionSpring(
|
|
59
|
+
tag: number,
|
|
60
|
+
to: number,
|
|
61
|
+
stiffness: number,
|
|
62
|
+
damping: number,
|
|
63
|
+
mass: number,
|
|
64
|
+
): void;
|
|
65
|
+
/** Forget a motion value or interpolation, its animation and its bindings. */
|
|
66
|
+
motionDispose(tag: number): void;
|
|
67
|
+
/** prop: 0 opacity, 1 translateX, 2 translateY, 3 scale, 4 visible (> 0.5). */
|
|
68
|
+
motionBind(view: number, prop: number, node: number): void;
|
|
69
|
+
motionBindScroll(scrollView: number, value: number): void;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The TurboModule name the native side registers. */
|
|
73
|
+
export const NATIVE_MODULE_NAME = "NativeOrbit";
|
|
74
|
+
|
|
75
|
+
let host: OrbitHost | null = null;
|
|
76
|
+
|
|
77
|
+
/** Give orbit its native host, once, before `mount`. */
|
|
78
|
+
export function useHost(next: OrbitHost): void {
|
|
79
|
+
host = next;
|
|
80
|
+
// aurora's motion values made from now on live in this host.
|
|
81
|
+
useNativeMotion();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function currentHost(): OrbitHost {
|
|
85
|
+
if (host === null) {
|
|
86
|
+
throw new OrbitError(
|
|
87
|
+
"E_ORBIT_NO_HOST",
|
|
88
|
+
`[orbit] no native host: call useHost(TurboModuleRegistry.getEnforcing("${NATIVE_MODULE_NAME}")) before mount()`,
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
return host;
|
|
92
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@c9up/orbit` — aurora components as native views on a phone.
|
|
3
|
+
*
|
|
4
|
+
* The JS side keeps a tree of native nodes that aurora's signals patch; each
|
|
5
|
+
* commit goes over JSI to a Rust host that lays it out (taffy), diffs it and
|
|
6
|
+
* hands mount operations to the platform's views. Animations and scroll-driven
|
|
7
|
+
* values run on the UI thread, without JS.
|
|
8
|
+
*
|
|
9
|
+
* useHost(TurboModuleRegistry.getEnforcing("NativeOrbit"))
|
|
10
|
+
* mount(App(), { viewport: Dimensions.get("window") })
|
|
11
|
+
*
|
|
12
|
+
* Screens: `createStack(Home)` in the root, `useScreen().push(Detail, props)`
|
|
13
|
+
* in a screen; the Android back button pops (see `@c9up/orbit/react-native`).
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export { colorToArgb } from "./color.js";
|
|
17
|
+
export {
|
|
18
|
+
afterCommit,
|
|
19
|
+
type MountOptions,
|
|
20
|
+
type OrbitReport,
|
|
21
|
+
type Root,
|
|
22
|
+
} from "./commit.js";
|
|
23
|
+
export { OrbitError, type OrbitErrorCode } from "./errors.js";
|
|
24
|
+
export { NATIVE_MODULE_NAME, type OrbitHost, useHost } from "./host.js";
|
|
25
|
+
export {
|
|
26
|
+
bindMotion,
|
|
27
|
+
bindScrollY,
|
|
28
|
+
MotionNode,
|
|
29
|
+
type MotionStyle,
|
|
30
|
+
MotionValue,
|
|
31
|
+
type SpringOptions,
|
|
32
|
+
type TimingOptions,
|
|
33
|
+
} from "./motion.js";
|
|
34
|
+
/** The app's ream server, one connection for pages, comet and relay. */
|
|
35
|
+
export {
|
|
36
|
+
connectServer,
|
|
37
|
+
type ServerConnection,
|
|
38
|
+
type ServerEvent,
|
|
39
|
+
ServerEventStream,
|
|
40
|
+
} from "./server.js";
|
|
41
|
+
export {
|
|
42
|
+
createStack,
|
|
43
|
+
/** What the back button does: pop the innermost shown stack (false: nothing to pop). */
|
|
44
|
+
goBack,
|
|
45
|
+
type Navigator,
|
|
46
|
+
type Screen,
|
|
47
|
+
type ScreenComponent,
|
|
48
|
+
type Stack,
|
|
49
|
+
type StackOptions,
|
|
50
|
+
useScreen,
|
|
51
|
+
} from "./stack.js";
|
|
52
|
+
export {
|
|
53
|
+
colorScheme,
|
|
54
|
+
type StyleTable,
|
|
55
|
+
unknownClasses,
|
|
56
|
+
useStyles,
|
|
57
|
+
} from "./styles.js";
|
|
58
|
+
export { type GestureEvent, mount } from "./template.js";
|
|
59
|
+
/** A standard `URL` in place of the platform's, when it is not one (React Native's). */
|
|
60
|
+
export { installStandardUrl, StandardURL } from "./url.js";
|
package/src/motion.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Motion values on the phone: aurora's `MotionValue`, driven by the Rust host
|
|
3
|
+
* on the UI thread. JS declares values, curves and bindings once, and a
|
|
4
|
+
* blocked JS thread stops none of it. The same component file animates in the
|
|
5
|
+
* browser, where aurora's DOM renderer hands the curves to the Web Animations
|
|
6
|
+
* API.
|
|
7
|
+
*/
|
|
8
|
+
import {
|
|
9
|
+
AuroraError,
|
|
10
|
+
type MotionDriver,
|
|
11
|
+
type MotionStyle,
|
|
12
|
+
type MotionValue,
|
|
13
|
+
readMotionStyle,
|
|
14
|
+
setMotionDriver,
|
|
15
|
+
} from "@c9up/aurora/core";
|
|
16
|
+
import { OrbitError } from "./errors.js";
|
|
17
|
+
import { currentHost } from "./host.js";
|
|
18
|
+
|
|
19
|
+
export {
|
|
20
|
+
MotionNode,
|
|
21
|
+
type MotionStyle,
|
|
22
|
+
MotionValue,
|
|
23
|
+
type SpringOptions,
|
|
24
|
+
type TimingOptions,
|
|
25
|
+
} from "@c9up/aurora/core";
|
|
26
|
+
|
|
27
|
+
/** aurora's motion values, held and played by orbit's native host. */
|
|
28
|
+
const nativeDriver: MotionDriver = {
|
|
29
|
+
value: (tag, initial) => currentHost().motionValue(tag, initial),
|
|
30
|
+
set: (tag, value) => currentHost().motionSet(tag, value),
|
|
31
|
+
stop: (tag) => currentHost().motionStop(tag),
|
|
32
|
+
frames: (tag, frames, to, loop) =>
|
|
33
|
+
currentHost().motionFrames(tag, [...frames], to, loop),
|
|
34
|
+
spring: (tag, to, stiffness, damping, mass) =>
|
|
35
|
+
currentHost().motionSpring(tag, to, stiffness, damping, mass),
|
|
36
|
+
interpolate: (tag, input, from, to) =>
|
|
37
|
+
currentHost().motionInterpolate(tag, input, from, to),
|
|
38
|
+
dispose: (tag) => currentHost().motionDispose(tag),
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/** @internal `useHost` installs it: motion values made from then on live in the host. */
|
|
42
|
+
export function useNativeMotion(): void {
|
|
43
|
+
setMotionDriver(nativeDriver);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const PROPS = {
|
|
47
|
+
opacity: 0,
|
|
48
|
+
translateX: 1,
|
|
49
|
+
translateY: 2,
|
|
50
|
+
scale: 3,
|
|
51
|
+
visible: 4,
|
|
52
|
+
} as const;
|
|
53
|
+
const KEYS = [
|
|
54
|
+
"opacity",
|
|
55
|
+
"translateX",
|
|
56
|
+
"translateY",
|
|
57
|
+
"scale",
|
|
58
|
+
"visible",
|
|
59
|
+
] as const;
|
|
60
|
+
|
|
61
|
+
/** Wire `style` to the view `viewTag`; call once the view is committed. */
|
|
62
|
+
export function bindMotion(viewTag: number, style: MotionStyle): void {
|
|
63
|
+
for (const key of KEYS) {
|
|
64
|
+
const node = style[key];
|
|
65
|
+
if (node) currentHost().motionBind(viewTag, PROPS[key], node.tag);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Feed `value` from the scroll offset of the scroll view `scrollViewTag`. */
|
|
70
|
+
export function bindScrollY(scrollViewTag: number, value: MotionValue): void {
|
|
71
|
+
currentHost().motionBindScroll(scrollViewTag, value.tag);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Read a `motion=${…}` attribute value, as aurora reads it on the web. */
|
|
75
|
+
export function readMotion(value: unknown): MotionStyle {
|
|
76
|
+
try {
|
|
77
|
+
return readMotionStyle(value);
|
|
78
|
+
} catch (error) {
|
|
79
|
+
if (error instanceof AuroraError) {
|
|
80
|
+
throw new OrbitError("E_ORBIT_INVALID_MOTION", error.message, {
|
|
81
|
+
cause: error,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
}
|