@glassly/miniapp 0.4.1 → 0.4.2-dev.1176

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 (78) hide show
  1. package/README.md +1 -0
  2. package/dist/display/index.d.ts +14 -4
  3. package/dist/display/index.d.ts.map +1 -1
  4. package/dist/display/index.js +11 -4
  5. package/dist/display/index.js.map +1 -1
  6. package/dist/display/profiles/index.d.ts +1 -1
  7. package/dist/display/profiles/index.d.ts.map +1 -1
  8. package/dist/display/profiles/index.js +1 -1
  9. package/dist/display/profiles/index.js.map +1 -1
  10. package/dist/display/profiles/nimo.d.ts +6 -0
  11. package/dist/display/profiles/nimo.d.ts.map +1 -1
  12. package/dist/display/profiles/nimo.js +17 -0
  13. package/dist/display/profiles/nimo.js.map +1 -1
  14. package/dist/display/scene/anchor.d.ts +16 -0
  15. package/dist/display/scene/anchor.d.ts.map +1 -0
  16. package/dist/display/scene/anchor.js +32 -0
  17. package/dist/display/scene/anchor.js.map +1 -0
  18. package/dist/display/scene/degrade.d.ts +25 -0
  19. package/dist/display/scene/degrade.d.ts.map +1 -0
  20. package/dist/display/scene/degrade.js +67 -0
  21. package/dist/display/scene/degrade.js.map +1 -0
  22. package/dist/display/scene/differ.d.ts +50 -0
  23. package/dist/display/scene/differ.d.ts.map +1 -0
  24. package/dist/display/scene/differ.js +129 -0
  25. package/dist/display/scene/differ.js.map +1 -0
  26. package/dist/display/scene/process.d.ts +41 -0
  27. package/dist/display/scene/process.d.ts.map +1 -0
  28. package/dist/display/scene/process.js +408 -0
  29. package/dist/display/scene/process.js.map +1 -0
  30. package/dist/display/scene/store.d.ts +48 -0
  31. package/dist/display/scene/store.d.ts.map +1 -0
  32. package/dist/display/scene/store.js +0 -0
  33. package/dist/display/scene/store.js.map +1 -0
  34. package/dist/display/scene/svgPath.d.ts +9 -0
  35. package/dist/display/scene/svgPath.d.ts.map +1 -0
  36. package/dist/display/scene/svgPath.js +167 -0
  37. package/dist/display/scene/svgPath.js.map +1 -0
  38. package/dist/host/index.d.ts +7 -0
  39. package/dist/host/index.d.ts.map +1 -0
  40. package/dist/host/index.js +7 -0
  41. package/dist/host/index.js.map +1 -0
  42. package/dist/host/uiShim.d.ts +38 -0
  43. package/dist/host/uiShim.d.ts.map +1 -0
  44. package/dist/host/uiShim.js +383 -0
  45. package/dist/host/uiShim.js.map +1 -0
  46. package/dist/host/webviewGlobals.d.ts +60 -0
  47. package/dist/host/webviewGlobals.d.ts.map +1 -0
  48. package/dist/host/webviewGlobals.js +211 -0
  49. package/dist/host/webviewGlobals.js.map +1 -0
  50. package/dist/mirror/canvas.d.ts +13 -2
  51. package/dist/mirror/canvas.d.ts.map +1 -1
  52. package/dist/mirror/canvas.js +42 -21
  53. package/dist/mirror/canvas.js.map +1 -1
  54. package/dist/mirror/index.d.ts +1 -1
  55. package/dist/mirror/index.d.ts.map +1 -1
  56. package/dist/mirror/index.js.map +1 -1
  57. package/dist/modules/display.d.ts +10 -0
  58. package/dist/modules/display.d.ts.map +1 -1
  59. package/dist/modules/display.js.map +1 -1
  60. package/dist/ui/index.d.ts +1 -1
  61. package/dist/ui/index.js +1 -1
  62. package/package.json +6 -2
  63. package/src/display/index.ts +15 -3
  64. package/src/display/profiles/index.ts +1 -1
  65. package/src/display/profiles/nimo.ts +18 -0
  66. package/src/display/scene/anchor.ts +38 -0
  67. package/src/display/scene/degrade.ts +83 -0
  68. package/src/display/scene/differ.ts +152 -0
  69. package/src/display/scene/process.ts +443 -0
  70. package/src/display/scene/store.ts +0 -0
  71. package/src/display/scene/svgPath.ts +163 -0
  72. package/src/host/index.ts +14 -0
  73. package/src/host/uiShim.ts +389 -0
  74. package/src/host/webviewGlobals.ts +251 -0
  75. package/src/mirror/canvas.ts +86 -43
  76. package/src/mirror/index.ts +1 -1
  77. package/src/modules/display.ts +4 -0
  78. package/src/ui/index.ts +1 -1
@@ -62,7 +62,38 @@ function canTween(previous: MirrorElement, next: MirrorElement): boolean {
62
62
  )
63
63
  }
64
64
 
65
- /** Browser-only renderer. Feed snapshots from session.mirror through your UI channel. */
65
+ /** What the renderer needs from its platform; the default is the browser's DOM. */
66
+ export interface MirrorCanvasEnvironment {
67
+ createCanvas(width: number, height: number): HTMLCanvasElement
68
+ loadImage(
69
+ src: string,
70
+ onLoad: (image: CanvasImageSource, width: number, height: number) => void,
71
+ onError: () => void,
72
+ ): void
73
+ createPath(d?: string): Path2D
74
+ requestFrame(callback: () => void): number
75
+ cancelFrame(handle: number): void
76
+ }
77
+
78
+ const domEnvironment: MirrorCanvasEnvironment = {
79
+ createCanvas(width, height) {
80
+ const surface = document.createElement("canvas")
81
+ surface.width = width
82
+ surface.height = height
83
+ return surface
84
+ },
85
+ loadImage(src, onLoad, onError) {
86
+ const image = new Image()
87
+ image.onload = () => onLoad(image, image.naturalWidth, image.naturalHeight)
88
+ image.onerror = onError
89
+ image.src = src
90
+ },
91
+ createPath: (d) => new Path2D(d),
92
+ requestFrame: (callback) => requestAnimationFrame(callback),
93
+ cancelFrame: (handle) => cancelAnimationFrame(handle),
94
+ }
95
+
96
+ /** Feed snapshots from session.mirror through your UI channel. A non-browser host passes its own environment. */
66
97
  export class MirrorCanvasRenderer {
67
98
  private frame: MirrorSnapshot | null = null
68
99
  private poses = new Map<string, Pose>()
@@ -72,7 +103,10 @@ export class MirrorCanvasRenderer {
72
103
  private disposed = false
73
104
  private readonly ctx: CanvasRenderingContext2D
74
105
 
75
- constructor(private readonly canvas: HTMLCanvasElement) {
106
+ constructor(
107
+ private readonly canvas: HTMLCanvasElement,
108
+ private readonly env: MirrorCanvasEnvironment = domEnvironment,
109
+ ) {
76
110
  const ctx = canvas.getContext("2d")
77
111
  if (!ctx) throw new Error("Canvas 2D is unavailable")
78
112
  this.ctx = ctx
@@ -138,9 +172,17 @@ export class MirrorCanvasRenderer {
138
172
  this.draw()
139
173
  }
140
174
 
175
+ redraw(): void {
176
+ this.draw()
177
+ }
178
+
179
+ get loading(): boolean {
180
+ return this.pending.size > 0
181
+ }
182
+
141
183
  dispose(): void {
142
184
  this.disposed = true
143
- if (this.raf !== null) cancelAnimationFrame(this.raf)
185
+ if (this.raf !== null) this.env.cancelFrame(this.raf)
144
186
  this.raf = null
145
187
  this.poses.clear()
146
188
  this.images.clear()
@@ -151,50 +193,51 @@ export class MirrorCanvasRenderer {
151
193
  if (this.images.has(data)) return this.images.get(data) ?? null
152
194
  if (this.pending.has(data)) return null
153
195
  this.pending.add(data)
154
- const image = new Image()
155
- image.onload = () => {
156
- this.pending.delete(data)
157
- if (this.disposed) return
158
- const content = this.frame?.content
159
- if (
160
- !(content?.kind === "bitmap" && content.data === data) &&
161
- !(content?.kind === "scene" && content.elements.some((e) => e.data === data))
162
- )
163
- return
164
- const surface = document.createElement("canvas")
165
- surface.width = image.naturalWidth
166
- surface.height = image.naturalHeight
167
- const ctx = surface.getContext("2d", {willReadFrequently: true})
168
- if (!ctx) return
169
- ctx.drawImage(image, 0, 0)
170
- const pixels = ctx.getImageData(0, 0, surface.width, surface.height)
171
- for (let i = 0; i < pixels.data.length; i += 4) {
172
- const alpha =
173
- ((0.2126 * pixels.data[i] + 0.7152 * pixels.data[i + 1] + 0.0722 * pixels.data[i + 2]) * pixels.data[i + 3]) /
174
- 255
175
- pixels.data[i] = 0
176
- pixels.data[i + 1] = 255
177
- pixels.data[i + 2] = 136
178
- pixels.data[i + 3] = alpha
179
- }
180
- ctx.putImageData(pixels, 0, 0)
181
- this.images.set(data, surface)
182
- this.draw()
183
- }
184
- image.onerror = () => {
185
- this.pending.delete(data)
186
- if (!this.disposed) this.images.set(data, null)
187
- }
188
196
  // Encoded images only: never fetch arbitrary URLs from scene data.
189
- image.src = data.startsWith("data:image/")
197
+ const src = data.startsWith("data:image/")
190
198
  ? data
191
199
  : `data:image/${data.startsWith("Qk") ? "bmp" : "png"};base64,${data}`
200
+ this.env.loadImage(
201
+ src,
202
+ (image, width, height) => {
203
+ this.pending.delete(data)
204
+ if (this.disposed) return
205
+ const content = this.frame?.content
206
+ if (
207
+ !(content?.kind === "bitmap" && content.data === data) &&
208
+ !(content?.kind === "scene" && content.elements.some((e) => e.data === data))
209
+ )
210
+ return
211
+ const surface = this.env.createCanvas(width, height)
212
+ const ctx = surface.getContext("2d", {willReadFrequently: true})
213
+ if (!ctx) return
214
+ ctx.drawImage(image, 0, 0)
215
+ const pixels = ctx.getImageData(0, 0, surface.width, surface.height)
216
+ for (let i = 0; i < pixels.data.length; i += 4) {
217
+ const alpha =
218
+ ((0.2126 * pixels.data[i] + 0.7152 * pixels.data[i + 1] + 0.0722 * pixels.data[i + 2]) *
219
+ pixels.data[i + 3]) /
220
+ 255
221
+ pixels.data[i] = 0
222
+ pixels.data[i + 1] = 255
223
+ pixels.data[i + 2] = 136
224
+ pixels.data[i + 3] = alpha
225
+ }
226
+ ctx.putImageData(pixels, 0, 0)
227
+ this.images.set(data, surface)
228
+ this.draw()
229
+ },
230
+ () => {
231
+ this.pending.delete(data)
232
+ if (!this.disposed) this.images.set(data, null)
233
+ },
234
+ )
192
235
  return null
193
236
  }
194
237
 
195
238
  private draw(): void {
196
239
  if (this.disposed || !this.frame) return
197
- if (this.raf !== null) cancelAnimationFrame(this.raf)
240
+ if (this.raf !== null) this.env.cancelFrame(this.raf)
198
241
  this.raf = null
199
242
  const {canvas, content} = this.frame
200
243
  const w = Math.max(1, Math.round(canvas?.width ?? 1)),
@@ -218,7 +261,7 @@ export class MirrorCanvasRenderer {
218
261
  animating ||= now < pose.start + pose.duration || now < pose.rotationStart + pose.rotationDuration
219
262
  this.paint(pose.element, poseAt(pose, now))
220
263
  }
221
- if (animating) this.raf = requestAnimationFrame(() => this.draw())
264
+ if (animating) this.raf = this.env.requestFrame(() => this.draw())
222
265
  }
223
266
 
224
267
  private paint(element: MirrorElement, g: SceneGeometry): void {
@@ -238,7 +281,7 @@ export class MirrorCanvasRenderer {
238
281
  }
239
282
  if (element.type === "text") {
240
283
  if (g.stroke > 0) {
241
- const border = new Path2D()
284
+ const border = this.env.createPath()
242
285
  border.roundRect(g.x, g.y, g.w, g.h, Math.max(0, Math.min(g.radius, g.w / 2, g.h / 2)))
243
286
  this.path(border, false, g)
244
287
  }
@@ -265,7 +308,7 @@ export class MirrorCanvasRenderer {
265
308
  })
266
309
  return
267
310
  }
268
- let path = new Path2D()
311
+ let path = this.env.createPath()
269
312
  const pts = g.points,
270
313
  cx = g.x + g.w / 2,
271
314
  cy = g.y + g.h / 2,
@@ -305,7 +348,7 @@ export class MirrorCanvasRenderer {
305
348
  break
306
349
  case "svg":
307
350
  if (!element.d) return
308
- path = new Path2D(element.d)
351
+ path = this.env.createPath(element.d)
309
352
  ctx.translate(g.x, g.y)
310
353
  ctx.scale(g.pathScale, g.pathScale)
311
354
  break
@@ -1,2 +1,2 @@
1
- export {MirrorCanvasRenderer} from "./canvas"
1
+ export {MirrorCanvasRenderer, type MirrorCanvasEnvironment} from "./canvas"
2
2
  export type {MirrorSnapshot, MirrorContent, MirrorElement} from "./types"
@@ -117,6 +117,10 @@ export interface RenderTransition {
117
117
  durationMs: number
118
118
  /** Default "ease". */
119
119
  easing?: RenderEasing
120
+ /** New to the screen: start this far off at ink 0 and tween into place. */
121
+ enter?: {dx?: number; dy?: number}
122
+ /** Leaving: tween this far off to ink 0; keep sending it until durationMs has passed. */
123
+ exit?: {dx?: number; dy?: number}
120
124
  }
121
125
 
122
126
  /**
package/src/ui/index.ts CHANGED
@@ -13,7 +13,7 @@
13
13
  * bus via `glassly.send(channel, payload)`.
14
14
  *
15
15
  * The `glassly` global is injected into every UI WebView at mount time
16
- * by the host's `uiShim` (see `@glassly/engine/services/uiShim`).
16
+ * by the host's `uiShim` (see `@glassly/miniapp/host`).
17
17
  * This entry point exports its TypeScript declaration so authors get
18
18
  * autocomplete and compile-time errors on channel names + payloads.
19
19
  */