@marver-design/marver 0.26.1 → 0.27.2

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 (109) hide show
  1. package/CHANGELOG.md +198 -0
  2. package/README.md +12 -4
  3. package/dist/{anchor-check-l-ny3UzH.mjs → anchor-check-gkxkPAIv.mjs} +1 -1
  4. package/dist/{board-tree-DRioF94d.mjs → board-tree-BXR8dw4b.mjs} +3 -1
  5. package/dist/{boards-CGg6piKF.mjs → boards-BHKP5yhI.mjs} +1 -1
  6. package/dist/{boards-DYG22tOe.mjs → boards-BqUqKNW8.mjs} +4 -4
  7. package/dist/{build-BDRiO_mP.mjs → build-VgNg4rr8.mjs} +8 -23
  8. package/dist/cli.mjs +45 -19
  9. package/dist/{collab-Dxg2trOK.mjs → collab-BEAcXrBb.mjs} +1 -1
  10. package/dist/{comments-BqXua-XL.mjs → comments-YEU1Zhv9.mjs} +4 -4
  11. package/dist/{context-HQ4Giw58.mjs → context-Ccjrhr1J.mjs} +3 -3
  12. package/dist/{daemon-DL6Fwq6D.mjs → daemon-Dc9AH0M3.mjs} +9 -7
  13. package/dist/detect-C0wpaePX.mjs +125 -0
  14. package/dist/dev-lock-C_FC70Mu.mjs +65 -0
  15. package/dist/dev-ri6nAlRp.mjs +1273 -0
  16. package/dist/exclusive-B1RtpeeP.mjs +150 -0
  17. package/dist/{init-D8DtrK_F.mjs → init-W0nxR1OY.mjs} +8 -6
  18. package/dist/{link-BNzLvdhI.mjs → link-CdTgn94Y.mjs} +23 -11
  19. package/dist/ls-BZ5TAtd5.mjs +38 -0
  20. package/dist/{manifest-Nl3KSVOk.mjs → manifest-aFyDmL8C.mjs} +2 -2
  21. package/dist/{marks-DyWMv_Z1.mjs → marks-BF9pNEgH.mjs} +3 -3
  22. package/dist/{marks-CKgzucHZ.mjs → marks-CnCCfAbD.mjs} +3 -3
  23. package/dist/{marver-id-gate-BjF6v4bT.mjs → marver-id-gate-D-R0hSt_.mjs} +1 -1
  24. package/dist/perf-DkAUFT6W.mjs +1319 -0
  25. package/dist/perf-jam-BaH3Lg0m.mjs +635 -0
  26. package/dist/plugin-B8UghYuF.mjs +5242 -0
  27. package/dist/{poster-BdtSKMDm.mjs → poster-BNgafWMK.mjs} +1 -1
  28. package/dist/{profile-WU9qcbIo.mjs → profile-jIS6BcAs.mjs} +1 -1
  29. package/dist/{publish-bakes-9RzRToXV.mjs → publish-bakes-38Nja78p.mjs} +3 -3
  30. package/dist/{queue-29ok5_eZ.mjs → queue-CAM_vNmX.mjs} +1 -1
  31. package/dist/recover-B8AiI29B.mjs +66 -0
  32. package/dist/{serve-C8rVheYk.mjs → serve-Dp3nHstu.mjs} +3 -3
  33. package/dist/{share--oFJ1bCI.mjs → share-Ce4tP7ji.mjs} +1 -1
  34. package/dist/{shot-XQV7HC9A.mjs → shot-CMQgMVZm.mjs} +1 -1
  35. package/dist/{shot-DQeR_Pfn.mjs → shot-Ch24tiMO.mjs} +36 -5
  36. package/dist/slots-CFNnegCc.mjs +478 -0
  37. package/dist/{sync-DKk-yT1V.mjs → sync-CQdGARJ9.mjs} +4 -3
  38. package/dist/{work-VvX_JIgl.mjs → work-Bxz-ObF3.mjs} +4 -2
  39. package/dist/{work-F5u-twpJ.mjs → work-CNhFF9pU.mjs} +5 -3
  40. package/docs/boards-and-folders.md +3 -1
  41. package/docs/jam-session.md +57 -2
  42. package/docs/performance.md +424 -0
  43. package/package.json +2 -2
  44. package/src/client/content/index.tsx +3 -0
  45. package/src/client/content/video.tsx +16 -4
  46. package/src/client/frame-host/bridge.js +22 -6
  47. package/src/client/frame-host/main.tsx +17 -5
  48. package/src/client/shell/App.tsx +188 -26
  49. package/src/client/shell/BoardList.tsx +5 -5
  50. package/src/client/shell/Comments.tsx +8 -5
  51. package/src/client/shell/ContextMenu.tsx +5 -3
  52. package/src/client/shell/JamSurface.tsx +4 -2
  53. package/src/client/shell/LockedApp.tsx +8 -5
  54. package/src/client/shell/Play.tsx +20 -10
  55. package/src/client/shell/Tip.tsx +3 -1
  56. package/src/client/shell/Toolbar.tsx +12 -1
  57. package/src/client/shell/api.ts +233 -0
  58. package/src/client/shell/app-bridge.ts +254 -0
  59. package/src/client/shell/app-perf.ts +92 -0
  60. package/src/client/shell/canvas/Canvas.tsx +141 -21
  61. package/src/client/shell/canvas/FrameNode.tsx +22 -9
  62. package/src/client/shell/canvas/admission.ts +31 -9
  63. package/src/client/shell/canvas/camera-broadcast.ts +15 -5
  64. package/src/client/shell/canvas/ctl.ts +8 -0
  65. package/src/client/shell/canvas/frame-registry.ts +28 -2
  66. package/src/client/shell/canvas/sleep.ts +60 -17
  67. package/src/client/shell/canvas/updates.ts +2 -1
  68. package/src/client/shell/comments-store.ts +12 -4
  69. package/src/client/shell/goto.ts +22 -10
  70. package/src/client/shell/inset.ts +44 -0
  71. package/src/client/shell/jam-capture.ts +41 -22
  72. package/src/client/shell/jam-keys.ts +12 -3
  73. package/src/client/shell/jam-panel.tsx +9 -3
  74. package/src/client/shell/jam-queue.ts +3 -3
  75. package/src/client/shell/jam-session.tsx +14 -8
  76. package/src/client/shell/jam-voice.ts +55 -15
  77. package/src/client/shell/legacy-links.ts +2 -2
  78. package/src/client/shell/main.tsx +18 -1
  79. package/src/client/shell/marks-ui.tsx +5 -3
  80. package/src/client/shell/marver-app.ts +21 -0
  81. package/src/client/shell/notes.ts +8 -1
  82. package/src/client/shell/perf-run-probe.ts +34 -0
  83. package/src/client/shell/perf-run.ts +70 -0
  84. package/src/client/shell/perf.ts +80 -58
  85. package/src/client/shell/recorder-core.ts +183 -0
  86. package/src/client/shell/recorder.ts +607 -0
  87. package/src/client/shell/store.ts +294 -110
  88. package/src/client/shell/styles.css +72 -15
  89. package/src/client/shell/unsaved.ts +81 -0
  90. package/src/client/shell/whiteout-probes.ts +141 -0
  91. package/src/shared/app-api.ts +205 -0
  92. package/src/shared/app-perf.ts +308 -0
  93. package/src/shared/logo-sniff.ts +86 -0
  94. package/src/shared/machine.ts +132 -0
  95. package/src/shared/perf-run.ts +177 -0
  96. package/templates/AGENTS-embedded.md +25 -4
  97. package/templates/AGENTS-studio.md +25 -4
  98. package/templates/demo/dark.tsx +1 -1
  99. package/templates/demo/welcome.tsx +1 -1
  100. package/templates/instructions/boards.md +3 -1
  101. package/templates/instructions/welcome.md +6 -3
  102. package/dist/bake-C-awKgLA.mjs +0 -786
  103. package/dist/config-Cf7EjAMf.mjs +0 -292
  104. package/dist/dev-Bw_qYWWq.mjs +0 -755
  105. package/dist/dev-lock-DWSpa6nj.mjs +0 -179
  106. package/dist/perf-C75MZ73h.mjs +0 -167
  107. package/dist/perf-DPbLGhhO.mjs +0 -49
  108. package/dist/perf-jam-BU3TVrZu.mjs +0 -239
  109. package/dist/plugin-DkGmZs_r.mjs +0 -2423
@@ -0,0 +1,254 @@
1
+ /**
2
+ * The shell's side of the Mac app (spec 28 A2.9; A2.14 #9, #13, #18, #26; v1.12 r1 #1, v1.13 r2 #1): what the canvas
3
+ * tells the app through `window.marverApp`, and what it does when the app speaks. Inert anywhere else - in Chrome, on
4
+ * a published canvas, under `marver shot` there is no `window.marverApp` (marver-app.ts), and nothing here listens,
5
+ * sends or runs a timer.
6
+ *
7
+ * What the shell owes the app is A2.9's order and whole states: `mv:hello` first (its version, the board its link
8
+ * names, and its `PAGE_ID`, the id the recorder stamps on every record), then `mv:theme`, `mv:busy` and `mv:menu`, each
9
+ * sent whole and again whenever it changes. The admission - the app binding a document at its trusted commit and the
10
+ * preload holding what the page said until then, replaying the latest of each, hello first (v1.12 r1 #1) - is the
11
+ * preload's: `window.marverApp` is the same before and after, and the shell never sees a `doc`.
12
+ *
13
+ * - `mv:busy` is `{ busy, unsaved, why }` from unsaved.ts's reasons: sent once at boot, so the app's record of a page
14
+ * that runs is unknown for one turn at most (v1.13 r2 #1), then on every change of either;
15
+ * - `mv:theme` is the ground under the strip: `play || viewTheme === 'dark'` (A2.14 #26; slides are play);
16
+ * - `mv:menu` is the canvas's View and Jam items, each with its key as its `hint` - the app shows it in the item's
17
+ * name, never as an accelerator, so the key stays the page's (A2.7); a click replays that key, so the menu does
18
+ * exactly what the key does;
19
+ * - `mv:app-state`: `shown` runs a resume reconcile, `untrusted` stops every microphone track the page holds (A2.14
20
+ * #13), `hidden` changes nothing (a hidden tab keeps running, A2.6).
21
+ *
22
+ * The one ground (0.27.2, A3.8 #27, #31): `mv:hello` says the inset the shell applied (inset.ts's `TOP`: 44 only when
23
+ * the app said so and gave the whole presentation contract), and each `mv:present { epoch, w, h, frames }` is answered
24
+ * once the viewport has that size (500 ms at most) and `frames` animation frames have passed, with the size it was
25
+ * answered at - a newer request cancels an older wait, which is never answered. A request that never fits is answered
26
+ * at the size found; main refuses it and the tab stays covered. rAF does not run in a hidden page, so a request to a
27
+ * page nobody sees waits by itself. App 0.1 says no inset and asks nothing: none of it runs there.
28
+ */
29
+ import { BOUNDS, type AppState, type Busy, type Hello, type MarverAppV1, type MenuItem, type Present } from '../../shared/app-api.ts'
30
+ import { APP } from './marver-app.ts'
31
+ import { TOP } from './inset.ts'
32
+ import { api, canvasStopped } from './api.ts'
33
+ import { asksBeforeLeaving, busyWhen, busyWhy, isUnsaved, leaveNow, onWorkChange, workChanged } from './unsaved.ts'
34
+ import { CONFIG, PUBLISHED, SOURCE_REVEALED, useStore } from './store.ts'
35
+ import { PAGE_ID } from './recorder.ts'
36
+ import { bootHash, UNPLACED } from './hash.ts'
37
+ import { useComments } from './comments-store.ts'
38
+ import { useNotes } from './notes.ts'
39
+ import { useJamCapture } from './jam-capture.ts'
40
+ import { useJamQueue } from './jam-queue.ts'
41
+ import { useJamUi } from './jam-state.ts'
42
+ import { useJamVoice, voiceUntrusted } from './jam-voice.ts'
43
+
44
+ /** The ground under the strip (A2.14 #26): the shell's own - never a frame's pinned theme. */
45
+ export const shellDark = (): boolean => { const s = useStore.getState(); return !!s.play || s.viewTheme === 'dark' }
46
+
47
+ /** `mv:hello`: within the contract's bounds, or the honest fallback (an unknown version, no board). */
48
+ export function helloNow(): Hello {
49
+ const v = CONFIG.version
50
+ const version = typeof v === 'string' && v.length <= BOUNDS.version && BOUNDS.versionChars.test(v) ? v : '0.0.0'
51
+ const b = bootHash.board
52
+ return { version, board: typeof b === 'string' && b.length <= BOUNDS.board ? b : null, page: PAGE_ID, inset: TOP }
53
+ }
54
+
55
+ /** How long the shell waits for its viewport to reach a request's size before it answers with the size it has. */
56
+ export const PRESENT_FIT_MS = 500
57
+
58
+ /**
59
+ * The presentation's answer (A3.8 #27): one request at a time - a newer one cancels the wait of an older, which is
60
+ * never answered. Wait for the asked viewport (1 px either way), then `frames` animation frames (1-8), then answer
61
+ * with the viewport as it is. Exported for the browser tests' fake bridge; startAppBridge installs it.
62
+ */
63
+ export function answerPresents(app: Pick<MarverAppV1, 'onPresent' | 'presented'>, call: (fn: () => void) => void = (fn) => fn()): () => void {
64
+ if (typeof app.onPresent !== 'function' || typeof app.presented !== 'function') return () => {}
65
+ let want = -1
66
+ return app.onPresent((r: Present) => {
67
+ if (!r || typeof r.epoch !== 'number') return
68
+ want = r.epoch
69
+ const since = performance.now()
70
+ const frames = Math.min(BOUNDS.frames, Math.max(1, r.frames | 0))
71
+ const step = (left: number) => requestAnimationFrame(() => {
72
+ if (want !== r.epoch) return // superseded: never answered
73
+ const fits = Math.abs(innerWidth - r.w) <= 1 && Math.abs(innerHeight - r.h) <= 1
74
+ if (!fits && performance.now() - since < PRESENT_FIT_MS) return step(left) // the resize has not reached this document yet
75
+ if (fits && left > 1) return step(left - 1)
76
+ call(() => app.presented!({ epoch: r.epoch, w: innerWidth, h: innerHeight }))
77
+ })
78
+ step(frames)
79
+ })
80
+ }
81
+
82
+ /** `mv:busy` now. */
83
+ export function busyNow(): Busy {
84
+ const why = busyWhy()
85
+ return { busy: why.length > 0, unsaved: why.some(isUnsaved), why }
86
+ }
87
+
88
+ // ---- the menu (A2.7: the View and Jam items, today's keys) --------------------------------------------------
89
+
90
+ interface Item {
91
+ id: string
92
+ group: MenuItem['group']
93
+ label: string
94
+ hint: string
95
+ /** the key a click replays - what App.tsx, Play.tsx and jamKey read: key, code and the modifiers */
96
+ key: KeyboardEventInit
97
+ /** a checkbox item: its mark is the page's state */
98
+ checked?: () => boolean
99
+ /** offered only while this answers true */
100
+ when?: () => boolean
101
+ }
102
+ const jamOn = () => !PUBLISHED && !!CONFIG.jam
103
+ const laser = (inspect: boolean) => () => { const s = useStore.getState(); return s.laser && s.laserInspect === inspect }
104
+ const ITEMS: readonly Item[] = [
105
+ { id: 'fit-all', group: 'view', label: 'Fit All', hint: '⇧1', key: { key: '!', code: 'Digit1', shiftKey: true } },
106
+ { id: 'fit-selection', group: 'view', label: 'Fit Selection', hint: '⇧2', key: { key: '@', code: 'Digit2', shiftKey: true } },
107
+ { id: 'actual-size', group: 'view', label: 'Actual Size', hint: '⇧0', key: { key: ')', code: 'Digit0', shiftKey: true } },
108
+ { id: 'play', group: 'view', label: 'Play', hint: 'P', key: { key: 'p', code: 'KeyP' } },
109
+ { id: 'hide-ui', group: 'view', label: 'Hide UI', hint: 'H', key: { key: 'h', code: 'KeyH' }, checked: () => document.body.classList.contains('sh-hide-ui') },
110
+ { id: 'tidy', group: 'view', label: 'Tidy', hint: 'T', key: { key: 't', code: 'KeyT' } },
111
+ { id: 'comments', group: 'view', label: 'Comments', hint: 'C', key: { key: 'c', code: 'KeyC' }, checked: () => useComments.getState().commentMode },
112
+ { id: 'pins', group: 'view', label: 'Pins', hint: '⇧C', key: { key: 'C', code: 'KeyC', shiftKey: true }, checked: () => useComments.getState().show },
113
+ { id: 'notes', group: 'view', label: 'Notes', hint: 'N', key: { key: 'n', code: 'KeyN' }, checked: () => useNotes.getState().all },
114
+ { id: 'laser', group: 'view', label: 'Laser', hint: 'L', key: { key: 'l', code: 'KeyL' }, checked: laser(false), when: () => SOURCE_REVEALED },
115
+ // with Option a Mac types a symbol (⌥L is "¬"): the shell names an Alt shortcut by the key pressed
116
+ { id: 'inspect', group: 'view', label: 'Inspect', hint: '⌥L', key: { key: '¬', code: 'KeyL', altKey: true }, checked: laser(true), when: () => SOURCE_REVEALED },
117
+ // D cycles the themes: a plain item, no mark (A2.14 #19)
118
+ { id: 'theme', group: 'view', label: 'Theme', hint: 'D', key: { key: 'd', code: 'KeyD' }, when: () => CONFIG.themes.length > 1 },
119
+ { id: 'session', group: 'jam', label: 'Session', hint: 'J', key: { key: 'j', code: 'KeyJ' }, checked: () => !!useJamQueue.getState().session, when: jamOn },
120
+ { id: 'capture', group: 'jam', label: 'Capture', hint: 'S', key: { key: 's', code: 'KeyS' }, checked: () => !!useJamCapture.getState().current, when: jamOn },
121
+ { id: 'lane', group: 'jam', label: 'The Lane', hint: 'Q', key: { key: 'q', code: 'KeyQ' }, checked: () => useJamUi.getState().laneKeys, when: jamOn },
122
+ { id: 'next-review', group: 'jam', label: 'Next to Review', hint: 'V', key: { key: 'v', code: 'KeyV' }, when: jamOn },
123
+ ]
124
+ const offered = (it: Item) => { try { return it.when?.() ?? true } catch { return false } }
125
+
126
+ /** `mv:menu` now: at most 40 items, ids, labels and hints within the contract's bounds. */
127
+ export function menuNow(): MenuItem[] {
128
+ return ITEMS.filter(offered).slice(0, BOUNDS.menuItems).map((it) => ({
129
+ id: it.id, group: it.group, label: it.label, hint: it.hint,
130
+ ...(it.checked ? { checked: (() => { try { return !!it.checked!() } catch { return false } })() } : {}),
131
+ }))
132
+ }
133
+
134
+ /** A menu item chosen in the app: its key, replayed on the shell's window - the same handlers, the same rules (a
135
+ * text field holding the focus does not stop it: the person chose the item). An id the page does not offer now
136
+ * does nothing. One id is the app's own, never in the menu: the tab's right-click Mark All as Seen clears every
137
+ * frame's "new" in this canvas, here and on disk, as the sidebar's Mark all as seen does for a board. */
138
+ export function runMenuItem(id: string): void {
139
+ if (id === 'mark-all-seen') {
140
+ const s = useStore.getState()
141
+ if (!PUBLISHED && s.marks) s.markSeen(Object.keys(s.marks.changed))
142
+ return
143
+ }
144
+ const it = ITEMS.find((x) => x.id === id)
145
+ if (!it || !offered(it)) return
146
+ window.dispatchEvent(new KeyboardEvent('keydown', { ...it.key, bubbles: true, cancelable: true }))
147
+ }
148
+
149
+ // ---- coming back into view ----------------------------------------------------------------------------------
150
+
151
+ let reconciling = false
152
+ /** The tab is shown again (`mv:app-state shown`, Codex r1 #14): the marks, the working state and the Jam queue are
153
+ * read again, and the board's file hash - a board changed on disk meanwhile is loaded again, never over an edit
154
+ * this page holds. One at a time: a `shown` while one runs is dropped. */
155
+ export async function resumeReconcile(): Promise<void> {
156
+ if (reconciling || PUBLISHED || canvasStopped()) return
157
+ reconciling = true
158
+ try {
159
+ const s = useStore.getState()
160
+ void s.syncMarks()
161
+ void s.syncWorking()
162
+ if (jamOn()) void useJamQueue.getState().load()
163
+ const board = s.board
164
+ if (!s.manifest || board === UNPLACED || s.dirty || s.gesture) return
165
+ const r = await api(`boards/${board}`)
166
+ if (!r.ok) return
167
+ const body = await r.json().catch(() => null) as { sha256?: unknown } | null
168
+ const now = useStore.getState()
169
+ if (now.board !== board || now.dirty || now.gesture) return
170
+ const disk = typeof body?.sha256 === 'string' ? body.sha256 : null
171
+ if (disk !== now.boardHash) await now.boot()
172
+ } catch { /* the next shown, or the live socket, heals it */ } finally { reconciling = false }
173
+ }
174
+
175
+ function onAppState(s: AppState): void {
176
+ if (s === 'shown') void resumeReconcile()
177
+ else if (s === 'untrusted') voiceUntrusted()
178
+ }
179
+
180
+ // ---- the bridge ---------------------------------------------------------------------------------------------
181
+
182
+ let started = false
183
+
184
+ /** main.tsx, before the first render: hello, theme, busy, menu - then each again on its change. Nothing without
185
+ * `window.marverApp`. */
186
+ export function startAppBridge(): void {
187
+ const app = APP
188
+ if (!app || started) return
189
+ started = true
190
+ /** a bridge call that throws (the app went away under the page) never breaks the page */
191
+ const call = (fn: () => void) => { try { fn() } catch { /* the page goes on */ } }
192
+
193
+ // what the app says, listened for before the page says anything
194
+ call(() => app.onAppState((s) => onAppState(s)))
195
+ call(() => app.onMenuClick((p) => runMenuItem(typeof p?.id === 'string' ? p.id : '')))
196
+ // the one ground's presentation requests (0.27.2): answered only when the shell took the inset - a shell that did
197
+ // not is placed below the strip and never asked (A3.8 #31)
198
+ if (TOP) call(() => { answerPresents(app, call) })
199
+
200
+ // 1. hello, first (A2.14 #9)
201
+ call(() => app.hello(helloNow()))
202
+
203
+ // 2. the theme, at boot and on its change
204
+ let lastDark: boolean | null = null
205
+ const sendTheme = () => { const dark = shellDark(); if (dark === lastDark) return; lastDark = dark; call(() => app.theme({ dark })) }
206
+ sendTheme()
207
+ useStore.subscribe((s, p) => { if (!!s.play !== !!p.play || s.viewTheme !== p.viewTheme) sendTheme() })
208
+
209
+ // 3. busy: the unsaved reasons every browser registers, and the three only the app hears
210
+ busyWhen('jam-session', () => !!useJamQueue.getState().session)
211
+ busyWhen('microphone', () => useJamVoice.getState().live)
212
+ busyWhen('agent', () => useStore.getState().working.length > 0)
213
+ useJamQueue.subscribe((s, p) => { if (!!s.session !== !!p.session) workChanged() })
214
+ useJamVoice.subscribe((s, p) => { if (s.live !== p.live) workChanged() })
215
+ useStore.subscribe((s, p) => { if ((s.working.length > 0) !== (p.working.length > 0)) workChanged() })
216
+ let lastBusy = ''
217
+ let again: ReturnType<typeof setTimeout> | undefined
218
+ const sendBusy = () => {
219
+ const b = busyNow()
220
+ const k = JSON.stringify(b)
221
+ if (k !== lastBusy) { lastBusy = k; call(() => app.busy(b)) }
222
+ // while busy, looked at again each second: a reason with no hook of its own going away (a field emptied by
223
+ // sending its words, a field removed) is told within a second; at rest, no timer
224
+ clearTimeout(again)
225
+ again = b.busy ? setTimeout(sendBusy, 1000) : undefined
226
+ }
227
+ onWorkChange(sendBusy)
228
+ sendBusy() // the first mv:busy, at boot (v1.13 r2 #1)
229
+
230
+ // 4. the menu, on any change of what it shows
231
+ let lastMenu = ''
232
+ let menuQueued = false
233
+ const sendMenu = () => {
234
+ menuQueued = false
235
+ const items = menuNow()
236
+ const k = JSON.stringify(items)
237
+ if (k !== lastMenu) { lastMenu = k; call(() => app.menu(items)) }
238
+ }
239
+ const menuChanged = () => { if (menuQueued) return; menuQueued = true; queueMicrotask(sendMenu) }
240
+ sendMenu()
241
+ useStore.subscribe((s, p) => { if (s.laser !== p.laser || s.laserInspect !== p.laserInspect) menuChanged() })
242
+ useComments.subscribe((s, p) => { if (s.commentMode !== p.commentMode || s.show !== p.show) menuChanged() })
243
+ useNotes.subscribe((s, p) => { if (s.all !== p.all) menuChanged() })
244
+ useJamQueue.subscribe((s, p) => { if (!!s.session !== !!p.session) menuChanged() })
245
+ useJamCapture.subscribe((s, p) => { if (!!s.current !== !!p.current) menuChanged() })
246
+ useJamUi.subscribe((s, p) => { if (s.laneKeys !== p.laneKeys) menuChanged() })
247
+ // Hide UI is a class on the body (Toolbar.tsx): its one source of truth
248
+ new MutationObserver(menuChanged).observe(document.body, { attributes: true, attributeFilter: ['class'] })
249
+ }
250
+
251
+ // dev-only inspection hook for tests (never installed in published builds)
252
+ if (typeof window !== 'undefined' && import.meta.env.DEV) {
253
+ (window as unknown as { __mvApp: unknown }).__mvApp = { active: !!APP, busy: busyNow, menu: menuNow, asks: asksBeforeLeaving, leaveNow, page: PAGE_ID }
254
+ }
@@ -0,0 +1,92 @@
1
+ /**
2
+ * The shell's side of `mv:perf` (spec 28 A3.8 #9, #10, #16): what a 0.27.2 shell tells the Mac app's recorder -
3
+ * its gestures (perf.ts's begin and end, the white-out detector's window), its probe answers (whiteout-probes.ts,
4
+ * given by the canvas: `answerProbesWith`)
5
+ * and its board's frame count at each commit. Inert unless `window.marverApp.perf` exists (app 0.2): in Chrome, on
6
+ * a published canvas and inside app 0.1 nothing here sends, listens or runs a timer. The preload sends `mem` and
7
+ * `shown` by itself; the shell never sees a `doc`.
8
+ *
9
+ * FROZEN signatures (A3.8 #53); the recorder stream fills them (the probes' geometry is the flash stream's
10
+ * whiteout-probes.ts). Every message goes through `APP.perf` in a try: the app going away never breaks the page.
11
+ *
12
+ * As built: a gesture's `on` and `off` carry one `seq` (a new start while one is open ends it first, so main never
13
+ * holds two); `board` is sent when the frame count or the sleeping count changed, read at each change of the board's
14
+ * nodes and again at each gesture's end (frames fall asleep without the store moving), never more.
15
+ */
16
+ import type { PerfMsg } from '../../shared/app-api.ts'
17
+ import { APP } from './marver-app.ts'
18
+ import { useStore } from './store.ts'
19
+ import { sleepingCount } from './canvas/sleep.ts'
20
+
21
+ /** Whether this page talks to the recorder at all. */
22
+ export const PERF_ON = typeof APP?.perf === 'function'
23
+
24
+ /** The white-out probes (whiteout-probes.ts' pick and verify), given by the canvas (Canvas.tsx): canvas code, never
25
+ * in an all-boards-locked bundle, whose shell has no canvas - an ask there goes unanswered, which main reads as an
26
+ * inconclusive sample, as it reads any probe answer that never came. */
27
+ export interface Probes { pick(tick: number): PerfMsg; verify(tick: number): PerfMsg }
28
+ let probes: Probes | null = null
29
+ /** Canvas.tsx, at import: the probes this page answers with. */
30
+ export function answerProbesWith(p: Probes): void { probes = p }
31
+
32
+ let started = false
33
+ let seq = 0
34
+ let open: number | null = null
35
+ let lastBoard = ''
36
+
37
+ /** The board now: its frames and how many sleep, sent when either changed. */
38
+ function boardNow(): void {
39
+ try { boardChanged(useStore.getState().nodes.length, sleepingCount()) } catch { /* the page goes on */ }
40
+ }
41
+
42
+ /** main.tsx, after startAppBridge(): the probes' ask listener and the board subscription. Nothing without `APP.perf`. */
43
+ export function startAppPerf(): void {
44
+ if (!PERF_ON || started) return
45
+ started = true
46
+ try {
47
+ APP!.onPerfAsk?.((a) => {
48
+ if (a.kind !== 'probes' || !probes) return
49
+ try { sendPerf(a.at === 'before' ? probes.pick(a.tick) : probes.verify(a.tick)) } catch { /* no answer: main reads the sample as inconclusive */ }
50
+ })
51
+ } catch { /* the app's own */ }
52
+ useStore.subscribe((s, prev) => { if (s.nodes !== prev.nodes) boardNow() })
53
+ boardNow()
54
+ }
55
+
56
+ /** perf.ts's begin(): a gesture starts. */
57
+ export function gestureOn(type?: 'pan' | 'zoom' | 'layout'): void {
58
+ if (!PERF_ON) return
59
+ if (open !== null) gestureOff()
60
+ seq = seq >= 2 ** 31 - 2 ? 1 : seq + 1
61
+ open = seq
62
+ sendPerf({ kind: 'gesture', on: true, seq, ...(type ? { type } : {}) })
63
+ }
64
+
65
+ /** perf.ts's end(): the gesture that started last ends. */
66
+ export function gestureOff(): void {
67
+ if (!PERF_ON || open === null) return
68
+ const s = open
69
+ open = null
70
+ sendPerf({ kind: 'gesture', on: false, seq: s })
71
+ boardNow()
72
+ }
73
+
74
+ /** The board committed: its frame count and how many sleep. */
75
+ export function boardChanged(frames: number, asleep: number): void {
76
+ if (!PERF_ON) return
77
+ const k = `${frames}/${asleep}`
78
+ if (k === lastBoard) return
79
+ lastBoard = k
80
+ sendPerf({ kind: 'board', frames, asleep })
81
+ }
82
+
83
+ /** The display's refresh rate main last said (`display` ask), for gesture records' `hz`; null until it says. */
84
+ export function displayHz(): number | null {
85
+ try { return APP?.perfInfo?.().hz ?? null } catch { return null }
86
+ }
87
+
88
+ /** One message to the app, never throwing. */
89
+ export function sendPerf(p: PerfMsg): void {
90
+ if (!PERF_ON) return
91
+ try { APP!.perf!(p) } catch { /* the page goes on */ }
92
+ }
@@ -7,7 +7,18 @@ import { revealFrame } from '../goto.ts'
7
7
  import { startPerf } from '../perf.ts'
8
8
  import { FrameNode, HEADER } from './FrameNode.tsx'
9
9
  import { NOTE_GAP } from '../notes.ts'
10
- import { startCameraBroadcast, setCameraScale } from './camera-broadcast.ts'
10
+ import { onMotion, startCameraBroadcast, setCameraScale } from './camera-broadcast.ts'
11
+ import { attachCanvas, recorder } from '../recorder.ts'
12
+ import { sleepingCount } from './sleep.ts'
13
+ import { aimed } from './admission.ts'
14
+ import { keepOverResume, resumed, WS } from '../api.ts'
15
+ import { APP_REOPEN } from '../marver-app.ts'
16
+ import { TOP } from '../inset.ts'
17
+ import { answerProbesWith } from '../app-perf.ts'
18
+ import { pick, verify } from '../whiteout-probes.ts'
19
+
20
+ // the white-out probes the Mac app's recorder asks for (app-perf.ts): the canvas's own, so a locked bundle has none
21
+ answerProbesWith({ pick, verify })
11
22
 
12
23
  /**
13
24
  * The world. rzpp owns pan/zoom; nodes are absolutely positioned children of #sh-world.
@@ -31,6 +42,62 @@ import { startCameraBroadcast, setCameraScale } from './camera-broadcast.ts'
31
42
  */
32
43
  const GAP = 32
33
44
  const BAR = 56
45
+
46
+ /** The camera survives a reload (spec 28 A2.9): kept per canvas and board, `mv-camera:<workspace>:<board>`, a second
47
+ * after it settles, and the next load of that board opens there - when its link names no selection, or when the Mac
48
+ * app reloaded the tab after a crash (`marverApp.reopen`). A1's camera carried over a resume wins. Dev canvases only:
49
+ * a published one has no workspace, and opens on the whole board as before. */
50
+ type Camera = { x: number; y: number; scale: number }
51
+ const cameraKey = (board: string) => `mv-camera:${WS}:${board}`
52
+ function savedCamera(board: string): Camera | null {
53
+ if (!WS) return null
54
+ try {
55
+ const c = JSON.parse(localStorage.getItem(cameraKey(board)) ?? 'null')
56
+ return c && [c.x, c.y, c.scale].every((v) => typeof v === 'number' && Number.isFinite(v)) && c.scale >= 0.05 && c.scale <= 5 ? { x: c.x, y: c.y, scale: c.scale } : null
57
+ } catch { return null }
58
+ }
59
+ let keepTimer: ReturnType<typeof setTimeout> | undefined
60
+ /** the camera write waiting for its second of quiet, if any */
61
+ let keepPending: (() => void) | undefined
62
+ /** Where the camera's flight lands (a fit, a zoom), for canvasCtl.landing: read only while rzpp animates. */
63
+ let flight: Camera | null = null
64
+ function keepCamera(x: number, y: number, scale: number) {
65
+ if (!WS) return
66
+ const board = useStore.getState().board
67
+ clearTimeout(keepTimer)
68
+ const write = () => {
69
+ keepPending = undefined
70
+ if (useStore.getState().board !== board) return // switched meanwhile: this camera was the other board's
71
+ try { localStorage.setItem(cameraKey(board), JSON.stringify({ x: Math.round(x * 100) / 100, y: Math.round(y * 100) / 100, scale })) } catch { /* storage off: the next load fits */ }
72
+ }
73
+ keepPending = write
74
+ keepTimer = setTimeout(write, 1000)
75
+ }
76
+ /**
77
+ * The pending camera, written now (spec 28 A3.8 #43): the page is hidden or leaving, and the Mac app may unload a
78
+ * hidden tab, which ends its page with the second of quiet not yet over. A hide mid-flight writes where the flight
79
+ * was going: a hidden page's animation frames stop, so it would not land before the page went. Nothing pending,
80
+ * nothing written. In Chrome too - a tab switch, a closed tab.
81
+ */
82
+ export function flushCamera(): void {
83
+ if (flight && canvasCtl.cameraBusy()) keepCamera(flight.x, flight.y, flight.scale)
84
+ if (!keepPending) return
85
+ clearTimeout(keepTimer)
86
+ keepPending()
87
+ }
88
+ let leaveWatched = false
89
+ /**
90
+ * The hide and the leave write the pending camera: listened for from the canvas's first mount, so after the page
91
+ * recorder's own listeners (startRecorder runs before the first render) - its cut of a load still waiting comes first,
92
+ * as `hidden`. Registered at import, ahead of the recorder's, the camera's pagehide write let a navigation away cut
93
+ * that load as `error` instead (page-recorder-browser's "the page goes").
94
+ */
95
+ function watchLeave(): void {
96
+ if (leaveWatched || typeof document === 'undefined') return
97
+ leaveWatched = true
98
+ document.addEventListener('visibilitychange', () => { if (document.visibilityState === 'hidden') flushCamera() })
99
+ addEventListener('pagehide', flushCamera)
100
+ }
34
101
  function insets(el: HTMLElement) {
35
102
  const c = el.getBoundingClientRect()
36
103
  const rect = (q: string) => document.querySelector(q)?.getBoundingClientRect()
@@ -44,7 +111,8 @@ function insets(el: HTMLElement) {
44
111
  return {
45
112
  left: (leftEdge ? Math.max(0, leftEdge - c.left) : 0) + GAP,
46
113
  right: GAP + 16,
47
- top: (pill ? Math.max(0, pill.bottom - c.top) : 0) + 12 + BAR,
114
+ // the toolbar's foot, or the Mac app's strip when the toolbar is hidden (Hide UI) - the strip is always there
115
+ top: Math.max(pill ? pill.bottom - c.top : 0, TOP) + 12 + BAR,
48
116
  bottom: (lane ? Math.max(0, c.bottom - lane.top) : 0) + GAP + 16,
49
117
  }
50
118
  }
@@ -58,23 +126,22 @@ function insets(el: HTMLElement) {
58
126
  */
59
127
  const GRID = 20
60
128
  function paintGrid(positionX: number, positionY: number, scale: number) {
61
- const el = document.querySelector('.sh-canvas') as HTMLElement | null
62
- if (!el) return
129
+ // on .sh-app, which the canvas's ::before and the scroll edge (A3.8 #31) both inherit them from - the same element the
130
+ // camera's own variables go on every frame, so the style work per frame does not grow
131
+ const app = document.querySelector('.sh-app') as HTMLElement | null
132
+ if (!app) return
63
133
  let step = GRID * scale
64
134
  while (step < 14) step *= 2
65
135
  while (step >= 28) step /= 2
66
136
  const t = (step - 14) / 14
67
- el.style.setProperty('--grid-size', `${step}px ${step}px`)
68
- el.style.setProperty('--grid-pos', `${positionX % step}px ${positionY % step}px`)
69
- el.style.setProperty('--grid-alpha', (0.35 + 0.65 * Math.min(1, t)).toFixed(3))
137
+ app.style.setProperty('--grid-size', `${step}px ${step}px`)
138
+ app.style.setProperty('--grid-pos', `${positionX % step}px ${positionY % step}px`)
139
+ app.style.setProperty('--grid-alpha', (0.35 + 0.65 * Math.min(1, t)).toFixed(3))
70
140
  // screen-space overlays (selection bar) position themselves from these - no re-render per frame
71
- const app = document.querySelector('.sh-app') as HTMLElement | null
72
- if (app) {
73
- app.style.setProperty('--sh-s', String(scale))
74
- app.style.setProperty('--sh-inv', String(1 / scale)) // world-units-per-screen-px, for zoom-invariant strokes
75
- app.style.setProperty('--sh-tx', `${positionX}px`)
76
- app.style.setProperty('--sh-ty', `${positionY}px`)
77
- }
141
+ app.style.setProperty('--sh-s', String(scale))
142
+ app.style.setProperty('--sh-inv', String(1 / scale)) // world-units-per-screen-px, for zoom-invariant strokes
143
+ app.style.setProperty('--sh-tx', `${positionX}px`)
144
+ app.style.setProperty('--sh-ty', `${positionY}px`)
78
145
  }
79
146
 
80
147
  /**
@@ -142,9 +209,19 @@ export function Canvas() {
142
209
  const gesture = useStore((s) => s.gesture)
143
210
  const setScale = useStore((s) => s.setScale)
144
211
  const ref = useRef<ReactZoomPanPinchContentRef>(null)
212
+ // every camera move the canvas aims (a fit, a zoom, a camera restored): where it lands, while rzpp flies there, and the
213
+ // boot queue told - the first frame of a board starts now, ranked on that view, not once the flight is over
214
+ const fly = (x: number, y: number, scale: number, ms: number) => {
215
+ flight = ms ? { x, y, scale } : null
216
+ ref.current?.setTransform(x, y, scale, ms, 'easeOut')
217
+ aimed()
218
+ }
145
219
  const scaleTimer = useRef(0)
146
220
 
147
- useEffect(() => { startPerf(); startCameraBroadcast() }, []) // B0.4: frame-time sampler + image-LOD camera signal
221
+ // B0.4: frame-time sampler + image-LOD camera signal; the recorder hears when the camera stops and what sleeps (spec 28 A1.7)
222
+ useEffect(() => { startPerf(); startCameraBroadcast(); return attachCanvas({ onMotion, sleeping: sleepingCount }) }, [])
223
+ // the pending camera written as the page hides or leaves (A3.8 #43), after the recorder's own listeners
224
+ useEffect(() => { watchLeave() }, [])
148
225
 
149
226
  useEffect(() => {
150
227
  const wrap = () => document.querySelector('.sh-canvas') as HTMLElement | null
@@ -163,11 +240,15 @@ export function Canvas() {
163
240
  let px = p.left + (aw - w * scale) / 2
164
241
  let py = p.top + (ah - h * scale) / 2
165
242
  if (focus) {
166
- // clamp order matters: left/top win when the band is tighter than the content
243
+ // clamp order matters: left/top win when the band is tighter than the content. The centre is the usable
244
+ // rectangle's: below the Mac app's strip when the shell took the inset (TOP 44)
167
245
  px = Math.max(Math.min((vw - w * scale) / 2, vw - p.right - w * scale), p.left)
168
- py = Math.max(Math.min((vh - h * scale) / 2, vh - p.bottom - h * scale), p.top)
246
+ py = Math.max(Math.min(TOP + (vh - TOP - h * scale) / 2, vh - p.bottom - h * scale), p.top)
169
247
  }
170
- ref.current.setTransform(px - x * scale, py - y * scale, scale, 320, 'easeOut')
248
+ const tx = px - x * scale, ty = py - y * scale
249
+ // where this fit lands, in world px: the first one after a board's commit shows its visible cohort (spec 28 A1.7)
250
+ recorder.placed({ x: -tx / scale, y: -ty / scale, w: vw / scale, h: vh / scale })
251
+ fly(tx, ty, scale, 320)
171
252
  }
172
253
  const fitKeys = (keys: string[], focus: boolean) => {
173
254
  const st = useStore.getState()
@@ -205,11 +286,35 @@ export function Canvas() {
205
286
  const el = wrap(), inst = ref.current
206
287
  if (!el || !inst) return
207
288
  const { positionX, positionY, scale } = inst.instance.transformState
208
- const cx = el.clientWidth / 2, cy = el.clientHeight / 2
209
- const k = target / scale // zoom about the viewport center
210
- inst.setTransform(cx - (cx - positionX) * k, cy - (cy - positionY) * k, target, 250, 'easeOut')
289
+ const cx = el.clientWidth / 2, cy = TOP + (el.clientHeight - TOP) / 2
290
+ const k = target / scale // zoom about the usable rectangle's centre (below the Mac app's strip with the inset)
291
+ fly(cx - (cx - positionX) * k, cy - (cy - positionY) * k, target, 250)
211
292
  }
212
293
  canvasCtl.cameraBusy = () => !!ref.current?.instance.animation
294
+ // the camera as the world is drawn now, mid-flight included: the white-out probes' bracket (spec 28 A3.8 #16)
295
+ canvasCtl.transform = () => {
296
+ const t = ref.current?.instance.transformState
297
+ return t ? { x: t.positionX, y: t.positionY, s: t.scale } : null
298
+ }
299
+ // a rect on screen now, where it will be when the flight lands: the same world point under the landing transform
300
+ canvasCtl.landing = (r: DOMRect) => {
301
+ const el = wrap(), inst = ref.current
302
+ if (!flight || !el || !inst?.instance.animation) return r
303
+ const { positionX, positionY, scale } = inst.instance.transformState
304
+ const c = el.getBoundingClientRect(), k = flight.scale / scale
305
+ return new DOMRect(c.left + flight.x + (r.left - c.left - positionX) * k, c.top + flight.y + (r.top - c.top - positionY) * k, r.width * k, r.height * k)
306
+ }
307
+ // a page that reloads itself when its canvas comes back (spec 28 A1.11) opens where the person was looking
308
+ const unkeep = keepOverResume(() => {
309
+ const t = ref.current?.instance.transformState
310
+ return t ? { board: useStore.getState().board, camera: { x: t.positionX, y: t.positionY, scale: t.scale } } : {}
311
+ })
312
+ canvasCtl.view = () => {
313
+ const el = wrap(), inst = ref.current
314
+ if (!el || !inst) return null
315
+ const { positionX, positionY, scale } = inst.instance.transformState
316
+ return { x: -positionX / scale, y: -positionY / scale, w: el.clientWidth / scale, h: el.clientHeight / scale }
317
+ }
213
318
  canvasCtl.holdView = () => {
214
319
  heldView = null
215
320
  const el = wrap(), inst = ref.current
@@ -222,6 +327,7 @@ export function Canvas() {
222
327
  // the reflow commits before the next frame; an anchor nothing consumed must never steer a later change
223
328
  requestAnimationFrame(() => { if (heldView === held) heldView = null })
224
329
  }
330
+ return unkeep
225
331
  }, [])
226
332
 
227
333
  // first load opens on the whole board (same as ⇧1) - the default 100% transform is an
@@ -233,12 +339,25 @@ export function Canvas() {
233
339
  if (booted.current || nodes.length === 0) return
234
340
  let revealed = false
235
341
  booted.current = true
342
+ // the page before this one reloaded itself when its canvas came back: its camera, on the board it showed
343
+ const board = useStore.getState().board
344
+ const carried = resumed?.camera && resumed.board === board ? resumed.camera : null
345
+ // else the camera this board was left at (A2.9) - a link naming a selection fits it instead, unless the app
346
+ // reloaded this tab after a crash
347
+ const named = !!(bootHash.n?.length || bootHash.f?.length || bootHash.s?.length)
348
+ const cam = carried ?? (!named || APP_REOPEN ? savedCamera(board) : null)
236
349
  const fit = () => {
237
350
  const keys = linkTargets(bootHash, useStore.getState().nodes)
238
351
  if (keys.length) {
239
352
  useStore.setState({ selection: keys })
240
353
  const st = useStore.getState() // a link opened is a look (marks not in yet: their first load acknowledges it)
241
354
  st.look(keys)
355
+ }
356
+ const el = document.querySelector('.sh-canvas') as HTMLElement | null
357
+ if (cam && el && ref.current) {
358
+ recorder.placed({ x: -cam.x / cam.scale, y: -cam.y / cam.scale, w: el.clientWidth / cam.scale, h: el.clientHeight / cam.scale })
359
+ fly(cam.x, cam.y, cam.scale, 0)
360
+ } else if (keys.length) {
242
361
  canvasCtl.fitNodes(keys)
243
362
  } else {
244
363
  canvasCtl.fitAll()
@@ -402,6 +521,7 @@ export function Canvas() {
402
521
  onTransformed={(r) => {
403
522
  paintGrid(r.state.positionX, r.state.positionY, r.state.scale)
404
523
  setCameraScale(r.state.scale) // keep the LOD's settle-scale current (cheap number write, no React)
524
+ if (booted.current) keepCamera(r.state.positionX, r.state.positionY, r.state.scale) // over a reload (A2.9)
405
525
  clearTimeout(scaleTimer.current)
406
526
  scaleTimer.current = window.setTimeout(() => setScale(r.state.scale), 120)
407
527
  }}