@tarve/headless 0.7.0 → 0.8.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/README.md +34 -4
- package/package.json +4 -3
- package/src/bridge.ts +35 -0
- package/src/index.ts +3 -0
- package/src/png.ts +7 -3
- package/src/runtime.ts +42 -14
- package/src/tree.ts +106 -0
- package/src/wasm.d.ts +4 -0
- package/wasm/baseline/tarve_web.d.ts +155 -0
- package/wasm/baseline/tarve_web.js +611 -0
- package/wasm/baseline/tarve_web_bg.wasm +0 -0
- package/wasm/baseline/tarve_web_bg.wasm.d.ts +36 -0
- package/wasm/tarve_web.d.ts +17 -4
- package/wasm/tarve_web.js +68 -14
- package/wasm/tarve_web_bg.wasm +0 -0
- package/wasm/tarve_web_bg.wasm.d.ts +5 -3
package/README.md
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Render Tarve apps to pixels with no window, no GPU and no native library. It runs the
|
|
4
4
|
WebAssembly build of Tarve's native tree — the same Taffy layout, Parley text, input handling
|
|
5
|
-
and Vello CPU painter a window uses — inside Bun, so UI tests and
|
|
6
|
-
CI machine (Linux, macOS, Windows, x64 or ARM).
|
|
5
|
+
and Vello CPU painter a window uses — inside Bun, Node.js (26.10+) or txiki.js, so UI tests and
|
|
6
|
+
visual snapshots run on any CI machine (Linux, macOS, Windows, x64 or ARM). See [Runtimes](#runtimes).
|
|
7
7
|
|
|
8
8
|
Text always renders with the bundled Inter and JetBrains Mono, never with system fonts, and the
|
|
9
9
|
clock only moves when you advance it. The same view gives the same bytes on every machine.
|
|
@@ -15,10 +15,11 @@ bun add -d @tarve/headless
|
|
|
15
15
|
## Render to an image
|
|
16
16
|
|
|
17
17
|
```tsx
|
|
18
|
+
import { writeFileSync } from "node:fs";
|
|
18
19
|
import { Button } from "@tarve/core";
|
|
19
20
|
import { renderToPng } from "@tarve/headless";
|
|
20
21
|
|
|
21
|
-
|
|
22
|
+
writeFileSync("button.png", await renderToPng(<Button>Save</Button>, { width: 200, height: 80, scale: 2 }));
|
|
22
23
|
```
|
|
23
24
|
|
|
24
25
|
A view that is not a `Window` is wrapped in one (`width`/`height` default to 640×480, `theme`
|
|
@@ -44,6 +45,11 @@ test("saves", async () => {
|
|
|
44
45
|
`createHeadlessApp(view, options)` returns the `AppHandle` itself, plus `pixels()`, `png()` and
|
|
45
46
|
`settle()` (wait until layout feedback and re-renders stop).
|
|
46
47
|
|
|
48
|
+
`app.bridge.scene()` is the compiled scene tree as the app last sent it (ids, kinds, text, values,
|
|
49
|
+
`control` roles and labels; `sceneVersion` bumps on each change), and `app.bridge.emit(event)`
|
|
50
|
+
delivers a native event to the app as the native tree would. [`@tarve/test`](../test) builds
|
|
51
|
+
Testing Library-style queries on them.
|
|
52
|
+
|
|
47
53
|
## Visual snapshots
|
|
48
54
|
|
|
49
55
|
```tsx
|
|
@@ -80,6 +86,30 @@ follows `performance.now()` like a window.
|
|
|
80
86
|
the main script, as in a window; web-style `/assets/a.png` paths resolve from `assetRoot`.
|
|
81
87
|
- Only the bundled fonts exist unless you register more: Arabic, CJK and emoji text needs a font.
|
|
82
88
|
|
|
89
|
+
## Runtimes
|
|
90
|
+
|
|
91
|
+
The package uses only `node:fs`, `node:path`, `node:zlib` (to read PNG snapshots), `fetch` and
|
|
92
|
+
WebAssembly; PNG encoding deflates in the WebAssembly runtime itself. Its files
|
|
93
|
+
(the WebAssembly runtime, the fonts) are `import … with { type: "file" }` imports, which Tarve
|
|
94
|
+
resolves on every runtime.
|
|
95
|
+
|
|
96
|
+
| Runtime | How | Status |
|
|
97
|
+
|---|---|---|
|
|
98
|
+
| Bun 1.4+ | `bun app.tsx`, `bun test` | Everything |
|
|
99
|
+
| Node.js 26.10+ | `tarve run --runtime node app.tsx` (bundles the TSX and its files) | Everything |
|
|
100
|
+
| txiki.js 26.6 | `tarve build --runtime txiki app.tsx`, then run the executable | Everything |
|
|
101
|
+
|
|
102
|
+
The WebAssembly runtime comes in two builds, chosen by feature detection: `simd` (wasm simd128,
|
|
103
|
+
about twice as fast to paint) and `baseline`, for engines without WebAssembly SIMD such as
|
|
104
|
+
txiki.js's WAMR. They render the same pixels; `(await loadHeadlessRuntime()).build` says which runs.
|
|
105
|
+
|
|
106
|
+
`encodePng` deflates with the WebAssembly runtime, so it needs `await loadHeadlessRuntime()` first
|
|
107
|
+
(`renderToPng`, `png()`, `capture` and `matchImageSnapshot` load it). txiki.js has no synchronous
|
|
108
|
+
`node:zlib` of its own; a Tarve txiki.js executable gets one from its native library, which reads
|
|
109
|
+
the stored PNG snapshots.
|
|
110
|
+
|
|
111
|
+
`bun run smoke:headless-runtimes` runs the same scenarios on all three runtimes.
|
|
112
|
+
|
|
83
113
|
## Building from the repository
|
|
84
114
|
|
|
85
115
|
The WebAssembly runtime is built, not committed:
|
|
@@ -87,6 +117,6 @@ The WebAssembly runtime is built, not committed:
|
|
|
87
117
|
```sh
|
|
88
118
|
rustup target add wasm32-unknown-unknown
|
|
89
119
|
cargo install wasm-bindgen-cli --version 0.2.129
|
|
90
|
-
bun run build:wasm
|
|
120
|
+
bun run build:wasm # wasm/ (simd) and wasm/baseline/
|
|
91
121
|
bun run test:headless
|
|
92
122
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tarve/headless",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Render Tarve UIs to pixels without a window or GPU: tests, visual snapshots and image generation on any platform",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"keywords": [
|
|
@@ -40,9 +40,10 @@
|
|
|
40
40
|
"verify:pack": "bun scripts/verify-pack.ts"
|
|
41
41
|
},
|
|
42
42
|
"peerDependencies": {
|
|
43
|
-
"@tarve/core": "^0.
|
|
43
|
+
"@tarve/core": "^0.8.0"
|
|
44
44
|
},
|
|
45
45
|
"engines": {
|
|
46
|
-
"bun": ">=1.4.0"
|
|
46
|
+
"bun": ">=1.4.0",
|
|
47
|
+
"node": ">=26.10.0"
|
|
47
48
|
}
|
|
48
49
|
}
|
package/src/bridge.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { dirname, extname, isAbsolute, resolve } from "node:path";
|
|
|
3
3
|
import type { NativeBridge, RgbaImage } from "@tarve/core";
|
|
4
4
|
import { encodePng } from "./png";
|
|
5
5
|
import { headlessRuntime, type WebTreeHandle } from "./runtime";
|
|
6
|
+
import { SceneTreeMirror, type SceneNode } from "./tree";
|
|
6
7
|
|
|
7
8
|
type SceneDocument = Parameters<NativeBridge["start"]>[0];
|
|
8
9
|
type NativeEvent = Parameters<Parameters<NativeBridge["start"]>[1]>[0];
|
|
@@ -50,10 +51,42 @@ export class HeadlessBridge implements NativeBridge {
|
|
|
50
51
|
/** Remote images being fetched; `settle` waits for them. */
|
|
51
52
|
private fetching = 0;
|
|
52
53
|
|
|
54
|
+
/** The scene tree the app sent, kept in step with every command (see `scene`). */
|
|
55
|
+
private readonly mirror = new SceneTreeMirror();
|
|
56
|
+
|
|
53
57
|
constructor(private readonly options: HeadlessBridgeOptions = {}) {}
|
|
54
58
|
|
|
59
|
+
/**
|
|
60
|
+
* The compiled scene tree as the app last sent it (`compileTree`'s document root with every
|
|
61
|
+
* later patch applied): ids, kinds, text, values, `control` roles and labels. No layout; use
|
|
62
|
+
* `inspect` for boxes. `undefined` before the app starts.
|
|
63
|
+
*/
|
|
64
|
+
scene(): SceneNode | undefined {
|
|
65
|
+
return this.mirror.root();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Bumped whenever the app sends the tree a change, so callers can cache what they derive from `scene()`. */
|
|
69
|
+
get sceneVersion(): number {
|
|
70
|
+
return this.mirror.version;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Delivers `event` to the app exactly as if the native tree had emitted it: the app dispatches
|
|
75
|
+
* it to the node's handlers (`onClick`, `onChange`, `onKeyDown`…) and re-renders. Prefer
|
|
76
|
+
* `debug` input, which goes through hit testing and focus like a user; use this for events
|
|
77
|
+
* input cannot produce headless.
|
|
78
|
+
*/
|
|
79
|
+
emit(event: NativeEvent): void {
|
|
80
|
+
if (this.closed || !this.onEvent) throw new Error(this.closed ? "The headless app is closed" : "The headless app has not started");
|
|
81
|
+
this.activity++;
|
|
82
|
+
this.mirror.observe(event);
|
|
83
|
+
this.onEvent(event);
|
|
84
|
+
this.queueFlush();
|
|
85
|
+
}
|
|
86
|
+
|
|
55
87
|
start(document: SceneDocument, onEvent: (event: NativeEvent) => void): void {
|
|
56
88
|
this.onEvent = onEvent;
|
|
89
|
+
this.mirror.reset(document.root);
|
|
57
90
|
this.inlineImages(document);
|
|
58
91
|
const { WebTree } = headlessRuntime();
|
|
59
92
|
this.tree = new WebTree(JSON.stringify(document));
|
|
@@ -85,6 +118,7 @@ export class HeadlessBridge implements NativeBridge {
|
|
|
85
118
|
this.emitSoon({ type: "fileDialog", requestId: command.requestId, paths: [], error: "File dialogs are not available headless" });
|
|
86
119
|
return;
|
|
87
120
|
}
|
|
121
|
+
this.mirror.apply(command);
|
|
88
122
|
this.inlineImages(command);
|
|
89
123
|
try {
|
|
90
124
|
this.tree.command(JSON.stringify(command));
|
|
@@ -159,6 +193,7 @@ export class HeadlessBridge implements NativeBridge {
|
|
|
159
193
|
if (events.length > 0) this.activity++;
|
|
160
194
|
for (const event of events) {
|
|
161
195
|
if (this.closed) break;
|
|
196
|
+
this.mirror.observe(event);
|
|
162
197
|
this.onEvent?.(event);
|
|
163
198
|
}
|
|
164
199
|
if (this.options.clock === "realtime") this.scheduleTick();
|
package/src/index.ts
CHANGED
|
@@ -20,6 +20,7 @@ import { encodePng } from "./png";
|
|
|
20
20
|
import { loadHeadlessRuntime, type HeadlessRuntimeOptions } from "./runtime";
|
|
21
21
|
|
|
22
22
|
export { HeadlessBridge, type HeadlessBridgeOptions } from "./bridge";
|
|
23
|
+
export { SceneTreeMirror, type SceneNode } from "./tree";
|
|
23
24
|
export { encodePng } from "./png";
|
|
24
25
|
export {
|
|
25
26
|
loadHeadlessRuntime,
|
|
@@ -113,6 +114,8 @@ export interface ImageSnapshotOptions extends PixelComparisonOptions {
|
|
|
113
114
|
*/
|
|
114
115
|
export async function matchImageSnapshot(image: RgbaImage, file: string, options: ImageSnapshotOptions = {}): Promise<PixelComparison | undefined> {
|
|
115
116
|
const update = options.update ?? process.env.TARVE_UPDATE_SNAPSHOTS === "1";
|
|
117
|
+
// encodePng deflates in the WebAssembly runtime: an image from elsewhere needs it loaded too.
|
|
118
|
+
await loadHeadlessRuntime();
|
|
116
119
|
const write = (path: string) => {
|
|
117
120
|
mkdirSync(dirname(path), { recursive: true });
|
|
118
121
|
writeFileSync(path, encodePng(image.rgba, image.width, image.height));
|
package/src/png.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { headlessRuntime } from "./runtime";
|
|
2
2
|
|
|
3
3
|
const CRC_TABLE = new Uint32Array(256).map((_, n) => {
|
|
4
4
|
let c = n;
|
|
@@ -21,7 +21,11 @@ function chunk(type: string, data: Uint8Array): Buffer {
|
|
|
21
21
|
return out;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
/**
|
|
24
|
+
/**
|
|
25
|
+
* Encodes RGBA8 pixels as an 8-bit truecolour-with-alpha PNG. The image data is deflated by the
|
|
26
|
+
* WebAssembly runtime (the same code on Bun, Node.js and txiki.js), so call and await
|
|
27
|
+
* `loadHeadlessRuntime()` first; every render function already has.
|
|
28
|
+
*/
|
|
25
29
|
export function encodePng(rgba: Uint8Array, width: number, height: number): Uint8Array {
|
|
26
30
|
const stride = width * 4;
|
|
27
31
|
const raw = Buffer.alloc((stride + 1) * height);
|
|
@@ -37,7 +41,7 @@ export function encodePng(rgba: Uint8Array, width: number, height: number): Uint
|
|
|
37
41
|
return Buffer.concat([
|
|
38
42
|
Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
|
|
39
43
|
chunk("IHDR", header),
|
|
40
|
-
chunk("IDAT",
|
|
44
|
+
chunk("IDAT", headlessRuntime().zlib.deflate(raw, 9)),
|
|
41
45
|
chunk("IEND", new Uint8Array()),
|
|
42
46
|
]);
|
|
43
47
|
}
|
package/src/runtime.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
// `with { type: "file" }` is how Tarve apps reach files on every runtime: Bun resolves it to the
|
|
3
|
+
// path, the Node.js bundle (`tarve run --runtime node`) copies the file next to itself and the
|
|
4
|
+
// txiki.js build embeds it (packages/core/esbuild-file-imports.ts, txiki-build.ts).
|
|
5
|
+
import interFont from "../fonts/InterVariable.ttf" with { type: "file" };
|
|
6
|
+
import monoFont from "../fonts/JetBrainsMono.ttf" with { type: "file" };
|
|
3
7
|
|
|
4
8
|
/** The part of the wasm-bindgen output this package uses (see native/src/web.rs). */
|
|
5
9
|
export interface WebTreeHandle {
|
|
@@ -17,7 +21,15 @@ export interface WebTreeHandle {
|
|
|
17
21
|
free(): void;
|
|
18
22
|
}
|
|
19
23
|
|
|
20
|
-
|
|
24
|
+
/** zlib streams from the WebAssembly runtime (native/src/zlib.rs): the PNG encoder's, on every engine. */
|
|
25
|
+
export interface HeadlessZlib {
|
|
26
|
+
/** A zlib-wrapped deflate stream of `data` at `level` (0–9). */
|
|
27
|
+
deflate(data: Uint8Array, level: number): Uint8Array;
|
|
28
|
+
/** The bytes of a zlib stream; throws when `data` is not one. */
|
|
29
|
+
inflate(data: Uint8Array): Uint8Array;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface WasmModule extends HeadlessZlib {
|
|
21
33
|
initSync(options: { module: BufferSource }): { memory: WebAssembly.Memory };
|
|
22
34
|
registerFont(bytes: Uint8Array, generics: string): void;
|
|
23
35
|
WebTree: new (document: string) => WebTreeHandle;
|
|
@@ -38,6 +50,12 @@ export interface HeadlessRuntimeOptions {
|
|
|
38
50
|
export interface HeadlessRuntime {
|
|
39
51
|
memory: WebAssembly.Memory;
|
|
40
52
|
WebTree: WasmModule["WebTree"];
|
|
53
|
+
zlib: HeadlessZlib;
|
|
54
|
+
/**
|
|
55
|
+
* Which build runs: `simd` (wasm simd128, about twice as fast to paint) where the engine has
|
|
56
|
+
* WebAssembly SIMD, else `baseline`. Both render the same pixels.
|
|
57
|
+
*/
|
|
58
|
+
build: "simd" | "baseline";
|
|
41
59
|
}
|
|
42
60
|
|
|
43
61
|
const SANS = ["system-ui", "ui-sans-serif", "sans-serif", "serif", "ui-serif", "cursive", "fantasy", "emoji"];
|
|
@@ -47,8 +65,23 @@ let runtime: Promise<HeadlessRuntime> | undefined;
|
|
|
47
65
|
let loaded: HeadlessRuntime | undefined;
|
|
48
66
|
let wasmModule: WasmModule | undefined;
|
|
49
67
|
|
|
50
|
-
|
|
51
|
-
|
|
68
|
+
/** The smallest module with a v128 value: engines without WebAssembly SIMD (txiki.js's WAMR) reject it. */
|
|
69
|
+
const SIMD_PROBE = new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0, 1, 5, 1, 96, 0, 1, 123, 3, 2, 1, 0, 10, 10, 1, 8, 0, 65, 0, 253, 15, 253, 98, 11]);
|
|
70
|
+
|
|
71
|
+
type FileModule = { default: string };
|
|
72
|
+
|
|
73
|
+
/** The build this engine can compile, chosen by feature detection: the glue module and the path of its binary. */
|
|
74
|
+
async function wasmBuild(): Promise<{ build: HeadlessRuntime["build"]; module: WasmModule; binary: string }> {
|
|
75
|
+
const build = WebAssembly.validate(SIMD_PROBE) ? "simd" : "baseline";
|
|
76
|
+
try {
|
|
77
|
+
// Literal specifiers, so bundlers (Node.js and txiki.js builds) include both builds.
|
|
78
|
+
const [module, binary] = await (build === "simd"
|
|
79
|
+
? Promise.all([import("../wasm/tarve_web.js"), import("../wasm/tarve_web_bg.wasm", { with: { type: "file" } })])
|
|
80
|
+
: Promise.all([import("../wasm/baseline/tarve_web.js"), import("../wasm/baseline/tarve_web_bg.wasm", { with: { type: "file" } })]));
|
|
81
|
+
return { build, module: module as WasmModule, binary: (binary as unknown as FileModule).default };
|
|
82
|
+
} catch (cause) {
|
|
83
|
+
throw new Error(`@tarve/headless: could not load the ${build} WebAssembly runtime. In the Tarve repository, build it with \`bun run build:wasm\`.`, { cause });
|
|
84
|
+
}
|
|
52
85
|
}
|
|
53
86
|
|
|
54
87
|
/**
|
|
@@ -58,17 +91,12 @@ function asset(path: string): string {
|
|
|
58
91
|
*/
|
|
59
92
|
export function loadHeadlessRuntime(options: HeadlessRuntimeOptions = {}): Promise<HeadlessRuntime> {
|
|
60
93
|
runtime ??= (async () => {
|
|
61
|
-
const
|
|
62
|
-
const binary = asset("../wasm/tarve_web_bg.wasm");
|
|
63
|
-
if (!existsSync(glue) || !existsSync(binary)) {
|
|
64
|
-
throw new Error("@tarve/headless: the WebAssembly runtime is missing. Run `bun run build:wasm` in the Tarve repository.");
|
|
65
|
-
}
|
|
66
|
-
const module = (await import(glue)) as WasmModule;
|
|
94
|
+
const { build, module, binary } = await wasmBuild();
|
|
67
95
|
const { memory } = module.initSync({ module: readFileSync(binary) });
|
|
68
|
-
module.registerFont(readFileSync(
|
|
69
|
-
module.registerFont(readFileSync(
|
|
96
|
+
module.registerFont(readFileSync(interFont), SANS.join(","));
|
|
97
|
+
module.registerFont(readFileSync(monoFont), MONO.join(","));
|
|
70
98
|
wasmModule = module;
|
|
71
|
-
loaded = { memory, WebTree: module.WebTree };
|
|
99
|
+
loaded = { memory, WebTree: module.WebTree, zlib: { deflate: module.deflate, inflate: module.inflate }, build };
|
|
72
100
|
return loaded;
|
|
73
101
|
})();
|
|
74
102
|
return runtime.then((ready) => {
|
package/src/tree.ts
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import type { NativeBridge } from "@tarve/core";
|
|
2
|
+
|
|
3
|
+
type SceneDocument = Parameters<NativeBridge["start"]>[0];
|
|
4
|
+
type NativeCommand = Parameters<NativeBridge["send"]>[0];
|
|
5
|
+
type NativeEvent = Parameters<Parameters<NativeBridge["start"]>[1]>[0];
|
|
6
|
+
/** A node of the compiled scene tree, as `compileTree` produced it and the app sent it. */
|
|
7
|
+
export type SceneNode = SceneDocument["root"];
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The scene tree as the app last sent it to the bridge: the start document with every `update`,
|
|
11
|
+
* `patch` and `mutate` command applied. It is what the native tree holds (ids, kinds, text,
|
|
12
|
+
* values, `control` roles and labels, `labelledBy`), without layout.
|
|
13
|
+
*/
|
|
14
|
+
export class SceneTreeMirror {
|
|
15
|
+
/** Each node without its children; `children` holds their ids. */
|
|
16
|
+
private readonly nodes = new Map<string, SceneNode>();
|
|
17
|
+
private readonly children = new Map<string, string[]>();
|
|
18
|
+
private rootId: string | undefined;
|
|
19
|
+
private built: SceneNode | undefined;
|
|
20
|
+
/** Bumped on every applied change. */
|
|
21
|
+
version = 0;
|
|
22
|
+
|
|
23
|
+
reset(root: SceneNode): void {
|
|
24
|
+
this.nodes.clear();
|
|
25
|
+
this.children.clear();
|
|
26
|
+
this.rootId = root.id;
|
|
27
|
+
this.add(root);
|
|
28
|
+
this.changed();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
apply(command: NativeCommand): void {
|
|
32
|
+
switch (command.type) {
|
|
33
|
+
case "update":
|
|
34
|
+
this.reset(command.root);
|
|
35
|
+
return;
|
|
36
|
+
case "patch":
|
|
37
|
+
for (const node of command.nodes) this.patch(node);
|
|
38
|
+
break;
|
|
39
|
+
case "mutate":
|
|
40
|
+
for (const mutation of command.mutations) {
|
|
41
|
+
if (mutation.type === "create") this.add(mutation.node);
|
|
42
|
+
else if (mutation.type === "patch") this.patch(mutation.node);
|
|
43
|
+
else if (mutation.type === "children") this.children.set(mutation.id, [...mutation.children]);
|
|
44
|
+
else {
|
|
45
|
+
this.nodes.delete(mutation.id);
|
|
46
|
+
this.children.delete(mutation.id);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
break;
|
|
50
|
+
default:
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
this.changed();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Edits the native tree made itself and reported: typing changes an input's `value`, a slider's
|
|
58
|
+
* `control.value`. The app folds them into its shadow tree without sending a patch back.
|
|
59
|
+
*/
|
|
60
|
+
observe(event: NativeEvent): void {
|
|
61
|
+
if (event.type !== "change" && event.type !== "valueChange") return;
|
|
62
|
+
const node = this.nodes.get(event.id);
|
|
63
|
+
if (!node) return;
|
|
64
|
+
if (event.type === "change" && (node.kind === "input" || node.kind === "textarea")) {
|
|
65
|
+
this.nodes.set(event.id, { ...node, value: event.value });
|
|
66
|
+
} else if (event.type === "valueChange" && node.control) {
|
|
67
|
+
this.nodes.set(event.id, { ...node, control: { ...node.control, value: event.value } });
|
|
68
|
+
} else {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
this.changed();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The whole tree, nested; `undefined` before the app started. */
|
|
75
|
+
root(): SceneNode | undefined {
|
|
76
|
+
if (this.built || this.rootId === undefined) return this.built;
|
|
77
|
+
const build = (id: string): SceneNode | undefined => {
|
|
78
|
+
const node = this.nodes.get(id);
|
|
79
|
+
if (!node) return undefined;
|
|
80
|
+
const kids: SceneNode[] = [];
|
|
81
|
+
for (const child of this.children.get(id) ?? []) {
|
|
82
|
+
const built = build(child);
|
|
83
|
+
if (built) kids.push(built);
|
|
84
|
+
}
|
|
85
|
+
return { ...node, children: kids };
|
|
86
|
+
};
|
|
87
|
+
return (this.built = build(this.rootId));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
private changed(): void {
|
|
91
|
+
this.built = undefined;
|
|
92
|
+
this.version++;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
private add(node: SceneNode): void {
|
|
96
|
+
this.nodes.set(node.id, { ...node, children: [] });
|
|
97
|
+
this.children.set(node.id, node.children.map((child) => child.id));
|
|
98
|
+
for (const child of node.children) this.add(child);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Patches carry a node's own properties; its children stay. */
|
|
102
|
+
private patch(node: SceneNode): void {
|
|
103
|
+
this.nodes.set(node.id, { ...node, children: [] });
|
|
104
|
+
if (!this.children.has(node.id)) this.children.set(node.id, node.children.map((child) => child.id));
|
|
105
|
+
}
|
|
106
|
+
}
|
package/src/wasm.d.ts
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
|
|
4
|
+
export class WebTree {
|
|
5
|
+
free(): void;
|
|
6
|
+
[Symbol.dispose](): void;
|
|
7
|
+
/**
|
|
8
|
+
* Moves the native clock to `now_ms` without stepping motion. Call before `command` so
|
|
9
|
+
* transitions started by a patch begin now, not at the last painted frame.
|
|
10
|
+
*/
|
|
11
|
+
advanceClock(now_ms: number): void;
|
|
12
|
+
blur(): void;
|
|
13
|
+
/**
|
|
14
|
+
* Applies a JSON `NativeCommand`, including the diagnostic `inspect`, `input` and
|
|
15
|
+
* `motionAdvance` commands. Commands that need a desktop window are ignored.
|
|
16
|
+
*/
|
|
17
|
+
command(command: string): void;
|
|
18
|
+
/**
|
|
19
|
+
* CSS cursor for the hovered node: `text`, `col-resize`/`row-resize`, `pointer` or `default`.
|
|
20
|
+
*/
|
|
21
|
+
cursor(): string;
|
|
22
|
+
deviceHeight(): number;
|
|
23
|
+
deviceWidth(): number;
|
|
24
|
+
/**
|
|
25
|
+
* Decodes a fetched remote image, or records why it failed when `error` is set.
|
|
26
|
+
*/
|
|
27
|
+
finishRemoteImage(url: string, bytes: Uint8Array, error?: string | null): void;
|
|
28
|
+
/**
|
|
29
|
+
* Advances motion to `now_ms`, lays out and paints when something changed.
|
|
30
|
+
* Returns true when `pixels()` holds a new frame.
|
|
31
|
+
*/
|
|
32
|
+
frame(now_ms: number): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Sends a named key (`Enter`, `ShiftArrowLeft`, `Escape`…), as the desktop runtime does.
|
|
35
|
+
* Returns true when the tree handled it.
|
|
36
|
+
*/
|
|
37
|
+
key(name: string): boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Creates a tree from a `SceneDocument` JSON string.
|
|
40
|
+
*/
|
|
41
|
+
constructor(document: string);
|
|
42
|
+
/**
|
|
43
|
+
* Milliseconds until motion, caret blink or another clock needs a frame, or -1 when idle.
|
|
44
|
+
*/
|
|
45
|
+
nextTickMs(): number;
|
|
46
|
+
pixelsLen(): number;
|
|
47
|
+
/**
|
|
48
|
+
* Address of the last frame's opaque RGBA8 pixels in wasm memory, so the page can
|
|
49
|
+
* hand them to the canvas without copying.
|
|
50
|
+
*/
|
|
51
|
+
pixelsPtr(): number;
|
|
52
|
+
pointerContext(): void;
|
|
53
|
+
pointerDown(): void;
|
|
54
|
+
pointerLeave(): void;
|
|
55
|
+
pointerMove(x: number, y: number): void;
|
|
56
|
+
pointerUp(): void;
|
|
57
|
+
/**
|
|
58
|
+
* Sets the logical size and device pixel ratio of the canvas.
|
|
59
|
+
*/
|
|
60
|
+
resize(width: number, height: number, scale: number): void;
|
|
61
|
+
selectedText(): string | undefined;
|
|
62
|
+
/**
|
|
63
|
+
* Drains pending native events as a JSON array.
|
|
64
|
+
*/
|
|
65
|
+
takeEvents(): string;
|
|
66
|
+
/**
|
|
67
|
+
* Remote (`http(s)://`) image URLs painted since the last call, as a JSON array.
|
|
68
|
+
* The host fetches them and hands the bytes to `finishRemoteImage`.
|
|
69
|
+
*/
|
|
70
|
+
takeRemoteImageRequests(): string;
|
|
71
|
+
typeText(text: string): void;
|
|
72
|
+
wheel(delta_x: number, delta_y: number): void;
|
|
73
|
+
/**
|
|
74
|
+
* The `windowAction` (`minimize`, `toggleMaximize`, `toggleFullscreen`, `close`) of a node, if it has one.
|
|
75
|
+
*/
|
|
76
|
+
windowAction(id: string): string | undefined;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* A zlib-wrapped deflate stream of `data` at `level` (0–9): @tarve/headless's PNG encoder,
|
|
81
|
+
* the same miniz_oxide the native library gives txiki.js and Android (src/zlib.rs).
|
|
82
|
+
*/
|
|
83
|
+
export function deflate(data: Uint8Array, level: number): Uint8Array;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The bytes of a zlib stream; throws when `data` is not one.
|
|
87
|
+
*/
|
|
88
|
+
export function inflate(data: Uint8Array): Uint8Array;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Registers a TrueType/OpenType font for every tree created afterwards.
|
|
92
|
+
* `generics` is a comma-separated list of CSS generic families (`system-ui,sans-serif`).
|
|
93
|
+
*/
|
|
94
|
+
export function registerFont(bytes: Uint8Array, generics: string): void;
|
|
95
|
+
|
|
96
|
+
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
|
|
97
|
+
|
|
98
|
+
export interface InitOutput {
|
|
99
|
+
readonly memory: WebAssembly.Memory;
|
|
100
|
+
readonly __wbg_webtree_free: (a: number, b: number) => void;
|
|
101
|
+
readonly deflate: (a: number, b: number, c: number, d: number) => void;
|
|
102
|
+
readonly inflate: (a: number, b: number, c: number) => void;
|
|
103
|
+
readonly registerFont: (a: number, b: number, c: number, d: number) => void;
|
|
104
|
+
readonly webtree_advanceClock: (a: number, b: number) => void;
|
|
105
|
+
readonly webtree_blur: (a: number) => void;
|
|
106
|
+
readonly webtree_command: (a: number, b: number, c: number, d: number) => void;
|
|
107
|
+
readonly webtree_cursor: (a: number, b: number) => void;
|
|
108
|
+
readonly webtree_deviceHeight: (a: number) => number;
|
|
109
|
+
readonly webtree_deviceWidth: (a: number) => number;
|
|
110
|
+
readonly webtree_finishRemoteImage: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => void;
|
|
111
|
+
readonly webtree_frame: (a: number, b: number, c: number) => void;
|
|
112
|
+
readonly webtree_key: (a: number, b: number, c: number) => number;
|
|
113
|
+
readonly webtree_new: (a: number, b: number, c: number) => void;
|
|
114
|
+
readonly webtree_nextTickMs: (a: number) => number;
|
|
115
|
+
readonly webtree_pixelsLen: (a: number) => number;
|
|
116
|
+
readonly webtree_pixelsPtr: (a: number) => number;
|
|
117
|
+
readonly webtree_pointerContext: (a: number) => void;
|
|
118
|
+
readonly webtree_pointerDown: (a: number) => void;
|
|
119
|
+
readonly webtree_pointerLeave: (a: number) => void;
|
|
120
|
+
readonly webtree_pointerMove: (a: number, b: number, c: number) => void;
|
|
121
|
+
readonly webtree_pointerUp: (a: number) => void;
|
|
122
|
+
readonly webtree_resize: (a: number, b: number, c: number, d: number) => void;
|
|
123
|
+
readonly webtree_selectedText: (a: number, b: number) => void;
|
|
124
|
+
readonly webtree_takeEvents: (a: number, b: number) => void;
|
|
125
|
+
readonly webtree_takeRemoteImageRequests: (a: number, b: number) => void;
|
|
126
|
+
readonly webtree_typeText: (a: number, b: number, c: number) => void;
|
|
127
|
+
readonly webtree_wheel: (a: number, b: number, c: number) => void;
|
|
128
|
+
readonly webtree_windowAction: (a: number, b: number, c: number, d: number) => void;
|
|
129
|
+
readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
|
|
130
|
+
readonly __wbindgen_export: (a: number, b: number) => number;
|
|
131
|
+
readonly __wbindgen_export2: (a: number, b: number, c: number) => void;
|
|
132
|
+
readonly __wbindgen_export3: (a: number, b: number, c: number, d: number) => number;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export type SyncInitInput = BufferSource | WebAssembly.Module;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Instantiates the given `module`, which can either be bytes or
|
|
139
|
+
* a precompiled `WebAssembly.Module`.
|
|
140
|
+
*
|
|
141
|
+
* @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
|
|
142
|
+
*
|
|
143
|
+
* @returns {InitOutput}
|
|
144
|
+
*/
|
|
145
|
+
export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
|
|
149
|
+
* for everything else, calls `WebAssembly.instantiate` directly.
|
|
150
|
+
*
|
|
151
|
+
* @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
|
|
152
|
+
*
|
|
153
|
+
* @returns {Promise<InitOutput>}
|
|
154
|
+
*/
|
|
155
|
+
export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
|