react-native-roku 0.0.0-stage → 0.2.0-preview.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.
- package/CHANGELOG.md +318 -0
- package/CONTRIBUTING.md +34 -0
- package/LICENSE +21 -0
- package/NOTICE.md +10 -0
- package/README.md +66 -2
- package/bin/react-native-roku.mjs +91 -0
- package/build/assets.cjs +311 -0
- package/build/assets.d.cts +50 -0
- package/build/compatibility.d.mts +40 -0
- package/build/compatibility.mjs +272 -0
- package/build/dev.d.mts +34 -0
- package/build/dev.mjs +297 -0
- package/build/devWorkspace.d.mts +11 -0
- package/build/devWorkspace.mjs +86 -0
- package/build/device.d.mts +47 -0
- package/build/device.mjs +310 -0
- package/build/environment.d.mts +29 -0
- package/build/environment.mjs +144 -0
- package/build/esbuild.d.mts +5 -0
- package/build/esbuild.mjs +118 -0
- package/build/fontVariants.d.mts +27 -0
- package/build/fontVariants.mjs +424 -0
- package/build/frontend.d.mts +52 -0
- package/build/frontend.mjs +75 -0
- package/build/identity.mjs +45 -0
- package/build/index.d.mts +22 -0
- package/build/index.mjs +87 -0
- package/build/intl.mjs +121 -0
- package/build/metro.cjs +38 -0
- package/build/metro.d.cts +37 -0
- package/build/metroFrontend.mjs +222 -0
- package/build/metroMetadata.mjs +119 -0
- package/build/metroModules.mjs +397 -0
- package/build/metroRefresh.d.mts +64 -0
- package/build/metroRefresh.mjs +346 -0
- package/build/metroTransformer.cjs +146 -0
- package/build/nativeTextMemo.mjs +296 -0
- package/build/nativewind.d.mts +9 -0
- package/build/nativewind.mjs +92 -0
- package/build/project.d.mts +111 -0
- package/build/project.mjs +615 -0
- package/build/reactCompiler.mjs +113 -0
- package/build/reactProfile.cjs +35 -0
- package/build/refreshRuntime.mjs +187 -0
- package/build/refreshSourceMap.mjs +53 -0
- package/build/release.mjs +99 -0
- package/build/resolution.d.mts +3 -0
- package/build/resolution.mjs +25 -0
- package/build/surfaces.d.mts +1 -0
- package/build/surfaces.mjs +42 -0
- package/build/testing/node.d.mts +29 -0
- package/build/testing/node.mjs +106 -0
- package/build/yoga.d.mts +8 -0
- package/build/yoga.mjs +186 -0
- package/compatibility/public-api.json +327 -0
- package/compatibility/registry.json +2258 -0
- package/config/release.json +15 -0
- package/config/toolchain.json +34 -0
- package/docs/compatibility.md +813 -0
- package/docs/getting-started.md +460 -0
- package/docs/maintenance.md +362 -0
- package/docs/qualification.md +241 -0
- package/docs/standalone-testing.md +56 -0
- package/examples/hello-roku/README.md +35 -0
- package/examples/hello-roku/package.template.json +31 -0
- package/examples/hello-roku/pnpm-workspace.yaml +11 -0
- package/examples/hello-roku/roku.config.json +22 -0
- package/examples/hello-roku/src/App.tsx +54 -0
- package/examples/hello-roku/src/Catalog.tsx +200 -0
- package/examples/hello-roku/src/assets.d.ts +16 -0
- package/examples/hello-roku/src/catalog-cover.png +0 -0
- package/examples/hello-roku/tsconfig.json +1 -0
- package/examples/hello-roku/tsconfig.react.json +1 -0
- package/native/components/RNRAppControl.brs +215 -0
- package/native/components/RNRAppControl.xml +13 -0
- package/native/components/RNRDeviceFacts.brs +19 -0
- package/native/components/RNRDeviceFacts.xml +9 -0
- package/native/components/RNRDeviceFactsTask.brs +74 -0
- package/native/components/RNRDeviceFactsTask.xml +9 -0
- package/native/components/RNRTextRaster.brs +15 -0
- package/native/components/RNRTextRaster.xml +9 -0
- package/native/components/RNRTextRasterTask.brs +77 -0
- package/native/components/RNRTextRasterTask.xml +9 -0
- package/native/manifest.json +7 -0
- package/package.json +183 -3
- package/src/animation/clock.ts +10 -0
- package/src/animation/derivedValue.ts +163 -0
- package/src/animation/legacyAnimated.ts +255 -0
- package/src/animation/reanimated.ts +243 -0
- package/src/animation/sharedValue.ts +285 -0
- package/src/animation/worklets.ts +23 -0
- package/src/application/AppRegistry.ts +31 -0
- package/src/application/mountApplication.ts +136 -0
- package/src/graphics/BitmapLayer.tsx +68 -0
- package/src/graphics/RoundedBorder.tsx +116 -0
- package/src/graphics/TextRasterCache.ts +259 -0
- package/src/graphics/gradient.ts +37 -0
- package/src/graphics/inlineRaster.ts +85 -0
- package/src/graphics/png.ts +77 -0
- package/src/graphics/rasterPixels.ts +89 -0
- package/src/graphics/surfaces.ts +105 -0
- package/src/graphics/textRaster.types.ts +60 -0
- package/src/graphics/textRasterGeometry.ts +116 -0
- package/src/host/host.types.ts +57 -0
- package/src/index.ts +22 -0
- package/src/interop/nativewind.ts +2 -0
- package/src/layout/FontResources.ts +252 -0
- package/src/layout/affine.ts +89 -0
- package/src/layout/flexLayout.ts +398 -0
- package/src/layout/fontFaces.d.mts +19 -0
- package/src/layout/fontFaces.mjs +85 -0
- package/src/layout/fontVariants.d.mts +2 -0
- package/src/layout/fontVariants.mjs +8 -0
- package/src/layout/inlineParagraph.ts +158 -0
- package/src/layout/inlineText.ts +68 -0
- package/src/layout/layoutReuse.ts +117 -0
- package/src/layout/nativeNodeFields.ts +179 -0
- package/src/layout/preparedFields.ts +78 -0
- package/src/layout/textLayout.ts +138 -0
- package/src/layout/transform.ts +113 -0
- package/src/media/SceneGraphAudio.ts +161 -0
- package/src/media/SceneGraphVideo.ts +167 -0
- package/src/media/audioSession.ts +84 -0
- package/src/media/index.ts +2 -0
- package/src/media/videoErrorDetails.ts +40 -0
- package/src/media/videoSession.ts +136 -0
- package/src/navigation/RouterStore.ts +244 -0
- package/src/navigation/expoRouter.ts +115 -0
- package/src/react-native/NativeInput.ts +494 -0
- package/src/react-native/NativeSystemState.ts +95 -0
- package/src/react-native/accessibility.ts +265 -0
- package/src/react-native/activityIndicator.ts +72 -0
- package/src/react-native/compilerPressable.ts +26 -0
- package/src/react-native/createNativeRoot.ts +2126 -0
- package/src/react-native/directionalFocus.ts +55 -0
- package/src/react-native/environment.ts +275 -0
- package/src/react-native/flatList.ts +1001 -0
- package/src/react-native/flatListViewability.ts +231 -0
- package/src/react-native/focusEvents.ts +85 -0
- package/src/react-native/image.ts +247 -0
- package/src/react-native/index.ts +54 -0
- package/src/react-native/interactionManager.ts +76 -0
- package/src/react-native/keyboard.ts +60 -0
- package/src/react-native/linearGradient.ts +94 -0
- package/src/react-native/modal.tsx +148 -0
- package/src/react-native/nativeHostContext.ts +80 -0
- package/src/react-native/nativeModules.ts +86 -0
- package/src/react-native/nativeRoot.types.ts +22 -0
- package/src/react-native/nativeViewHandle.ts +191 -0
- package/src/react-native/nativewindPressable.ts +38 -0
- package/src/react-native/panResponder.ts +73 -0
- package/src/react-native/primitives.ts +685 -0
- package/src/react-native/safeArea.ts +76 -0
- package/src/react-native/scrollView.ts +289 -0
- package/src/react-native/styleInterop.ts +8 -0
- package/src/react-native/styleSheet.ts +54 -0
- package/src/react-native/textInput.ts +334 -0
- package/src/react-native/unsupported.ts +25 -0
- package/src/renderer/SceneRoot.ts +1006 -0
- package/src/scene/SdkDeviceFacts.ts +151 -0
- package/src/scene/SdkDeviceServices.ts +72 -0
- package/src/scene/SdkSceneGraph.ts +124 -0
- package/src/scene/SdkTextEntry.ts +321 -0
- package/src/scene/SdkTextMetrics.ts +136 -0
- package/src/scene/SdkTextRasterTransport.ts +191 -0
- package/src/scene/SdkWebTransport.ts +122 -0
- package/src/scene/createRokuHost.ts +213 -0
- package/src/scene/sceneGraph.types.ts +38 -0
- package/src/services/DeviceServices.ts +53 -0
- package/src/services/RegistryStorage.ts +85 -0
- package/src/services/RokuKeySession.ts +29 -0
- package/src/services/asyncStorage.ts +23 -0
- package/src/services/base64.ts +52 -0
- package/src/services/crypto.ts +18 -0
- package/src/services/expoConstants.ts +11 -0
- package/src/services/expoLinking.ts +12 -0
- package/src/services/expoStatusBar.ts +4 -0
- package/src/services/installWebRuntime.ts +105 -0
- package/src/services/intlEnglish.ts +54 -0
- package/src/services/languageRuntime.ts +65 -0
- package/src/services/nativeTimers.ts +96 -0
- package/src/services/secureStore.ts +23 -0
- package/src/services/textEntryRequest.ts +22 -0
- package/src/services/utf8.ts +57 -0
- package/src/services/webRuntime.ts +346 -0
- package/src/testing/CanvasResources.ts +141 -0
- package/src/testing/CanvasSceneGraph.ts +496 -0
- package/src/testing/CanvasTextEntry.ts +237 -0
- package/src/testing/PreviewTextObservations.ts +82 -0
- package/src/testing/RecordingSceneGraph.ts +136 -0
- package/src/types/normalize-colors.d.ts +4 -0
- package/src/vector/dynamicSvg.tsx +226 -0
- package/src/vector/svg.ts +222 -0
- package/src/vector/svg.types.ts +4 -0
- package/src/vector/svgElements.ts +58 -0
- package/src/vector/svgKey.d.mts +4 -0
- package/src/vector/svgKey.mjs +16 -0
- package/src/vector/svgPath.ts +157 -0
- package/src/vector/svgPixels.ts +133 -0
- package/tsconfig.consumer.json +29 -0
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import type { TextEntryPort } from "../services/DeviceServices";
|
|
2
|
+
import { validateTextEntryRequest, validateTextEntryText } from "../services/textEntryRequest";
|
|
3
|
+
import { RokuHostError } from "../scene/sceneGraph.types";
|
|
4
|
+
|
|
5
|
+
export interface CanvasTextEntryPort extends TextEntryPort {
|
|
6
|
+
isOpen(): boolean;
|
|
7
|
+
dispose(): void;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** Browser IO adapter; the application retains validation and focus policy. */
|
|
11
|
+
export function createCanvasTextEntry(owner: Document = document): CanvasTextEntryPort {
|
|
12
|
+
const editors = new Set<{ dialog: HTMLDialogElement; abandon(): void }>();
|
|
13
|
+
let disposed = false;
|
|
14
|
+
return {
|
|
15
|
+
isOpen: () => [...editors].some((entry) => entry.dialog.open),
|
|
16
|
+
open(request) {
|
|
17
|
+
if (disposed || [...editors].some((entry) => entry.dialog.open) || editors.size >= 32)
|
|
18
|
+
throw new RokuHostError("C07_TEXT_ENTRY", "Text entry is unavailable or already active");
|
|
19
|
+
validateTextEntryRequest(request);
|
|
20
|
+
const dialog = owner.createElement("dialog"),
|
|
21
|
+
form = owner.createElement("form"),
|
|
22
|
+
label = owner.createElement("label"),
|
|
23
|
+
input = owner.createElement("input"),
|
|
24
|
+
accept = owner.createElement("button"),
|
|
25
|
+
cancel = owner.createElement("button");
|
|
26
|
+
form.method = "dialog";
|
|
27
|
+
// keyboardType selects an editor domain; product validation stays in React.
|
|
28
|
+
form.noValidate = true;
|
|
29
|
+
label.textContent = request.title;
|
|
30
|
+
input.value = request.text;
|
|
31
|
+
input.maxLength = request.maxLength;
|
|
32
|
+
input.type = request.secure ? "password" : request.domain === "email" ? "email" : "text";
|
|
33
|
+
input.inputMode =
|
|
34
|
+
request.domain === "numeric" ? "numeric" : request.domain === "email" ? "email" : "text";
|
|
35
|
+
input.autocomplete = "off";
|
|
36
|
+
label.append(input);
|
|
37
|
+
accept.textContent = "OK";
|
|
38
|
+
accept.type = "submit";
|
|
39
|
+
cancel.textContent = "Cancel";
|
|
40
|
+
cancel.type = "button";
|
|
41
|
+
form.append(label, accept, cancel);
|
|
42
|
+
dialog.append(form);
|
|
43
|
+
let resolveResult!: (text: string | null) => void,
|
|
44
|
+
resolveClosed!: (observed: boolean) => void;
|
|
45
|
+
const result = new Promise<string | null>((resolve) => {
|
|
46
|
+
resolveResult = resolve;
|
|
47
|
+
});
|
|
48
|
+
const closed = new Promise<boolean>((resolve) => {
|
|
49
|
+
resolveClosed = resolve;
|
|
50
|
+
});
|
|
51
|
+
let closing = false,
|
|
52
|
+
finished = false,
|
|
53
|
+
settled = false;
|
|
54
|
+
const textListeners = new Set<(text: string) => void>();
|
|
55
|
+
type Selection = Readonly<{ start: number; end: number }>;
|
|
56
|
+
const selectionListeners = new Set<(selection: Selection) => void>();
|
|
57
|
+
let currentText = input.value,
|
|
58
|
+
observedText: string | undefined,
|
|
59
|
+
observedSelection: Selection | undefined,
|
|
60
|
+
selectionRevision = 0,
|
|
61
|
+
revision = 0;
|
|
62
|
+
const settle = (text: string | null) => {
|
|
63
|
+
if (!settled) {
|
|
64
|
+
settled = true;
|
|
65
|
+
resolveResult(text);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
const removers: (() => void)[] = [];
|
|
69
|
+
const finish = (observed: boolean) => {
|
|
70
|
+
if (finished) return;
|
|
71
|
+
finished = true;
|
|
72
|
+
textListeners.clear();
|
|
73
|
+
selectionListeners.clear();
|
|
74
|
+
observedSelection = undefined;
|
|
75
|
+
currentText = "";
|
|
76
|
+
observedText = undefined;
|
|
77
|
+
settle(null);
|
|
78
|
+
input.value = "";
|
|
79
|
+
for (const remove of removers) remove();
|
|
80
|
+
editors.delete(entry);
|
|
81
|
+
dialog.remove();
|
|
82
|
+
resolveClosed(observed);
|
|
83
|
+
};
|
|
84
|
+
const close = (text: string | null) => {
|
|
85
|
+
if (finished || closing) return;
|
|
86
|
+
closing = true;
|
|
87
|
+
textListeners.clear();
|
|
88
|
+
selectionListeners.clear();
|
|
89
|
+
observedSelection = undefined;
|
|
90
|
+
currentText = "";
|
|
91
|
+
observedText = undefined;
|
|
92
|
+
settle(text);
|
|
93
|
+
input.value = "";
|
|
94
|
+
try {
|
|
95
|
+
dialog.close();
|
|
96
|
+
} catch (error) {
|
|
97
|
+
finish(false);
|
|
98
|
+
throw error;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
const entry = {
|
|
102
|
+
dialog,
|
|
103
|
+
abandon() {
|
|
104
|
+
try {
|
|
105
|
+
close(null);
|
|
106
|
+
} finally {
|
|
107
|
+
finish(false);
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
const listen = (target: EventTarget, name: string, handler: EventListener) => {
|
|
112
|
+
target.addEventListener(name, handler);
|
|
113
|
+
removers.push(() => target.removeEventListener(name, handler));
|
|
114
|
+
};
|
|
115
|
+
const notify = <Value>(listener: (value: Value) => void, value: Value) => {
|
|
116
|
+
try {
|
|
117
|
+
listener(value);
|
|
118
|
+
} catch (error) {
|
|
119
|
+
try {
|
|
120
|
+
close(null);
|
|
121
|
+
} catch {
|
|
122
|
+
/* Preserve the observer failure after attempting retirement. */
|
|
123
|
+
}
|
|
124
|
+
throw error;
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
const publishSelection = () => {
|
|
128
|
+
if (finished || closing || input.value !== currentText) return;
|
|
129
|
+
const start = input.selectionStart,
|
|
130
|
+
end = input.selectionEnd;
|
|
131
|
+
// Email and other DOM input types may not expose selection. Never
|
|
132
|
+
// synthesize an end-of-text cursor for those editors.
|
|
133
|
+
if (start === null || end === null || start < 0 || end < start || end > currentText.length)
|
|
134
|
+
return;
|
|
135
|
+
const previous = observedSelection;
|
|
136
|
+
observedSelection = { start, end };
|
|
137
|
+
if (previous?.start === start && previous.end === end) return;
|
|
138
|
+
const publication = ++selectionRevision,
|
|
139
|
+
textRevision = revision;
|
|
140
|
+
for (const listener of [...selectionListeners]) {
|
|
141
|
+
if (finished || closing || textRevision !== revision || publication !== selectionRevision)
|
|
142
|
+
break;
|
|
143
|
+
if (selectionListeners.has(listener)) notify(listener, { start, end });
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
listen(input, "input", () => {
|
|
147
|
+
if (finished || closing) return;
|
|
148
|
+
const text = input.value.slice(0, request.maxLength);
|
|
149
|
+
if (text !== currentText) {
|
|
150
|
+
currentText = text;
|
|
151
|
+
observedText = text;
|
|
152
|
+
observedSelection = undefined;
|
|
153
|
+
const publication = ++revision;
|
|
154
|
+
for (const listener of [...textListeners]) {
|
|
155
|
+
if (finished || closing || revision !== publication) break;
|
|
156
|
+
if (textListeners.has(listener)) notify(listener, text);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
publishSelection();
|
|
160
|
+
});
|
|
161
|
+
for (const name of ["select", "selectionchange", "keyup", "click"])
|
|
162
|
+
listen(input, name, publishSelection);
|
|
163
|
+
listen(owner, "selectionchange", publishSelection);
|
|
164
|
+
listen(form, "submit", (event) => {
|
|
165
|
+
event.preventDefault();
|
|
166
|
+
close(input.value.slice(0, request.maxLength));
|
|
167
|
+
});
|
|
168
|
+
listen(cancel, "click", () => close(null));
|
|
169
|
+
listen(dialog, "cancel", (event) => {
|
|
170
|
+
event.preventDefault();
|
|
171
|
+
close(null);
|
|
172
|
+
});
|
|
173
|
+
listen(dialog, "keydown", (event) => event.stopPropagation());
|
|
174
|
+
listen(dialog, "keyup", (event) => event.stopPropagation());
|
|
175
|
+
listen(dialog, "close", () => finish(true));
|
|
176
|
+
editors.add(entry);
|
|
177
|
+
try {
|
|
178
|
+
owner.body.append(dialog);
|
|
179
|
+
dialog.showModal();
|
|
180
|
+
input.focus();
|
|
181
|
+
publishSelection();
|
|
182
|
+
} catch (error) {
|
|
183
|
+
finish(false);
|
|
184
|
+
throw error;
|
|
185
|
+
}
|
|
186
|
+
return {
|
|
187
|
+
result,
|
|
188
|
+
closed,
|
|
189
|
+
setText(text) {
|
|
190
|
+
validateTextEntryText(text);
|
|
191
|
+
if (finished || closing) return false;
|
|
192
|
+
text = text.slice(0, request.maxLength);
|
|
193
|
+
if (text === input.value) {
|
|
194
|
+
if (text !== currentText) observedText = undefined;
|
|
195
|
+
currentText = text;
|
|
196
|
+
publishSelection();
|
|
197
|
+
return true;
|
|
198
|
+
}
|
|
199
|
+
currentText = text;
|
|
200
|
+
observedText = undefined;
|
|
201
|
+
observedSelection = undefined;
|
|
202
|
+
revision++;
|
|
203
|
+
input.value = text;
|
|
204
|
+
publishSelection();
|
|
205
|
+
return true;
|
|
206
|
+
},
|
|
207
|
+
subscribeText(listener) {
|
|
208
|
+
if (finished || closing) return () => {};
|
|
209
|
+
textListeners.add(listener);
|
|
210
|
+
if (observedText !== undefined) notify(listener, observedText);
|
|
211
|
+
return () => textListeners.delete(listener);
|
|
212
|
+
},
|
|
213
|
+
subscribeSelection(listener) {
|
|
214
|
+
if (finished || closing) return () => {};
|
|
215
|
+
selectionListeners.add(listener);
|
|
216
|
+
if (observedSelection !== undefined) notify(listener, { ...observedSelection });
|
|
217
|
+
return () => selectionListeners.delete(listener);
|
|
218
|
+
},
|
|
219
|
+
close: () => close(null),
|
|
220
|
+
};
|
|
221
|
+
},
|
|
222
|
+
dispose() {
|
|
223
|
+
if (disposed) return;
|
|
224
|
+
disposed = true;
|
|
225
|
+
let failure: unknown;
|
|
226
|
+
for (const entry of [...editors]) {
|
|
227
|
+
try {
|
|
228
|
+
entry.abandon();
|
|
229
|
+
} catch (error) {
|
|
230
|
+
failure ??= error;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
editors.clear();
|
|
234
|
+
if (failure) throw failure;
|
|
235
|
+
},
|
|
236
|
+
};
|
|
237
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { TextRasterLease, TextRasterProvider } from "../graphics/textRaster.types";
|
|
2
|
+
|
|
3
|
+
interface TextObservationNode {
|
|
4
|
+
readonly fields: Readonly<Record<string, unknown>>;
|
|
5
|
+
readonly children: readonly TextObservationNode[];
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/** Development observations describe attached, enabled scene text. They do not
|
|
9
|
+
* certify clipping, native presentation, or pixels. Retired raster history must
|
|
10
|
+
* never satisfy a later UI assertion. */
|
|
11
|
+
export function observePreviewText(source: TextRasterProvider) {
|
|
12
|
+
const images = new Map<string, Map<string, number>>();
|
|
13
|
+
let retired = false;
|
|
14
|
+
const track = (lease: TextRasterLease, text: string): TextRasterLease => {
|
|
15
|
+
if (retired) {
|
|
16
|
+
lease.release();
|
|
17
|
+
return { image: lease.image, release() {} };
|
|
18
|
+
}
|
|
19
|
+
const values = images.get(lease.image.uri) ?? new Map<string, number>();
|
|
20
|
+
images.set(lease.image.uri, values);
|
|
21
|
+
values.set(text, (values.get(text) ?? 0) + 1);
|
|
22
|
+
let released = false;
|
|
23
|
+
return {
|
|
24
|
+
image: lease.image,
|
|
25
|
+
release() {
|
|
26
|
+
if (released) return;
|
|
27
|
+
released = true;
|
|
28
|
+
const owners = (values.get(text) ?? 1) - 1;
|
|
29
|
+
if (owners) values.set(text, owners);
|
|
30
|
+
else values.delete(text);
|
|
31
|
+
if (!values.size && images.get(lease.image.uri) === values) images.delete(lease.image.uri);
|
|
32
|
+
lease.release();
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
const assertActive = () => {
|
|
37
|
+
if (retired) throw Error("Preview text observer retired");
|
|
38
|
+
};
|
|
39
|
+
const provider: TextRasterProvider = {
|
|
40
|
+
async acquire(request) {
|
|
41
|
+
assertActive();
|
|
42
|
+
return track(await source.acquire(request), request.layout.lines.join("\n"));
|
|
43
|
+
},
|
|
44
|
+
...(source.acquireInline
|
|
45
|
+
? {
|
|
46
|
+
async acquireInline(
|
|
47
|
+
request: Parameters<NonNullable<TextRasterProvider["acquireInline"]>>[0],
|
|
48
|
+
) {
|
|
49
|
+
assertActive();
|
|
50
|
+
return track(
|
|
51
|
+
await source.acquireInline!(request),
|
|
52
|
+
request.runs.map((run) => run.text).join(""),
|
|
53
|
+
);
|
|
54
|
+
},
|
|
55
|
+
}
|
|
56
|
+
: {}),
|
|
57
|
+
...(source.acquirePixels ? { acquirePixels: source.acquirePixels.bind(source) } : {}),
|
|
58
|
+
dispose() {
|
|
59
|
+
if (retired) return;
|
|
60
|
+
retired = true;
|
|
61
|
+
images.clear();
|
|
62
|
+
source.dispose();
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
return {
|
|
66
|
+
provider,
|
|
67
|
+
snapshot(root: TextObservationNode): string[] {
|
|
68
|
+
if (retired) return [];
|
|
69
|
+
const texts = new Set<string>();
|
|
70
|
+
const visit = (node: TextObservationNode) => {
|
|
71
|
+
if (node.fields.visible === false || node.fields.opacity === 0) return;
|
|
72
|
+
if (typeof node.fields.text === "string" && node.fields.text) texts.add(node.fields.text);
|
|
73
|
+
if (typeof node.fields.uri === "string")
|
|
74
|
+
for (const value of images.get(node.fields.uri)?.keys() ?? [])
|
|
75
|
+
if (value) texts.add(value);
|
|
76
|
+
node.children.forEach(visit);
|
|
77
|
+
};
|
|
78
|
+
visit(root);
|
|
79
|
+
return [...texts];
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import {
|
|
2
|
+
RokuHostError,
|
|
3
|
+
type SceneFieldListener,
|
|
4
|
+
type SceneGraphPort,
|
|
5
|
+
type SceneNodeRef,
|
|
6
|
+
type SceneValue,
|
|
7
|
+
} from "../scene/sceneGraph.types";
|
|
8
|
+
|
|
9
|
+
export interface RecordedSceneNode extends SceneNodeRef {
|
|
10
|
+
readonly fields: Record<string, SceneValue>;
|
|
11
|
+
readonly children: RecordedSceneNode[];
|
|
12
|
+
parent: RecordedSceneNode | null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Host conformance adapter; its output is never device or performance evidence. */
|
|
16
|
+
export class RecordingSceneGraph implements SceneGraphPort {
|
|
17
|
+
readonly root: RecordedSceneNode = {
|
|
18
|
+
id: 0,
|
|
19
|
+
type: "root",
|
|
20
|
+
fields: {},
|
|
21
|
+
children: [],
|
|
22
|
+
parent: null,
|
|
23
|
+
};
|
|
24
|
+
readonly operations: Array<{
|
|
25
|
+
operation: string;
|
|
26
|
+
node: number;
|
|
27
|
+
field?: string;
|
|
28
|
+
value?: unknown;
|
|
29
|
+
}> = [];
|
|
30
|
+
readonly nodes = new Map<number, RecordedSceneNode>([[0, this.root]]);
|
|
31
|
+
private nextId = 0;
|
|
32
|
+
private readonly listeners = new Map<string, Set<SceneFieldListener>>();
|
|
33
|
+
hostCommits = 0;
|
|
34
|
+
|
|
35
|
+
create(type: string): RecordedSceneNode {
|
|
36
|
+
const node: RecordedSceneNode = {
|
|
37
|
+
id: ++this.nextId,
|
|
38
|
+
type,
|
|
39
|
+
fields: {},
|
|
40
|
+
children: [],
|
|
41
|
+
parent: null,
|
|
42
|
+
};
|
|
43
|
+
this.nodes.set(node.id, node);
|
|
44
|
+
this.operations.push({ operation: "create", node: node.id, value: type });
|
|
45
|
+
return node;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
set(ref: SceneNodeRef, field: string, value: SceneValue): void {
|
|
49
|
+
const node = this.lookup(ref);
|
|
50
|
+
node.fields[field] = value;
|
|
51
|
+
this.operations.push({ operation: "set", node: node.id, field, value });
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
insert(parentRef: SceneNodeRef, childRef: SceneNodeRef, beforeRef?: SceneNodeRef): void {
|
|
55
|
+
const parent = this.lookup(parentRef),
|
|
56
|
+
child = this.lookup(childRef);
|
|
57
|
+
if (parent === child) throw new RokuHostError("TREE_CYCLE", "A node cannot own itself");
|
|
58
|
+
for (let ancestor: RecordedSceneNode | null = parent; ancestor; ancestor = ancestor.parent) {
|
|
59
|
+
if (ancestor === child)
|
|
60
|
+
throw new RokuHostError("TREE_CYCLE", "Cannot attach an ancestor as a child");
|
|
61
|
+
}
|
|
62
|
+
if (beforeRef?.id === child.id) return;
|
|
63
|
+
const before = beforeRef && this.lookup(beforeRef);
|
|
64
|
+
if (before && before.parent !== parent)
|
|
65
|
+
throw new RokuHostError("TREE_ANCHOR", "Insertion anchor has another parent");
|
|
66
|
+
if (child.parent) child.parent.children.splice(child.parent.children.indexOf(child), 1);
|
|
67
|
+
parent.children.splice(
|
|
68
|
+
before ? parent.children.indexOf(before) : parent.children.length,
|
|
69
|
+
0,
|
|
70
|
+
child,
|
|
71
|
+
);
|
|
72
|
+
child.parent = parent;
|
|
73
|
+
this.operations.push({
|
|
74
|
+
operation: "insert",
|
|
75
|
+
node: child.id,
|
|
76
|
+
value: { parent: parent.id, before: before?.id },
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
remove(parentRef: SceneNodeRef, childRef: SceneNodeRef): void {
|
|
81
|
+
const parent = this.lookup(parentRef),
|
|
82
|
+
child = this.lookup(childRef);
|
|
83
|
+
if (child.parent !== parent)
|
|
84
|
+
throw new RokuHostError("TREE_PARENT", "Removal parent does not own the node");
|
|
85
|
+
parent.children.splice(parent.children.indexOf(child), 1);
|
|
86
|
+
const retire = (node: RecordedSceneNode) => {
|
|
87
|
+
for (const descendant of [...node.children]) retire(descendant);
|
|
88
|
+
for (const key of this.listeners.keys())
|
|
89
|
+
if (key.startsWith(`${node.id}:`)) this.listeners.delete(key);
|
|
90
|
+
this.nodes.delete(node.id);
|
|
91
|
+
node.parent = null;
|
|
92
|
+
};
|
|
93
|
+
retire(child);
|
|
94
|
+
this.operations.push({ operation: "remove", node: child.id });
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
subscribe(ref: SceneNodeRef, field: string, listener: SceneFieldListener): () => void {
|
|
98
|
+
this.lookup(ref);
|
|
99
|
+
const key = `${ref.id}:${field}`;
|
|
100
|
+
const callbacks = this.listeners.get(key) ?? new Set();
|
|
101
|
+
callbacks.add(listener);
|
|
102
|
+
this.listeners.set(key, callbacks);
|
|
103
|
+
return () => {
|
|
104
|
+
callbacks.delete(listener);
|
|
105
|
+
if (callbacks.size === 0) this.listeners.delete(key);
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
emit(ref: SceneNodeRef, field: string, value: unknown): void {
|
|
110
|
+
if (!this.nodes.has(ref.id)) return;
|
|
111
|
+
for (const listener of [...(this.listeners.get(`${ref.id}:${field}`) ?? [])]) listener(value);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
batch(work: () => void): void {
|
|
115
|
+
work();
|
|
116
|
+
this.hostCommits++;
|
|
117
|
+
}
|
|
118
|
+
get listenerCount(): number {
|
|
119
|
+
return [...this.listeners.values()].reduce((total, listeners) => total + listeners.size, 0);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
snapshot(node = this.root): unknown {
|
|
123
|
+
return {
|
|
124
|
+
type: node.type,
|
|
125
|
+
fields: { ...node.fields },
|
|
126
|
+
children: node.children.map((child) => this.snapshot(child)),
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
private lookup(ref: SceneNodeRef): RecordedSceneNode {
|
|
131
|
+
const node = this.nodes.get(ref.id);
|
|
132
|
+
if (!node || node !== ref)
|
|
133
|
+
throw new RokuHostError("STALE_NODE", `Unknown or retired node ${ref.id}`);
|
|
134
|
+
return node;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { createElement, useMemo, useState, type ReactNode } from "react";
|
|
2
|
+
import { Text, type useElement } from "../react-native/primitives";
|
|
3
|
+
import { RokuHostError } from "../scene/sceneGraph.types";
|
|
4
|
+
import { BitmapLayer } from "../graphics/BitmapLayer";
|
|
5
|
+
import { shapes, type Shape, type SvgProps } from "./svgElements";
|
|
6
|
+
import { svgPath, type Point } from "./svgPath";
|
|
7
|
+
import { svgPixels, type VectorPaint } from "./svgPixels";
|
|
8
|
+
|
|
9
|
+
type Attributes = Record<string, unknown>;
|
|
10
|
+
type Draw =
|
|
11
|
+
{ kind: "paint"; paint: VectorPaint } | { kind: "text"; props: Attributes; text: string };
|
|
12
|
+
const number = (value: unknown, fallback = 0) => {
|
|
13
|
+
const parsed = value === undefined ? fallback : Number(value);
|
|
14
|
+
if (!Number.isFinite(parsed) || Math.abs(parsed) > 1e7)
|
|
15
|
+
throw new RokuHostError("C09_SVG_NUMBER", "SVG requires finite coordinates");
|
|
16
|
+
return parsed;
|
|
17
|
+
};
|
|
18
|
+
function drawings(nodes: readonly Shape[], inherited: Attributes = {}): Draw[] {
|
|
19
|
+
return nodes.flatMap(([tag, attributes]): Draw[] => {
|
|
20
|
+
if (attributes.transform || attributes.clipPath || attributes.mask)
|
|
21
|
+
throw new RokuHostError(
|
|
22
|
+
"C09_SVG_TRANSFORM",
|
|
23
|
+
"Dynamic vector transforms and masks need a graphics adapter",
|
|
24
|
+
);
|
|
25
|
+
const props: Attributes = {
|
|
26
|
+
...inherited,
|
|
27
|
+
...attributes,
|
|
28
|
+
opacity: number(inherited.opacity, 1) * number(attributes.opacity, 1),
|
|
29
|
+
};
|
|
30
|
+
if (tag === "g") {
|
|
31
|
+
const parent = { ...props };
|
|
32
|
+
delete parent.children;
|
|
33
|
+
return drawings(shapes(attributes.children), parent);
|
|
34
|
+
}
|
|
35
|
+
if (tag === "text") {
|
|
36
|
+
if (typeof attributes.children !== "string" && typeof attributes.children !== "number")
|
|
37
|
+
throw new RokuHostError("C09_SVG_TEXT", "Dynamic SVG text requires a plain text run");
|
|
38
|
+
return [{ kind: "text", props, text: String(attributes.children) }];
|
|
39
|
+
}
|
|
40
|
+
let paths: Point[][];
|
|
41
|
+
if (tag === "path") paths = svgPath(String(props.d ?? ""));
|
|
42
|
+
else if (tag === "rect") {
|
|
43
|
+
const x = number(props.x),
|
|
44
|
+
y = number(props.y),
|
|
45
|
+
w = number(props.width),
|
|
46
|
+
h = number(props.height);
|
|
47
|
+
if (props.rx || props.ry)
|
|
48
|
+
throw new RokuHostError(
|
|
49
|
+
"C09_SVG_RECT",
|
|
50
|
+
"Rounded dynamic SVG rectangles need a path adapter",
|
|
51
|
+
);
|
|
52
|
+
paths = [
|
|
53
|
+
[
|
|
54
|
+
[x, y],
|
|
55
|
+
[x + w, y],
|
|
56
|
+
[x + w, y + h],
|
|
57
|
+
[x, y + h],
|
|
58
|
+
[x, y],
|
|
59
|
+
],
|
|
60
|
+
];
|
|
61
|
+
} else if (tag === "line")
|
|
62
|
+
paths = [
|
|
63
|
+
[
|
|
64
|
+
[number(props.x1), number(props.y1)],
|
|
65
|
+
[number(props.x2), number(props.y2)],
|
|
66
|
+
],
|
|
67
|
+
];
|
|
68
|
+
else if (tag === "circle" || tag === "ellipse") {
|
|
69
|
+
const cx = number(props.cx),
|
|
70
|
+
cy = number(props.cy),
|
|
71
|
+
rx = number(props.rx ?? props.r),
|
|
72
|
+
ry = number(props.ry ?? props.r);
|
|
73
|
+
paths = [
|
|
74
|
+
Array.from(
|
|
75
|
+
{ length: 129 },
|
|
76
|
+
(_, i) =>
|
|
77
|
+
[
|
|
78
|
+
cx + rx * Math.cos((i * Math.PI) / 64),
|
|
79
|
+
cy + ry * Math.sin((i * Math.PI) / 64),
|
|
80
|
+
] as Point,
|
|
81
|
+
),
|
|
82
|
+
];
|
|
83
|
+
} else throw new RokuHostError("C09_SVG_SHAPE", `Dynamic SVG ${tag} is not admitted`);
|
|
84
|
+
return [
|
|
85
|
+
{
|
|
86
|
+
kind: "paint",
|
|
87
|
+
paint: {
|
|
88
|
+
paths,
|
|
89
|
+
fill: String(props.fill ?? "black"),
|
|
90
|
+
stroke: props.stroke === undefined ? undefined : String(props.stroke),
|
|
91
|
+
strokeWidth: number(props.strokeWidth, 1),
|
|
92
|
+
opacity: number(props.opacity, 1),
|
|
93
|
+
fillOpacity: number(props.fillOpacity, 1),
|
|
94
|
+
strokeOpacity: number(props.strokeOpacity, 1),
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
];
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function Bitmap({ commands, box }: { commands: readonly VectorPaint[]; box: readonly number[] }) {
|
|
102
|
+
const width = Math.ceil(box[2]),
|
|
103
|
+
height = Math.ceil(box[3]);
|
|
104
|
+
const shifted = commands.map((command) => ({
|
|
105
|
+
...command,
|
|
106
|
+
paths: command.paths.map((path) => path.map(([x, y]) => [x - box[0], y - box[1]] as Point)),
|
|
107
|
+
}));
|
|
108
|
+
return createElement(BitmapLayer, {
|
|
109
|
+
identity: JSON.stringify(shifted),
|
|
110
|
+
width,
|
|
111
|
+
height,
|
|
112
|
+
pixels: () => svgPixels(width, height, shifted),
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function SvgLabel({
|
|
117
|
+
draw,
|
|
118
|
+
scale,
|
|
119
|
+
left,
|
|
120
|
+
top,
|
|
121
|
+
}: {
|
|
122
|
+
draw: Extract<Draw, { kind: "text" }>;
|
|
123
|
+
scale: number;
|
|
124
|
+
left: number;
|
|
125
|
+
top: number;
|
|
126
|
+
}) {
|
|
127
|
+
const [width, setWidth] = useState(0),
|
|
128
|
+
fontSize = number(draw.props.fontSize, 16) * scale;
|
|
129
|
+
const anchor = draw.props.textAnchor ?? "start";
|
|
130
|
+
return createElement(
|
|
131
|
+
Text,
|
|
132
|
+
{
|
|
133
|
+
style: {
|
|
134
|
+
position: "absolute",
|
|
135
|
+
left:
|
|
136
|
+
left +
|
|
137
|
+
number(draw.props.x) * scale -
|
|
138
|
+
width * (anchor === "middle" ? 0.5 : anchor === "end" ? 1 : 0),
|
|
139
|
+
top: top + number(draw.props.y) * scale - fontSize,
|
|
140
|
+
fontSize,
|
|
141
|
+
color: String(draw.props.fill ?? "black"),
|
|
142
|
+
opacity: number(draw.props.opacity, 1),
|
|
143
|
+
fontFamily: typeof draw.props.fontFamily === "string" ? draw.props.fontFamily : undefined,
|
|
144
|
+
},
|
|
145
|
+
onLayout(event: unknown) {
|
|
146
|
+
const next = (event as { nativeEvent: { layout: { width: number } } }).nativeEvent.layout
|
|
147
|
+
.width;
|
|
148
|
+
setWidth((old) => (old === next ? old : next));
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
draw.text,
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** Bitmap paths and native font labels retain SVG source order and measured placement. */
|
|
156
|
+
export function DynamicSvg({
|
|
157
|
+
props,
|
|
158
|
+
element,
|
|
159
|
+
children,
|
|
160
|
+
box,
|
|
161
|
+
}: {
|
|
162
|
+
props: SvgProps;
|
|
163
|
+
element: ReturnType<typeof useElement>;
|
|
164
|
+
children: readonly Shape[];
|
|
165
|
+
box: readonly number[];
|
|
166
|
+
}) {
|
|
167
|
+
const [size, setSize] = useState<{ width: number; height: number }>();
|
|
168
|
+
const commands = useMemo(
|
|
169
|
+
() =>
|
|
170
|
+
drawings(children, {
|
|
171
|
+
fill: props.fill ?? "black",
|
|
172
|
+
stroke: props.stroke,
|
|
173
|
+
strokeWidth: props.strokeWidth,
|
|
174
|
+
}),
|
|
175
|
+
[children, props.fill, props.stroke, props.strokeWidth],
|
|
176
|
+
);
|
|
177
|
+
const layers: Array<readonly VectorPaint[] | Extract<Draw, { kind: "text" }>> = [];
|
|
178
|
+
for (const command of commands) {
|
|
179
|
+
if (command.kind === "text") layers.push(command);
|
|
180
|
+
else {
|
|
181
|
+
const last = layers.at(-1);
|
|
182
|
+
if (Array.isArray(last)) (last as VectorPaint[]).push(command.paint);
|
|
183
|
+
else layers.push([command.paint]);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
const scale = size ? Math.min(size.width / box[2], size.height / box[3]) : 0;
|
|
187
|
+
const left = size ? (size.width - box[2] * scale) / 2 - box[0] * scale : 0,
|
|
188
|
+
top = size ? (size.height - box[3] * scale) / 2 - box[1] * scale : 0;
|
|
189
|
+
const items: ReactNode[] = layers.map((layer, index) =>
|
|
190
|
+
Array.isArray(layer)
|
|
191
|
+
? createElement(Bitmap, { key: index, commands: layer, box })
|
|
192
|
+
: size
|
|
193
|
+
? createElement(SvgLabel, {
|
|
194
|
+
key: index,
|
|
195
|
+
draw: layer as Extract<Draw, { kind: "text" }>,
|
|
196
|
+
scale,
|
|
197
|
+
left,
|
|
198
|
+
top,
|
|
199
|
+
})
|
|
200
|
+
: null,
|
|
201
|
+
);
|
|
202
|
+
return createElement(
|
|
203
|
+
"group",
|
|
204
|
+
{
|
|
205
|
+
fields: element.fields,
|
|
206
|
+
layout: {
|
|
207
|
+
...element.layout,
|
|
208
|
+
onLayout(event: unknown) {
|
|
209
|
+
const value = (
|
|
210
|
+
event as {
|
|
211
|
+
nativeEvent: { layout: { width: number; height: number } };
|
|
212
|
+
}
|
|
213
|
+
).nativeEvent.layout;
|
|
214
|
+
if (value.width > 0 && value.height > 0)
|
|
215
|
+
setSize((previous) =>
|
|
216
|
+
previous?.width === value.width && previous.height === value.height
|
|
217
|
+
? previous
|
|
218
|
+
: { width: value.width, height: value.height },
|
|
219
|
+
);
|
|
220
|
+
props.onLayout?.(event);
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
},
|
|
224
|
+
...items,
|
|
225
|
+
);
|
|
226
|
+
}
|