@inflatable-cookie/longhorn-poodle-svelte 0.1.0

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/LICENSE +21 -0
  2. package/README.md +37 -0
  3. package/package.json +171 -0
  4. package/src/bindings/index.ts +4 -0
  5. package/src/bindings/layout.svelte.ts +143 -0
  6. package/src/bindings/lifecycle.svelte.ts +246 -0
  7. package/src/bindings/optimistic.svelte.ts +96 -0
  8. package/src/bindings/scheduler.ts +13 -0
  9. package/src/bindings/surface-transfer-actions.ts +281 -0
  10. package/src/bindings/surface-transfer.svelte.ts +230 -0
  11. package/src/bindings/surfaces.svelte.ts +179 -0
  12. package/src/bindings/transfer-actions.ts +240 -0
  13. package/src/bindings/transfer-model.ts +138 -0
  14. package/src/bindings/transfer.svelte.ts +388 -0
  15. package/src/bindings/window-chrome.ts +63 -0
  16. package/src/bindings/window-drag.ts +161 -0
  17. package/src/commands/poodle/CommandPaletteBinding.svelte +51 -0
  18. package/src/commands/poodle/KeybindingSettings.svelte +138 -0
  19. package/src/commands/poodle/projectors.ts +37 -0
  20. package/src/commands/poodle.ts +9 -0
  21. package/src/commands/svelte/lifecycle.ts +13 -0
  22. package/src/commands/svelte/session.svelte.ts +212 -0
  23. package/src/commands/svelte.ts +2 -0
  24. package/src/config/poodle/BackupSettingsPage.svelte +295 -0
  25. package/src/config/poodle/RestoreSettingsPage.svelte +535 -0
  26. package/src/config/poodle/StorageSettingsPage.svelte +361 -0
  27. package/src/config/poodle/restore-model.ts +63 -0
  28. package/src/config/poodle/types.ts +11 -0
  29. package/src/config/poodle.ts +8 -0
  30. package/src/history/svelte/lifecycle.ts +13 -0
  31. package/src/history/svelte/session.svelte.ts +97 -0
  32. package/src/history/svelte.ts +2 -0
  33. package/src/history-tree/svelte/lifecycle.ts +11 -0
  34. package/src/history-tree/svelte/session.svelte.ts +27 -0
  35. package/src/history-tree/svelte.ts +3 -0
  36. package/src/licence/poodle/LicenceActivation.svelte +62 -0
  37. package/src/licence/poodle/LicenceSeats.svelte +57 -0
  38. package/src/licence/poodle/LicenceStatus.svelte +34 -0
  39. package/src/licence/poodle/LicenceSurface.svelte.ts +43 -0
  40. package/src/licence/poodle.ts +3 -0
  41. package/src/native-content/index.ts +4 -0
  42. package/src/native-content/lifecycle.ts +13 -0
  43. package/src/native-content/session.svelte.ts +526 -0
  44. package/src/native-content/viewport.ts +27 -0
  45. package/src/native-content/visibility.ts +19 -0
  46. package/src/notifications/poodle/NotificationPanel.svelte +66 -0
  47. package/src/notifications/poodle/NotificationToastHost.svelte +40 -0
  48. package/src/notifications/poodle/NotificationToastStack.svelte +22 -0
  49. package/src/notifications/poodle/projectors.ts +30 -0
  50. package/src/notifications/poodle.ts +4 -0
  51. package/src/notifications/svelte/lifecycle.ts +11 -0
  52. package/src/notifications/svelte/session.svelte.ts +57 -0
  53. package/src/notifications/svelte.ts +2 -0
  54. package/src/operation/poodle/OperationPanel.svelte +200 -0
  55. package/src/operation/poodle/projectors.ts +78 -0
  56. package/src/operation/poodle.ts +2 -0
  57. package/src/operation/svelte/lifecycle.ts +13 -0
  58. package/src/operation/svelte/session.svelte.ts +80 -0
  59. package/src/operation/svelte.ts +2 -0
  60. package/src/poodle/LayoutDockRegion.svelte +148 -0
  61. package/src/poodle/LayoutSplitView.svelte +120 -0
  62. package/src/poodle/LayoutTabs.svelte +91 -0
  63. package/src/poodle/binding.ts +401 -0
  64. package/src/poodle/document.ts +117 -0
  65. package/src/poodle/index.ts +8 -0
  66. package/src/poodle/projectors.ts +125 -0
  67. package/src/poodle/transfer.ts +188 -0
  68. package/src/poodle/types.ts +89 -0
  69. package/src/settings/poodle/SettingsShell.svelte +382 -0
  70. package/src/settings/poodle.ts +2 -0
  71. package/src/settings/svelte/context.ts +61 -0
  72. package/src/settings/svelte/document.svelte.ts +202 -0
  73. package/src/settings/svelte/errors.ts +84 -0
  74. package/src/settings/svelte/lifecycle.ts +18 -0
  75. package/src/settings/svelte/page.svelte.ts +266 -0
  76. package/src/settings/svelte/runtime.svelte.ts +187 -0
  77. package/src/settings/svelte/scope.svelte.ts +123 -0
  78. package/src/settings/svelte/session.svelte.ts +288 -0
  79. package/src/settings/svelte/types.ts +90 -0
  80. package/src/settings/svelte.ts +6 -0
  81. package/src/svelte.d.ts +6 -0
  82. package/src/update/poodle/UpdateCentre.svelte +64 -0
  83. package/src/update/poodle/UpdateSettings.svelte +64 -0
  84. package/src/update/poodle/UpdateStatus.svelte +51 -0
  85. package/src/update/poodle/UpdateSurface.svelte.ts +88 -0
  86. package/src/update/poodle.ts +3 -0
@@ -0,0 +1,57 @@
1
+ <script lang="ts">
2
+ import { LicenceSeats as PoodleLicenceSeats } from "@inflatable-cookie/poodle-svelte";
3
+ import type { LicenceController } from "@inflatable-cookie/longhorn/licence";
4
+
5
+ import { sampleLicenceController } from "./LicenceSurface.svelte.ts";
6
+
7
+ /**
8
+ * Binds a `LicenceController` to Poodle's `LicenceSeats`.
9
+ *
10
+ * The answer to "I got a new laptop": the seats that are not this machine
11
+ * are the ones a release frees, without a support conversation.
12
+ *
13
+ * `onRename` is wired since 2026-08-14: the protocol gained
14
+ * `LicenceRenameSeatCommand`, so the capability the component offered now
15
+ * exists. The label stays the customer's word, and clearing it renames to
16
+ * null rather than to an empty string.
17
+ */
18
+ interface Props {
19
+ controller: LicenceController;
20
+ title?: string;
21
+ confirmRelease?: boolean;
22
+ }
23
+
24
+ let { controller, title = "Activated machines", confirmRelease = true }: Props = $props();
25
+
26
+ const licence = sampleLicenceController(() => controller);
27
+ let releasing = $state<string | null>(null);
28
+
29
+ function rename(detail: { machineId: string; label: string | null }): void {
30
+ // Poodle's editor can hand back an empty string for a cleared field; the
31
+ // protocol treats empty as a mistake and null as "unnamed", so the
32
+ // normalisation happens here, at the seam.
33
+ const label = detail.label !== null && detail.label.trim().length === 0 ? null : detail.label;
34
+ void controller.renameSeat(detail.machineId, label);
35
+ }
36
+
37
+ function release(detail: { machineId: string }): void {
38
+ releasing = detail.machineId;
39
+ void controller.releaseSeat(detail.machineId).finally(() => {
40
+ releasing = null;
41
+ });
42
+ }
43
+ </script>
44
+
45
+ {#if licence.seats.length > 0}
46
+ <!-- An empty list means the authority does not account for seats, which is
47
+ not a state to render — showing "0 machines" would imply an accounting
48
+ that is not happening. -->
49
+ <PoodleLicenceSeats
50
+ seats={licence.seats}
51
+ pendingMachineId={releasing}
52
+ {title}
53
+ {confirmRelease}
54
+ onRelease={release}
55
+ onRename={rename}
56
+ />
57
+ {/if}
@@ -0,0 +1,34 @@
1
+ <script lang="ts">
2
+ import { LicenceStatus as PoodleLicenceStatus } from "@inflatable-cookie/poodle-svelte";
3
+ import type { LicenceController } from "@inflatable-cookie/longhorn/licence";
4
+
5
+ import { sampleLicenceController } from "./LicenceSurface.svelte.ts";
6
+
7
+ /**
8
+ * Binds a `LicenceController` to Poodle's `LicenceStatus`.
9
+ *
10
+ * Rendered only when a licence is held: unlicensed is the absence of a
11
+ * licence rather than a usability state, and the activation surface is what
12
+ * an unlicensed operator should be looking at.
13
+ */
14
+ interface Props {
15
+ controller: LicenceController;
16
+ title?: string;
17
+ }
18
+
19
+ let { controller, title = "Licence" }: Props = $props();
20
+
21
+ const licence = sampleLicenceController(() => controller);
22
+ </script>
23
+
24
+ {#if licence.activated && licence.usability !== undefined && licence.trustBasis !== undefined}
25
+ <PoodleLicenceStatus
26
+ usability={licence.usability}
27
+ trustBasis={licence.trustBasis}
28
+ useUntil={licence.useUntil}
29
+ updateUntil={licence.updateUntil}
30
+ usable={licence.usable}
31
+ attention={licence.attention}
32
+ {title}
33
+ />
34
+ {/if}
@@ -0,0 +1,43 @@
1
+ import type { LicenceController } from "@inflatable-cookie/longhorn/licence";
2
+
3
+ /**
4
+ * The controller's reads, sampled so Svelte can see them change.
5
+ *
6
+ * The same shape as `sampleUpdateController` and for the same measured reason:
7
+ * `LicenceController` is a plain class, so its getters are not tracked
8
+ * dependencies, and Poodle's components re-derive from the props they were
9
+ * handed. The bump has to happen on the side that owns the expressions.
10
+ *
11
+ * The lifecycle is deliberately not touched. Whoever constructed the
12
+ * controller starts and stops it.
13
+ */
14
+ export function sampleLicenceController(getController: () => LicenceController) {
15
+ let controller = getController();
16
+ let notify = $state(0);
17
+
18
+ $effect(() => {
19
+ const nextController = getController();
20
+ if (nextController !== controller) {
21
+ controller = nextController;
22
+ notify += 1;
23
+ }
24
+ const unobserve = nextController.observe(() => {
25
+ notify += 1;
26
+ });
27
+ return () => unobserve();
28
+ });
29
+
30
+ return {
31
+ get status() { void notify; return controller.status; },
32
+ get activated() { void notify; return controller.activated; },
33
+ get usable() { void notify; return controller.usable; },
34
+ get usability() { void notify; return controller.usability; },
35
+ get trustBasis() { void notify; return controller.trustBasis; },
36
+ get useUntil() { void notify; return controller.useUntil ?? null; },
37
+ get updateUntil() { void notify; return controller.updateUntil ?? null; },
38
+ get attention() { void notify; return controller.attention; },
39
+ get seats() { void notify; return controller.seats; },
40
+ get pending() { void notify; return controller.pending; },
41
+ get lastRejection() { void notify; return controller.lastRejection; },
42
+ };
43
+ }
@@ -0,0 +1,3 @@
1
+ export { default as LicenceActivation } from "./poodle/LicenceActivation.svelte";
2
+ export { default as LicenceSeats } from "./poodle/LicenceSeats.svelte";
3
+ export { default as LicenceStatus } from "./poodle/LicenceStatus.svelte";
@@ -0,0 +1,4 @@
1
+ export * from "./session.svelte.ts";
2
+ export * from "./lifecycle.ts";
3
+ export * from "./viewport.ts";
4
+ export * from "./visibility.ts";
@@ -0,0 +1,13 @@
1
+ import { onMount } from "svelte";
2
+
3
+ import type { NativeContentSession } from "./session.svelte.ts";
4
+
5
+ export function useNativeContentSession(getSession: () => NativeContentSession): void {
6
+ onMount(() => {
7
+ const session = getSession();
8
+ void session.start().catch(() => undefined);
9
+ return () => {
10
+ void session.stop();
11
+ };
12
+ });
13
+ }
@@ -0,0 +1,526 @@
1
+ import {
2
+ isNewerNativeContentSnapshot,
3
+ type ClientRect,
4
+ type DesiredUpdate,
5
+ type DesiredVisibility,
6
+ type FocusIntent,
7
+ type InputRoutingMode,
8
+ type NativeContentClient,
9
+ type NativeContentConnection,
10
+ type NativeContentProtocolRejection,
11
+ type NativeContentSnapshot,
12
+ type ScaleFactor,
13
+ } from "@inflatable-cookie/longhorn/native-content";
14
+
15
+ export type NativeContentSessionClient = Pick<NativeContentClient, "connect">;
16
+
17
+ export interface NativeContentResizeObserver {
18
+ observe(target: Element): void;
19
+ disconnect(): void;
20
+ }
21
+
22
+ export type NativeContentResizeObserverFactory = (
23
+ callback: () => void,
24
+ ) => NativeContentResizeObserver;
25
+
26
+ export type NativeContentSessionStatus =
27
+ | { readonly kind: "idle" }
28
+ | { readonly kind: "connecting" }
29
+ | { readonly kind: "ready" }
30
+ | { readonly kind: "updating" }
31
+ | {
32
+ readonly kind: "rejected";
33
+ readonly rejection: NativeContentProtocolRejection;
34
+ }
35
+ | { readonly kind: "failed"; readonly error: unknown };
36
+
37
+ export interface NativeContentSessionOptions {
38
+ readonly client: NativeContentSessionClient;
39
+ readonly scale: ScaleFactor;
40
+ readonly visibility: DesiredVisibility;
41
+ readonly focus: FocusIntent;
42
+ readonly inputRouting: InputRoutingMode;
43
+ readonly resizeObserverFactory?: NativeContentResizeObserverFactory;
44
+ }
45
+
46
+ /** One checked native-content connection and viewport policy per Svelte mount. */
47
+ export class NativeContentSession {
48
+ readonly #client: NativeContentSessionClient;
49
+ readonly #resizeObserverFactory: NativeContentResizeObserverFactory;
50
+ #status = $state.raw<NativeContentSessionStatus>({ kind: "idle" });
51
+ #snapshot = $state.raw<NativeContentSnapshot | undefined>(undefined);
52
+ #viewport = $state.raw<ClientRect | undefined>(undefined);
53
+ #scale = $state<ScaleFactor>(1000);
54
+ #visibility = $state.raw<DesiredVisibility>({ state: "visible" });
55
+ #focus = $state<FocusIntent>("unchanged");
56
+ #inputRouting = $state<InputRoutingMode>("disabled");
57
+ #viewportElement: HTMLElement | undefined;
58
+ #viewportBinding = 0;
59
+ #observer: NativeContentResizeObserver | undefined;
60
+ #connection: NativeContentConnection | undefined;
61
+ #startTask: Promise<void> | undefined;
62
+ #stopTask: Promise<void> | undefined;
63
+ #pumpTask: Promise<void> | undefined;
64
+ #pumpRequested = false;
65
+ #lifecycle = 0;
66
+ #active = false;
67
+
68
+ constructor(options: NativeContentSessionOptions) {
69
+ assertScale(options.scale);
70
+ this.#client = options.client;
71
+ this.#scale = options.scale;
72
+ this.#visibility = copyVisibility(options.visibility);
73
+ this.#focus = options.focus;
74
+ this.#inputRouting = options.inputRouting;
75
+ this.#resizeObserverFactory =
76
+ options.resizeObserverFactory ?? defaultResizeObserverFactory;
77
+ }
78
+
79
+ get status(): NativeContentSessionStatus {
80
+ return this.#status;
81
+ }
82
+
83
+ get snapshot(): NativeContentSnapshot | undefined {
84
+ return this.#snapshot;
85
+ }
86
+
87
+ get viewport(): ClientRect | undefined {
88
+ return this.#viewport;
89
+ }
90
+
91
+ get scale(): ScaleFactor {
92
+ return this.#scale;
93
+ }
94
+
95
+ get visibility(): DesiredVisibility {
96
+ return copyVisibility(this.#visibility);
97
+ }
98
+
99
+ get focus(): FocusIntent {
100
+ return this.#focus;
101
+ }
102
+
103
+ get inputRouting(): InputRoutingMode {
104
+ return this.#inputRouting;
105
+ }
106
+
107
+ start(): Promise<void> {
108
+ if (this.#startTask !== undefined) return this.#startTask;
109
+ if (this.#active) return Promise.resolve();
110
+
111
+ const task =
112
+ this.#stopTask === undefined
113
+ ? this.#begin()
114
+ : this.#stopTask.then(() => this.#begin());
115
+ this.#startTask = task;
116
+ void task.then(
117
+ () => {
118
+ if (this.#startTask === task) this.#startTask = undefined;
119
+ },
120
+ () => {
121
+ if (this.#startTask === task) this.#startTask = undefined;
122
+ },
123
+ );
124
+ return task;
125
+ }
126
+
127
+ stop(): Promise<void> {
128
+ if (this.#stopTask !== undefined) return this.#stopTask;
129
+ if (!this.#active && this.#connection === undefined) {
130
+ this.#status = { kind: "idle" };
131
+ this.#snapshot = undefined;
132
+ return Promise.resolve();
133
+ }
134
+
135
+ ++this.#lifecycle;
136
+ this.#active = false;
137
+ this.#pumpRequested = false;
138
+ this.#pumpTask = undefined;
139
+ this.#disconnectObserver();
140
+ const connection = this.#connection;
141
+ this.#connection = undefined;
142
+ this.#snapshot = undefined;
143
+ this.#status = { kind: "idle" };
144
+
145
+ const task = (async () => {
146
+ try {
147
+ await connection?.dispose();
148
+ } catch (error) {
149
+ this.#status = { kind: "failed", error };
150
+ throw error;
151
+ }
152
+ })();
153
+ this.#stopTask = task;
154
+ void task.then(
155
+ () => {
156
+ if (this.#stopTask === task) this.#stopTask = undefined;
157
+ },
158
+ () => {
159
+ if (this.#stopTask === task) this.#stopTask = undefined;
160
+ },
161
+ );
162
+ return task;
163
+ }
164
+
165
+ bindViewport(element: HTMLElement): () => void {
166
+ const binding = ++this.#viewportBinding;
167
+ this.#disconnectObserver();
168
+ this.#viewportElement = element;
169
+ this.refreshViewport();
170
+ if (this.#active) this.#installObserver();
171
+
172
+ return () => {
173
+ if (binding !== this.#viewportBinding) return;
174
+ ++this.#viewportBinding;
175
+ this.#disconnectObserver();
176
+ this.#viewportElement = undefined;
177
+ this.#viewport = undefined;
178
+ this.#pumpRequested = false;
179
+ };
180
+ }
181
+
182
+ refreshViewport(): ClientRect {
183
+ const element = this.#viewportElement;
184
+ if (element === undefined) {
185
+ throw new NativeContentViewportUnboundError();
186
+ }
187
+ const viewport = measureViewport(element);
188
+ if (!equalRect(viewport, this.#viewport)) {
189
+ this.#viewport = viewport;
190
+ this.#requestPump();
191
+ }
192
+ return viewport;
193
+ }
194
+
195
+ setScale(scale: ScaleFactor): void {
196
+ assertScale(scale);
197
+ if (scale === this.#scale) return;
198
+ this.#scale = scale;
199
+ this.#requestPump();
200
+ }
201
+
202
+ setVisibilityPolicy(visibility: DesiredVisibility): void {
203
+ if (equalVisibility(visibility, this.#visibility)) return;
204
+ this.#visibility = copyVisibility(visibility);
205
+ this.#requestPump();
206
+ }
207
+
208
+ setFocusIntent(focus: FocusIntent): void {
209
+ if (focus === this.#focus) return;
210
+ this.#focus = focus;
211
+ this.#requestPump();
212
+ }
213
+
214
+ setInputRouting(inputRouting: InputRoutingMode): void {
215
+ if (inputRouting === this.#inputRouting) return;
216
+ this.#inputRouting = inputRouting;
217
+ this.#requestPump();
218
+ }
219
+
220
+ async whenSettled(): Promise<void> {
221
+ while (this.#pumpTask !== undefined) {
222
+ await this.#pumpTask;
223
+ }
224
+ }
225
+
226
+ async #begin(): Promise<void> {
227
+ const lifecycle = ++this.#lifecycle;
228
+ this.#active = true;
229
+ this.#status = { kind: "connecting" };
230
+
231
+ try {
232
+ this.#installObserver();
233
+ let connection: NativeContentConnection | undefined;
234
+ let earlySnapshot: NativeContentSnapshot | undefined;
235
+ let earlyFailure: unknown;
236
+ connection = this.#client.connect(
237
+ (snapshot) => {
238
+ if (connection === undefined) {
239
+ earlySnapshot = snapshot;
240
+ } else if (this.#isCurrent(lifecycle, connection)) {
241
+ this.#acceptSnapshot(snapshot);
242
+ }
243
+ },
244
+ (failure) => {
245
+ if (connection === undefined) {
246
+ earlyFailure = failure.error;
247
+ } else if (this.#isCurrent(lifecycle, connection)) {
248
+ this.#status = { kind: "failed", error: failure.error };
249
+ }
250
+ },
251
+ );
252
+ this.#connection = connection;
253
+ if (earlyFailure !== undefined) {
254
+ this.#status = { kind: "failed", error: earlyFailure };
255
+ }
256
+ if (earlySnapshot !== undefined) this.#acceptSnapshot(earlySnapshot);
257
+ const snapshot = await connection.ready;
258
+ if (!this.#isCurrent(lifecycle, connection)) return;
259
+ this.#acceptSnapshot(connection.current() ?? snapshot);
260
+ } catch (error) {
261
+ if (lifecycle === this.#lifecycle) {
262
+ this.#active = false;
263
+ this.#disconnectObserver();
264
+ this.#status = { kind: "failed", error };
265
+ }
266
+ const connection = this.#connection;
267
+ this.#connection = undefined;
268
+ await connection?.dispose().catch(() => undefined);
269
+ throw error;
270
+ }
271
+ }
272
+
273
+ #acceptSnapshot(snapshot: NativeContentSnapshot): void {
274
+ const current = this.#snapshot;
275
+ const sameCursor =
276
+ current?.cursor.authority_epoch === snapshot.cursor.authority_epoch &&
277
+ current.cursor.client_epoch === snapshot.cursor.client_epoch &&
278
+ current.cursor.desired_revision === snapshot.cursor.desired_revision &&
279
+ current.cursor.observed_revision === snapshot.cursor.observed_revision;
280
+ if (
281
+ current !== undefined &&
282
+ !sameCursor &&
283
+ !isNewerNativeContentSnapshot(snapshot, current)
284
+ ) {
285
+ return;
286
+ }
287
+ this.#snapshot = snapshot;
288
+ if (this.#status.kind !== "updating") this.#status = { kind: "ready" };
289
+ this.#requestPump();
290
+ }
291
+
292
+ #requestPump(): void {
293
+ if (!this.#active || this.#connection === undefined) return;
294
+ this.#pumpRequested = true;
295
+ if (this.#pumpTask !== undefined) return;
296
+
297
+ const lifecycle = this.#lifecycle;
298
+ const connection = this.#connection;
299
+ const task = this.#drain(lifecycle, connection).catch((error) => {
300
+ if (this.#isCurrent(lifecycle, connection)) {
301
+ this.#pumpRequested = false;
302
+ this.#status = { kind: "failed", error };
303
+ }
304
+ });
305
+ this.#pumpTask = task;
306
+ void task.then(() => {
307
+ if (this.#pumpTask === task) this.#pumpTask = undefined;
308
+ if (this.#pumpRequested && this.#isCurrent(lifecycle, connection)) {
309
+ this.#requestPump();
310
+ }
311
+ });
312
+ }
313
+
314
+ async #drain(
315
+ lifecycle: number,
316
+ connection: NativeContentConnection,
317
+ ): Promise<void> {
318
+ while (this.#pumpRequested && this.#isCurrent(lifecycle, connection)) {
319
+ this.#pumpRequested = false;
320
+ await this.#updateOnce(lifecycle, connection);
321
+ }
322
+ }
323
+
324
+ async #updateOnce(
325
+ lifecycle: number,
326
+ connection: NativeContentConnection,
327
+ ): Promise<void> {
328
+ const snapshot = connection.current() ?? this.#snapshot;
329
+ const viewport = this.#viewport;
330
+ if (snapshot === undefined || viewport === undefined) return;
331
+
332
+ const update = this.#desiredUpdate(snapshot, viewport);
333
+ if (equalDesired(update, snapshot.desired)) {
334
+ this.#status = { kind: "ready" };
335
+ return;
336
+ }
337
+
338
+ const generation = snapshot.cursor.attach_generation;
339
+ this.#status = { kind: "updating" };
340
+ const result = await connection.updateDesired(update);
341
+ if (!this.#isCurrent(lifecycle, connection)) return;
342
+
343
+ const current = connection.current();
344
+ if (
345
+ current !== undefined &&
346
+ current.cursor.attach_generation !== generation
347
+ ) {
348
+ this.#acceptSnapshot(current);
349
+ return;
350
+ }
351
+ if (result.status === "rejected") {
352
+ this.#status = { kind: "rejected", rejection: result.rejection };
353
+ return;
354
+ }
355
+
356
+ const admitted = current ?? result.snapshot;
357
+ if (admitted.cursor.attach_generation === generation) {
358
+ this.#acceptSnapshot(admitted);
359
+ }
360
+ }
361
+
362
+ #desiredUpdate(
363
+ snapshot: NativeContentSnapshot,
364
+ viewport: ClientRect,
365
+ ): DesiredUpdate {
366
+ return {
367
+ generation: snapshot.cursor.attach_generation,
368
+ host_window_id: snapshot.desired.host_window_id,
369
+ viewport,
370
+ scale: this.#scale,
371
+ rounding: snapshot.desired.rounding,
372
+ presence: snapshot.desired.presence,
373
+ visibility: copyVisibility(this.#visibility),
374
+ focus: this.#focus,
375
+ input_routing: this.#inputRouting,
376
+ };
377
+ }
378
+
379
+ #installObserver(): void {
380
+ if (
381
+ !this.#active ||
382
+ this.#viewportElement === undefined ||
383
+ this.#observer !== undefined
384
+ ) {
385
+ return;
386
+ }
387
+ const observer = this.#resizeObserverFactory(() => {
388
+ try {
389
+ this.refreshViewport();
390
+ } catch (error) {
391
+ if (this.#active) this.#status = { kind: "failed", error };
392
+ }
393
+ });
394
+ observer.observe(this.#viewportElement);
395
+ this.#observer = observer;
396
+ }
397
+
398
+ #disconnectObserver(): void {
399
+ this.#observer?.disconnect();
400
+ this.#observer = undefined;
401
+ }
402
+
403
+ #isCurrent(
404
+ lifecycle: number,
405
+ connection: NativeContentConnection,
406
+ ): boolean {
407
+ return (
408
+ this.#active &&
409
+ lifecycle === this.#lifecycle &&
410
+ connection === this.#connection
411
+ );
412
+ }
413
+ }
414
+
415
+ function defaultResizeObserverFactory(
416
+ callback: () => void,
417
+ ): NativeContentResizeObserver {
418
+ if (typeof globalThis.ResizeObserver !== "function") {
419
+ throw new NativeContentResizeObserverUnavailableError();
420
+ }
421
+ return new globalThis.ResizeObserver(() => callback());
422
+ }
423
+
424
+ function measureViewport(element: HTMLElement): ClientRect {
425
+ const rect = element.getBoundingClientRect();
426
+ for (const [name, value] of [
427
+ ["left", rect.left],
428
+ ["top", rect.top],
429
+ ["width", rect.width],
430
+ ["height", rect.height],
431
+ ] as const) {
432
+ if (!Number.isFinite(value)) {
433
+ throw new NativeContentViewportMeasurementError(name, value);
434
+ }
435
+ }
436
+ if (rect.width < 0 || rect.height < 0) {
437
+ throw new NativeContentViewportMeasurementError(
438
+ "extent",
439
+ Math.min(rect.width, rect.height),
440
+ );
441
+ }
442
+ return {
443
+ origin: { x: rect.left, y: rect.top },
444
+ size: { width: rect.width, height: rect.height },
445
+ };
446
+ }
447
+
448
+ function assertScale(scale: ScaleFactor): void {
449
+ if (
450
+ !Number.isSafeInteger(scale) ||
451
+ scale < 1 ||
452
+ scale > 0xffff_ffff
453
+ ) {
454
+ throw new RangeError(
455
+ "native-content scale must be integer thousandths in the u32 range",
456
+ );
457
+ }
458
+ }
459
+
460
+ function copyVisibility(visibility: DesiredVisibility): DesiredVisibility {
461
+ return visibility.state === "visible"
462
+ ? { state: "visible" }
463
+ : { state: "hidden", reason: visibility.reason };
464
+ }
465
+
466
+ function equalVisibility(
467
+ left: DesiredVisibility,
468
+ right: DesiredVisibility,
469
+ ): boolean {
470
+ return (
471
+ left.state === right.state &&
472
+ (left.state === "visible" ||
473
+ (right.state === "hidden" && left.reason === right.reason))
474
+ );
475
+ }
476
+
477
+ function equalRect(left: ClientRect, right: ClientRect | undefined): boolean {
478
+ return (
479
+ right !== undefined &&
480
+ left.origin.x === right.origin.x &&
481
+ left.origin.y === right.origin.y &&
482
+ left.size.width === right.size.width &&
483
+ left.size.height === right.size.height
484
+ );
485
+ }
486
+
487
+ function equalDesired(
488
+ update: DesiredUpdate,
489
+ desired: NativeContentSnapshot["desired"],
490
+ ): boolean {
491
+ return (
492
+ update.generation === desired.generation &&
493
+ update.host_window_id === desired.host_window_id &&
494
+ equalRect(update.viewport, desired.viewport) &&
495
+ update.scale === desired.scale &&
496
+ update.rounding === desired.rounding &&
497
+ update.presence === desired.presence &&
498
+ equalVisibility(update.visibility, desired.visibility) &&
499
+ update.focus === desired.focus &&
500
+ update.input_routing === desired.input_routing
501
+ );
502
+ }
503
+
504
+ export class NativeContentViewportUnboundError extends Error {
505
+ constructor() {
506
+ super("native-content viewport element is not bound");
507
+ this.name = "NativeContentViewportUnboundError";
508
+ }
509
+ }
510
+
511
+ export class NativeContentViewportMeasurementError extends Error {
512
+ constructor(
513
+ readonly field: string,
514
+ readonly value: number,
515
+ ) {
516
+ super(`native-content viewport ${field} is invalid: ${value}`);
517
+ this.name = "NativeContentViewportMeasurementError";
518
+ }
519
+ }
520
+
521
+ export class NativeContentResizeObserverUnavailableError extends Error {
522
+ constructor() {
523
+ super("ResizeObserver is required for native-content viewport binding");
524
+ this.name = "NativeContentResizeObserverUnavailableError";
525
+ }
526
+ }