@1agh/maude 2.0.3 → 2.0.6
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.
- package/README.md +6 -5
- package/apps/hub/src/project-transactions/kernel.mjs +10 -1
- package/apps/studio/access/filters.ts +12 -5
- package/apps/studio/access/known-canvases.ts +24 -1
- package/apps/studio/access/refused-inputs.ts +89 -0
- package/apps/studio/access/request.ts +0 -1
- package/apps/studio/acp/adapter-stderr.ts +43 -13
- package/apps/studio/acp/background-tasks.ts +155 -0
- package/apps/studio/acp/bridge.ts +44 -5
- package/apps/studio/acp/connected-fact.ts +15 -4
- package/apps/studio/acp/index.ts +14 -5
- package/apps/studio/actions/agents.ts +10 -0
- package/apps/studio/actions/defs/app.ts +12 -0
- package/apps/studio/actions/defs/view.ts +12 -0
- package/apps/studio/actions.manifest.json +25 -1
- package/apps/studio/actions.parity.json +13 -1
- package/apps/studio/annotations/ui/element-node.tsx +139 -54
- package/apps/studio/annotations/ui/lod.tsx +1 -1
- package/apps/studio/annotations/ui/text-style.ts +1 -1
- package/apps/studio/annotations-layer.tsx +0 -12
- package/apps/studio/api.ts +73 -24
- package/apps/studio/artboard-live-gate.tsx +267 -0
- package/apps/studio/artboard-live.ts +336 -0
- package/apps/studio/artboard-webgl.ts +668 -0
- package/apps/studio/bin/_agent-browser-bin.mjs +8 -0
- package/apps/studio/bin/_agent-browser-safe.mjs +2 -1
- package/apps/studio/bin/_canvas-reasons.mjs +34 -0
- package/apps/studio/bin/_ds-check-live.mjs +37 -0
- package/apps/studio/bin/_ds-check.mjs +4 -1
- package/apps/studio/bin/_ds-prove-judge.mjs +167 -0
- package/apps/studio/bin/_ds-prove-playwright.mjs +183 -105
- package/apps/studio/bin/_import-asset.mjs +2 -1
- package/apps/studio/bin/_perf-probe.mjs +2 -1
- package/apps/studio/bin/_png-playwright.mjs +10 -1
- package/apps/studio/bin/_pw-launch.mjs +14 -0
- package/apps/studio/bin/_studio-verb.mjs +24 -3
- package/apps/studio/bin/_thumb-blank.mjs +70 -0
- package/apps/studio/bin/_thumb-cover.mjs +35 -0
- package/apps/studio/bin/_thumbs-playwright.mjs +93 -13
- package/apps/studio/bin/ds-check.sh +5 -1
- package/apps/studio/bin/perf.sh +11 -1
- package/apps/studio/bin/server-up.sh +7 -2
- package/apps/studio/bin/smoke.sh +19 -0
- package/apps/studio/boot-progress.ts +31 -0
- package/apps/studio/canvas-build-sandbox.ts +275 -42
- package/apps/studio/canvas-build-worker.ts +10 -0
- package/apps/studio/canvas-create.ts +24 -0
- package/apps/studio/canvas-lib.tsx +131 -12
- package/apps/studio/canvas-module-cache.ts +93 -0
- package/apps/studio/canvas-source/errors.ts +39 -0
- package/apps/studio/canvas-source/waiting.ts +290 -0
- package/apps/studio/client/actions/shell-key-handlers.js +5 -0
- package/apps/studio/client/app.jsx +19 -9
- package/apps/studio/client/canvas-url.js +13 -0
- package/apps/studio/client/github.js +23 -3
- package/apps/studio/client/home/HomeAccounts.jsx +262 -0
- package/apps/studio/client/home/Shelves.jsx +4 -3
- package/apps/studio/client/home/home-accounts-model.js +121 -0
- package/apps/studio/client/home/home-api.js +36 -19
- package/apps/studio/client/home/home-app.jsx +141 -40
- package/apps/studio/client/home/home-model.js +172 -6
- package/apps/studio/client/home/home.css +169 -5
- package/apps/studio/client/home/shell-mode.js +8 -1
- package/apps/studio/client/hooks/use-git-actions.jsx +2 -1
- package/apps/studio/client/hooks/use-project-data.jsx +2 -0
- package/apps/studio/client/hooks/use-shell-core.jsx +3 -0
- package/apps/studio/client/hooks/use-tabs.jsx +64 -35
- package/apps/studio/client/index-loader.ts +23 -3
- package/apps/studio/client/panels/ChatPanel.jsx +49 -57
- package/apps/studio/client/panels/CreateProject.jsx +19 -5
- package/apps/studio/client/panels/DiffView.jsx +314 -248
- package/apps/studio/client/panels/TeamProjects.jsx +119 -69
- package/apps/studio/client/panels/acp-runtime.js +25 -0
- package/apps/studio/client/panels/caps-cache.js +46 -0
- package/apps/studio/client/panels/chat-finish.js +66 -0
- package/apps/studio/client/shell/frame-cap.js +53 -0
- package/apps/studio/client/shell/viewport.jsx +45 -8
- package/apps/studio/client/styles/_index.css +2 -0
- package/apps/studio/client/tree/tree.jsx +48 -21
- package/apps/studio/client/v2/access/access-client.js +2 -0
- package/apps/studio/client/v2/access/access-host.jsx +12 -1
- package/apps/studio/client/v2/access/trashed-banner.jsx +6 -3
- package/apps/studio/client/v2/ai/Advanced.jsx +12 -0
- package/apps/studio/client/v2/ai/BackgroundStrip.jsx +120 -0
- package/apps/studio/client/v2/ai/Composer.jsx +27 -10
- package/apps/studio/client/v2/ai/StoppedDivider.jsx +2 -1
- package/apps/studio/client/v2/ai/ai-run.css +87 -3
- package/apps/studio/client/v2/ai/background-model.js +113 -0
- package/apps/studio/client/v2/ai/needs-you-store.js +2 -1
- package/apps/studio/client/v2/ai/run-api.js +7 -3
- package/apps/studio/client/v2/ai/runs-store.js +2 -1
- package/apps/studio/client/v2/annotations/vote-store.js +11 -5
- package/apps/studio/client/v2/assets/asset-state-lane.js +6 -3
- package/apps/studio/client/v2/assets/assets-bus.js +10 -8
- package/apps/studio/client/v2/assets/browse-view.jsx +2 -0
- package/apps/studio/client/v2/assets/ds-library-view.jsx +47 -0
- package/apps/studio/client/v2/assets/spotlight-model.js +75 -0
- package/apps/studio/client/v2/assets/spotlight.css +153 -0
- package/apps/studio/client/v2/assets/spotlight.jsx +199 -0
- package/apps/studio/client/v2/assets/tile-menu.jsx +3 -1
- package/apps/studio/client/v2/assets/tile.jsx +3 -0
- package/apps/studio/client/v2/assets/use-assets.js +2 -1
- package/apps/studio/client/v2/canvases/canvases-panel.jsx +13 -2
- package/apps/studio/client/v2/canvases/covers.js +11 -5
- package/apps/studio/client/v2/canvases/folder-ask.js +139 -0
- package/apps/studio/client/v2/canvases/folder-dialogs.jsx +150 -0
- package/apps/studio/client/v2/canvases/trash-canvas.js +10 -2
- package/apps/studio/client/v2/canvases/trash-queue.js +152 -0
- package/apps/studio/client/v2/canvases/use-canvas-index.js +42 -18
- package/apps/studio/client/v2/canvases/use-pins.js +2 -1
- package/apps/studio/client/v2/code/canvas-error-card.jsx +70 -5
- package/apps/studio/client/v2/code/canvas-imports-card.jsx +15 -14
- package/apps/studio/client/v2/code/canvas-imports-words.js +44 -0
- package/apps/studio/client/v2/code/canvas-waiting-card.jsx +128 -0
- package/apps/studio/client/v2/code/canvas-waiting.js +164 -0
- package/apps/studio/client/v2/code/code-bus.js +5 -0
- package/apps/studio/client/v2/code/code.css +67 -0
- package/apps/studio/client/v2/code/use-canvas-source.js +19 -4
- package/apps/studio/client/v2/create/NameDialog.jsx +4 -2
- package/apps/studio/client/v2/create/name-dialog.css +10 -0
- package/apps/studio/client/v2/ds/advanced-host.jsx +11 -23
- package/apps/studio/client/v2/ds/ds-refresh.js +161 -0
- package/apps/studio/client/v2/ds/library-host.jsx +45 -45
- package/apps/studio/client/v2/ds/make-host.jsx +13 -12
- package/apps/studio/client/v2/ds/off-system-host.jsx +18 -16
- package/apps/studio/client/v2/ds/review-host.jsx +7 -1
- package/apps/studio/client/v2/ds/truth-host.jsx +15 -14
- package/apps/studio/client/v2/ds/upgrade-host.jsx +26 -10
- package/apps/studio/client/v2/empty/arriving.jsx +29 -12
- package/apps/studio/client/v2/empty/empty-host.jsx +8 -2
- package/apps/studio/client/v2/empty/not-local.jsx +31 -1
- package/apps/studio/client/v2/empty/use-empty-canvas.js +10 -1
- package/apps/studio/client/v2/export/last-finished.js +2 -1
- package/apps/studio/client/v2/history/branch-actions.js +13 -9
- package/apps/studio/client/v2/history/git-advanced.jsx +8 -1
- package/apps/studio/client/v2/history/git-model.js +21 -5
- package/apps/studio/client/v2/history/git-parts.jsx +76 -43
- package/apps/studio/client/v2/history/history.css +6 -0
- package/apps/studio/client/v2/left-panel/assets-panel.jsx +87 -10
- package/apps/studio/client/v2/left-panel/use-trash.js +2 -1
- package/apps/studio/client/v2/onboarding/AdvancedSheets.jsx +100 -33
- package/apps/studio/client/v2/onboarding/HubSheet.jsx +51 -83
- package/apps/studio/client/v2/onboarding/onboarding-model.js +10 -6
- package/apps/studio/client/v2/presence/faces.jsx +42 -14
- package/apps/studio/client/v2/present/order-notes-artboards.js +2 -1
- package/apps/studio/client/v2/search/search-recent.js +3 -1
- package/apps/studio/client/v2/search/search-remote.js +6 -4
- package/apps/studio/client/v2/settings/connections-model.js +1 -1
- package/apps/studio/client/v2/shell/account-card.jsx +10 -4
- package/apps/studio/client/v2/shell/canvas-crash-guard.js +137 -0
- package/apps/studio/client/v2/shell/github-signin-host.jsx +42 -0
- package/apps/studio/client/v2/shell/menu-runner.js +12 -0
- package/apps/studio/client/v2/shell/occlusion-insets.js +14 -2
- package/apps/studio/client/v2/shell/project-account-card.jsx +203 -0
- package/apps/studio/client/v2/shell/project-account-model.js +182 -0
- package/apps/studio/client/v2/shell/recovery-store.js +2 -1
- package/apps/studio/client/v2/shell/served-routes.js +120 -0
- package/apps/studio/client/v2/shell/share-cluster-host.jsx +139 -11
- package/apps/studio/client/v2/shell/share-cluster.css +58 -0
- package/apps/studio/client/v2/shell/share-cluster.jsx +26 -7
- package/apps/studio/client/v2/shell/shell-activity.js +19 -0
- package/apps/studio/client/v2/shell/team-switch.js +94 -0
- package/apps/studio/client/v2/shell/too-heavy-card.jsx +38 -0
- package/apps/studio/client/v2/shell/use-account.js +8 -1
- package/apps/studio/client/v2/shell/use-occluded-insets.js +3 -0
- package/apps/studio/client/v2/sync-states/conflict-card.jsx +7 -2
- package/apps/studio/client/v2/undo/use-undo-projection.jsx +3 -1
- package/apps/studio/cloud/endpoints.ts +23 -1
- package/apps/studio/collab/persistence.ts +9 -1
- package/apps/studio/collab/registry.ts +32 -0
- package/apps/studio/collab/room.ts +8 -0
- package/apps/studio/config-reload-wire.ts +53 -0
- package/apps/studio/dist/client.bundle.js +1098 -1102
- package/apps/studio/dist/styles.css +1 -1
- package/apps/studio/ds/board/specimen.tsx +12 -1
- package/apps/studio/ds/canvas-cli.ts +58 -2
- package/apps/studio/ds/check.ts +26 -1
- package/apps/studio/ds/cli.ts +7 -3
- package/apps/studio/ds/convert.ts +34 -6
- package/apps/studio/ds/ds-verb.ts +13 -0
- package/apps/studio/ds/post-build.ts +38 -0
- package/apps/studio/ds/upgrade-apply.ts +27 -5
- package/apps/studio/ds/upgrade-cli.ts +22 -2
- package/apps/studio/ds/upgrade-prove.ts +198 -15
- package/apps/studio/ds/upgrade-stage.ts +4 -1
- package/apps/studio/editing/minimap-context.tsx +3 -3
- package/apps/studio/editing/page-activity.ts +251 -0
- package/apps/studio/editing/rotate-handle.tsx +9 -5
- package/apps/studio/editing/sticky-name.tsx +4 -4
- package/apps/studio/history/restore-artboard-action.ts +40 -0
- package/apps/studio/history/trash.ts +6 -0
- package/apps/studio/home/accounts.ts +87 -0
- package/apps/studio/home/hub.ts +77 -175
- package/apps/studio/home/index-reader.ts +24 -2
- package/apps/studio/home/routes.ts +66 -8
- package/apps/studio/http.ts +131 -68
- package/apps/studio/index-flight.ts +40 -0
- package/apps/studio/managed-projects.ts +52 -24
- package/apps/studio/migrate/hub.ts +7 -1
- package/apps/studio/routes/canvas-source.ts +44 -2
- package/apps/studio/routes/ds-convert.ts +18 -0
- package/apps/studio/routes/history.ts +28 -5
- package/apps/studio/routes/hub-signin.ts +115 -0
- package/apps/studio/routes/index.ts +4 -0
- package/apps/studio/routes/table.ts +7 -0
- package/apps/studio/routes/trash.ts +19 -1
- package/apps/studio/server.ts +19 -10
- package/apps/studio/sync/accepted-cold-start.ts +54 -16
- package/apps/studio/sync/accepted-link.ts +9 -4
- package/apps/studio/sync/asset-push.ts +146 -23
- package/apps/studio/sync/asset-state.ts +3 -2
- package/apps/studio/sync/code-module-consent.ts +74 -0
- package/apps/studio/sync/file-ledger.ts +35 -0
- package/apps/studio/sync/file-membership.ts +28 -3
- package/apps/studio/sync/file-plane.ts +483 -86
- package/apps/studio/sync/file-pull.ts +304 -33
- package/apps/studio/sync/hub-browser-signin.ts +377 -0
- package/apps/studio/sync/hub-link.ts +1 -2
- package/apps/studio/sync/hub-text-undo.ts +30 -10
- package/apps/studio/sync/index.ts +381 -161
- package/apps/studio/sync/journal-client.ts +94 -5
- package/apps/studio/sync/pending-deletes.ts +100 -0
- package/apps/studio/sync/projection.ts +121 -14
- package/apps/studio/sync/pull-pacer.ts +160 -0
- package/apps/studio/sync/pull-priority.ts +119 -0
- package/apps/studio/sync/seed-progress.ts +58 -3
- package/apps/studio/sync/status.ts +3 -1
- package/apps/studio/sync/transaction-client.ts +71 -6
- package/apps/studio/sync/workspace-signin.ts +6 -256
- package/apps/studio/sync/writer-registry.ts +5 -1
- package/apps/studio/thumbs/service.ts +12 -2
- package/apps/studio/thumbs/validate.ts +7 -0
- package/apps/studio/ui-prefs-migrate.ts +4 -0
- package/apps/studio/video-comp.tsx +23 -0
- package/apps/studio/video-links-overlay.tsx +3 -2
- package/apps/studio/whats-new.json +126 -0
- package/cli/lib/bash-guard.mjs +13 -1
- package/cli/lib/design-help.gen.mjs +1 -1
- package/cli/lib/design-hook.mjs +20 -0
- package/cli/lib/studio-locate.mjs +35 -10
- package/cli/lib/workspace-plan.mjs +14 -0
- package/package.json +8 -8
|
@@ -0,0 +1,668 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file artboard-webgl.ts — 2.0.5: the per-canvas WebGL context budget
|
|
3
|
+
* @scope apps/studio/artboard-webgl.ts (DOM half of artboard-live.ts `planWebgl`)
|
|
4
|
+
* @purpose A canvas of 88 three.js artboards created 88 WebGL contexts; the browsers drop the oldest past
|
|
5
|
+
* ~16 and WebKit kills the page long before memory runs out. Every WebGL context a canvas creates
|
|
6
|
+
* is tracked here (a `getContext` wrapper, installed only when artboards may sleep — never in a
|
|
7
|
+
* capture). At most `WEBGL_LIVE_CAP` stay live: those of live artboards nearest the viewport
|
|
8
|
+
* centre. The rest are released through `WEBGL_lose_context` and restored when their artboard is
|
|
9
|
+
* live again.
|
|
10
|
+
*
|
|
11
|
+
* What a released canvas shows: its last frame, in an overlay laid ABOVE the canvas while the
|
|
12
|
+
* canvas itself is `visibility: hidden` — Blink paints its own lost-context icon over a lost WebGL
|
|
13
|
+
* canvas, so a picture under it (the first 2.0.5 build used the canvas's CSS background) never
|
|
14
|
+
* shows. A context created past the cap (one React commit mounts every artboard of a canvas opened
|
|
15
|
+
* zoomed out) is released at once, before it drew — the browser would otherwise drop the oldest
|
|
16
|
+
* for good. Such a never-awake artboard gets its first frame read later, ONE context at a time and
|
|
17
|
+
* only while the cap has room ("priming": restore, let the page draw a few frames, read, release).
|
|
18
|
+
* A page that never draws while asleep sleeps as a placeholder with the artboard's name.
|
|
19
|
+
*
|
|
20
|
+
* We cancel the `webglcontextlost` of a loss WE caused (that is what lets `restoreContext()` work)
|
|
21
|
+
* and leave a loss the browser caused to the page. three.js re-initialises on
|
|
22
|
+
* `webglcontextrestored` by itself; raw WebGL code must listen for it (or gate its loop on
|
|
23
|
+
* `useArtboardActive()`) — the canvas plugin docs say so.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import { type GlCandidate, planWebgl, WEBGL_LIVE_CAP } from './artboard-live.ts';
|
|
27
|
+
|
|
28
|
+
type GL = WebGLRenderingContext | WebGL2RenderingContext;
|
|
29
|
+
type LoseExt = { loseContext(): void; restoreContext(): void };
|
|
30
|
+
|
|
31
|
+
interface Entry {
|
|
32
|
+
key: number;
|
|
33
|
+
canvas: HTMLCanvasElement;
|
|
34
|
+
ext: LoseExt | null;
|
|
35
|
+
/**
|
|
36
|
+
* live · lost by us · being lost (reading its frame first) · restored only to read a first frame ·
|
|
37
|
+
* lost by the browser.
|
|
38
|
+
*/
|
|
39
|
+
state: 'live' | 'ours' | 'losing' | 'priming' | 'browser';
|
|
40
|
+
lastLive: number;
|
|
41
|
+
/** The last frame read from it (a data URL), kept across wakes as a fallback. */
|
|
42
|
+
snap: string | null;
|
|
43
|
+
/** No frame could be read (the page drew nothing): it sleeps as a placeholder until it wakes. */
|
|
44
|
+
noFrame: boolean;
|
|
45
|
+
/** The picture above the hidden canvas while it sleeps. */
|
|
46
|
+
overlay: HTMLDivElement | null;
|
|
47
|
+
/** We called `loseContext()` and have not restored it since. */
|
|
48
|
+
glLost: boolean;
|
|
49
|
+
/** …and its `webglcontextlost` has not come yet. */
|
|
50
|
+
pendingLoss: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Our loss event has come (and was cancelled): only now does `restoreContext()` work — called earlier it
|
|
53
|
+
* fails silently and the page never draws again. A restore asked for before it waits for the event.
|
|
54
|
+
*/
|
|
55
|
+
lostSeen: boolean;
|
|
56
|
+
/** The canvas's own inline `visibility`, put back when it shows again. */
|
|
57
|
+
savedVisibility: string | null;
|
|
58
|
+
watchers: { ro: ResizeObserver | null; mo: MutationObserver | null } | null;
|
|
59
|
+
/** The most recent drawn frame of it while live (a page that stops drawing when asleep has no fresh one). */
|
|
60
|
+
keeper: FrameKeeper | null;
|
|
61
|
+
/** Frames until its next keep (the first comes soon after it wakes). */
|
|
62
|
+
sampleIn: number;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Holds the most recent drawn frame of a live canvas: a GPU copy, encoded only when it is needed. */
|
|
66
|
+
export interface FrameKeeper {
|
|
67
|
+
/** Copy the canvas's current frame in; `false` (the older copy kept) when the canvas is blank. */
|
|
68
|
+
copy(canvas: HTMLCanvasElement): boolean;
|
|
69
|
+
/** The kept frame as an image URL, or `null` when none was kept. */
|
|
70
|
+
url(): string | null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface WebglBudgetOptions {
|
|
74
|
+
/** Read the canvas's current frame as an image URL; `null` when there is nothing (blank) to read. */
|
|
75
|
+
readFrame?: (canvas: HTMLCanvasElement) => string | null;
|
|
76
|
+
/** A keeper per sampled canvas (default: two ≤640 px 2D canvases). */
|
|
77
|
+
keeper?: () => FrameKeeper;
|
|
78
|
+
/** Run `fn` at the next animation frame. */
|
|
79
|
+
frame?: (fn: () => void) => void;
|
|
80
|
+
/** Pause between two first-frame reads (default `PRIME_GAP_MS`). */
|
|
81
|
+
primeGapMs?: number;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const SNAPSHOT_MAX = 640;
|
|
85
|
+
/** Frames to wait for a page to draw before giving up on a frame (WebKit reads blank until it composited). */
|
|
86
|
+
const READ_FRAMES = 8;
|
|
87
|
+
/** A restored context gets this many frames to re-initialise and draw before the first read. */
|
|
88
|
+
const PRIME_SETTLE = 2;
|
|
89
|
+
/** Live contexts' frames are kept every this many frames (~1 s): one GPU copy + a 16 px readback each. */
|
|
90
|
+
const SAMPLE_EVERY = 60;
|
|
91
|
+
/** …the first one this many frames after it went live. */
|
|
92
|
+
const FIRST_SAMPLE = 10;
|
|
93
|
+
/** Pause between two first-frame reads: each restores a context, so the page re-initialises its scene. */
|
|
94
|
+
const PRIME_GAP_MS = 150;
|
|
95
|
+
|
|
96
|
+
/** Every pixel the same and transparent or black: the page has drawn nothing into it. */
|
|
97
|
+
function isBlank(data: Uint8ClampedArray): boolean {
|
|
98
|
+
const [r, g, b, a] = [data[0], data[1], data[2], data[3]];
|
|
99
|
+
if (!(a === 0 || (r === 0 && g === 0 && b === 0))) return false;
|
|
100
|
+
for (let i = 4; i < data.length; i += 4)
|
|
101
|
+
if (data[i] !== r || data[i + 1] !== g || data[i + 2] !== b || data[i + 3] !== a) return false;
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** The default keeper: the frame is drawn into a scratch canvas, checked on a 16 px probe, then kept. */
|
|
106
|
+
export function canvasFrameKeeper(): FrameKeeper {
|
|
107
|
+
let keep: HTMLCanvasElement | null = null;
|
|
108
|
+
let scratch: HTMLCanvasElement | null = null;
|
|
109
|
+
let probe: CanvasRenderingContext2D | null = null;
|
|
110
|
+
return {
|
|
111
|
+
copy(c) {
|
|
112
|
+
if (!c.width || !c.height || typeof document === 'undefined') return false;
|
|
113
|
+
try {
|
|
114
|
+
const k = Math.min(1, SNAPSHOT_MAX / Math.max(c.width, c.height));
|
|
115
|
+
const w = Math.max(1, Math.round(c.width * k));
|
|
116
|
+
const h = Math.max(1, Math.round(c.height * k));
|
|
117
|
+
scratch ??= document.createElement('canvas');
|
|
118
|
+
if (scratch.width !== w || scratch.height !== h) {
|
|
119
|
+
scratch.width = w;
|
|
120
|
+
scratch.height = h;
|
|
121
|
+
}
|
|
122
|
+
const g = scratch.getContext('2d');
|
|
123
|
+
if (!g) return false;
|
|
124
|
+
g.clearRect(0, 0, w, h);
|
|
125
|
+
g.drawImage(c, 0, 0, w, h);
|
|
126
|
+
if (!probe) {
|
|
127
|
+
const pc = document.createElement('canvas');
|
|
128
|
+
pc.width = 16;
|
|
129
|
+
pc.height = 16;
|
|
130
|
+
probe = pc.getContext('2d', { willReadFrequently: true });
|
|
131
|
+
}
|
|
132
|
+
if (probe) {
|
|
133
|
+
probe.clearRect(0, 0, 16, 16);
|
|
134
|
+
probe.drawImage(scratch, 0, 0, 16, 16);
|
|
135
|
+
if (isBlank(probe.getImageData(0, 0, 16, 16).data)) return false;
|
|
136
|
+
}
|
|
137
|
+
[keep, scratch] = [scratch, keep];
|
|
138
|
+
return true;
|
|
139
|
+
} catch {
|
|
140
|
+
return false; // a tainted or unreadable canvas
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
url() {
|
|
144
|
+
try {
|
|
145
|
+
return keep ? keep.toDataURL('image/png') : null;
|
|
146
|
+
} catch {
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** The default `readFrame`: a ≤640 px PNG of the drawing buffer, or `null` when it is blank. */
|
|
154
|
+
export function readCanvasFrame(c: HTMLCanvasElement): string | null {
|
|
155
|
+
const k = canvasFrameKeeper();
|
|
156
|
+
return k.copy(c) ? k.url() : null;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function artboardName(c: HTMLCanvasElement): string {
|
|
160
|
+
const board = c.closest('[data-dc-screen]');
|
|
161
|
+
if (!board) return '';
|
|
162
|
+
const label = board.querySelector('.dc-artboard-label');
|
|
163
|
+
const text = label instanceof HTMLInputElement ? label.value : (label?.textContent ?? '').trim();
|
|
164
|
+
return text || board.getAttribute('data-dc-screen') || '';
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export class WebglBudget {
|
|
168
|
+
private entries = new Map<number, Entry>();
|
|
169
|
+
private byCanvas = new WeakMap<HTMLCanvasElement, Entry>();
|
|
170
|
+
private seq = 0;
|
|
171
|
+
private queued = false;
|
|
172
|
+
private toPlace = new Set<Entry>();
|
|
173
|
+
private placeQueued = false;
|
|
174
|
+
private readonly readFrame: (c: HTMLCanvasElement) => string | null;
|
|
175
|
+
private readonly frame: (fn: () => void) => void;
|
|
176
|
+
private readonly newKeeper: () => FrameKeeper;
|
|
177
|
+
private sampling = false;
|
|
178
|
+
private readonly primeGapMs: number;
|
|
179
|
+
private primeAfter = 0;
|
|
180
|
+
private primeTimer: ReturnType<typeof setTimeout> | null = null;
|
|
181
|
+
|
|
182
|
+
constructor(
|
|
183
|
+
private readonly isLive: (artboardId: string | null) => boolean,
|
|
184
|
+
private readonly cap: number = WEBGL_LIVE_CAP,
|
|
185
|
+
opts: WebglBudgetOptions = {}
|
|
186
|
+
) {
|
|
187
|
+
this.readFrame = opts.readFrame ?? readCanvasFrame;
|
|
188
|
+
this.newKeeper = opts.keeper ?? canvasFrameKeeper;
|
|
189
|
+
this.primeGapMs = opts.primeGapMs ?? PRIME_GAP_MS;
|
|
190
|
+
this.frame =
|
|
191
|
+
opts.frame ??
|
|
192
|
+
((fn) => {
|
|
193
|
+
if (typeof requestAnimationFrame === 'function') requestAnimationFrame(() => fn());
|
|
194
|
+
else setTimeout(fn, 16);
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Wraps `HTMLCanvasElement.prototype.getContext` once per window. Returns the uninstall. */
|
|
199
|
+
install(win: Window & typeof globalThis): () => void {
|
|
200
|
+
const proto = win.HTMLCanvasElement?.prototype;
|
|
201
|
+
if (!proto) return () => {};
|
|
202
|
+
const orig = proto.getContext;
|
|
203
|
+
const budget = this;
|
|
204
|
+
const wrapped = function (this: HTMLCanvasElement, type: string, ...rest: unknown[]) {
|
|
205
|
+
const ctx = (orig as (...a: unknown[]) => unknown).call(this, type, ...rest);
|
|
206
|
+
if (ctx && typeof type === 'string' && /webgl/i.test(type)) budget.track(this, ctx as GL);
|
|
207
|
+
return ctx;
|
|
208
|
+
} as typeof proto.getContext;
|
|
209
|
+
proto.getContext = wrapped;
|
|
210
|
+
return () => {
|
|
211
|
+
if (proto.getContext === wrapped) proto.getContext = orig;
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
track(canvas: HTMLCanvasElement, gl: GL): void {
|
|
216
|
+
if (this.byCanvas.has(canvas)) return;
|
|
217
|
+
let ext: LoseExt | null = null;
|
|
218
|
+
try {
|
|
219
|
+
ext = (gl.getExtension('WEBGL_lose_context') as LoseExt | null) ?? null;
|
|
220
|
+
} catch {
|
|
221
|
+
ext = null;
|
|
222
|
+
}
|
|
223
|
+
const e: Entry = {
|
|
224
|
+
key: ++this.seq,
|
|
225
|
+
canvas,
|
|
226
|
+
ext,
|
|
227
|
+
state: 'live',
|
|
228
|
+
lastLive: Date.now(),
|
|
229
|
+
snap: null,
|
|
230
|
+
noFrame: false,
|
|
231
|
+
overlay: null,
|
|
232
|
+
glLost: false,
|
|
233
|
+
pendingLoss: false,
|
|
234
|
+
lostSeen: false,
|
|
235
|
+
savedVisibility: null,
|
|
236
|
+
watchers: null,
|
|
237
|
+
keeper: null,
|
|
238
|
+
sampleIn: FIRST_SAMPLE,
|
|
239
|
+
};
|
|
240
|
+
this.entries.set(e.key, e);
|
|
241
|
+
this.byCanvas.set(canvas, e);
|
|
242
|
+
canvas.addEventListener('webglcontextlost', (ev) => {
|
|
243
|
+
if (!e.pendingLoss) {
|
|
244
|
+
e.state = 'browser';
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
ev.preventDefault();
|
|
248
|
+
e.pendingLoss = false;
|
|
249
|
+
// The browser allows a restore only once this dispatch has returned (Blink reads `defaultPrevented`
|
|
250
|
+
// after it; a microtask still runs inside it), so act a task later.
|
|
251
|
+
setTimeout(() => {
|
|
252
|
+
e.lostSeen = true;
|
|
253
|
+
// Asked back (woken, or picked for a first-frame read) before the loss even arrived: restore now.
|
|
254
|
+
if (e.state === 'live' || e.state === 'priming') this.restoreNow(e);
|
|
255
|
+
else this.update(); // it can be primed now
|
|
256
|
+
}, 0);
|
|
257
|
+
});
|
|
258
|
+
canvas.addEventListener('webglcontextrestored', () => {
|
|
259
|
+
if (e.state === 'browser') e.state = 'live';
|
|
260
|
+
});
|
|
261
|
+
if (ext) this.evictOverCap(e);
|
|
262
|
+
// Created while the page builds: plan once the current task (the whole React commit) is done.
|
|
263
|
+
this.update();
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** Re-plan soon (coalesced to one microtask). */
|
|
267
|
+
update(): void {
|
|
268
|
+
if (this.queued) return;
|
|
269
|
+
this.queued = true;
|
|
270
|
+
queueMicrotask(() => {
|
|
271
|
+
this.queued = false;
|
|
272
|
+
this.apply();
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
stats(): { tracked: number; live: number; lost: number } {
|
|
277
|
+
let live = 0;
|
|
278
|
+
for (const e of this.entries.values()) if (e.state === 'live') live += 1;
|
|
279
|
+
return { tracked: this.entries.size, live, lost: this.entries.size - live };
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** Plan and act now. */
|
|
283
|
+
apply(): void {
|
|
284
|
+
const win = typeof window === 'undefined' ? null : window;
|
|
285
|
+
const cx = (win?.innerWidth ?? 0) / 2;
|
|
286
|
+
const cy = (win?.innerHeight ?? 0) / 2;
|
|
287
|
+
const now = Date.now();
|
|
288
|
+
const cands: GlCandidate[] = [];
|
|
289
|
+
for (const e of [...this.entries.values()]) {
|
|
290
|
+
if (!e.canvas.isConnected) {
|
|
291
|
+
// Unmounted (a light-mode artboard, a re-render): free its context instead of waiting for GC.
|
|
292
|
+
this.drop(e);
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
if (e.state === 'browser' || !e.ext) continue;
|
|
296
|
+
const board = e.canvas.closest('[data-dc-screen]');
|
|
297
|
+
const live = this.isLive(board ? board.getAttribute('data-dc-screen') : null);
|
|
298
|
+
const r = e.canvas.getBoundingClientRect();
|
|
299
|
+
const distance = Math.hypot(r.left + r.width / 2 - cx, r.top + r.height / 2 - cy);
|
|
300
|
+
cands.push({ key: e.key, live, distance, lastLive: e.lastLive, lost: e.state !== 'live' });
|
|
301
|
+
}
|
|
302
|
+
const { lose, restore } = planWebgl(cands, this.cap);
|
|
303
|
+
for (const key of lose) {
|
|
304
|
+
const e = this.entries.get(key);
|
|
305
|
+
if (e?.state === 'live') this.loseWithSnapshot(e);
|
|
306
|
+
}
|
|
307
|
+
const restoring = new Set(restore);
|
|
308
|
+
for (const e of this.entries.values()) {
|
|
309
|
+
// Live artboards come first: a first-frame read gives its slot back (and is tried again later).
|
|
310
|
+
if (restore.length && e.state === 'priming' && !restoring.has(e.key)) {
|
|
311
|
+
this.cover(e);
|
|
312
|
+
this.loseNow(e);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
for (const key of restore) {
|
|
316
|
+
const e = this.entries.get(key);
|
|
317
|
+
if (e) this.restore(e, now);
|
|
318
|
+
}
|
|
319
|
+
for (const c of cands) {
|
|
320
|
+
const e = this.entries.get(c.key);
|
|
321
|
+
if (e && e.state === 'live') e.lastLive = now;
|
|
322
|
+
}
|
|
323
|
+
if (this.busy() > 0) this.sample();
|
|
324
|
+
// A never-awake artboard: read its first frame, one context at a time, while the cap has room.
|
|
325
|
+
const primeWait = this.primeAfter - now;
|
|
326
|
+
if (primeWait > 0) {
|
|
327
|
+
if (!this.primeTimer)
|
|
328
|
+
this.primeTimer = setTimeout(() => {
|
|
329
|
+
this.primeTimer = null;
|
|
330
|
+
this.update();
|
|
331
|
+
}, primeWait);
|
|
332
|
+
} else if (
|
|
333
|
+
this.busy() < this.cap &&
|
|
334
|
+
![...this.entries.values()].some((e) => e.state === 'priming')
|
|
335
|
+
) {
|
|
336
|
+
const next = cands
|
|
337
|
+
.filter((c) => {
|
|
338
|
+
const e = this.entries.get(c.key);
|
|
339
|
+
return (
|
|
340
|
+
e?.state === 'ours' && e.lostSeen && !e.snap && !e.noFrame && !restoring.has(c.key)
|
|
341
|
+
);
|
|
342
|
+
})
|
|
343
|
+
.sort((a, b) => a.distance - b.distance)[0];
|
|
344
|
+
const e = next && this.entries.get(next.key);
|
|
345
|
+
if (e) this.prime(e);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** While any context is live: keep its frame every `SAMPLE_EVERY` frames (after the page drew it). */
|
|
350
|
+
private sample(): void {
|
|
351
|
+
if (this.sampling) return;
|
|
352
|
+
this.sampling = true;
|
|
353
|
+
const step = () => {
|
|
354
|
+
let any = false;
|
|
355
|
+
for (const e of this.entries.values()) {
|
|
356
|
+
if (e.state !== 'live' || e.glLost || !e.canvas.isConnected) continue;
|
|
357
|
+
any = true;
|
|
358
|
+
e.sampleIn -= 1;
|
|
359
|
+
if (e.sampleIn <= 0) {
|
|
360
|
+
e.sampleIn = SAMPLE_EVERY;
|
|
361
|
+
e.keeper ??= this.newKeeper();
|
|
362
|
+
e.keeper.copy(e.canvas);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
if (!any) {
|
|
366
|
+
this.sampling = false;
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
this.frame(step);
|
|
370
|
+
};
|
|
371
|
+
this.frame(step);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/** Contexts holding a slot: live, about to be lost, or restored to read a frame. */
|
|
375
|
+
private busy(): number {
|
|
376
|
+
let n = 0;
|
|
377
|
+
for (const e of this.entries.values())
|
|
378
|
+
if (e.state === 'live' || e.state === 'losing' || e.state === 'priming') n += 1;
|
|
379
|
+
return n;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Past the cap at creation: release one now — a not-live artboard's first, oldest first, never the one
|
|
384
|
+
* just created (its page is still initialising it). It has drawn nothing yet; it is primed later.
|
|
385
|
+
*/
|
|
386
|
+
private evictOverCap(fresh: Entry): void {
|
|
387
|
+
if (this.busy() <= this.cap) return;
|
|
388
|
+
let victim: Entry | null = null;
|
|
389
|
+
let victimLive = true;
|
|
390
|
+
for (const e of this.entries.values()) {
|
|
391
|
+
if (e === fresh || !e.ext) continue;
|
|
392
|
+
if (e.state !== 'live' && e.state !== 'losing' && e.state !== 'priming') continue;
|
|
393
|
+
const board = e.canvas.closest('[data-dc-screen]');
|
|
394
|
+
const live = this.isLive(board ? board.getAttribute('data-dc-screen') : null);
|
|
395
|
+
if (!victim || (victimLive && !live)) {
|
|
396
|
+
victim = e;
|
|
397
|
+
victimLive = live;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
if (!victim) return;
|
|
401
|
+
this.cover(victim);
|
|
402
|
+
this.loseNow(victim);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/** Read its frame (waiting up to `READ_FRAMES` for one), cover the canvas with it, release the context. */
|
|
406
|
+
private loseWithSnapshot(e: Entry): void {
|
|
407
|
+
e.state = 'losing';
|
|
408
|
+
this.readThenLose(e, 'losing', 1);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/** Restore a never-awake context only to read its first frame, then release it again. */
|
|
412
|
+
private prime(e: Entry): void {
|
|
413
|
+
e.state = 'priming';
|
|
414
|
+
this.cover(e); // the placeholder stays on top while it draws
|
|
415
|
+
this.showCanvas(e); // …but the canvas must paint, or WebKit has no frame to read
|
|
416
|
+
if (!this.restoreNow(e)) {
|
|
417
|
+
e.noFrame = true;
|
|
418
|
+
this.cover(e);
|
|
419
|
+
this.hideCanvas(e);
|
|
420
|
+
e.state = 'ours';
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
this.readThenLose(e, 'priming', PRIME_SETTLE);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
private readThenLose(e: Entry, as: 'losing' | 'priming', from: number): void {
|
|
427
|
+
let n = 0;
|
|
428
|
+
const step = () => {
|
|
429
|
+
if (e.state !== as) return; // restored (or dropped) before the frame came
|
|
430
|
+
n += 1;
|
|
431
|
+
if (n >= from) {
|
|
432
|
+
// In a frame our callback runs after the page's own render loop (it registered first), so the
|
|
433
|
+
// drawing buffer still holds the frame the page just drew.
|
|
434
|
+
const url = e.canvas.isConnected ? this.readFrame(e.canvas) : null;
|
|
435
|
+
if (url) {
|
|
436
|
+
e.snap = url;
|
|
437
|
+
e.noFrame = false;
|
|
438
|
+
} else if (n < from + READ_FRAMES) {
|
|
439
|
+
this.frame(step);
|
|
440
|
+
return;
|
|
441
|
+
} else {
|
|
442
|
+
// Nothing fresh (a page that stops drawing once asleep): the frame kept while it was live.
|
|
443
|
+
const kept = e.keeper?.url() ?? null;
|
|
444
|
+
if (kept) e.snap = kept;
|
|
445
|
+
else if (!e.snap) e.noFrame = true;
|
|
446
|
+
}
|
|
447
|
+
e.keeper = null;
|
|
448
|
+
this.cover(e);
|
|
449
|
+
this.loseNow(e);
|
|
450
|
+
// A slot is free: the next never-awake one can be primed — after a pause, so a canvas opened
|
|
451
|
+
// zoomed out fills in over a few seconds instead of re-initialising every scene at once.
|
|
452
|
+
if (as === 'priming') this.primeAfter = Date.now() + this.primeGapMs;
|
|
453
|
+
this.update();
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
this.frame(step);
|
|
457
|
+
};
|
|
458
|
+
this.frame(step);
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
private loseNow(e: Entry): void {
|
|
462
|
+
e.state = 'ours';
|
|
463
|
+
this.hideCanvas(e);
|
|
464
|
+
if (e.glLost) return; // still lost from before (a wake that had not come through yet)
|
|
465
|
+
e.glLost = true;
|
|
466
|
+
e.pendingLoss = true;
|
|
467
|
+
try {
|
|
468
|
+
e.ext?.loseContext();
|
|
469
|
+
} catch {
|
|
470
|
+
e.pendingLoss = false; // already lost
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/** `restoreContext()` once our loss event has come; before it, the event handler does it. */
|
|
475
|
+
private restoreNow(e: Entry): boolean {
|
|
476
|
+
if (!e.lostSeen) return true;
|
|
477
|
+
e.lostSeen = false;
|
|
478
|
+
e.glLost = false;
|
|
479
|
+
try {
|
|
480
|
+
e.ext?.restoreContext();
|
|
481
|
+
return true;
|
|
482
|
+
} catch {
|
|
483
|
+
return false; // not restorable — the page keeps a blank canvas until it rebuilds
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
private restore(e: Entry, now: number): void {
|
|
488
|
+
if (e.state === 'losing') {
|
|
489
|
+
e.state = 'live';
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
if (e.state !== 'ours' && e.state !== 'priming') return;
|
|
493
|
+
const wasPriming = e.state === 'priming';
|
|
494
|
+
e.state = 'live';
|
|
495
|
+
e.lastLive = now;
|
|
496
|
+
e.noFrame = false;
|
|
497
|
+
e.sampleIn = FIRST_SAMPLE;
|
|
498
|
+
this.showCanvas(e);
|
|
499
|
+
if (!wasPriming) this.restoreNow(e);
|
|
500
|
+
// Leave the picture above the canvas until the page has drawn again.
|
|
501
|
+
this.frame(() => this.frame(() => this.uncover(e)));
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
private drop(e: Entry): void {
|
|
505
|
+
this.entries.delete(e.key);
|
|
506
|
+
this.uncover(e, true);
|
|
507
|
+
if (e.state === 'live' || e.state === 'losing' || e.state === 'priming') {
|
|
508
|
+
e.state = 'ours';
|
|
509
|
+
try {
|
|
510
|
+
e.ext?.loseContext();
|
|
511
|
+
} catch {
|
|
512
|
+
/* already lost */
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
// ── the picture above a sleeping canvas ─────────────────────────────────────────────────────────
|
|
518
|
+
|
|
519
|
+
/** Lay (or refresh) the overlay right after the canvas: its last frame, or a placeholder with a name. */
|
|
520
|
+
private cover(e: Entry): void {
|
|
521
|
+
const c = e.canvas;
|
|
522
|
+
const parent = c.parentNode;
|
|
523
|
+
if (!parent || typeof document === 'undefined') return;
|
|
524
|
+
let o = e.overlay;
|
|
525
|
+
if (!o) {
|
|
526
|
+
o = document.createElement('div');
|
|
527
|
+
o.setAttribute('aria-hidden', 'true');
|
|
528
|
+
const s = o.style;
|
|
529
|
+
s.position = 'absolute';
|
|
530
|
+
s.pointerEvents = 'none';
|
|
531
|
+
s.boxSizing = 'border-box';
|
|
532
|
+
s.margin = '0';
|
|
533
|
+
s.overflow = 'hidden';
|
|
534
|
+
s.display = 'flex';
|
|
535
|
+
s.alignItems = 'center';
|
|
536
|
+
s.justifyContent = 'center';
|
|
537
|
+
s.textAlign = 'center';
|
|
538
|
+
e.overlay = o;
|
|
539
|
+
}
|
|
540
|
+
if (o.parentNode !== parent || o.previousSibling !== c) parent.insertBefore(o, c.nextSibling);
|
|
541
|
+
const s = o.style;
|
|
542
|
+
if (e.snap) {
|
|
543
|
+
o.setAttribute('data-dc-gl-snapshot', 'frame');
|
|
544
|
+
o.textContent = '';
|
|
545
|
+
s.backgroundColor = 'transparent';
|
|
546
|
+
s.backgroundImage = `url("${e.snap}")`;
|
|
547
|
+
s.backgroundSize = '100% 100%';
|
|
548
|
+
s.backgroundRepeat = 'no-repeat';
|
|
549
|
+
} else {
|
|
550
|
+
// The same quiet tile in either theme: a neutral wash, the name in the artboard's own text colour.
|
|
551
|
+
o.setAttribute('data-dc-gl-snapshot', 'placeholder');
|
|
552
|
+
o.textContent = artboardName(c);
|
|
553
|
+
s.backgroundImage = 'none';
|
|
554
|
+
s.backgroundColor = 'rgb(128 128 128 / 0.14)';
|
|
555
|
+
s.color = 'rgb(128 128 128)';
|
|
556
|
+
s.font = '500 14px/1.3 system-ui, sans-serif';
|
|
557
|
+
s.padding = '8px';
|
|
558
|
+
}
|
|
559
|
+
this.schedulePlace(e);
|
|
560
|
+
if (!e.watchers) {
|
|
561
|
+
const ro =
|
|
562
|
+
typeof ResizeObserver === 'function'
|
|
563
|
+
? new ResizeObserver(() => this.schedulePlace(e))
|
|
564
|
+
: null;
|
|
565
|
+
ro?.observe(c);
|
|
566
|
+
const mo =
|
|
567
|
+
typeof MutationObserver === 'function'
|
|
568
|
+
? new MutationObserver(() => {
|
|
569
|
+
if (!c.isConnected) this.drop(e);
|
|
570
|
+
else if (c.parentNode !== e.overlay?.parentNode) {
|
|
571
|
+
// Moved by its page: follow it.
|
|
572
|
+
const asleep = e.state !== 'live';
|
|
573
|
+
this.uncover(e, true);
|
|
574
|
+
if (asleep) {
|
|
575
|
+
this.cover(e);
|
|
576
|
+
this.hideCanvas(e);
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
})
|
|
580
|
+
: null;
|
|
581
|
+
mo?.observe(parent, { childList: true });
|
|
582
|
+
e.watchers = { ro, mo };
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/** Placement is batched (all reads, then all writes): a commit of 60 artboards forces two layouts, not 120. */
|
|
587
|
+
private schedulePlace(e: Entry): void {
|
|
588
|
+
this.toPlace.add(e);
|
|
589
|
+
if (this.placeQueued) return;
|
|
590
|
+
this.placeQueued = true;
|
|
591
|
+
queueMicrotask(() => {
|
|
592
|
+
this.placeQueued = false;
|
|
593
|
+
const list = [...this.toPlace].filter((x) => x.overlay && x.canvas.isConnected);
|
|
594
|
+
this.toPlace.clear();
|
|
595
|
+
this.placeAll(list);
|
|
596
|
+
});
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/** Same box as the canvas, in the canvas's own containing block (corrected if it differs from offsetParent). */
|
|
600
|
+
private placeAll(list: Entry[]): void {
|
|
601
|
+
if (!list.length) return;
|
|
602
|
+
try {
|
|
603
|
+
const boxes = list.map((e) => {
|
|
604
|
+
const c = e.canvas;
|
|
605
|
+
return {
|
|
606
|
+
left: c.offsetLeft,
|
|
607
|
+
top: c.offsetTop,
|
|
608
|
+
w: c.offsetWidth,
|
|
609
|
+
h: c.offsetHeight,
|
|
610
|
+
radius: getComputedStyle(c).borderRadius,
|
|
611
|
+
};
|
|
612
|
+
});
|
|
613
|
+
list.forEach((e, i) => {
|
|
614
|
+
const b = boxes[i];
|
|
615
|
+
const s = (e.overlay as HTMLDivElement).style;
|
|
616
|
+
if (!b) return;
|
|
617
|
+
s.left = `${b.left}px`;
|
|
618
|
+
s.top = `${b.top}px`;
|
|
619
|
+
s.width = `${b.w}px`;
|
|
620
|
+
s.height = `${b.h}px`;
|
|
621
|
+
s.borderRadius = b.radius;
|
|
622
|
+
if (!e.snap) s.fontSize = `${Math.max(12, Math.round(b.w / 10))}px`;
|
|
623
|
+
});
|
|
624
|
+
const fixes = list.map((e) => {
|
|
625
|
+
const o = e.overlay as HTMLDivElement;
|
|
626
|
+
const cr = e.canvas.getBoundingClientRect();
|
|
627
|
+
const or = o.getBoundingClientRect();
|
|
628
|
+
if (!(or.width > 0 && o.offsetWidth > 0)) return null;
|
|
629
|
+
const k = or.width / o.offsetWidth; // the artboard's zoom
|
|
630
|
+
const dx = (cr.left - or.left) / k;
|
|
631
|
+
const dy = (cr.top - or.top) / k;
|
|
632
|
+
return Math.abs(dx) > 0.5 || Math.abs(dy) > 0.5 ? { dx, dy } : null;
|
|
633
|
+
});
|
|
634
|
+
list.forEach((e, i) => {
|
|
635
|
+
const f = fixes[i];
|
|
636
|
+
const b = boxes[i];
|
|
637
|
+
if (!f || !b) return;
|
|
638
|
+
const s = (e.overlay as HTMLDivElement).style;
|
|
639
|
+
s.left = `${b.left + f.dx}px`;
|
|
640
|
+
s.top = `${b.top + f.dy}px`;
|
|
641
|
+
});
|
|
642
|
+
} catch {
|
|
643
|
+
/* no layout (a test DOM): the offsets stand */
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
private uncover(e: Entry, force = false): void {
|
|
648
|
+
if (!force && e.state !== 'live') return;
|
|
649
|
+
e.watchers?.ro?.disconnect();
|
|
650
|
+
e.watchers?.mo?.disconnect();
|
|
651
|
+
e.watchers = null;
|
|
652
|
+
e.overlay?.remove();
|
|
653
|
+
e.overlay = null;
|
|
654
|
+
this.showCanvas(e);
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
/** Hidden while lost: nothing the browser paints for a lost context can show. */
|
|
658
|
+
private hideCanvas(e: Entry): void {
|
|
659
|
+
if (e.savedVisibility === null) e.savedVisibility = e.canvas.style.visibility;
|
|
660
|
+
e.canvas.style.visibility = 'hidden';
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
private showCanvas(e: Entry): void {
|
|
664
|
+
if (e.savedVisibility === null) return;
|
|
665
|
+
e.canvas.style.visibility = e.savedVisibility;
|
|
666
|
+
e.savedVisibility = null;
|
|
667
|
+
}
|
|
668
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// _agent-browser-bin.mjs — the one lookup for the agent-browser executable (2.0.5 app finding).
|
|
2
|
+
// The desktop sidecar pins the bundled engine with MAUDE_AGENT_BROWSER (sidecar.rs); the shell
|
|
3
|
+
// helpers read the same name (screenshot.sh, canvas-rects.sh, perf.sh, photo-bg-remove.sh).
|
|
4
|
+
// MAUDE_AGENT_BROWSER_BIN is the older spelling and keeps working. Neither set: PATH lookup.
|
|
5
|
+
|
|
6
|
+
export function resolveAgentBrowserBin(env = process.env) {
|
|
7
|
+
return env.MAUDE_AGENT_BROWSER || env.MAUDE_AGENT_BROWSER_BIN || 'agent-browser';
|
|
8
|
+
}
|
|
@@ -106,6 +106,7 @@ import { spawnSync } from 'node:child_process';
|
|
|
106
106
|
import { dirname, join } from 'node:path';
|
|
107
107
|
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
108
108
|
|
|
109
|
+
import { resolveAgentBrowserBin } from './_agent-browser-bin.mjs';
|
|
109
110
|
import { urlRefusal } from './_url-guard.mjs';
|
|
110
111
|
|
|
111
112
|
const HERE = dirname(fileURLToPath(import.meta.url));
|
|
@@ -239,7 +240,7 @@ function main() {
|
|
|
239
240
|
const [sub, ...rest] = argv;
|
|
240
241
|
const env = safeEnv(process.env);
|
|
241
242
|
const result = spawnSync(
|
|
242
|
-
|
|
243
|
+
resolveAgentBrowserBin(),
|
|
243
244
|
['--config', EMPTY_CONFIG_PATH, sub, ...rest, '--allowed-domains', 'localhost,127.0.0.1'],
|
|
244
245
|
{ stdio: 'inherit', env }
|
|
245
246
|
);
|