@glassly/miniapp 0.4.1 → 0.4.2-beta.1181

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
package/dist/ui/index.js 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
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@glassly/miniapp",
3
- "version": "0.4.1",
3
+ "version": "0.4.2-beta.1181",
4
4
  "description": "SDK for building Glassly local miniapps (browser/WebView)",
5
5
  "author": "Glassly Labs",
6
6
  "license": "MIT",
@@ -45,6 +45,10 @@
45
45
  "types": "./dist/display/index.d.ts",
46
46
  "default": "./dist/display/index.js"
47
47
  },
48
+ "./host": {
49
+ "types": "./dist/host/index.d.ts",
50
+ "default": "./dist/host/index.js"
51
+ },
48
52
  "./package.json": "./package.json",
49
53
  "./mirror": {
50
54
  "types": "./dist/mirror/index.d.ts",
@@ -73,7 +77,7 @@
73
77
  }
74
78
  },
75
79
  "dependencies": {
76
- "@glassly/cloud-protocol": "0.4.1",
80
+ "@glassly/cloud-protocol": "0.4.2-beta.1181",
77
81
  "eventemitter3": "^5.0.4"
78
82
  },
79
83
  "devDependencies": {
@@ -3,9 +3,10 @@
3
3
  * miniapp needs to lay out a frame the way the host measures it: the glasses'
4
4
  * display profiles (glyph widths, line heights), pixel-accurate measurement
5
5
  * and wrapping, pixel-art icons as scene images, and the scene element types
6
- * the host's render pipeline consumes. Pure TypeScript with no host imports;
7
- * the engine's scene pipeline (`processScene`) uses these same modules, so a
8
- * miniapp that wraps text with them gets the rows the glasses will draw.
6
+ * the host's render pipeline consumes. Pure TypeScript with no host imports.
7
+ * The host's scene pipeline (`processScene`, `diffScene`, `SceneStore`) lives
8
+ * here too: the Glassly App and the desktop simulator both run it, so a
9
+ * miniapp that wraps text with these modules gets the rows the glasses draw.
9
10
  */
10
11
 
11
12
  export type {
@@ -33,6 +34,7 @@ export {
33
34
  RAYNEO_IO_PROFILE,
34
35
  RAYNEO_IO_CFW_PROFILE,
35
36
  MEMOMIND_ONE_PROFILE,
37
+ NIMO_CFW_PROFILE,
36
38
  NIMO_PROFILE,
37
39
  RAYNEO_IO_HYPHEN_WIDTH_PX,
38
40
  RAYNEO_IO_SPACE_WIDTH_PX,
@@ -86,5 +88,15 @@ export {
86
88
  POINT_SHAPE_TYPES,
87
89
  } from "./scene/types"
88
90
 
91
+ export {resolveAnchors} from "./scene/anchor"
92
+ export {diffScene} from "./scene/differ"
93
+ export type {DiffResult, DiffableElement} from "./scene/differ"
94
+ export {degradeScene} from "./scene/degrade"
95
+ export type {DegradedScene} from "./scene/degrade"
96
+ export {processScene, profileLineHeightPx, wrapsOnDevice} from "./scene/process"
97
+ export type {ProcessedScene} from "./scene/process"
98
+ export {SceneStore} from "./scene/store"
99
+ export {compileSvgPath, svgPath} from "./scene/svgPath"
100
+
89
101
  export {SHAPE_TYPES, enterGeometry, sceneGeometry, geometryEqual, tweenAt, levelOpacity} from "../mirror/geometry"
90
102
  export type {MirrorSceneElement, SceneGeometry} from "../mirror/geometry"
@@ -37,4 +37,4 @@ export {
37
37
 
38
38
  export {MEMOMIND_ONE_PROFILE} from "./memomind-one"
39
39
 
40
- export {NIMO_PROFILE} from "./nimo"
40
+ export {NIMO_CFW_PROFILE, NIMO_PROFILE} from "./nimo"
@@ -1,3 +1,4 @@
1
+ import {RAYNEO_IO_CFW_PROFILE} from "./rayneo-io"
1
2
  import type {DisplayProfile} from "./types"
2
3
 
3
4
  /**
@@ -146,3 +147,20 @@ export const NIMO_PROFILE: DisplayProfile = {
146
147
  noEndChars: ["(", "[", "{", "(", "【", "「"],
147
148
  },
148
149
  }
150
+
151
+ /**
152
+ * NIMO glasses on the Glassly CFW: a 540x280 canvas the phone draws text onto
153
+ * with the 20 px system font, as on the MemoMind One, until the firmware has a
154
+ * font of its own. One scene message is at most 12,284 bytes.
155
+ */
156
+ export const NIMO_CFW_PROFILE: DisplayProfile = {
157
+ ...RAYNEO_IO_CFW_PROFILE,
158
+ id: "nimo-cfw",
159
+ name: "NIMO (Glassly CFW)",
160
+ displayWidthPx: 540,
161
+ displayHeightPx: 280,
162
+ maxLines: 10,
163
+ lineHeightPx: 27,
164
+ maxPayloadBytes: 12284,
165
+ bleChunkSize: 501,
166
+ }
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Anchor resolution — turn corner-anchored boxes into plain top-left boxes
3
+ * against the device's real canvas. Runs in emitScene BEFORE the canPosition
4
+ * branch so both processScene (clamping) and degradeScene (reading order)
5
+ * only ever see concrete coordinates.
6
+ */
7
+
8
+ import type {SceneBox, SceneBoxInput, SceneElementInput} from "./types"
9
+
10
+ function resolveBox(box: SceneBoxInput, width: number, height: number): SceneBox {
11
+ const {anchor, x, y, w, h} = box
12
+ const right = anchor === "top-right" || anchor === "bottom-right"
13
+ const bottom = anchor === "bottom-left" || anchor === "bottom-right"
14
+ return {
15
+ x: right ? width - x - w : x,
16
+ y: bottom ? height - y - h : y,
17
+ w,
18
+ h,
19
+ }
20
+ }
21
+
22
+ /**
23
+ * Resolve every anchored box in a scene to top-left coordinates on a W×H
24
+ * canvas. Unanchored elements pass through unchanged (same reference — no
25
+ * copy churn on the common path), as do malformed ones so processScene's
26
+ * validation still sees and reports them. Non-finite insets survive the
27
+ * subtraction as non-finite and are dropped by the same validation.
28
+ */
29
+ export function resolveAnchors(
30
+ input: readonly SceneElementInput[],
31
+ width: number,
32
+ height: number,
33
+ ): SceneElementInput[] {
34
+ return input.map((el) => {
35
+ if (!el || typeof el !== "object" || !el.box || !el.box.anchor) return el
36
+ return {...el, box: resolveBox(el.box, width, height)}
37
+ })
38
+ }
@@ -0,0 +1,83 @@
1
+ /**
2
+ * Degrade — compile a scene for devices that can't position elements
3
+ * (`canPosition: false`: G1, Z100). Output is a LEGACY layout that rides the
4
+ * existing display path — DisplayProcessor wraps it there, exactly as today,
5
+ * which is what keeps sugar byte-identical on old hardware (spec §2/§5:
6
+ * degrade output must be UNWRAPPED; wrapping twice breaks the goldens).
7
+ *
8
+ * Rules (design doc §3.4.8): content-preserving, never-erroring,
9
+ * honestly-reported. Text collapses in reading order (y, then x) joined by
10
+ * blank lines; EXACTLY two text elements sharing a y-range with disjoint
11
+ * x-ranges become a double_text_wall (ColumnComposer path downstream); rects
12
+ * drop silently (decoration); images drop + report.
13
+ */
14
+
15
+ import type {SceneElementInput} from "./types"
16
+
17
+ export interface DegradedScene {
18
+ /** Legacy layout for the existing display path, or null for an empty scene (caller clears). */
19
+ layout: {layoutType: string; [key: string]: unknown} | null
20
+ degraded: boolean
21
+ dropped: string[]
22
+ }
23
+
24
+ type TextEl = Extract<SceneElementInput, {type: "text"}>
25
+
26
+ function yOverlap(a: TextEl, b: TextEl): boolean {
27
+ return a.box.y < b.box.y + b.box.h && b.box.y < a.box.y + a.box.h
28
+ }
29
+
30
+ function xDisjoint(a: TextEl, b: TextEl): boolean {
31
+ return a.box.x + a.box.w <= b.box.x || b.box.x + b.box.w <= a.box.x
32
+ }
33
+
34
+ export function degradeScene(input: readonly SceneElementInput[]): DegradedScene {
35
+ const dropped: string[] = []
36
+ let degraded = false
37
+
38
+ const texts: TextEl[] = []
39
+ input.forEach((el, index) => {
40
+ if (el && typeof el === "object" && Array.isArray((el as {points?: unknown}).points)) {
41
+ return // vector shapes: decoration on a text wall
42
+ }
43
+ if (!el || typeof el !== "object" || !el.box) {
44
+ // Malformed element — report it like the positioning pipeline does
45
+ // instead of silently pretending the render was clean.
46
+ dropped.push((el as {id?: string} | null)?.id ?? `element[${index}]`)
47
+ degraded = true
48
+ return
49
+ }
50
+ if (el.type === "text") {
51
+ texts.push(el)
52
+ } else if (el.type === "image") {
53
+ dropped.push(el.id ?? `image[${index}]`)
54
+ degraded = true
55
+ }
56
+ // rects and other shapes: silent drop — pure decoration on a text wall.
57
+ })
58
+
59
+ if (texts.length === 0) {
60
+ return {layout: null, degraded, dropped}
61
+ }
62
+
63
+ // Row exception: exactly 2 columns with clean separation — the shipped
64
+ // double_text_wall mechanism (scope guard: nothing fancier; this is NOT a
65
+ // layout engine).
66
+ if (texts.length === 2 && yOverlap(texts[0], texts[1]) && xDisjoint(texts[0], texts[1])) {
67
+ const [left, right] = texts[0].box.x <= texts[1].box.x ? [texts[0], texts[1]] : [texts[1], texts[0]]
68
+ return {
69
+ layout: {layoutType: "double_text_wall", topText: left.text ?? "", bottomText: right.text ?? ""},
70
+ degraded,
71
+ dropped,
72
+ }
73
+ }
74
+
75
+ // Reading order: y, then x; join with blank lines. Downstream wrap clips
76
+ // overflow per the device profile.
77
+ const ordered = [...texts].sort((a, b) => a.box.y - b.box.y || a.box.x - b.box.x)
78
+ const text = ordered
79
+ .map((t) => t.text ?? "")
80
+ .filter((t) => t.length > 0)
81
+ .join("\n\n")
82
+ return {layout: {layoutType: "text_wall", text}, degraded, dropped}
83
+ }
@@ -0,0 +1,152 @@
1
+ /**
2
+ * Scene differ — matches elements between the last frame sent to the device and
3
+ * the next scene, and classifies each element's change. Pure set arithmetic:
4
+ * nothing in here is device-specific (spec §1).
5
+ *
6
+ * Matching rules (design doc §3.4.1, in order):
7
+ * 1. same explicit `id` + same `type` ⇒ same element
8
+ * 2. no id: same `type` + identical box ⇒ same element
9
+ * 3. no id: same `type`, by order among the still-unmatched
10
+ * 4. anything unmatched in prev ⇒ removed; unmatched in next ⇒ created
11
+ *
12
+ * Matched no-id elements INHERIT the previous element's assigned id so the
13
+ * device-side id⇒firmware-component mapping stays stable; fresh elements get a
14
+ * synthesized id from the caller (SceneStore counter). Synthesized ids are
15
+ * prefixed "~" so they can never collide with app-supplied ids.
16
+ */
17
+
18
+ import type {FrameElement, SceneBox, SceneChange, SceneElementType, ScenePoint, SceneTransition} from "./types"
19
+ import {boxesEqual} from "./types"
20
+
21
+ /** A processed (clamped/wrapped) element, pre-diff: id optional, no annotation yet. */
22
+ export interface DiffableElement {
23
+ /** SVG path and its checked firmware command stream (Q4 coordinates). */
24
+ d?: string
25
+ path?: number[]
26
+ pathScale?: number
27
+ fillRule?: "nonzero" | "evenodd"
28
+ rotation?: number
29
+ pivot?: ScenePoint
30
+ depth?: number
31
+
32
+ id?: string
33
+ type: SceneElementType
34
+ box: SceneBox
35
+ text?: string
36
+ data?: string
37
+ style?: FrameElement["style"]
38
+ points?: ScenePoint[]
39
+ startDeg?: number
40
+ endDeg?: number
41
+ transition?: SceneTransition
42
+ contentHash: string
43
+ }
44
+
45
+ export interface DiffResult {
46
+ elements: FrameElement[]
47
+ removed: string[]
48
+ }
49
+
50
+ function classify(prev: FrameElement, next: DiffableElement): SceneChange {
51
+ if (!boxesEqual(prev.box, next.box)) return "moved"
52
+ if (prev.contentHash !== next.contentHash) return "updated"
53
+ return "unchanged"
54
+ }
55
+
56
+ /**
57
+ * Assemble the annotated FrameElement, OMITTING optional fields that are
58
+ * undefined. A present key with an `undefined` value cannot cross the Expo
59
+ * Android bridge — its `Map<String, Any>` converter rejects it ("Value is
60
+ * undefined, expected an Object"), which surfaces as ERR_UNEXPECTED from
61
+ * displayEvent. (iOS's `[String: Any]` bridge silently drops nils, so the
62
+ * defect was invisible there.) Keeping the keys absent — not undefined —
63
+ * matches the optional-field contract in types.ts.
64
+ */
65
+ function buildFrameElement(el: DiffableElement, id: string, change: SceneChange): FrameElement {
66
+ const out: FrameElement = {id, type: el.type, box: el.box, contentHash: el.contentHash, change}
67
+ if (el.text !== undefined) out.text = el.text
68
+ if (el.data !== undefined) out.data = el.data
69
+ if (el.style !== undefined) out.style = el.style
70
+ if (el.points !== undefined) out.points = el.points
71
+ if (el.startDeg !== undefined) out.startDeg = el.startDeg
72
+ if (el.endDeg !== undefined) out.endDeg = el.endDeg
73
+ if (el.transition !== undefined) out.transition = el.transition
74
+ if (el.d !== undefined) out.d = el.d
75
+ if (el.path !== undefined) out.path = el.path
76
+ if (el.pathScale !== undefined) out.pathScale = el.pathScale
77
+ if (el.fillRule !== undefined) out.fillRule = el.fillRule
78
+ if (el.rotation !== undefined) out.rotation = el.rotation
79
+ if (el.pivot !== undefined) out.pivot = el.pivot
80
+ if (el.depth !== undefined) out.depth = el.depth
81
+ return out
82
+ }
83
+
84
+ /**
85
+ * Diff `next` against `prev` (the last frame sent for this app/view/device;
86
+ * empty array for a fresh scene). `nextSyntheticId` mints ids for elements the
87
+ * app didn't name — must be stable per (app, view) across calls (SceneStore).
88
+ */
89
+ export function diffScene(
90
+ prev: readonly FrameElement[],
91
+ next: readonly DiffableElement[],
92
+ nextSyntheticId: () => string,
93
+ ): DiffResult {
94
+ const prevUnmatched = new Set(prev.map((_, i) => i))
95
+ const prevByExplicitId = new Map<string, number>()
96
+ prev.forEach((el, i) => {
97
+ if (!el.id.startsWith("~")) prevByExplicitId.set(`${el.type}:${el.id}`, i)
98
+ })
99
+
100
+ // matches[nextIndex] = prevIndex | undefined
101
+ const matches: (number | undefined)[] = new Array(next.length).fill(undefined)
102
+
103
+ // Pass 1: explicit id + type.
104
+ next.forEach((el, i) => {
105
+ if (!el.id) return
106
+ const j = prevByExplicitId.get(`${el.type}:${el.id}`)
107
+ if (j !== undefined && prevUnmatched.has(j)) {
108
+ matches[i] = j
109
+ prevUnmatched.delete(j)
110
+ }
111
+ })
112
+
113
+ // Pass 2: no-id elements match a no-id prev element of the same type with an
114
+ // identical box.
115
+ next.forEach((el, i) => {
116
+ if (el.id || matches[i] !== undefined) return
117
+ for (const j of prevUnmatched) {
118
+ const p = prev[j]
119
+ if (p.id.startsWith("~") && p.type === el.type && boxesEqual(p.box, el.box)) {
120
+ matches[i] = j
121
+ prevUnmatched.delete(j)
122
+ break
123
+ }
124
+ }
125
+ })
126
+
127
+ // Pass 3: remaining no-id elements match remaining no-id prev of the same
128
+ // type in order (k-th unmatched text ↔ k-th unmatched text).
129
+ next.forEach((el, i) => {
130
+ if (el.id || matches[i] !== undefined) return
131
+ for (const j of prevUnmatched) {
132
+ const p = prev[j]
133
+ if (p.id.startsWith("~") && p.type === el.type) {
134
+ matches[i] = j
135
+ prevUnmatched.delete(j)
136
+ break
137
+ }
138
+ }
139
+ })
140
+
141
+ const elements: FrameElement[] = next.map((el, i) => {
142
+ const j = matches[i]
143
+ if (j !== undefined) {
144
+ const p = prev[j]
145
+ return buildFrameElement(el, el.id ?? p.id, classify(p, el))
146
+ }
147
+ return buildFrameElement(el, el.id ?? nextSyntheticId(), "created")
148
+ })
149
+
150
+ const removed = [...prevUnmatched].map((j) => prev[j].id)
151
+ return {elements, removed}
152
+ }