@stacksjs/components 0.2.919 → 0.2.921
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/dist/RenderSurface-z9zr4d75.stx +137 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +24 -24
- package/dist/render-surface/render-surface.js +145 -0
- package/dist/ui/render-surface/RenderSurface.d.ts +45 -0
- package/dist/ui/render-surface/index.d.ts +17 -0
- package/dist/ui/render-surface/render-surface.d.ts +94 -0
- package/package.json +8 -3
- package/src/index.ts +1 -0
- package/src/ui/render-surface/README.md +69 -0
- package/src/ui/render-surface/RenderSurface.stx +137 -0
- package/src/ui/render-surface/index.ts +27 -0
- package/src/ui/render-surface/render-surface.ts +244 -0
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
<script server>
|
|
2
|
+
/*
|
|
3
|
+
* <RenderSurface>: a box shared TypeScript draws into, frame by frame, with
|
|
4
|
+
* no WebView (stacksjs/stx#2108, craft-native/craft#578).
|
|
5
|
+
*
|
|
6
|
+
* <RenderSurface class="flex-1" filter="nearest"
|
|
7
|
+
* @ready="loop = startRenderLoop($event.surface, paint)"
|
|
8
|
+
* @restored="loop.wake()" @error="report($event)" />
|
|
9
|
+
*
|
|
10
|
+
* On a native screen it is the host's RenderSurface, drawn with Metal; in a
|
|
11
|
+
* browser it is a <canvas>. Frames go in through `presentFrame` or
|
|
12
|
+
* `startRenderLoop` from `@stacksjs/components/render-surface`, by the name
|
|
13
|
+
* `ready` carries.
|
|
14
|
+
*
|
|
15
|
+
* Props:
|
|
16
|
+
* - `surface`: the name frames are given to. Unique on the screen; when it is
|
|
17
|
+
* left out each instance gets its own. A second live surface of a name
|
|
18
|
+
* already taken stays blank and reports `DUPLICATE`.
|
|
19
|
+
* - `filter`: `nearest` (the default, for pixel art) or `linear`, used when a
|
|
20
|
+
* frame's size differs from the box's pixels.
|
|
21
|
+
* - `opaque`: true by default. A surface that is not opaque takes
|
|
22
|
+
* premultiplied alpha.
|
|
23
|
+
* - `label`: what a screen reader says for it.
|
|
24
|
+
* - `unavailableText`: what shows in its place where nothing can draw one.
|
|
25
|
+
*
|
|
26
|
+
* Events:
|
|
27
|
+
* - `ready` the first time it has a size, then `resize` on each change, both
|
|
28
|
+
* `{ surface, width, height, scale, pixelWidth, pixelHeight }`;
|
|
29
|
+
* - `lost` (`{ surface, reason }`) when the app goes to the background on a
|
|
30
|
+
* phone, and `restored` (the metrics again) when it returns: nothing is
|
|
31
|
+
* kept across a loss, so draw a whole frame again;
|
|
32
|
+
* - `error` with `{ surface, code, message }`: `UNAVAILABLE` where this device
|
|
33
|
+
* cannot draw one (the box then says so rather than staying blank), or
|
|
34
|
+
* `DUPLICATE`.
|
|
35
|
+
*/
|
|
36
|
+
export const surface = $props.surface || `surface-${$uid}`
|
|
37
|
+
export const filter = $props.filter === 'linear' ? 'linear' : 'nearest'
|
|
38
|
+
export const opaque = $bool($props.opaque, true)
|
|
39
|
+
export const className = $props.className || $props.class || ''
|
|
40
|
+
export const label = $props.label || ''
|
|
41
|
+
export const unavailableText = $props.unavailableText || 'This device cannot draw this view.'
|
|
42
|
+
</script>
|
|
43
|
+
|
|
44
|
+
<script client>
|
|
45
|
+
import { claimCanvasSurface, renderSurfaceHost } from './render-surface'
|
|
46
|
+
|
|
47
|
+
const emit = defineEmits(['ready', 'resize', 'lost', 'restored', 'error'])
|
|
48
|
+
const name = {{ surface }}
|
|
49
|
+
const filter = {{ filter }}
|
|
50
|
+
const opaque = {{ opaque }}
|
|
51
|
+
const nativeHost = !!window.stx._host.isNative
|
|
52
|
+
const canvasRef = useRef('canvasRef')
|
|
53
|
+
const unavailable = state(false)
|
|
54
|
+
let claim = null
|
|
55
|
+
let observer = null
|
|
56
|
+
let sized = false
|
|
57
|
+
|
|
58
|
+
// A host event carries its payload as nativeEvent; the shared runtime may
|
|
59
|
+
// also hand it over as it is.
|
|
60
|
+
function payload(event) {
|
|
61
|
+
return (event && event.nativeEvent) || event || {}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function metrics(event) {
|
|
65
|
+
const m = payload(event)
|
|
66
|
+
return { surface: name, width: m.width, height: m.height, scale: m.scale, pixelWidth: m.pixelWidth, pixelHeight: m.pixelHeight }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function fail(error) {
|
|
70
|
+
if (error.code === 'UNAVAILABLE') unavailable.set(true)
|
|
71
|
+
// Reported, never swallowed: a DUPLICATE is a page mistake, and the box
|
|
72
|
+
// stays blank until it is fixed.
|
|
73
|
+
emit('error', { surface: name, code: String(error.code || 'ERROR'), message: String(error.message || '') })
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function hostReady(event) { emit('ready', metrics(event)) }
|
|
77
|
+
function hostResize(event) { emit('resize', metrics(event)) }
|
|
78
|
+
function hostLost(event) { emit('lost', { surface: name, reason: String(payload(event).reason || 'background') }) }
|
|
79
|
+
function hostRestored(event) { emit('restored', metrics(event)) }
|
|
80
|
+
function hostError(event) { fail(payload(event)) }
|
|
81
|
+
|
|
82
|
+
// A browser has no surface events of its own. The canvas's size stands in
|
|
83
|
+
// for them: its first size is `ready`, each change after it `resize`.
|
|
84
|
+
function measure() {
|
|
85
|
+
const canvas = canvasRef.value
|
|
86
|
+
if (!canvas || !claim || claim.error) return
|
|
87
|
+
const box = canvas.getBoundingClientRect()
|
|
88
|
+
if (box.width <= 0 || box.height <= 0) return
|
|
89
|
+
const scale = window.devicePixelRatio || 1
|
|
90
|
+
const detail = { surface: name, width: box.width, height: box.height, scale, pixelWidth: Math.round(box.width * scale), pixelHeight: Math.round(box.height * scale) }
|
|
91
|
+
emit(sized ? 'resize' : 'ready', detail)
|
|
92
|
+
sized = true
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
onMount(() => {
|
|
96
|
+
// Per mount: an :if can take the surface away and bring it back with the
|
|
97
|
+
// same setup, and coming back is a new surface with a ready of its own.
|
|
98
|
+
sized = false
|
|
99
|
+
if (nativeHost) {
|
|
100
|
+
// The host also says UNAVAILABLE once at mount; knowing it here shows the
|
|
101
|
+
// fallback without waiting for that.
|
|
102
|
+
if (renderSurfaceHost() === 'none') unavailable.set(true)
|
|
103
|
+
return
|
|
104
|
+
}
|
|
105
|
+
const canvas = canvasRef.value
|
|
106
|
+
if (!canvas) return
|
|
107
|
+
canvas.style.imageRendering = filter === 'nearest' ? 'pixelated' : 'auto'
|
|
108
|
+
claim = claimCanvasSurface(name, canvas, { opaque })
|
|
109
|
+
if (claim.error) { fail(claim.error); return }
|
|
110
|
+
// The observer's first answer, after layout, is the ready. Measured here
|
|
111
|
+
// instead, a surface mounted again under an :if said ready before the
|
|
112
|
+
// page's listener was back on it, and the page never heard.
|
|
113
|
+
if (typeof ResizeObserver === 'function') {
|
|
114
|
+
observer = new ResizeObserver(() => measure())
|
|
115
|
+
observer.observe(canvas)
|
|
116
|
+
}
|
|
117
|
+
else setTimeout(measure, 0)
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
onDestroy(() => {
|
|
121
|
+
sized = false
|
|
122
|
+
if (observer) observer.disconnect()
|
|
123
|
+
observer = null
|
|
124
|
+
if (claim && claim.release) claim.release()
|
|
125
|
+
claim = null
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
defineExpose({ surface: name, unavailable })
|
|
129
|
+
</script>
|
|
130
|
+
|
|
131
|
+
{{-- data-stx-layout-root: a phone has no display:contents, so the scope around
|
|
132
|
+
this root is a real box there, and an unsized one left the surface 0
|
|
133
|
+
points wide in a row: the host never said ready. --}}
|
|
134
|
+
<div class="relative flex flex-col min-h-0 min-w-0 {{ className }}" data-render-surface data-stx-layout-root>
|
|
135
|
+
<canvas data-native="RenderSurface" ref="canvasRef" surface="{{ surface }}" filter="{{ filter }}" :opaque="{{ opaque }}" role="img" @if(label) aria-label="{{ label }}" @endif class="flex-1 block w-full min-h-0 min-w-0" @ready="hostReady($event)" @resize="hostResize($event)" @lost="hostLost($event)" @restored="hostRestored($event)" @error="hostError($event)"></canvas>
|
|
136
|
+
<div :show="unavailable()" role="status" class="absolute inset-0 flex items-center justify-center p-4 text-center text-sm text-fg-muted">{{ unavailableText }}</div>
|
|
137
|
+
</div>
|
package/dist/index.d.ts
CHANGED
|
@@ -86,6 +86,7 @@ export * from './ui/preview-frame/index';
|
|
|
86
86
|
export * from './ui/progress/index';
|
|
87
87
|
export * from './ui/radio/index';
|
|
88
88
|
export * from './ui/radio-group/index';
|
|
89
|
+
export * from './ui/render-surface/index';
|
|
89
90
|
export * from './ui/segmented-control/index';
|
|
90
91
|
export * from './ui/select/index';
|
|
91
92
|
export * from './ui/settings/index';
|