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,494 @@
|
|
|
1
|
+
import { RokuHostError } from "../scene/sceneGraph.types";
|
|
2
|
+
|
|
3
|
+
export type NativeRemoteKey =
|
|
4
|
+
| "up"
|
|
5
|
+
| "down"
|
|
6
|
+
| "left"
|
|
7
|
+
| "right"
|
|
8
|
+
| "select"
|
|
9
|
+
| "back"
|
|
10
|
+
| "playPause"
|
|
11
|
+
| "rewind"
|
|
12
|
+
| "fastForward"
|
|
13
|
+
| "options";
|
|
14
|
+
export type NativeFocusRequestOrigin = "imperative" | "directional";
|
|
15
|
+
type NativeDirection = Extract<NativeRemoteKey, "up" | "down" | "left" | "right">;
|
|
16
|
+
const directions: readonly NativeRemoteKey[] = ["up", "right", "down", "left"];
|
|
17
|
+
const MAX_QUEUED_DIRECTIONS = 8;
|
|
18
|
+
const FOCUS_OBSERVATION_TIMEOUT_MS = 250;
|
|
19
|
+
type QueuedSelect = { key: "select"; phase: "down" | "up"; gesture: object };
|
|
20
|
+
|
|
21
|
+
export interface NativeRemoteEvent {
|
|
22
|
+
readonly key: NativeRemoteKey;
|
|
23
|
+
readonly phase: "down" | "repeat" | "up";
|
|
24
|
+
}
|
|
25
|
+
export interface TVEvent {
|
|
26
|
+
readonly tag?: number;
|
|
27
|
+
readonly eventType: NativeRemoteKey | "cancel";
|
|
28
|
+
readonly reason?: "focus-request" | "inactive" | "text-entry";
|
|
29
|
+
readonly eventKeyAction: 0 | 1;
|
|
30
|
+
readonly repeat: boolean;
|
|
31
|
+
}
|
|
32
|
+
export interface NativeInputTarget {
|
|
33
|
+
readonly tag?: number;
|
|
34
|
+
enabled(): boolean;
|
|
35
|
+
focus(focused: boolean): void;
|
|
36
|
+
pressed(pressed: boolean): void;
|
|
37
|
+
activate(): void;
|
|
38
|
+
longPress?(): void;
|
|
39
|
+
readonly longPressDelay?: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Focus is observed from the host. A gainFocus request never changes this owner. */
|
|
43
|
+
export class NativeInput {
|
|
44
|
+
private readonly targets = new Map<string, NativeInputTarget>();
|
|
45
|
+
private readonly observers = new Set<(event: TVEvent) => unknown>();
|
|
46
|
+
private readonly backHandlers = new Map<() => boolean, object>();
|
|
47
|
+
private focused: string | null = null;
|
|
48
|
+
private held: {
|
|
49
|
+
id: string;
|
|
50
|
+
target: NativeInputTarget;
|
|
51
|
+
long: boolean;
|
|
52
|
+
timer?: ReturnType<typeof setTimeout>;
|
|
53
|
+
} | null = null;
|
|
54
|
+
private retired = false;
|
|
55
|
+
private active = true;
|
|
56
|
+
private gestureRevision = 0;
|
|
57
|
+
private focusRevision = 0;
|
|
58
|
+
private requestedFocus: {
|
|
59
|
+
id: string;
|
|
60
|
+
origin: NativeFocusRequestOrigin;
|
|
61
|
+
from: string | null;
|
|
62
|
+
timer: ReturnType<typeof setTimeout>;
|
|
63
|
+
} | null = null;
|
|
64
|
+
// A request may have moved real focus even if its observation went missing.
|
|
65
|
+
private recovery: { from: string | null } | null = null;
|
|
66
|
+
private readonly queuedDirections: ({ key: NativeDirection; repeat: boolean } | QueuedSelect)[] =
|
|
67
|
+
[];
|
|
68
|
+
private queuedSelect: object | null = null;
|
|
69
|
+
private selectDown = false;
|
|
70
|
+
private blockedSelect = false;
|
|
71
|
+
private renderer: ((work: () => void) => void) | undefined;
|
|
72
|
+
private navigation: ((direction: NativeDirection) => boolean) | undefined;
|
|
73
|
+
isTargetEnabled(id: string): boolean {
|
|
74
|
+
return this.targets.get(id)?.enabled() === true;
|
|
75
|
+
}
|
|
76
|
+
/** A refused/retired native request must not leave remote input fenced. */
|
|
77
|
+
cancelFocusRequest(id: string): void {
|
|
78
|
+
if (this.requestedFocus?.id === id) this.retireFocusRequest();
|
|
79
|
+
}
|
|
80
|
+
/** Visibility/modal/disabled changes can retire a held press without a
|
|
81
|
+
* native focus change. Revealing the same target cannot revive that press. */
|
|
82
|
+
reconcileTargets(): void {
|
|
83
|
+
if (this.retired) return;
|
|
84
|
+
if (this.requestedFocus !== null && !this.isTargetEnabled(this.requestedFocus.id))
|
|
85
|
+
this.retireFocusRequest();
|
|
86
|
+
if (!this.held || this.held.target.enabled()) return;
|
|
87
|
+
this.batch(() => {
|
|
88
|
+
this.blockedSelect ||= this.selectDown;
|
|
89
|
+
this.cancelHeld(true);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
requestFocus(
|
|
93
|
+
id: string,
|
|
94
|
+
origin: NativeFocusRequestOrigin = "imperative",
|
|
95
|
+
sample?: (from: string | null, confirm: (observedID: string) => void) => void,
|
|
96
|
+
): void {
|
|
97
|
+
if (
|
|
98
|
+
this.retired ||
|
|
99
|
+
!this.targets.has(id) ||
|
|
100
|
+
(id === this.focused && this.requestedFocus === null && this.recovery === null)
|
|
101
|
+
)
|
|
102
|
+
return;
|
|
103
|
+
if (origin === "imperative") this.clearQueuedInput();
|
|
104
|
+
this.clearFocusRequest();
|
|
105
|
+
const from = this.navigationFocusID;
|
|
106
|
+
const deadline = performance.now() + FOCUS_OBSERVATION_TIMEOUT_MS;
|
|
107
|
+
const expire = () => {
|
|
108
|
+
if (this.requestedFocus?.timer !== timer) return;
|
|
109
|
+
// A native timer scheduled during a delayed SDK turn can fire before
|
|
110
|
+
// this request has had its full observation interval.
|
|
111
|
+
const remaining = deadline - performance.now();
|
|
112
|
+
if (remaining > 0) {
|
|
113
|
+
timer = setTimeout(expire, Math.ceil(remaining));
|
|
114
|
+
this.requestedFocus.timer = timer;
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
this.abandonFocusRequest();
|
|
118
|
+
const recovery = this.recovery;
|
|
119
|
+
sample?.(from, (observedID) => {
|
|
120
|
+
if (this.recovery === recovery && this.active && !this.requestedFocus)
|
|
121
|
+
this.observeFocus(observedID, true);
|
|
122
|
+
});
|
|
123
|
+
};
|
|
124
|
+
let timer = setTimeout(expire, FOCUS_OBSERVATION_TIMEOUT_MS);
|
|
125
|
+
this.requestedFocus = { id, origin, from, timer };
|
|
126
|
+
this.blockedSelect ||= this.selectDown;
|
|
127
|
+
this.cancelHeld(true);
|
|
128
|
+
for (const observer of [...this.observers])
|
|
129
|
+
observer({
|
|
130
|
+
eventType: "cancel",
|
|
131
|
+
reason: "focus-request",
|
|
132
|
+
eventKeyAction: 1,
|
|
133
|
+
repeat: false,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
bindNavigation(navigate: NonNullable<NativeInput["navigation"]>): () => void {
|
|
137
|
+
this.assertActive();
|
|
138
|
+
if (this.navigation)
|
|
139
|
+
throw new RokuHostError("C07_INPUT_ROOT", "An input port already has a focus executor");
|
|
140
|
+
this.navigation = navigate;
|
|
141
|
+
return () => {
|
|
142
|
+
if (this.navigation === navigate) this.navigation = undefined;
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
get focusedID(): string | null {
|
|
146
|
+
return this.focused;
|
|
147
|
+
}
|
|
148
|
+
get navigationFocusID(): string | null {
|
|
149
|
+
return this.focused ?? this.recovery?.from ?? null;
|
|
150
|
+
}
|
|
151
|
+
get targetCount(): number {
|
|
152
|
+
return this.targets.size;
|
|
153
|
+
}
|
|
154
|
+
get observerCount(): number {
|
|
155
|
+
return this.observers.size + this.backHandlers.size;
|
|
156
|
+
}
|
|
157
|
+
bindRenderer(batch: (work: () => void) => void): () => void {
|
|
158
|
+
this.assertActive();
|
|
159
|
+
if (this.renderer)
|
|
160
|
+
throw new RokuHostError("C07_INPUT_ROOT", "An input port already belongs to a mounted root");
|
|
161
|
+
this.renderer = batch;
|
|
162
|
+
return () => {
|
|
163
|
+
if (this.renderer === batch) this.renderer = undefined;
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
register(id: string, target: NativeInputTarget): () => void {
|
|
167
|
+
this.assertActive();
|
|
168
|
+
if (this.targets.has(id))
|
|
169
|
+
throw new RokuHostError("C07_INPUT_ID", `Duplicate input identity ${id}`);
|
|
170
|
+
if (
|
|
171
|
+
target.longPressDelay != null &&
|
|
172
|
+
(!(target.longPressDelay >= 0) || !Number.isFinite(target.longPressDelay))
|
|
173
|
+
)
|
|
174
|
+
throw new RokuHostError("C07_INPUT_DELAY", "Long-press delay must be nonnegative and finite");
|
|
175
|
+
this.targets.set(id, target);
|
|
176
|
+
return () => {
|
|
177
|
+
if (this.targets.get(id) !== target) return;
|
|
178
|
+
if (this.held?.id === id) this.cancelHeld(false);
|
|
179
|
+
this.targets.delete(id);
|
|
180
|
+
if (this.requestedFocus?.id === id) this.retireFocusRequest();
|
|
181
|
+
if (this.focused === id) this.focused = null;
|
|
182
|
+
if (this.recovery?.from === id) this.recovery.from = null;
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
observeFocus(id: string, focused: boolean): void {
|
|
186
|
+
if (this.retired) return;
|
|
187
|
+
let acknowledgedRevision: number | undefined;
|
|
188
|
+
this.batch(() => {
|
|
189
|
+
const target = this.targets.get(id);
|
|
190
|
+
if (!target) return;
|
|
191
|
+
const current = (revision: number) =>
|
|
192
|
+
!this.retired && this.focusRevision === revision && this.targets.get(id) === target;
|
|
193
|
+
if (focused) {
|
|
194
|
+
const isDirectional = this.requestedFocus?.origin === "directional";
|
|
195
|
+
const acknowledgesRequest = this.requestedFocus?.id === id;
|
|
196
|
+
if (this.focused === id && this.requestedFocus && !acknowledgesRequest) return;
|
|
197
|
+
if (acknowledgesRequest) this.clearFocusRequest();
|
|
198
|
+
if (this.recovery && this.requestedFocus) {
|
|
199
|
+
// An observation for another target can arrive while a recovery retry
|
|
200
|
+
// is in flight. It cannot release the retry's activation fence.
|
|
201
|
+
this.clearQueuedInput();
|
|
202
|
+
} else {
|
|
203
|
+
if (isDirectional && !acknowledgesRequest) this.retireFocusRequest();
|
|
204
|
+
this.recovery = null;
|
|
205
|
+
}
|
|
206
|
+
if (this.focused === id) return;
|
|
207
|
+
const revision = ++this.focusRevision;
|
|
208
|
+
const oldID = this.focused,
|
|
209
|
+
old = oldID && this.targets.get(oldID);
|
|
210
|
+
this.cancelHeld(true);
|
|
211
|
+
if (!current(revision)) return;
|
|
212
|
+
this.focused = id;
|
|
213
|
+
if (old && this.targets.get(oldID!) === old) old.focus(false);
|
|
214
|
+
// Cancellation and blur can synchronously replace a route/target or
|
|
215
|
+
// publish a newer observation. Their retired continuation owns neither.
|
|
216
|
+
if (!current(revision) || this.focused !== id) return;
|
|
217
|
+
target.focus(true);
|
|
218
|
+
if (isDirectional && acknowledgesRequest && current(revision))
|
|
219
|
+
acknowledgedRevision = revision;
|
|
220
|
+
} else if (this.focused === id) {
|
|
221
|
+
const revision = ++this.focusRevision;
|
|
222
|
+
this.cancelHeld(true);
|
|
223
|
+
if (!current(revision) || this.focused !== id) return;
|
|
224
|
+
this.focused = null;
|
|
225
|
+
target.focus(false);
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
// Let the observed React state submit before replaying the next native
|
|
229
|
+
// presentation. Its writes must not supersede the next prepared target.
|
|
230
|
+
if (acknowledgedRevision === this.focusRevision && !this.retired) this.replayQueuedDirections();
|
|
231
|
+
}
|
|
232
|
+
subscribe(observer: (event: TVEvent) => unknown): () => void {
|
|
233
|
+
this.assertActive();
|
|
234
|
+
this.observers.add(observer);
|
|
235
|
+
return () => this.observers.delete(observer);
|
|
236
|
+
}
|
|
237
|
+
addBackHandler(handler: () => boolean): () => void {
|
|
238
|
+
this.assertActive();
|
|
239
|
+
// Match callback deduplication without letting a stale removal own a later
|
|
240
|
+
// registration of that callback.
|
|
241
|
+
const registration = this.backHandlers.get(handler) ?? {};
|
|
242
|
+
this.backHandlers.set(handler, registration);
|
|
243
|
+
return () => {
|
|
244
|
+
if (this.backHandlers.get(handler) === registration) this.backHandlers.delete(handler);
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
dispatch(event: NativeRemoteEvent): boolean {
|
|
248
|
+
this.assertActive();
|
|
249
|
+
if (!this.active) return false;
|
|
250
|
+
if (directions.includes(event.key)) return this.dispatchDirection(event);
|
|
251
|
+
if (event.key === "select" && this.queueSelect(event)) return true;
|
|
252
|
+
return this.dispatchNow(event);
|
|
253
|
+
}
|
|
254
|
+
private queueSelect(event: NativeRemoteEvent): boolean {
|
|
255
|
+
if (this.queuedSelect) {
|
|
256
|
+
if (event.phase === "up") {
|
|
257
|
+
this.queuedDirections.push({ key: "select", phase: "up", gesture: this.queuedSelect });
|
|
258
|
+
this.queuedSelect = null;
|
|
259
|
+
}
|
|
260
|
+
return true;
|
|
261
|
+
}
|
|
262
|
+
// Preserve ordering behind accepted directions. Imperative requests still
|
|
263
|
+
// fence Select, and the normal dispatch path validates the observed owner.
|
|
264
|
+
if (
|
|
265
|
+
event.phase !== "down" ||
|
|
266
|
+
this.requestedFocus?.origin !== "directional" ||
|
|
267
|
+
this.recovery ||
|
|
268
|
+
this.selectDown ||
|
|
269
|
+
this.blockedSelect ||
|
|
270
|
+
this.queuedDirections.length >= MAX_QUEUED_DIRECTIONS + 2
|
|
271
|
+
)
|
|
272
|
+
return false;
|
|
273
|
+
this.queuedSelect = {};
|
|
274
|
+
this.queuedDirections.push({ key: "select", phase: "down", gesture: this.queuedSelect });
|
|
275
|
+
return true;
|
|
276
|
+
}
|
|
277
|
+
private currentGesture(revision: number): boolean {
|
|
278
|
+
return !this.retired && this.active && revision === this.gestureRevision;
|
|
279
|
+
}
|
|
280
|
+
private notifyObservers(event: NativeRemoteEvent, revision: number): boolean {
|
|
281
|
+
if (!this.observers.size) return false;
|
|
282
|
+
let handled = false;
|
|
283
|
+
for (const observer of [...this.observers]) {
|
|
284
|
+
if (!this.currentGesture(revision)) break;
|
|
285
|
+
handled =
|
|
286
|
+
observer({
|
|
287
|
+
tag: this.focused ? this.targets.get(this.focused)?.tag : undefined,
|
|
288
|
+
eventType: event.key,
|
|
289
|
+
eventKeyAction: event.phase === "up" ? 1 : 0,
|
|
290
|
+
repeat: event.phase === "repeat",
|
|
291
|
+
}) === true || handled;
|
|
292
|
+
}
|
|
293
|
+
return handled;
|
|
294
|
+
}
|
|
295
|
+
private dispatchDirection(event: NativeRemoteEvent): boolean {
|
|
296
|
+
let handled = false;
|
|
297
|
+
const revision = this.gestureRevision;
|
|
298
|
+
this.batch(() => {
|
|
299
|
+
handled = this.notifyObservers(event, revision);
|
|
300
|
+
if (!this.currentGesture(revision)) {
|
|
301
|
+
handled = true;
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
const key = event.key as NativeDirection;
|
|
305
|
+
if (event.phase === "up") {
|
|
306
|
+
const queuedRepeat = this.queuedDirections.findIndex(
|
|
307
|
+
(direction) => direction.key !== "select" && direction.repeat && direction.key === key,
|
|
308
|
+
);
|
|
309
|
+
if (queuedRepeat >= 0) this.queuedDirections.splice(queuedRepeat, 1);
|
|
310
|
+
} else if (!handled) {
|
|
311
|
+
handled = this.navigate(key, event.phase === "repeat");
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
return handled;
|
|
315
|
+
}
|
|
316
|
+
private dispatchNow(event: NativeRemoteEvent): boolean {
|
|
317
|
+
if (event.key === "back" && event.phase === "down") this.abandonFocusRequest();
|
|
318
|
+
if (event.key === "select") {
|
|
319
|
+
if (event.phase === "down") this.selectDown = true;
|
|
320
|
+
else if (event.phase === "up") this.selectDown = false;
|
|
321
|
+
if (this.requestedFocus || this.recovery || this.blockedSelect) {
|
|
322
|
+
if (event.phase !== "up") this.blockedSelect = true;
|
|
323
|
+
else this.blockedSelect = false;
|
|
324
|
+
return false;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
let handled = false;
|
|
328
|
+
const revision = this.gestureRevision;
|
|
329
|
+
this.batch(() => {
|
|
330
|
+
handled = this.notifyObservers(event, revision);
|
|
331
|
+
if (!this.currentGesture(revision)) {
|
|
332
|
+
handled = true;
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
if (event.key === "select" && this.blockedSelect) return;
|
|
336
|
+
if (event.key === "back" && event.phase === "down" && !handled) {
|
|
337
|
+
for (const [handler, registration] of [...this.backHandlers].reverse()) {
|
|
338
|
+
if (this.backHandlers.get(handler) !== registration) continue;
|
|
339
|
+
if (handler() || this.retired || !this.active || revision !== this.gestureRevision) {
|
|
340
|
+
handled = true;
|
|
341
|
+
break;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
if (event.key !== "select") return;
|
|
346
|
+
if (event.phase === "up") {
|
|
347
|
+
const held = this.held;
|
|
348
|
+
if (!held) return;
|
|
349
|
+
this.held = null;
|
|
350
|
+
clearTimeout(held.timer);
|
|
351
|
+
if (this.targets.get(held.id) !== held.target) return;
|
|
352
|
+
const focusRevision = this.focusRevision;
|
|
353
|
+
held.target.pressed(false);
|
|
354
|
+
if (
|
|
355
|
+
this.active &&
|
|
356
|
+
!this.retired &&
|
|
357
|
+
revision === this.gestureRevision &&
|
|
358
|
+
focusRevision === this.focusRevision &&
|
|
359
|
+
!this.requestedFocus &&
|
|
360
|
+
this.targets.get(held.id) === held.target &&
|
|
361
|
+
held.id === this.focused &&
|
|
362
|
+
held.target.enabled() &&
|
|
363
|
+
!held.long
|
|
364
|
+
)
|
|
365
|
+
held.target.activate();
|
|
366
|
+
handled = true;
|
|
367
|
+
} else if (event.phase === "down" && !this.held && this.focused) {
|
|
368
|
+
const id = this.focused,
|
|
369
|
+
target = this.targets.get(id);
|
|
370
|
+
if (!target?.enabled()) return;
|
|
371
|
+
const held = (this.held = { id, target, long: false } as NonNullable<NativeInput["held"]>);
|
|
372
|
+
target.pressed(true);
|
|
373
|
+
if (this.held !== held || this.retired) return;
|
|
374
|
+
if (target.longPress)
|
|
375
|
+
held.timer = setTimeout(
|
|
376
|
+
() =>
|
|
377
|
+
this.batch(() => {
|
|
378
|
+
if (this.held !== held || !target.enabled() || this.focused !== id) return;
|
|
379
|
+
held.long = true;
|
|
380
|
+
target.longPress?.();
|
|
381
|
+
}),
|
|
382
|
+
target.longPressDelay ?? 500,
|
|
383
|
+
);
|
|
384
|
+
handled = true;
|
|
385
|
+
} else handled = this.held !== null;
|
|
386
|
+
});
|
|
387
|
+
return handled;
|
|
388
|
+
}
|
|
389
|
+
dispose(): void {
|
|
390
|
+
if (this.retired) return;
|
|
391
|
+
this.cancelHeld(false);
|
|
392
|
+
this.retired = true;
|
|
393
|
+
this.targets.clear();
|
|
394
|
+
this.observers.clear();
|
|
395
|
+
this.backHandlers.clear();
|
|
396
|
+
this.focused = null;
|
|
397
|
+
this.retireFocusRequest();
|
|
398
|
+
this.recovery = null;
|
|
399
|
+
this.selectDown = false;
|
|
400
|
+
this.blockedSelect = false;
|
|
401
|
+
this.renderer = undefined;
|
|
402
|
+
this.navigation = undefined;
|
|
403
|
+
}
|
|
404
|
+
setActive(active: boolean): void {
|
|
405
|
+
if (this.retired || this.active === active) return;
|
|
406
|
+
this.active = active;
|
|
407
|
+
if (!active) this.cancelInteraction("inactive");
|
|
408
|
+
}
|
|
409
|
+
/** An admitted external editor owns the gesture even before native focus-loss
|
|
410
|
+
* or key-up arrives. This cancels work; it does not acknowledge native focus. */
|
|
411
|
+
cancelPendingInput(): void {
|
|
412
|
+
this.cancelInteraction("text-entry");
|
|
413
|
+
}
|
|
414
|
+
private cancelInteraction(reason: "inactive" | "text-entry"): void {
|
|
415
|
+
if (this.retired) return;
|
|
416
|
+
const revision = ++this.gestureRevision;
|
|
417
|
+
this.batch(() => {
|
|
418
|
+
this.retireFocusRequest();
|
|
419
|
+
this.recovery = null;
|
|
420
|
+
this.selectDown = false;
|
|
421
|
+
this.blockedSelect = false;
|
|
422
|
+
this.cancelHeld(true);
|
|
423
|
+
for (const observer of [...this.observers]) {
|
|
424
|
+
if (this.retired || revision !== this.gestureRevision) break;
|
|
425
|
+
observer({ eventType: "cancel", reason, eventKeyAction: 1, repeat: false });
|
|
426
|
+
}
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
private navigate(key: NativeDirection, repeat: boolean): boolean {
|
|
430
|
+
if (this.requestedFocus?.origin !== "directional") return this.navigation?.(key) ?? false;
|
|
431
|
+
if (
|
|
432
|
+
this.queuedDirections.filter((entry) => entry.key !== "select").length <
|
|
433
|
+
MAX_QUEUED_DIRECTIONS &&
|
|
434
|
+
(!repeat ||
|
|
435
|
+
!this.queuedDirections.some(
|
|
436
|
+
(direction) => direction.key !== "select" && direction.repeat && direction.key === key,
|
|
437
|
+
))
|
|
438
|
+
)
|
|
439
|
+
this.queuedDirections.push({ key, repeat });
|
|
440
|
+
return true;
|
|
441
|
+
}
|
|
442
|
+
private replayQueuedDirections(): void {
|
|
443
|
+
// Resolve each step from the observed native card. Several accepted key
|
|
444
|
+
// downs can arrive before gainFocus produces its committed observation.
|
|
445
|
+
while (this.active && !this.retired && this.requestedFocus === null) {
|
|
446
|
+
const next = this.queuedDirections.shift();
|
|
447
|
+
if (!next) return;
|
|
448
|
+
if (next.key === "select") {
|
|
449
|
+
if (next.phase === "down" && this.queuedSelect === next.gesture) this.queuedSelect = null;
|
|
450
|
+
this.dispatchNow(next);
|
|
451
|
+
} else {
|
|
452
|
+
this.navigation?.(next.key);
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
private abandonFocusRequest(): void {
|
|
457
|
+
if (this.requestedFocus) {
|
|
458
|
+
const from = this.requestedFocus.from;
|
|
459
|
+
this.recovery = { from: from && this.targets.has(from) ? from : null };
|
|
460
|
+
}
|
|
461
|
+
this.retireFocusRequest();
|
|
462
|
+
}
|
|
463
|
+
private clearFocusRequest(): void {
|
|
464
|
+
if (!this.requestedFocus) return;
|
|
465
|
+
clearTimeout(this.requestedFocus.timer);
|
|
466
|
+
this.requestedFocus = null;
|
|
467
|
+
}
|
|
468
|
+
private retireFocusRequest(): void {
|
|
469
|
+
this.clearFocusRequest();
|
|
470
|
+
this.clearQueuedInput();
|
|
471
|
+
}
|
|
472
|
+
private clearQueuedInput(): void {
|
|
473
|
+
this.queuedDirections.length = 0;
|
|
474
|
+
this.blockedSelect ||= this.queuedSelect !== null;
|
|
475
|
+
this.queuedSelect = null;
|
|
476
|
+
}
|
|
477
|
+
private batch(work: () => void) {
|
|
478
|
+
if (!this.retired) {
|
|
479
|
+
if (this.renderer) this.renderer(work);
|
|
480
|
+
else work();
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
private assertActive() {
|
|
484
|
+
if (this.retired) throw new RokuHostError("C07_INPUT_DISPOSED", "The input port was disposed");
|
|
485
|
+
}
|
|
486
|
+
private cancelHeld(notify: boolean) {
|
|
487
|
+
const held = this.held;
|
|
488
|
+
this.held = null;
|
|
489
|
+
if (held) {
|
|
490
|
+
clearTimeout(held.timer);
|
|
491
|
+
if (notify && this.targets.get(held.id) === held.target) held.target.pressed(false);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { RokuHostError } from "../scene/sceneGraph.types";
|
|
2
|
+
|
|
3
|
+
export type AppStateStatus = "active" | "inactive" | "background";
|
|
4
|
+
export type ColorSchemeName = "light" | "dark" | null;
|
|
5
|
+
export interface SystemSnapshot {
|
|
6
|
+
readonly appState: AppStateStatus | null;
|
|
7
|
+
readonly colorScheme: ColorSchemeName;
|
|
8
|
+
readonly reduceMotion?: boolean;
|
|
9
|
+
readonly screenReader?: boolean;
|
|
10
|
+
readonly timeZone?: string;
|
|
11
|
+
}
|
|
12
|
+
export type SystemEvent = {
|
|
13
|
+
readonly type:
|
|
14
|
+
"appState" | "appearance" | "reduceMotion" | "screenReader" | "memoryWarning" | "timeZone";
|
|
15
|
+
readonly value: unknown;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/** Facts are published by a lifecycle/accessibility adapter, never inferred from renderer identity. */
|
|
19
|
+
export class NativeSystemState {
|
|
20
|
+
private snapshot: SystemSnapshot;
|
|
21
|
+
private override: ColorSchemeName = null;
|
|
22
|
+
private readonly listeners = new Set<(event: SystemEvent) => void>();
|
|
23
|
+
constructor(
|
|
24
|
+
initial: Partial<SystemSnapshot> = {},
|
|
25
|
+
private readonly announce?: (message: string, options?: { queue?: boolean }) => void,
|
|
26
|
+
) {
|
|
27
|
+
this.snapshot = Object.freeze({
|
|
28
|
+
appState: null,
|
|
29
|
+
colorScheme: null,
|
|
30
|
+
...initial,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
getSnapshot(): SystemSnapshot {
|
|
34
|
+
return this.snapshot;
|
|
35
|
+
}
|
|
36
|
+
getColorScheme(): ColorSchemeName {
|
|
37
|
+
return this.override ?? this.snapshot.colorScheme;
|
|
38
|
+
}
|
|
39
|
+
subscribe(listener: (event: SystemEvent) => void): () => void {
|
|
40
|
+
this.listeners.add(listener);
|
|
41
|
+
return () => this.listeners.delete(listener);
|
|
42
|
+
}
|
|
43
|
+
get listenerCount(): number {
|
|
44
|
+
return this.listeners.size;
|
|
45
|
+
}
|
|
46
|
+
publish(next: Partial<SystemSnapshot>): void {
|
|
47
|
+
const previousColor = this.getColorScheme();
|
|
48
|
+
const previous = this.snapshot;
|
|
49
|
+
this.snapshot = Object.freeze({ ...previous, ...next });
|
|
50
|
+
if (previous.appState !== this.snapshot.appState)
|
|
51
|
+
this.emit({ type: "appState", value: this.snapshot.appState });
|
|
52
|
+
if (previousColor !== this.getColorScheme())
|
|
53
|
+
this.emit({ type: "appearance", value: this.getColorScheme() });
|
|
54
|
+
if (previous.reduceMotion !== this.snapshot.reduceMotion)
|
|
55
|
+
this.emit({ type: "reduceMotion", value: this.snapshot.reduceMotion });
|
|
56
|
+
if (previous.screenReader !== this.snapshot.screenReader)
|
|
57
|
+
this.emit({ type: "screenReader", value: this.snapshot.screenReader });
|
|
58
|
+
if (previous.timeZone !== this.snapshot.timeZone)
|
|
59
|
+
this.emit({ type: "timeZone", value: this.snapshot.timeZone });
|
|
60
|
+
}
|
|
61
|
+
setColorScheme(scheme: ColorSchemeName | "unspecified"): void {
|
|
62
|
+
const before = this.getColorScheme();
|
|
63
|
+
this.override = scheme === "unspecified" ? null : scheme;
|
|
64
|
+
if (before !== this.getColorScheme())
|
|
65
|
+
this.emit({ type: "appearance", value: this.getColorScheme() });
|
|
66
|
+
}
|
|
67
|
+
async isReduceMotionEnabled(): Promise<boolean> {
|
|
68
|
+
return this.known(this.snapshot.reduceMotion, "reduced motion");
|
|
69
|
+
}
|
|
70
|
+
async isScreenReaderEnabled(): Promise<boolean> {
|
|
71
|
+
return this.known(this.snapshot.screenReader, "screen reader");
|
|
72
|
+
}
|
|
73
|
+
announceForAccessibility(message: string, options?: { queue?: boolean }): void {
|
|
74
|
+
if (!this.announce)
|
|
75
|
+
throw new RokuHostError(
|
|
76
|
+
"C25_ANNOUNCE",
|
|
77
|
+
"The host has no accessibility announcement capability",
|
|
78
|
+
);
|
|
79
|
+
this.announce(message, options);
|
|
80
|
+
}
|
|
81
|
+
publishMemoryWarning(): void {
|
|
82
|
+
this.emit({ type: "memoryWarning", value: undefined });
|
|
83
|
+
}
|
|
84
|
+
private known(value: boolean | undefined, name: string): boolean {
|
|
85
|
+
if (value === undefined)
|
|
86
|
+
throw new RokuHostError(
|
|
87
|
+
"C25_PREFERENCE_PENDING",
|
|
88
|
+
`The device ${name} preference has not been supplied`,
|
|
89
|
+
);
|
|
90
|
+
return value;
|
|
91
|
+
}
|
|
92
|
+
private emit(event: SystemEvent) {
|
|
93
|
+
for (const listener of [...this.listeners]) listener(event);
|
|
94
|
+
}
|
|
95
|
+
}
|