跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

remote-display.mjs

84 行 · 3.7 KB历史下载
1// The guest reports its running pixel density. Zoom changes its logical workspace size,
2// while noVNC fits the resulting framebuffer into the available CSS area.
3// A guest that follows its viewer is given pixelRatio, the density of the
4// screen showing it: it renders one pixel per screen pixel, and zoom changes
5// the density instead of the pixel count. The reduced-clarity setting (1.5)
6// keeps three quarters of those pixels in each direction.
7export function displayGeometry({ width, height }, { desktop = false, zoom = 1, density = 2, pixelRatio } = {}) {
8 if (width < (desktop ? 320 : 160) || height < 240) return null;
9 const follow = pixelRatio > 0;
10 let scale = density === 1.5 ? 1.5 : 2, ratio = scale / zoom;
11 if (follow) ratio = Math.max(1, Math.max(1, Math.min(2, pixelRatio)) * scale / 2);
12 const limit = Math.min(1, 8192 / (width * ratio), 8192 / (height * ratio));
13 // Chromium applies a density in steps of 1/64.
14 if (follow) scale = Math.round(Math.max(0.75, Math.min(4, ratio * limit * zoom)) * 64) / 64;
15 return {
16 width: Math.max(desktop ? 320 : Math.ceil(160 * scale), Math.floor(width * ratio * limit)),
17 height: Math.max(desktop ? 240 : Math.ceil(240 * scale), Math.floor(height * ratio * limit)),
18 ...(follow ? { scale } : {}),
19 };
20}
21
22// Reports that the window moved to a screen of another density, or that the
23// page zoom changed. A media query matches one ratio only, so each change
24// registers the next. Returns the function that stops watching.
25export function watchPixelRatio(changed, view = window) {
26 let query, stopped = false;
27 const update = () => {
28 if (stopped) return;
29 listen();
30 changed();
31 };
32 const listen = () => {
33 query = view.matchMedia(`(resolution: ${view.devicePixelRatio}dppx)`);
34 // Older WebKit has no events here; the next window resize still adapts.
35 query.addEventListener?.("change", update, { once: true });
36 };
37 listen();
38 return () => { stopped = true; query.removeEventListener?.("change", update); };
39}
40
41// Serialize guest changes: a slow old request must finish before the newest
42// size is applied. Failed changes get bounded retries even without another
43// window resize. Disposing never starts another request.
44export function displaySizeSync(resize, { onError = () => {}, onSuccess = () => {}, delay = 160 } = {}) {
45 let desired, applied = "", timer, busy = false, disposed = false, retries = 0;
46 const key = (size) => size ? `${size.width}x${size.height}@${size.scale ?? ""}` : "";
47 const schedule = (ms) => { clearTimeout(timer); timer = setTimeout(apply, ms); };
48 async function apply() {
49 timer = null;
50 if (disposed || busy || !desired || key(desired) === applied) return;
51 const target = desired;
52 let retryDelay;
53 busy = true;
54 try {
55 await resize(target);
56 if (!disposed) {
57 applied = key(target);
58 if (key(desired) === applied) { retries = 0; onSuccess(); }
59 }
60 } catch (error) {
61 if (!disposed && key(desired) === key(target)) {
62 onError(error);
63 if (retries < 3) retryDelay = 500 * 2 ** retries++;
64 }
65 } finally {
66 busy = false;
67 if (!disposed) {
68 if (key(desired) !== key(target)) schedule(delay);
69 else if (retryDelay !== undefined) schedule(retryDelay);
70 }
71 }
72 }
73 return {
74 update(size) {
75 if (disposed || !size || key(size) === key(desired)) return;
76 desired = size;
77 // Replace a retry timer, but let an ordinary resize tick fire during a drag.
78 if (!busy && (timer == null || retries > 0)) schedule(delay);
79 retries = 0;
80 },
81 retry() { if (!disposed) { applied = ""; retries = 0; schedule(0); } },
82 dispose() { disposed = true; clearTimeout(timer); },
83 };
84}