@glassly/miniapp 0.3.7 → 0.3.8-beta.984

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 (86) hide show
  1. package/dist/background/index.d.ts +1 -1
  2. package/dist/background/index.d.ts.map +1 -1
  3. package/dist/display/index.d.ts +1 -1
  4. package/dist/display/index.d.ts.map +1 -1
  5. package/dist/display/index.js +1 -1
  6. package/dist/display/index.js.map +1 -1
  7. package/dist/display/measurer/TextMeasurer.d.ts.map +1 -1
  8. package/dist/display/measurer/TextMeasurer.js +10 -1
  9. package/dist/display/measurer/TextMeasurer.js.map +1 -1
  10. package/dist/display/measurer/script-detection.d.ts +13 -0
  11. package/dist/display/measurer/script-detection.d.ts.map +1 -1
  12. package/dist/display/measurer/script-detection.js +84 -0
  13. package/dist/display/measurer/script-detection.js.map +1 -1
  14. package/dist/display/profiles/index.d.ts +2 -1
  15. package/dist/display/profiles/index.d.ts.map +1 -1
  16. package/dist/display/profiles/index.js +3 -2
  17. package/dist/display/profiles/index.js.map +1 -1
  18. package/dist/display/profiles/memomind-one.d.ts +3 -0
  19. package/dist/display/profiles/memomind-one.d.ts.map +1 -0
  20. package/dist/display/profiles/memomind-one.js +14 -0
  21. package/dist/display/profiles/memomind-one.js.map +1 -0
  22. package/dist/display/profiles/rayneo-io.d.ts +11 -1
  23. package/dist/display/profiles/rayneo-io.d.ts.map +1 -1
  24. package/dist/display/profiles/rayneo-io.js +73 -3
  25. package/dist/display/profiles/rayneo-io.js.map +1 -1
  26. package/dist/display/profiles/types.d.ts +5 -0
  27. package/dist/display/profiles/types.d.ts.map +1 -1
  28. package/dist/display/wrapper/TextWrapper.d.ts.map +1 -1
  29. package/dist/display/wrapper/TextWrapper.js +13 -18
  30. package/dist/display/wrapper/TextWrapper.js.map +1 -1
  31. package/dist/globals.d.ts +16 -4
  32. package/dist/globals.d.ts.map +1 -1
  33. package/dist/globals.js.map +1 -1
  34. package/dist/index.d.ts +1 -1
  35. package/dist/index.d.ts.map +1 -1
  36. package/dist/index.js.map +1 -1
  37. package/dist/modules/actions.d.ts.map +1 -1
  38. package/dist/modules/actions.js +5 -1
  39. package/dist/modules/actions.js.map +1 -1
  40. package/dist/modules/display.d.ts +21 -0
  41. package/dist/modules/display.d.ts.map +1 -1
  42. package/dist/modules/display.js +19 -0
  43. package/dist/modules/display.js.map +1 -1
  44. package/dist/modules/navigation.d.ts +2 -0
  45. package/dist/modules/navigation.d.ts.map +1 -1
  46. package/dist/modules/navigation.js.map +1 -1
  47. package/dist/protocol.d.ts +13 -0
  48. package/dist/protocol.d.ts.map +1 -1
  49. package/dist/protocol.js +13 -0
  50. package/dist/protocol.js.map +1 -1
  51. package/dist/react/capsuleHeaderLayout.d.ts +8 -1
  52. package/dist/react/capsuleHeaderLayout.d.ts.map +1 -1
  53. package/dist/react/capsuleHeaderLayout.js +11 -0
  54. package/dist/react/capsuleHeaderLayout.js.map +1 -1
  55. package/dist/react/useCapsuleHeaderStyle.d.ts +3 -1
  56. package/dist/react/useCapsuleHeaderStyle.d.ts.map +1 -1
  57. package/dist/react/useCapsuleHeaderStyle.js +3 -1
  58. package/dist/react/useCapsuleHeaderStyle.js.map +1 -1
  59. package/dist/react/useSafeArea.d.ts +13 -7
  60. package/dist/react/useSafeArea.d.ts.map +1 -1
  61. package/dist/react/useSafeArea.js +39 -10
  62. package/dist/react/useSafeArea.js.map +1 -1
  63. package/dist/session.d.ts +8 -0
  64. package/dist/session.d.ts.map +1 -1
  65. package/dist/session.js +16 -0
  66. package/dist/session.js.map +1 -1
  67. package/package.json +2 -2
  68. package/src/background/index.ts +1 -0
  69. package/src/display/index.ts +2 -0
  70. package/src/display/measurer/TextMeasurer.ts +9 -1
  71. package/src/display/measurer/script-detection.ts +90 -0
  72. package/src/display/profiles/index.ts +9 -2
  73. package/src/display/profiles/memomind-one.ts +15 -0
  74. package/src/display/profiles/rayneo-io.ts +79 -3
  75. package/src/display/profiles/types.ts +6 -0
  76. package/src/display/wrapper/TextWrapper.ts +13 -19
  77. package/src/globals.ts +16 -4
  78. package/src/index.ts +1 -0
  79. package/src/modules/actions.ts +14 -7
  80. package/src/modules/display.ts +31 -0
  81. package/src/modules/navigation.ts +2 -0
  82. package/src/protocol.ts +15 -0
  83. package/src/react/capsuleHeaderLayout.ts +16 -1
  84. package/src/react/useCapsuleHeaderStyle.ts +3 -1
  85. package/src/react/useSafeArea.ts +49 -14
  86. package/src/session.ts +20 -0
@@ -6,8 +6,8 @@ import {DisplayProfile} from "./types"
6
6
  * The stock firmware renders subtitle text itself (business 19, font_size 2)
7
7
  * with a near-uniform sans face; these widths are estimates that keep the
8
8
  * phone-side wrapper slightly conservative so a line never overflows on the
9
- * glasses. On the Glassly CFW the engine wraps against the 540×180 scene
10
- * canvas instead and this profile is not used.
9
+ * glasses. Scene text on the Glassly CFW is wrapped with
10
+ * `RAYNEO_IO_CFW_PROFILE` instead.
11
11
  */
12
12
  const RAYNEO_IO_GLYPH_WIDTHS: Record<string, number> = {
13
13
  // Space and punctuation
@@ -135,7 +135,7 @@ const RAYNEO_IO_GLYPH_WIDTHS: Record<string, number> = {
135
135
  * - Max payload: 96 characters per subtitle update
136
136
  *
137
137
  * With the Glassly CFW the engine renders scene frames onto the 540×180
138
- * page instead and wraps against that canvas.
138
+ * page instead and wraps them with `RAYNEO_IO_CFW_PROFILE`.
139
139
  */
140
140
  export const RAYNEO_IO_PROFILE: DisplayProfile = {
141
141
  id: "rayneo-io",
@@ -180,6 +180,82 @@ export const RAYNEO_IO_PROFILE: DisplayProfile = {
180
180
  },
181
181
  }
182
182
 
183
+ /**
184
+ * Advance in px of each glyph of the Glassly CFW's builtin font (SF Pro 20 px,
185
+ * revision 8+, kerning ignored): printable Latin-1 0x20..0xFF, then
186
+ * `RAYNEO_IO_CFW_EXTRA_GLYPHS`. 0 = no glyph. Same table as the Bluetooth SDK's
187
+ * `RayNeoIOCfwText`, which cuts every line wider than its box to these widths.
188
+ */
189
+ const RAYNEO_IO_CFW_ADVANCES = [
190
+ 4, 5, 8, 12, 12, 16, 13, 5, 6, 6, 8, 12, 4, 9, 4, 6, 12, 9, 11, 12, 12, 12, 12, 11, 12, 12, 4, 4, 12, 12, 12, 10, 17,
191
+ 13, 12, 14, 14, 11, 11, 14, 14, 4, 10, 12, 10, 17, 14, 15, 12, 15, 12, 12, 12, 14, 13, 18, 13, 12, 12, 6, 6, 6, 12,
192
+ 11, 10, 10, 11, 10, 11, 10, 6, 11, 11, 4, 4, 10, 4, 16, 11, 11, 11, 11, 6, 9, 6, 11, 10, 14, 9, 10, 9, 6, 4, 6, 12, 0,
193
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 12, 12, 12, 12,
194
+ 4, 11, 10, 17, 7, 12, 12, 1, 12, 10, 8, 12, 6, 6, 10, 12, 11, 4, 10, 5, 7, 12, 14, 14, 15, 10, 13, 13, 13, 13, 13, 13,
195
+ 19, 14, 11, 11, 11, 11, 4, 4, 4, 4, 14, 14, 15, 15, 15, 15, 15, 12, 15, 14, 14, 14, 14, 12, 12, 11, 10, 10, 10, 10,
196
+ 10, 10, 17, 10, 10, 10, 10, 10, 4, 4, 4, 4, 11, 11, 11, 11, 11, 11, 11, 12, 11, 11, 11, 11, 11, 10, 11, 10, 4, 4, 4,
197
+ 7, 7, 7, 11, 17, 12, 9, 12, 15, 18, 18, 16, 16, 17,
198
+ ]
199
+
200
+ /** Code points the revision 9 font carries beyond Latin-1, in firmware table order. */
201
+ const RAYNEO_IO_CFW_EXTRA_GLYPHS = [
202
+ 0x2018, 0x2019, 0x201a, 0x201c, 0x201d, 0x201e, 0x2013, 0x2014, 0x2026, 0x2022, 0x20ac, 0x2122, 0x2190, 0x2192,
203
+ 0x2191, 0x2193, 0x2713,
204
+ ]
205
+
206
+ function cfwGlyphWidths(): Map<string, number> {
207
+ const widths = new Map<string, number>()
208
+ RAYNEO_IO_CFW_ADVANCES.forEach((advance, index) => {
209
+ if (advance === 0) return
210
+ const code = index < 224 ? 0x20 + index : RAYNEO_IO_CFW_EXTRA_GLYPHS[index - 224]
211
+ widths.set(String.fromCodePoint(code), advance)
212
+ })
213
+ return widths
214
+ }
215
+
216
+ /**
217
+ * RayNeo iO with the Glassly CFW: scene text on the 540×180 page.
218
+ *
219
+ * The glasses draw text with their own SF Pro font, one line every 25 px, 2 px
220
+ * in from the box edge, and do not clip it, so the phone cuts any line that is
221
+ * still too wide. Text the font has no glyphs for (CJK, Cyrillic, ...) is
222
+ * rasterized on the phone with the 20 px system font; those widths are
223
+ * estimates.
224
+ */
225
+ export const RAYNEO_IO_CFW_PROFILE: DisplayProfile = {
226
+ ...RAYNEO_IO_PROFILE,
227
+ id: "rayneo-io-cfw",
228
+ name: "RayNeo iO (Glassly CFW)",
229
+
230
+ displayWidthPx: 540,
231
+ displayHeightPx: 180,
232
+ maxLines: 7,
233
+ // The pitch the firmware reports in IDENT.
234
+ lineHeightPx: 25,
235
+ textInsetPx: 2,
236
+
237
+ // Each line goes out as its own inline record of at most 128 bytes; this
238
+ // only has to outlast seven full lines.
239
+ maxPayloadBytes: 1024,
240
+
241
+ fontMetrics: {
242
+ glyphWidths: cfwGlyphWidths(),
243
+ defaultGlyphWidth: 11,
244
+ renderFormula: (glyphWidth: number) => glyphWidth,
245
+ uniformScripts: {
246
+ cjk: 20,
247
+ hiragana: 20,
248
+ katakana: 20,
249
+ korean: 20,
250
+ cyrillic: 13,
251
+ },
252
+ fallback: {
253
+ latinMaxWidth: 19, // 'Æ'
254
+ unknownBehavior: "useLatinMax",
255
+ },
256
+ },
257
+ }
258
+
183
259
  /**
184
260
  * Get the hyphen width for RayNeo iO in pixels.
185
261
  * Used for line breaking calculations.
@@ -27,6 +27,12 @@ export interface DisplayProfile {
27
27
  */
28
28
  lineHeightPx?: number
29
29
 
30
+ /**
31
+ * Inset in px the device keeps on each side when it draws a text box; the
32
+ * scene pipeline wraps to the box width minus twice this. Absent = 0.
33
+ */
34
+ textInsetPx?: number
35
+
30
36
  /** Maximum safe payload size in bytes (for BLE transmission) */
31
37
  maxPayloadBytes: number
32
38
 
@@ -1,5 +1,5 @@
1
1
  import {TextMeasurer} from "../measurer/TextMeasurer"
2
- import {isCJKCharacter, needsHyphenForBreak} from "../measurer/script-detection"
2
+ import {breakUnits, isCJKCharacter, needsHyphenForBreak} from "../measurer/script-detection"
3
3
  import type {WrapOptions, WrapResult, LineMetrics, BreakMode} from "./types"
4
4
  import {DEFAULT_WRAP_OPTIONS} from "./types"
5
5
 
@@ -217,11 +217,11 @@ export class TextWrapper {
217
217
  let currentLine = ""
218
218
  let currentWidth = 0
219
219
 
220
- const chars = Array.from(text)
220
+ const chars = breakUnits(text)
221
221
 
222
222
  for (let i = 0; i < chars.length; i++) {
223
223
  const char = chars[i]
224
- const charWidth = this.measurer.measureChar(char)
224
+ const charWidth = this.measurer.measureText(char)
225
225
 
226
226
  // Would this character fit?
227
227
  if (currentWidth + charWidth <= maxWidthPx) {
@@ -429,14 +429,14 @@ export class TextWrapper {
429
429
  private hyphenateLongWord(word: string, maxWidthPx: number, opts: Required<WrapOptions>): string[] {
430
430
  const lines: string[] = []
431
431
  const hyphenWidth = this.measurer.measureChar(opts.hyphenChar)
432
- const chars = Array.from(word)
432
+ const chars = breakUnits(word)
433
433
 
434
434
  let currentLine = ""
435
435
  let currentWidth = 0
436
436
 
437
437
  for (let i = 0; i < chars.length; i++) {
438
438
  const char = chars[i]
439
- const charWidth = this.measurer.measureChar(char)
439
+ const charWidth = this.measurer.measureText(char)
440
440
  const isLastChar = i === chars.length - 1
441
441
 
442
442
  // Check if we need to break (leaving room for hyphen if not last char)
@@ -485,29 +485,23 @@ export class TextWrapper {
485
485
  hyphenWidth: number,
486
486
  opts: Required<WrapOptions>,
487
487
  ): {line: string; remainder: string; skipHyphen: boolean} {
488
- let adjustedLine = line
488
+ const units = breakUnits(line)
489
+ let kept = units.length
489
490
  let adjustedWidth = lineWidth
490
- let remainder = ""
491
491
 
492
492
  // Remove characters until hyphen fits
493
- while (adjustedWidth + hyphenWidth > maxWidthPx && adjustedLine.length > opts.minCharsBeforeHyphen) {
494
- const lastChar = adjustedLine[adjustedLine.length - 1]
495
- const lastCharWidth = this.measurer.measureChar(lastChar)
496
-
497
- adjustedLine = adjustedLine.slice(0, -1)
498
- adjustedWidth -= lastCharWidth
499
- remainder = lastChar + remainder
493
+ while (adjustedWidth + hyphenWidth > maxWidthPx && kept > opts.minCharsBeforeHyphen) {
494
+ kept--
495
+ adjustedWidth -= this.measurer.measureText(units[kept])
500
496
 
501
497
  // If we've backed off to a space, we've found a natural word boundary
502
498
  // No hyphen is needed - just trim the space and break there
503
- if (adjustedLine.length > 0 && adjustedLine[adjustedLine.length - 1] === " ") {
504
- // Trim trailing space from the line
505
- adjustedLine = adjustedLine.trimEnd()
506
- return {line: adjustedLine, remainder, skipHyphen: true}
499
+ if (kept > 0 && units[kept - 1] === " ") {
500
+ return {line: units.slice(0, kept).join("").trimEnd(), remainder: units.slice(kept).join(""), skipHyphen: true}
507
501
  }
508
502
  }
509
503
 
510
- return {line: adjustedLine, remainder, skipHyphen: false}
504
+ return {line: units.slice(0, kept).join(""), remainder: units.slice(kept).join(""), skipHyphen: false}
511
505
  }
512
506
 
513
507
  /**
package/src/globals.ts CHANGED
@@ -18,6 +18,12 @@ export interface MiniappCapsuleMenuRect {
18
18
  left: number
19
19
  width: number
20
20
  height: number
21
+ /**
22
+ * `corner` floats over the page's top-right; `rail` sits in the iPhone
23
+ * Duo's status rail, inside the side safe-area inset and clear of the page.
24
+ * Absent on hosts older than the field.
25
+ */
26
+ placement?: "corner" | "rail"
21
27
  }
22
28
 
23
29
  export type MiniappColorScheme = "light" | "dark"
@@ -33,12 +39,18 @@ export interface HostGlobals {
33
39
  miniappLocal?: boolean
34
40
  /** True when the miniapp is running via the dev workflow (QR sideload). */
35
41
  miniappDeveloperMode?: boolean
36
- /** Safe-area insets around the WebView content. Miniapps should pad accordingly. */
42
+ /**
43
+ * Safe-area insets around the WebView content. Miniapps should pad
44
+ * accordingly. On an iPhone Duo the status rail is an 84px side inset. The
45
+ * host updates this and dispatches `glassly:safe-area-change` when they
46
+ * change (a Duo folding or unfolding).
47
+ */
37
48
  safeAreaInsets?: MiniappSafeAreaInsets
38
49
  /**
39
- * Bounding rect (in CSS pixels) of the floating capsule menu the host draws
40
- * over the WebView. The menu lives in the top-right and miniapps should
41
- * avoid placing interactive elements beneath it.
50
+ * Bounding rect (in CSS pixels) of the capsule menu the host draws over the
51
+ * WebView: the top-right corner, or the Duo's status rail (see `placement`).
52
+ * Miniapps should avoid placing interactive elements beneath it. Updated
53
+ * with `safeAreaInsets`.
42
54
  */
43
55
  capsuleMenu?: MiniappCapsuleMenuRect
44
56
  /**
package/src/index.ts CHANGED
@@ -88,6 +88,7 @@ export type {
88
88
  RenderOptions,
89
89
  DisplayViewChange,
90
90
  DisplayViewChangeReason,
91
+ PrefetchResult,
91
92
  ShowViewResult,
92
93
  RenderRectStyle,
93
94
  RenderResult,
@@ -27,6 +27,8 @@ export interface ActionContext {
27
27
 
28
28
  export type ActionHandler = (params: Record<string, unknown>, ctx: ActionContext) => unknown | Promise<unknown>
29
29
 
30
+ const INVOKE_WAKE_SLACK_MS = 20_000
31
+
30
32
  export interface InvokeOptions {
31
33
  /** Overall timeout in ms (default 30s, max 120s — host-enforced). */
32
34
  timeoutMs?: number
@@ -96,13 +98,18 @@ export class ActionsModule {
96
98
  params?: Record<string, unknown>,
97
99
  opts?: InvokeOptions,
98
100
  ): Promise<TResult> {
99
- return this.session.sendRequest<TResult>({
100
- type: MiniappRequestType.ACTION_INVOKE,
101
- targetPackageName: packageName,
102
- actionId,
103
- params: params ?? {},
104
- ...(opts?.timeoutMs != null ? {timeoutMs: opts.timeoutMs} : {}),
105
- })
101
+ return this.session.sendRequest<TResult>(
102
+ {
103
+ type: MiniappRequestType.ACTION_INVOKE,
104
+ targetPackageName: packageName,
105
+ actionId,
106
+ params: params ?? {},
107
+ ...(opts?.timeoutMs != null ? {timeoutMs: opts.timeoutMs} : {}),
108
+ },
109
+ // The host arms its handler timeout only after a cold wake (≤10 s) and
110
+ // handler registration (≤5 s); the wire wait must outlast all three.
111
+ {timeoutMs: Math.min(Math.max(opts?.timeoutMs ?? 30_000, 6_000), 120_000) + INVOKE_WAKE_SLACK_MS},
112
+ )
106
113
  }
107
114
 
108
115
  /**
@@ -228,6 +228,16 @@ export interface ShowViewResult {
228
228
  reason?: string
229
229
  }
230
230
 
231
+ /**
232
+ * "queued" = the host has the frame and will keep its fonts and images on the
233
+ * glasses where it can. "unsupported" = the host refused: the app does not hold
234
+ * the dashboard role, or the host has no prefetch.
235
+ */
236
+ export interface PrefetchResult {
237
+ status: "queued" | "unsupported"
238
+ reason?: string
239
+ }
240
+
231
241
  /**
232
242
  * Why the showing view changed. "shown" and "hidden" answer this app's own
233
243
  * showView calls; "ineligible" means the glasses stopped being able to show
@@ -373,6 +383,27 @@ export class DisplayManager {
373
383
  }))
374
384
  }
375
385
 
386
+ /**
387
+ * Hand the host the frame the dashboard view would show if it opened now,
388
+ * without showing it. Glasses that cache fonts and images on the device
389
+ * (Glassly custom firmware) upload what the frame draws with while nothing
390
+ * is on screen, so the next open draws at once instead of waiting for the
391
+ * uploads. Send it while the view is closed, whenever its content changes;
392
+ * `prefetch([])` drops it. Only the active dashboard may prefetch, and only
393
+ * the "dashboard" view.
394
+ */
395
+ prefetch(elements: RenderElement[], options: {view?: "dashboard"} = {}): Promise<PrefetchResult> {
396
+ return this.session
397
+ .sendRequest<PrefetchResult>({type: MiniappRequestType.PREFETCH, view: options.view ?? "dashboard", elements})
398
+ .catch((err) => ({
399
+ status: "unsupported" as const,
400
+ reason:
401
+ typeof err === "object" && err !== null && "message" in err
402
+ ? String((err as {message: unknown}).message)
403
+ : String(err),
404
+ }))
405
+ }
406
+
376
407
  /**
377
408
  * The showing view changed: the host granted the dashboard view, this app
378
409
  * released it or lost the dashboard role, or the glasses stopped being
@@ -145,6 +145,8 @@ export type NavPlaceSuggestion = {
145
145
  mainText: string
146
146
  /** Secondary line, e.g. "66 Mint St, San Francisco". Empty when none. */
147
147
  secondaryText: string
148
+ /** Straight-line distance from `near` in meters. Absent without `near` or on older hosts. */
149
+ distanceMeters?: number
148
150
  }
149
151
 
150
152
  /** A place resolved from a suggestion via `placeDetails` — has coordinates to route to. */
package/src/protocol.ts CHANGED
@@ -52,6 +52,14 @@ export enum MiniappRequestType {
52
52
  */
53
53
  SHOW_VIEW = "miniapp_show_view",
54
54
 
55
+ /**
56
+ * Hand the host the frame a view would show if it opened now
57
+ * (display.prefetch), never displayed: glasses with an on-glass asset cache
58
+ * upload its fonts and images while the panel is idle. Only the active
59
+ * dashboard, for the "dashboard" view.
60
+ */
61
+ PREFETCH = "miniapp_prefetch",
62
+
55
63
  /**
56
64
  * Feed the device's firmware-rendered navigation UI (capability
57
65
  * `hasNativeNavigation`): structured turn-by-turn card fields and
@@ -362,6 +370,13 @@ export enum MiniappResponseType {
362
370
  /** Push: miniapp's foreground/background state changed. */
363
371
  VISIBILITY_CHANGE = "miniapp_visibility_change",
364
372
 
373
+ /**
374
+ * Push: the user opened this already-running miniapp again. Carries
375
+ * {launchSource}: "phone" or "glasses". A fresh spawn gets the source in
376
+ * CONNECT_ACK instead.
377
+ */
378
+ LAUNCH_CHANGE = "miniapp_launch_change",
379
+
365
380
  /**
366
381
  * Push: the host paused or resumed this miniapp. Carries {paused, reason?};
367
382
  * the reason is set while paused ("glasses_disconnected").
@@ -1,4 +1,19 @@
1
- import type {MiniappCapsuleMenuRect} from "../globals"
1
+ import type {MiniappCapsuleMenuRect, MiniappSafeAreaInsets} from "../globals"
2
+
3
+ /**
4
+ * Whether the capsule menu floats over the page content: false when it sits
5
+ * in a side safe-area inset (the iPhone Duo's status rail), where it covers
6
+ * nothing the page lays out. Hosts that predate `placement` report a corner
7
+ * rect on the Duo, which lands inside the rail inset all the same.
8
+ */
9
+ export function capsuleOverContent(
10
+ capsuleMenu: MiniappCapsuleMenuRect,
11
+ viewportWidth: number,
12
+ insets: MiniappSafeAreaInsets,
13
+ ): boolean {
14
+ if (capsuleMenu.placement === "rail") return false
15
+ return capsuleMenu.left < viewportWidth - insets.right && capsuleMenu.left + capsuleMenu.width > insets.left
16
+ }
2
17
 
3
18
  export function getCapsuleHeaderPaddingRight(
4
19
  capsuleMenu: MiniappCapsuleMenuRect,
@@ -4,7 +4,9 @@
4
4
  *
5
5
  * Use this when you want full control over header markup but want the
6
6
  * alignment math done for you. If you just want a stock header, use
7
- * <MiniappHeader> instead.
7
+ * <MiniappHeader> instead. When the capsule sits in the iPhone Duo's status
8
+ * rail, outside the page, the header takes the fallback height and margin
9
+ * and reserves nothing on the right.
8
10
  *
9
11
  * Example:
10
12
  * const style = useCapsuleHeaderStyle()
@@ -2,35 +2,70 @@
2
2
  * @fileoverview useSafeArea — React hook exposing the host's safe-area insets
3
3
  * and the capsule menu rect to miniapp UIs.
4
4
  *
5
- * The host injects these via window.Glassly before content loads. This hook
6
- * reads them once at mount — they don't currently change at runtime (the host
7
- * would have to force a reload to update them, e.g. on orientation change).
5
+ * The host injects these via window.Glassly before content loads and updates
6
+ * them when they change (an iPhone Duo folding moves its status rail),
7
+ * dispatching `glassly:safe-area-change`; the hook re-renders on that event
8
+ * and on viewport resizes.
8
9
  */
9
10
 
10
- import {useState} from "react"
11
+ import {useEffect, useState} from "react"
11
12
 
12
13
  import {getHostGlobals, type MiniappCapsuleMenuRect, type MiniappSafeAreaInsets} from "../globals"
14
+ import {capsuleOverContent} from "./capsuleHeaderLayout"
13
15
 
14
16
  const EMPTY_INSETS: MiniappSafeAreaInsets = {top: 0, bottom: 0, left: 0, right: 0}
15
17
 
16
18
  export interface UseSafeAreaResult {
17
- /** Pixel insets around the WebView content. Apply as padding on your root element. */
19
+ /**
20
+ * Pixel insets around the WebView content. Apply as padding on your root
21
+ * element. On an iPhone Duo the status rail is an 84px left or right inset:
22
+ * let backgrounds run under it and stop content at it.
23
+ */
18
24
  insets: MiniappSafeAreaInsets
19
25
  /**
20
- * Bounding rect of the host's floating capsule menu (top-right overlay).
21
- * Null when the host doesn't render one (e.g. older builds). Use this to
22
- * avoid placing clickable content underneath the menu.
26
+ * Bounding rect of the host's capsule menu when it floats over the page
27
+ * (top-right). Null when the host doesn't render one (e.g. older builds) or
28
+ * draws it outside the content, in the Duo's status rail. Use this to avoid
29
+ * placing clickable content underneath the menu.
23
30
  */
24
31
  capsuleMenu: MiniappCapsuleMenuRect | null
25
32
  }
26
33
 
34
+ function readSafeArea(): UseSafeAreaResult {
35
+ const globals = getHostGlobals()
36
+ const insets = globals.safeAreaInsets ?? EMPTY_INSETS
37
+ const capsuleMenu = globals.capsuleMenu ?? null
38
+ const viewportWidth = typeof window === "undefined" ? Number.POSITIVE_INFINITY : window.innerWidth
39
+ return {
40
+ insets,
41
+ capsuleMenu: capsuleMenu && capsuleOverContent(capsuleMenu, viewportWidth, insets) ? capsuleMenu : null,
42
+ }
43
+ }
44
+
45
+ function sameRect<T extends object>(a: T | null, b: T | null): boolean {
46
+ if (a === b) return true
47
+ if (!a || !b) return false
48
+ return (Object.keys(a) as (keyof T)[]).every((key) => a[key] === b[key])
49
+ }
50
+
27
51
  export function useSafeArea(): UseSafeAreaResult {
28
- const [result] = useState<UseSafeAreaResult>(() => {
29
- const globals = getHostGlobals()
30
- return {
31
- insets: globals.safeAreaInsets ?? EMPTY_INSETS,
32
- capsuleMenu: globals.capsuleMenu ?? null,
52
+ const [result, setResult] = useState<UseSafeAreaResult>(readSafeArea)
53
+ useEffect(() => {
54
+ const update = () =>
55
+ setResult((previous) => {
56
+ const next = readSafeArea()
57
+ return sameRect(previous.insets, next.insets) && sameRect(previous.capsuleMenu, next.capsuleMenu)
58
+ ? previous
59
+ : next
60
+ })
61
+ // The host may have updated the globals between the first render and now.
62
+ update()
63
+ window.addEventListener("glassly:safe-area-change", update)
64
+ window.addEventListener("resize", update)
65
+ return () => {
66
+ window.removeEventListener("glassly:safe-area-change", update)
67
+ window.removeEventListener("resize", update)
33
68
  }
34
- })
69
+ }, [])
35
70
  return result
36
71
  }
package/src/session.ts CHANGED
@@ -298,6 +298,8 @@ type SessionEmitterEvents = {
298
298
  beforeDisconnect: (reason: string) => void
299
299
  disconnect: (reason: string) => void
300
300
  visibility: (v: MiniappVisibility) => void
301
+ /** The user opened the running miniapp again, from the phone or the glasses menu. */
302
+ launch: (source: MiniappLaunchSource) => void
301
303
  pause: (reason: MiniappPauseReason) => void
302
304
  resume: () => void
303
305
  link: (link: MiniappLink) => void
@@ -754,6 +756,15 @@ export class MiniappSession<TChannels extends object = any> {
754
756
  return this.on("visibility", handler)
755
757
  }
756
758
 
759
+ /**
760
+ * Fires each time the user opens this miniapp while it is already running,
761
+ * with where they opened it from. `launchSource` is updated first. The
762
+ * initial source arrives with CONNECT_ACK and does not fire this.
763
+ */
764
+ onLaunch(handler: (source: MiniappLaunchSource) => void): () => void {
765
+ return this.on("launch", handler)
766
+ }
767
+
757
768
  /**
758
769
  * Fires when the host pauses this miniapp: the glasses disconnected and
759
770
  * stayed away. Streams stop and nothing reaches the glasses until
@@ -999,6 +1010,15 @@ export class MiniappSession<TChannels extends object = any> {
999
1010
  return
1000
1011
  }
1001
1012
 
1013
+ case MiniappResponseType.LAUNCH_CHANGE: {
1014
+ const source = payload.launchSource as MiniappLaunchSource | undefined
1015
+ if (source === "phone" || source === "glasses") {
1016
+ this.launchSource = source
1017
+ this.emitter.emit("launch", source)
1018
+ }
1019
+ return
1020
+ }
1021
+
1002
1022
  case MiniappResponseType.PAUSE_CHANGE: {
1003
1023
  const paused = payload.paused === true
1004
1024
  if (paused === this.paused) return