@bake-native/core 0.0.0 → 0.1.0-rc.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.
Files changed (143) hide show
  1. package/LICENSE +21 -0
  2. package/THIRD_PARTY_NOTICES.txt +548 -0
  3. package/bake-prelude.js +189 -0
  4. package/batch-contract.json +21 -0
  5. package/batch-wire.js +674 -0
  6. package/compat.json +64 -0
  7. package/controls.css +108 -0
  8. package/determinism.js +175 -0
  9. package/devtools.d.ts +13 -0
  10. package/devtools.js +73 -0
  11. package/dialogs.js +59 -0
  12. package/direct-style.js +105 -0
  13. package/direct-table.js +261 -0
  14. package/dist/bake.mjs +2211 -0
  15. package/dist/build-CMsvCcZ0.mjs +1 -0
  16. package/dist/build-CkLSZw06.mjs +5514 -0
  17. package/dist/build-tools-Dxyl6XAf.mjs +80 -0
  18. package/dist/client-Cm7o6dQd.mjs +17544 -0
  19. package/dist/compiler.mjs +4 -0
  20. package/dist/config-B1W8gfZY.mjs +12 -0
  21. package/dist/dev-Cskd8Hl4.mjs +986 -0
  22. package/dist/direct-report-Bdda-k_Y.mjs +153 -0
  23. package/dist/direct-report-Du44nNWl.mjs +1 -0
  24. package/dist/index.d.mts +176 -0
  25. package/dist/index.mjs +1 -0
  26. package/dist/intl-ROnAp2qO.mjs +75 -0
  27. package/dist/jiti-stub-CKcaShwE.mjs +5 -0
  28. package/dist/native-views-BuzB9x5H.mjs +7849 -0
  29. package/dist/octane/package.json +5 -0
  30. package/dist/package-KkUSMoji.mjs +2784 -0
  31. package/dist/react/package.json +13 -0
  32. package/dist/rolldown-runtime-D3KUYPsf.mjs +12 -0
  33. package/dist/run-D2aPm54a.mjs +6632 -0
  34. package/dist/scope-8W2aIuQ9.mjs +262 -0
  35. package/dist/screens/BakeNativeScreen.native.ts +9 -0
  36. package/dist/screens/package.json +5 -0
  37. package/dom-exception.js +31 -0
  38. package/element-api.js +158 -0
  39. package/first-screen.js +30 -0
  40. package/fonts/Geist-Italic-Variable.ttf +0 -0
  41. package/fonts/Geist-Variable.ttf +0 -0
  42. package/fonts/GeistMono-Italic-Variable.ttf +0 -0
  43. package/fonts/GeistMono-Variable.ttf +0 -0
  44. package/fonts/OFL.txt +93 -0
  45. package/fonts/README.md +27 -0
  46. package/fonts/geist-italic.css +8 -0
  47. package/fonts/geist-mono-italic.css +8 -0
  48. package/fonts/geist-mono.css +8 -0
  49. package/fonts/geist.css +8 -0
  50. package/header-icons.json +53 -0
  51. package/host-runtime.js +508 -0
  52. package/image-props.js +17 -0
  53. package/index.d.ts +8 -0
  54. package/index.js +7 -0
  55. package/licenses/Apache-2.0.txt +73 -0
  56. package/locale-entry.d.ts +1 -0
  57. package/locale-entry.js +1 -0
  58. package/locale.d.ts +28 -0
  59. package/locale.js +124 -0
  60. package/markup-support.js +87 -0
  61. package/media-element.js +115 -0
  62. package/native-args-entry.js +2 -0
  63. package/native-args.d.ts +7 -0
  64. package/native-args.js +155 -0
  65. package/native-entry.js +1 -0
  66. package/native-globals.d.ts +156 -0
  67. package/native-scenarios.d.ts +17 -0
  68. package/native-scenarios.js +46 -0
  69. package/native-support.json +349 -0
  70. package/native.d.ts +52 -0
  71. package/native.js +237 -0
  72. package/package.json +142 -4
  73. package/page-elements.js +123 -0
  74. package/runtime-provides.json +412 -0
  75. package/safe-area.d.ts +1 -0
  76. package/safe-area.js +44 -0
  77. package/swift-ui/modifiers.js +164 -0
  78. package/tab-icons.js +62 -0
  79. package/templates/create/tabs/AGENTS.md +22 -0
  80. package/templates/create/tabs/README.md +43 -0
  81. package/templates/create/tabs/_gitignore +13 -0
  82. package/templates/create/tabs/android/app/build.gradle.kts +79 -0
  83. package/templates/create/tabs/android/app/src/main/AndroidManifest.xml +39 -0
  84. package/templates/create/tabs/android/app/src/main/kotlin/MainActivity.kt +35 -0
  85. package/templates/create/tabs/android/app/src/main/res/values/themes.xml +8 -0
  86. package/templates/create/tabs/android/build.gradle.kts +5 -0
  87. package/templates/create/tabs/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  88. package/templates/create/tabs/android/gradle/wrapper/gradle-wrapper.properties +9 -0
  89. package/templates/create/tabs/android/gradle.properties +3 -0
  90. package/templates/create/tabs/android/gradlew +248 -0
  91. package/templates/create/tabs/android/settings.gradle.kts +47 -0
  92. package/templates/create/tabs/bake.config.ts +10 -0
  93. package/templates/create/tabs/ios/App/App.swift +30 -0
  94. package/templates/create/tabs/ios/build.sh +23 -0
  95. package/templates/create/tabs/ios/project.yml +57 -0
  96. package/templates/create/tabs/macos/Package.swift +24 -0
  97. package/templates/create/tabs/macos/Sources/__SWIFT_NAME__/App.swift +146 -0
  98. package/templates/create/tabs/macos/build.sh +4 -0
  99. package/templates/create/tabs/package.json +23 -0
  100. package/templates/create/tabs/pnpm-workspace.yaml +6 -0
  101. package/templates/create/tabs/src/App.tsrx +92 -0
  102. package/templates/create/tabs/src/App.tsx +67 -0
  103. package/templates/create/tabs/src/styles.css +32 -0
  104. package/templates/create/tabs/tsconfig.json +5 -0
  105. package/templates/create/tabs/windows/Cargo.toml +29 -0
  106. package/templates/create/tabs/windows/build.rs +9 -0
  107. package/templates/create/tabs/windows/src/main.rs +52 -0
  108. package/templates/create/tabs/windows/src/shell.rs +316 -0
  109. package/templates/module/__NAME__.kt +20 -0
  110. package/templates/module/__NAME__.native.ts +24 -0
  111. package/templates/module/__NAME__.swift +25 -0
  112. package/templates/module/__NAME__.web.ts +13 -0
  113. package/templates/module/__NAME__Test.kt +26 -0
  114. package/templates/module/__NAME__Tests.swift +25 -0
  115. package/templates/module/__SNAKE__.rs +43 -0
  116. package/text.d.ts +20 -0
  117. package/text.js +100 -0
  118. package/transform-entry.js +1 -0
  119. package/transform.d.ts +15 -0
  120. package/transform.js +98 -0
  121. package/tsconfig.native.json +16 -0
  122. package/ui/scope.js +103 -0
  123. package/ui/swift.js +106 -0
  124. package/ui/ui.css +272 -0
  125. package/view-ref-writer.js +268 -0
  126. package/view-ref.d.ts +26 -0
  127. package/view-ref.js +85 -0
  128. package/views/Video.native.ts +68 -0
  129. package/views/generated/VideoViewAndroid.h +68 -0
  130. package/views/generated/VideoViewJSI.h +94 -0
  131. package/views/generated/VideoViewSpec.kt +113 -0
  132. package/views/generated/apple/VideoViewJSI/VideoViewJSIApple.cpp +40 -0
  133. package/views/generated/apple/VideoViewJSI/include/VideoViewJSIApple.h +28 -0
  134. package/views/generated/apple/VideoViewSpec/VideoViewInterop.swift +39 -0
  135. package/views/generated/apple/VideoViewSpec/VideoViewSpec.swift +186 -0
  136. package/views/generated/video_view_spec.rs +189 -0
  137. package/views.d.ts +24 -0
  138. package/views.js +45 -0
  139. package/virtual-window.js +35 -0
  140. package/visibility.js +75 -0
  141. package/web-crypto.js +84 -0
  142. package/web-fetch.js +1071 -0
  143. package/README.md +0 -3
@@ -0,0 +1,268 @@
1
+ // The native writer of `View` refs (docs/design/view-ref.md): `setTransform` and `setOpacity`
2
+ // through the core's direct table (direct-table.js), so a write costs neither a render nor a
3
+ // batch, a restyle, a layout or a commit; every view written in a JS turn goes to the core in one
4
+ // call when the turn ends. host-runtime.js installs it in bundles that call one of the two
5
+ // (`bake build`: __BAKE_VIEW_REF__); view-ref.js hands its refs to the React and Octane `View`.
6
+ //
7
+ // A ref writes its view once the core knows it (after the commit that creates the view). Until
8
+ // then it keeps the latest values and asks again at the next frame. A commit that changes the
9
+ // view's declared `style.transform` or `style.opacity` is the later write: that property shows
10
+ // the declared value again (`classify` sees it, `commit` gives the view back). Where a
11
+ // transition, an animation, translate / rotate / scale or a rule owns the property, the core
12
+ // refuses the view: the write does not apply and the reason is reported once.
13
+ import { BOUND, ORDINARY, directRecord, directTable } from './direct-table.js';
14
+ import { transformFunctions, unitOpacity } from './view-ref.js';
15
+
16
+ const TRANSFORM = 1;
17
+ const OPACITY = 2;
18
+ /** Frames a waiting ref keeps asking for its view after the last commit. */
19
+ const IDLE_POLLS = 3;
20
+
21
+ // Regex literals, so a bundle without the writer drops this module whole.
22
+ const TRANSFORM_TEXT = /(?:^|;)\s*transform\s*:\s*([^;]*)/;
23
+ const OPACITY_TEXT = /(?:^|;)\s*opacity\s*:\s*([^;]*)/;
24
+ /** Declared inline `name` of host props' `style` (an object or CSS text), as text: the core
25
+ * compares declared values the same way, so a render that builds an equal `transform()` value
26
+ * again declares nothing new. */
27
+ function declared(style, name) {
28
+ const value = typeof style === 'string'
29
+ ? (name === 'transform' ? TRANSFORM_TEXT : OPACITY_TEXT).exec(style)?.[1].trim() : style?.[name];
30
+ return value == null ? '' : String(value);
31
+ }
32
+
33
+ /** Which of `transform` and `opacity` an update from host props `previous` to `next` declares
34
+ * anew. `changed` (Octane): `next` differs from `previous` only in `next[changed.name]`'s
35
+ * `changed.keys`. */
36
+ function declaredChange(previous, next, changed) {
37
+ if (changed !== undefined) {
38
+ if (changed.name !== 'style') return 0;
39
+ return (changed.keys.includes('transform') ? TRANSFORM : 0) | (changed.keys.includes('opacity') ? OPACITY : 0);
40
+ }
41
+ const a = previous?.style;
42
+ const b = next?.style;
43
+ if (a === b) return 0;
44
+ return (declared(a, 'transform') !== declared(b, 'transform') ? TRANSFORM : 0)
45
+ | (declared(a, 'opacity') !== declared(b, 'opacity') ? OPACITY : 0);
46
+ }
47
+
48
+ const writers = new WeakMap();
49
+
50
+ /** The writer of `global`'s VM. */
51
+ export function nativeViewRefs(global) {
52
+ let writer = writers.get(global);
53
+ if (writer === undefined) writers.set(global, (writer = createWriter(global)));
54
+ return writer;
55
+ }
56
+
57
+ function createWriter(global) {
58
+ const table = directTable(global);
59
+ const refs = new WeakMap();
60
+ // Records whose latest values wait for their view.
61
+ const holding = new Set();
62
+ // Records given back in this JS entry. The core applies their unbind before the next entry,
63
+ // so they bind again from there.
64
+ let released = [];
65
+ // Host props of this commit's updates -> the declared values they change.
66
+ const changes = new Map();
67
+ let views = 0;
68
+ let polling = false;
69
+ let settling = false;
70
+ let idle = 0;
71
+ // Reported once each: the table is full; the runtime has none (then nothing is written).
72
+ let full = false;
73
+ let unavailable = false;
74
+
75
+ function schedule() {
76
+ if (polling) return;
77
+ polling = true;
78
+ if (typeof global.requestAnimationFrame === 'function') global.requestAnimationFrame(poll);
79
+ else global.setTimeout(poll, 16);
80
+ }
81
+ // A new frame: the records that hold values ask for their views again.
82
+ function poll() {
83
+ polling = false;
84
+ for (const e of released) e.view.released = false;
85
+ released = [];
86
+ if (holding.size === 0) return;
87
+ // The runtime installs the table before any frame: a runtime that has none by now never will.
88
+ if (!table.available()) {
89
+ for (const e of [...holding]) {
90
+ e.refused = true;
91
+ e.view.reported = true;
92
+ drop(e);
93
+ }
94
+ unavailable = true;
95
+ return report('View ref: this runtime has no direct table, so setTransform and setOpacity do not apply. Set the transform and opacity through style.', 'E_UNAVAILABLE');
96
+ }
97
+ for (const e of holding) queue(e);
98
+ if (holding.size !== 0) settle();
99
+ }
100
+ // Binds the waiting records when the JS turn ends, so every view first written in a turn is
101
+ // bound by one call and shows its value with the turn's other writes.
102
+ function settle() {
103
+ if (settling) return;
104
+ settling = true;
105
+ queueMicrotask(() => {
106
+ settling = false;
107
+ if (!table.resolve(true)) idle = 0;
108
+ // A loop that writes every frame asks by itself; a single write needs the poll.
109
+ if (holding.size !== 0 && idle++ < IDLE_POLLS) schedule();
110
+ });
111
+ }
112
+ function queue(e) {
113
+ if (e.state !== ORDINARY || e.view.released) return;
114
+ if (table.wait(e)) return;
115
+ drop(e);
116
+ if (full) return;
117
+ full = true;
118
+ report('View ref: more views are written directly than the direct table holds (16,384). The writes to the others do not apply.', 'E_DIRECT_FULL');
119
+ }
120
+ function report(message, code) {
121
+ console.error(message);
122
+ global.__bakeReportError?.({ kind: 'runtime', message, stack: '', code });
123
+ }
124
+ function drop(e) {
125
+ e.view.t = undefined;
126
+ e.view.o = undefined;
127
+ holding.delete(e);
128
+ }
129
+ // Why the core refuses element `el` (`__bakeElement` op 8).
130
+ function refusal(el) {
131
+ try {
132
+ const why = global.__bakeElement(8, el);
133
+ if (typeof why === 'string') return why;
134
+ } catch {}
135
+ return 'something else owns its transform or opacity';
136
+ }
137
+ function refuse(e) {
138
+ drop(e);
139
+ if (e.view.reported) return;
140
+ e.view.reported = true;
141
+ report('View ref: setTransform and setOpacity do not apply to this view (element ' + e.el + '): ' + refusal(e.el)
142
+ + '. Remove that transition, animation or rule from the view, or set its transform and opacity through its style.', 'E_DIRECT_REFUSED');
143
+ }
144
+ // Keeps `e`'s latest values until its view is bound.
145
+ function hold(e) {
146
+ if (e.el === -1) return drop(e);
147
+ if (e.refused) return refuse(e);
148
+ if (unavailable) return drop(e);
149
+ holding.add(e);
150
+ // A write while the bundle first runs (a mount effect) can come before the runtime installed
151
+ // the table: the next frame asks.
152
+ if (!table.available()) return schedule();
153
+ queue(e);
154
+ settle();
155
+ }
156
+ table.onBound = (e) => {
157
+ const v = e.view;
158
+ if (v.t !== undefined) {
159
+ table.transform(e, v.t.$$bakeTransform);
160
+ v.wrote |= TRANSFORM;
161
+ }
162
+ if (v.o !== undefined) {
163
+ table.opacity(e, v.o);
164
+ v.wrote |= OPACITY;
165
+ }
166
+ drop(e);
167
+ };
168
+ table.onRefused = refuse;
169
+ table.onGone = gone;
170
+ function gone(e) {
171
+ views--;
172
+ drop(e);
173
+ }
174
+
175
+ // host-runtime.js calls `classify` for every update and `commit` for every batch.
176
+ function classify(previous, next, changed) {
177
+ if (views === 0) return;
178
+ const mask = declaredChange(previous, next, changed);
179
+ if (mask !== 0) changes.set(next, mask);
180
+ }
181
+ function commit(commands) {
182
+ idle = 0;
183
+ if (views !== 0) {
184
+ for (let i = 0; i < commands.length; i++) {
185
+ const c = commands[i];
186
+ if (c.op === 'destroy') {
187
+ const e = table.byElement.get(c.id);
188
+ if (e !== undefined && e.view !== undefined) {
189
+ table.forget(e);
190
+ gone(e);
191
+ }
192
+ } else if (c.op === 'update' && changes.size !== 0) {
193
+ const mask = changes.get(c.props);
194
+ const e = mask === undefined ? undefined : table.byElement.get(c.id);
195
+ if (e !== undefined && e.view !== undefined) declare(e, mask);
196
+ }
197
+ }
198
+ }
199
+ changes.clear();
200
+ if (holding.size !== 0) schedule();
201
+ }
202
+ // The commit declares `mask`'s properties of `e`'s view anew: the later write.
203
+ function declare(e, mask) {
204
+ const v = e.view;
205
+ if (mask & TRANSFORM) v.t = undefined;
206
+ if (mask & OPACITY) v.o = undefined;
207
+ if (v.t === undefined && v.o === undefined) holding.delete(e);
208
+ if (e.state !== BOUND || (v.wrote & mask) === 0) return;
209
+ // The core shows the declared value of a property this commit changed and keeps the last
210
+ // direct value of the other.
211
+ table.unbind(e);
212
+ v.wrote = 0;
213
+ v.released = true;
214
+ released.push(e);
215
+ schedule();
216
+ }
217
+ function record(el) {
218
+ // A signal bound in the view's style may have the element's record already.
219
+ const e = table.find(el.id) ?? directRecord(el.id);
220
+ if (e.view === undefined) {
221
+ e.view = { t: undefined, o: undefined, wrote: 0, released: false, reported: false };
222
+ views++;
223
+ table.byElement.set(el.id, e);
224
+ }
225
+ return e;
226
+ }
227
+
228
+ class NativeView {
229
+ constructor(el) {
230
+ this.$$bakeView = el;
231
+ // The element's record, made by the first write.
232
+ this.$$bakeRecord = undefined;
233
+ }
234
+ setTransform(t) {
235
+ const f = transformFunctions(t);
236
+ const e = (this.$$bakeRecord ??= record(this.$$bakeView));
237
+ if (e.state === BOUND) {
238
+ table.transform(e, f);
239
+ e.view.wrote |= TRANSFORM;
240
+ return;
241
+ }
242
+ e.view.t = t;
243
+ hold(e);
244
+ }
245
+ setOpacity(o) {
246
+ unitOpacity(o);
247
+ const e = (this.$$bakeRecord ??= record(this.$$bakeView));
248
+ if (e.state === BOUND) {
249
+ table.opacity(e, o);
250
+ e.view.wrote |= OPACITY;
251
+ return;
252
+ }
253
+ e.view.o = o;
254
+ hold(e);
255
+ }
256
+ }
257
+
258
+ return {
259
+ /** The `ViewRef` of host element handle `el`. */
260
+ ref(el) {
261
+ let ref = refs.get(el);
262
+ if (ref === undefined) refs.set(el, (ref = new NativeView(el)));
263
+ return ref;
264
+ },
265
+ classify,
266
+ commit,
267
+ };
268
+ }
package/view-ref.d.ts ADDED
@@ -0,0 +1,26 @@
1
+ import type { Transform } from './transform.js';
2
+
3
+ /**
4
+ * What the `ref` of a `View` holds: the view's transform and opacity, set from a frame loop
5
+ * without a render.
6
+ *
7
+ * const ball = useRef<ViewRef>(null);
8
+ * <View ref={ball} />
9
+ * ball.current?.setTransform(transform().translate(x, y));
10
+ *
11
+ * Both write the view's inline style, as `element.style.transform = ...` does on the web. On
12
+ * native targets the values go straight to the view: no render, no batch, no restyle and no
13
+ * layout. A later render that changes the view's `style.transform` or `style.opacity` sets that
14
+ * property again.
15
+ *
16
+ * A native view cannot be written while something else owns its transform or opacity: a CSS
17
+ * transition or animation on them, the `translate` / `rotate` / `scale` properties, or a rule
18
+ * that wins over its inline `transform`. The value does not apply and the reason is reported once
19
+ * as an error (`E_DIRECT_REFUSED`).
20
+ */
21
+ export interface ViewRef {
22
+ /** Sets the view's transform: `transform().translate(x, y).rotate(deg).scale(s)`. */
23
+ setTransform(transform: Transform): void;
24
+ /** Sets the view's opacity, 0 ... 1. */
25
+ setOpacity(opacity: number): void;
26
+ }
package/view-ref.js ADDED
@@ -0,0 +1,85 @@
1
+ // `View` refs (docs/design/view-ref.md): `ref.current.setTransform(transform().translate(x, y))`
2
+ // and `ref.current.setOpacity(0.5)` set a view's transform and opacity from a frame loop, as
3
+ // `element.style.transform = ...` does on the web. The React and Octane `View` hand out the same
4
+ // ref, one per host element (`viewRefCallback`).
5
+ //
6
+ // Web: the element's inline style. Native: the bundle's writer (view-ref-writer.js), which
7
+ // `bake build` includes when the bundle calls one of the two methods.
8
+
9
+ /** The functions of `transform()` value `t` (kind, x, y, ...). */
10
+ export function transformFunctions(t) {
11
+ const f = t?.$$bakeTransform;
12
+ if (!Array.isArray(f)) throw new TypeError('View ref: setTransform takes a transform() value, for example transform().translate(x, y)');
13
+ for (let i = 1; i < f.length; i += 3) {
14
+ if (!Number.isFinite(f[i] + f[i + 1])) throw new RangeError('View ref: setTransform takes finite numbers (' + t + ')');
15
+ }
16
+ return f;
17
+ }
18
+
19
+ /** Opacity `o`, a number from 0 to 1. */
20
+ export function unitOpacity(o) {
21
+ if (typeof o !== 'number' || !(o >= 0 && o <= 1)) throw new RangeError('View ref: setOpacity takes a number from 0 to 1 (' + o + ')');
22
+ return o;
23
+ }
24
+
25
+ class WebView {
26
+ constructor(el) {
27
+ this.$$bakeView = el;
28
+ }
29
+ setTransform(t) {
30
+ transformFunctions(t);
31
+ this.$$bakeView.style.transform = String(t);
32
+ }
33
+ setOpacity(o) {
34
+ this.$$bakeView.style.opacity = String(unitOpacity(o));
35
+ }
36
+ }
37
+
38
+ // A native bundle without the writer: `bake build` found no `.setTransform` or `.setOpacity` in it.
39
+ function excluded() {
40
+ throw new Error('View ref: this bundle was built without View refs, because bake build found no .setTransform( or .setOpacity( call in it. Call the method by its name on the ref (ref.current.setTransform(...)).');
41
+ }
42
+ const EXCLUDED = { setTransform: excluded, setOpacity: excluded };
43
+
44
+ const webRefs = new WeakMap();
45
+
46
+ /** The `ViewRef` of host element `el`: a DOM element, or a native element handle. */
47
+ export function viewRef(el) {
48
+ if (typeof __BAKE_TARGET__ === 'undefined' ? typeof el.id !== 'number' : __BAKE_TARGET__ === 'web') {
49
+ let ref = webRefs.get(el);
50
+ if (ref === undefined) webRefs.set(el, (ref = new WebView(el)));
51
+ return ref;
52
+ }
53
+ const writer = globalThis.__bakeViewRefs;
54
+ return writer === undefined ? EXCLUDED : writer.ref(el);
55
+ }
56
+
57
+ const hostRefs = new WeakMap();
58
+
59
+ /** The host `ref` of a `View` whose `ref` prop is `ref`: gives it the element's `ViewRef`. One
60
+ * host ref per `ref`, so the renderer keeps it attached across renders. `ref` is a ref object, a
61
+ * callback (which may return its cleanup), or in Octane an array of those. */
62
+ export function viewRefCallback(ref) {
63
+ let host = hostRefs.get(ref);
64
+ if (host === undefined) hostRefs.set(ref, (host = (el) => attach(ref, viewRef(el))));
65
+ return host;
66
+ }
67
+
68
+ function attach(ref, view) {
69
+ if (typeof ref === 'function') {
70
+ const cleanup = ref(view);
71
+ return typeof cleanup === 'function' ? cleanup : () => { ref(null); };
72
+ }
73
+ if (Array.isArray(ref)) {
74
+ const cleanups = ref.map((r) => (r == null ? undefined : attach(r, view)));
75
+ return () => { for (const cleanup of cleanups) cleanup?.(); };
76
+ }
77
+ ref.current = view;
78
+ return () => { ref.current = null; };
79
+ }
80
+
81
+ /** A written `View`'s style: a stacking context on every target, so what its ref writes moves
82
+ * and fades the whole subtree, descendants with a z-index included. */
83
+ export function writtenStyle(style) {
84
+ return typeof style === 'string' ? 'isolation:isolate;' + style : { isolation: 'isolate', ...style };
85
+ }
@@ -0,0 +1,68 @@
1
+ import type { NativeView } from '@bake-native/core';
2
+
3
+ export type Preload = 'auto' | 'metadata' | 'none';
4
+
5
+ export interface Metadata { duration: number; width: number; height: number }
6
+
7
+ export interface Progress { currentTime: number; duration: number }
8
+
9
+ export interface Failure { code: string; message: string }
10
+
11
+ /**
12
+ * The platform player behind the built-in <video> element (bake_core::media): AVPlayerLayer on iOS / macOS, MediaPlayer + TextureView on Android, Media Foundation in a child window on Windows. Internal: apps write <video>; the core turns its attributes, CSS and state into these props and the player's events into the element's DOM events. No system controls.
13
+ * @role group
14
+ * @size 300 150 reports
15
+ * @element video
16
+ */
17
+ export interface Video extends NativeView {
18
+ /** The media URL as the host opens it: asset://<file> (a bundle asset), https: or a path next to the bundle. */
19
+ source?: string;
20
+ /**
21
+ * Start playing once the media can play (the player attaches after hydration, so never on the baked first frame).
22
+ * @default false
23
+ */
24
+ autoplay?: boolean;
25
+ /**
26
+ * @default false
27
+ */
28
+ muted?: boolean;
29
+ /**
30
+ * @default false
31
+ */
32
+ loop?: boolean;
33
+ /**
34
+ * 'none': open the source only on play().
35
+ * @default 'auto'
36
+ */
37
+ preload?: Preload;
38
+ /**
39
+ * Show the picture once a frame is decoded. False while the core draws the poster (until the first play).
40
+ * @default false
41
+ */
42
+ showFrames?: boolean;
43
+ /** The picture's rect (CSS px, from the content box's top-left), from object-fit / object-position. It may overflow the box: the view clips it. Unset: the whole box. */
44
+ fitX?: number;
45
+ fitY?: number;
46
+ fitWidth?: number;
47
+ fitHeight?: number;
48
+ /** The media's duration (s) and natural size (px) are known. */
49
+ onLoadedMetadata(event: Metadata): void;
50
+ /** Playback started (play(), autoplay); a loop restarting is not a new play. */
51
+ onPlay(): void;
52
+ /** Playback paused (pause(), or the end without loop: then onEnded follows). */
53
+ onPause(): void;
54
+ /** Playback reached the end (not while looping). */
55
+ onEnded(): void;
56
+ /**
57
+ * The playback position moved (at most every 250 ms; the last position always arrives).
58
+ * @throttle 250
59
+ */
60
+ onTimeUpdate(event: Progress): void;
61
+ /** The media failed to load or play. */
62
+ onError(event: Failure): void;
63
+ /** Starts playback; resolves once the player accepted it (before the media is ready, once it is). */
64
+ play(): Promise<void>;
65
+ pause(): Promise<void>;
66
+ /** Moves the playback position (currentTime = seconds); resolves once the frame there is shown. */
67
+ seek(seconds: number): Promise<void>;
68
+ }
@@ -0,0 +1,68 @@
1
+ // Generated by `bake native view gen` from Video.native.ts (signature ae26a0af39526134). Do not edit:
2
+ // change the spec and generate again. docs/design/native-views.md
3
+
4
+ // Android: VideoViewJSI.h's Commands, implemented by calling the Kotlin entry points
5
+ // VideoViewSpec.call<Command>(node, ..., promise) through kolibri (no fbjni, no Java_ functions,
6
+ // no lookup by name per call: the class and method ids are resolved once). The JS thread makes the
7
+ // call; Kotlin hops to main once. The app's generated view library (bake native link) installs it
8
+ // with the engine hook and warms the classes in JNI_OnLoad (a native thread cannot find app classes).
9
+ #pragma once
10
+ #include "VideoViewJSI.h"
11
+ #include "BakeNativeViewsAndroid.h" // kolibri, Bindings, PromisePeer (native/android/bakenative/src/main/cpp)
12
+
13
+ namespace bake::views::video::android {
14
+
15
+ using bake::views::android::JBakeNativePromise;
16
+ using bake::views::android::PromisePeer;
17
+ namespace kb = expo::kolibri;
18
+
19
+ /// `dev.bakenative.views.generated.VideoViewSpec` (@CalledFromNative: R8 keeps it and these methods).
20
+ struct JSpec : kb::JavaClass<JSpec> {
21
+ static constexpr std::string_view descriptor = "dev/bakenative/views/generated/VideoViewSpec";
22
+ static constexpr Method<"callPause", void(jlong, kb::UnownedRef<JBakeNativePromise>)> callPause{};
23
+ static constexpr Method<"callPlay", void(jlong, kb::UnownedRef<JBakeNativePromise>)> callPlay{};
24
+ static constexpr Method<"callSeek", void(jlong, jdouble, kb::UnownedRef<JBakeNativePromise>)> callSeek{};
25
+ };
26
+
27
+ class Bridge final : public Commands {
28
+ public:
29
+ Bridge(bake_engine *engine, size_t index) : engine_(engine), index_(index) {}
30
+ void pause(int64_t node, Promise promise) override {
31
+ bake::views::android::Bindings::shared().call(engine_, index_, promise, [node, promise](JNIEnv *env, jobject view) {
32
+ auto peer = PromisePeer::make(env, promise);
33
+ JSpec::callPause(env, view, static_cast<jlong>(node), kb::UnownedRef<JBakeNativePromise>(peer.get()));
34
+ });
35
+ }
36
+ void play(int64_t node, Promise promise) override {
37
+ bake::views::android::Bindings::shared().call(engine_, index_, promise, [node, promise](JNIEnv *env, jobject view) {
38
+ auto peer = PromisePeer::make(env, promise);
39
+ JSpec::callPlay(env, view, static_cast<jlong>(node), kb::UnownedRef<JBakeNativePromise>(peer.get()));
40
+ });
41
+ }
42
+ void seek(int64_t node, double seconds, Promise promise) override {
43
+ bake::views::android::Bindings::shared().call(engine_, index_, promise, [node, promise, seconds = std::move(seconds)](JNIEnv *env, jobject view) {
44
+ auto peer = PromisePeer::make(env, promise);
45
+ JSpec::callSeek(env, view, static_cast<jlong>(node), seconds, kb::UnownedRef<JBakeNativePromise>(peer.get()));
46
+ });
47
+ }
48
+
49
+ private:
50
+ bake_engine *engine_;
51
+ size_t index_;
52
+ };
53
+
54
+ /// JNI_OnLoad (a Java thread): resolves the class and the entry points once.
55
+ inline void warm(JNIEnv *env) {
56
+ JSpec::javaClass(env);
57
+ JSpec::callPause.id(env);
58
+ JSpec::callPlay.id(env);
59
+ JSpec::callSeek.id(env);
60
+ }
61
+
62
+ /// The engine hook (JS thread, before the bundle runs): native.$views.Video.* for `engine`, whose
63
+ /// root serves the view at `index` of the app's views.
64
+ inline void install(facebook::jsi::Runtime &rt, bake_engine *engine, size_t index) {
65
+ video::install(rt, std::make_shared<Bridge>(engine, index));
66
+ }
67
+
68
+ } // namespace bake::views::video::android
@@ -0,0 +1,94 @@
1
+ // Generated by `bake native view gen` from Video.native.ts (signature ae26a0af39526134). Do not edit:
2
+ // change the spec and generate again. docs/design/native-views.md
3
+
4
+ // native.$views.Video.<command>(node, ...args) -> Promise: one JSI host function per command.
5
+ // Each converts its JSI arguments to C++ types here (no JSON, no lookup by name) and calls the
6
+ // platform's generated entry point: Swift (C++ interop: VideoViewSpec.call<Command>), Kotlin
7
+ // (kolibri: VideoViewSpec.call<Command>) or Rust (extern "C" bake_view_video_<command>).
8
+ // The platform side hops to main once, resolves the node to its view (queued until attach;
9
+ // E_DETACHED after removal) and settles the promise on the JS thread through bake::views::Promise.
10
+ #pragma once
11
+ #include <jsi/jsi.h>
12
+ #include <cstdint>
13
+ #include <memory>
14
+ #include <optional>
15
+ #include <string>
16
+ #include "BakeNativeViews.h" // bake::views::Promise, bake::views::define (runtime support)
17
+
18
+ namespace bake::views::video {
19
+
20
+ enum class Preload : int32_t { Auto = 0, Metadata = 1, None = 2 };
21
+ inline std::optional<Preload> PreloadFromIndex(double i) {
22
+ return i >= 0 && i < 3 && i == static_cast<int32_t>(i) ? std::optional<Preload>(static_cast<Preload>(static_cast<int32_t>(i))) : std::nullopt;
23
+ }
24
+
25
+ /// The platform's command entry points (generated per platform; main-thread hop inside).
26
+ struct Commands {
27
+ virtual ~Commands() = default;
28
+ virtual void pause(int64_t node, Promise promise) = 0;
29
+ virtual void play(int64_t node, Promise promise) = 0;
30
+ virtual void seek(int64_t node, double seconds, Promise promise) = 0;
31
+ };
32
+
33
+ /// Installs native.$views.Video.{pause, play, seek} on `rt` (the JS thread, before the bundle runs).
34
+ inline void install(facebook::jsi::Runtime &rt, std::shared_ptr<Commands> impl) {
35
+ using namespace facebook;
36
+ define(rt, "Video", "pause", 1, [impl](jsi::Runtime &rt, const jsi::Value *args, size_t count, Promise promise) {
37
+ if (count < 1 || !args[0].isNumber()) return promise.reject("E_INVALID", "Video.pause: expected (node)");
38
+ impl->pause(static_cast<int64_t>(args[0].getNumber()), std::move(promise));
39
+ });
40
+ define(rt, "Video", "play", 1, [impl](jsi::Runtime &rt, const jsi::Value *args, size_t count, Promise promise) {
41
+ if (count < 1 || !args[0].isNumber()) return promise.reject("E_INVALID", "Video.play: expected (node)");
42
+ impl->play(static_cast<int64_t>(args[0].getNumber()), std::move(promise));
43
+ });
44
+ define(rt, "Video", "seek", 2, [impl](jsi::Runtime &rt, const jsi::Value *args, size_t count, Promise promise) {
45
+ if (count < 2 || !args[0].isNumber()) return promise.reject("E_INVALID", "Video.seek: expected (node, seconds: number)");
46
+ auto seconds = args[1].isNumber() ? std::optional<double>(args[1].getNumber()) : std::nullopt;
47
+ if (!seconds) return promise.reject("E_INVALID", "Video.seek: seconds must be number");
48
+ impl->seek(static_cast<int64_t>(args[0].getNumber()), *seconds, std::move(promise));
49
+ });
50
+ }
51
+
52
+ #if defined(BAKE_VIEWS_RUST_HOST)
53
+ // Rust hosts (Windows): the commands go to the `extern "C"` entry points of video_view_spec.rs, which
54
+ // post them to the host's main thread. Define BAKE_VIEWS_RUST_HOST in exactly one translation unit
55
+ // (it defines bake_view_video_install).
56
+ extern "C" {
57
+ void bake_view_video_pause(const void *host, int64_t node, bake_js_promise *promise);
58
+ void bake_view_video_play(const void *host, int64_t node, bake_js_promise *promise);
59
+ void bake_view_video_seek(const void *host, int64_t node, double seconds, bake_js_promise *promise);
60
+ }
61
+
62
+ struct RustCommands final : Commands {
63
+ RustCommands(const void *host, void (*release)(const void *)) : host(host), release(release) {}
64
+ ~RustCommands() override { if (release) release(host); }
65
+ void pause(int64_t node, Promise promise) override {
66
+ bake_view_video_pause(host, node, promise.release());
67
+ }
68
+ void play(int64_t node, Promise promise) override {
69
+ bake_view_video_play(host, node, promise.release());
70
+ }
71
+ void seek(int64_t node, double seconds, Promise promise) override {
72
+ bake_view_video_seek(host, node, seconds, promise.release());
73
+ }
74
+ const void *host;
75
+ void (*release)(const void *);
76
+ };
77
+
78
+ } // namespace bake::views::video
79
+
80
+ /// Installs the commands on `engine`'s runtime (JS thread); `release(host)` runs when the runtime drops them.
81
+ extern "C" bool bake_view_video_install(void *engine, const void *host, void (*release)(const void *)) {
82
+ auto impl = std::make_shared<bake::views::video::RustCommands>(host, release);
83
+ auto rt = static_cast<facebook::jsi::Runtime *>(bake_engine_jsi(static_cast<bake_engine *>(engine)));
84
+ if (!rt) return false;
85
+ try {
86
+ bake::views::video::install(*rt, impl);
87
+ return true;
88
+ } catch (...) {
89
+ return false;
90
+ }
91
+ }
92
+ #else
93
+ } // namespace bake::views::video
94
+ #endif