Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
feat(remote-desktop): 按控制端画面比例调整并恢复电脑分辨率 (#4485)
Dashe3f40ca3d763
diff --git a/apps/desktop/native/remote-desktop/macos-viewer-display.m b/apps/desktop/native/remote-desktop/macos-viewer-display.mnew file mode 100644index 0000000000..476d6692d2--- /dev/null+++ b/apps/desktop/native/remote-desktop/macos-viewer-display.m@@ -0,0 +1,133 @@+#import <Foundation/Foundation.h>+#import <CoreGraphics/CoreGraphics.h>++// Experimental CoreGraphics SPI, also used by Chromium's virtual display tests.+// Resolve classes at runtime: an OS without this SPI must retain normal displays.+@interface NSObject (CindyVirtualDisplay)+- (id)initWithDescriptor:(id)descriptor;+- (id)initWithWidth:(unsigned int)width height:(unsigned int)height refreshRate:(double)rate;+- (BOOL)applySettings:(id)settings;+- (unsigned int)displayID;+@end++static id display;+static CGDirectDisplayID sourceDisplay;+static CGDisplayModeRef sourceMode;++static void cleanup(void) {+ if (sourceDisplay && CGDisplayIsOnline(sourceDisplay)) {+ CGDisplayConfigRef config;+ if (CGBeginDisplayConfiguration(&config) == kCGErrorSuccess) {+ CGConfigureDisplayMirrorOfDisplay(config, sourceDisplay, kCGNullDirectDisplay);+ if (sourceMode) CGConfigureDisplayWithDisplayMode(config, sourceDisplay, sourceMode, NULL);+ CGCompleteDisplayConfiguration(config, kCGConfigureForSession);+ }+ }+ display = nil;+ if (sourceMode) CGDisplayModeRelease(sourceMode);+ sourceMode = NULL;+}+static void emit(NSDictionary *value) {+ NSData *data = [NSJSONSerialization dataWithJSONObject:value options:0 error:nil];+ fwrite(data.bytes, 1, data.length, stdout);+ fputc('\n', stdout);+ fflush(stdout);+}++static BOOL available(void) {+ return NSClassFromString(@"CGVirtualDisplay") && NSClassFromString(@"CGVirtualDisplayDescriptor") &&+ NSClassFromString(@"CGVirtualDisplaySettings") && NSClassFromString(@"CGVirtualDisplayMode");+}++static BOOL setSize(unsigned int width, unsigned int height) {+ if (!display) {+ id descriptor = [NSClassFromString(@"CGVirtualDisplayDescriptor") new];+ [descriptor setValue:@"Cindy Remote Desktop" forKey:@"name"];+ [descriptor setValue:@(4096) forKey:@"maxPixelsWide"];+ [descriptor setValue:@(4096) forKey:@"maxPixelsHigh"];+ [descriptor setValue:@(0x4349) forKey:@"vendorID"];+ [descriptor setValue:@(1) forKey:@"productID"];+ [descriptor setValue:@(getpid()) forKey:@"serialNum"];+ if ([descriptor respondsToSelector:NSSelectorFromString(@"setSerialNumber:")])+ [descriptor setValue:@(getpid()) forKey:@"serialNumber"];+ [descriptor setValue:dispatch_get_main_queue() forKey:@"queue"];+ [descriptor setValue:[NSValue valueWithSize:NSMakeSize(300, 300)] forKey:@"sizeInMillimeters"];+ display = [[NSClassFromString(@"CGVirtualDisplay") alloc] initWithDescriptor:descriptor];+ if (!display) return NO;+ }+ id mode = [[NSClassFromString(@"CGVirtualDisplayMode") alloc] initWithWidth:width height:height refreshRate:60];+ id settings = [NSClassFromString(@"CGVirtualDisplaySettings") new];+ [settings setValue:@0 forKey:@"hiDPI"];+ [settings setValue:@[mode] forKey:@"modes"];+ return [display applySettings:settings];+}++int main(int argc, const char *argv[]) {+ @autoreleasepool {+ if (argc == 2 && strcmp(argv[1], "--probe") == 0) {+ emit(@{@"available": @(available())});+ return available() ? 0 : 2;+ }+ if (!available()) return 2;+ signal(SIGTERM, SIG_IGN);+ signal(SIGINT, SIG_IGN);+ dispatch_source_t termination = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGTERM, 0, dispatch_get_main_queue());+ dispatch_source_set_event_handler(termination, ^{ cleanup(); exit(0); });+ dispatch_resume(termination);+ dispatch_source_t interrupt = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGINT, 0, dispatch_get_main_queue());+ dispatch_source_set_event_handler(interrupt, ^{ cleanup(); exit(0); });+ dispatch_resume(interrupt);+ // Only a live owning process retains the display. EOF/exit releases it.+ dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{+ char *line = NULL;+ size_t capacity = 0;+ while (getline(&line, &capacity, stdin) > 0) {+ NSData *data = [[NSString stringWithUTF8String:line] dataUsingEncoding:NSUTF8StringEncoding];+ NSDictionary *command = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];+ dispatch_sync(dispatch_get_main_queue(), ^{+ @try {+ if (![command isKindOfClass:NSDictionary.class]) { emit(@{@"error": @"INVALID_REQUEST"}); return; }+ // Electron's display IDs travel as decimal strings, unlike JSON dimensions.+ long long sourceNumber = [command[@"sourceDisplayId"] longLongValue];+ if (sourceNumber <= 0 || sourceNumber > UINT32_MAX) {+ emit(@{@"error": @"DISPLAY_SOURCE_UNAVAILABLE"}); return;+ }+ unsigned int source = (unsigned int)sourceNumber;+ if (!sourceDisplay) {+ if (!source || !CGDisplayIsOnline(source) || CGDisplayIsInMirrorSet(source)) {+ emit(@{@"error": @"DISPLAY_SOURCE_UNAVAILABLE"}); return;+ }+ sourceDisplay = source;+ sourceMode = CGDisplayCopyDisplayMode(source);+ } else if (source && source != sourceDisplay) {+ emit(@{@"error": @"DISPLAY_SOURCE_UNAVAILABLE"}); return;+ }+ unsigned int width = [command[@"width"] unsignedIntValue];+ unsigned int height = [command[@"height"] unsignedIntValue];+ if (width < 320 || height < 320 || width > 4096 || height > 4096 || !setSize(width, height)) {+ emit(@{@"error": @"DISPLAY_SIZE_FAILED"});+ return;+ }+ CGDisplayConfigRef config;+ if (CGBeginDisplayConfiguration(&config) != kCGErrorSuccess) {+ emit(@{@"error": @"DISPLAY_MIRROR_FAILED"}); return;+ }+ if (CGConfigureDisplayMirrorOfDisplay(config, sourceDisplay, [display displayID]) != kCGErrorSuccess) {+ CGCancelDisplayConfiguration(config);+ emit(@{@"error": @"DISPLAY_MIRROR_FAILED"}); return;+ }+ if (CGCompleteDisplayConfiguration(config, kCGConfigureForSession) != kCGErrorSuccess) {+ emit(@{@"error": @"DISPLAY_MIRROR_FAILED"}); return;+ }+ emit(@{@"id": @([display displayID]), @"width": @(width), @"height": @(height)});+ } @catch (NSException *exception) {+ emit(@{@"error": @"DISPLAY_UNAVAILABLE"});+ }+ });+ }+ free(line);+ dispatch_async(dispatch_get_main_queue(), ^{ cleanup(); exit(0); });+ });+ dispatch_main();+ }+}diff --git a/apps/desktop/scripts/viewer-display-native-smoke.mjs b/apps/desktop/scripts/viewer-display-native-smoke.mjsnew file mode 100644index 0000000000..a75bcf77e6--- /dev/null+++ b/apps/desktop/scripts/viewer-display-native-smoke.mjs@@ -0,0 +1,79 @@+/** Explicit macOS integration test: temporarily mirrors the main desktop, then restores it.+ * Run: node apps/desktop/scripts/viewer-display-native-smoke.mjs+ * No captures or input injection. Not part of the default unit suite.+ */+import assert from 'node:assert/strict';+import { execFile, spawn } from 'node:child_process';+import fs from 'node:fs/promises';+import os from 'node:os';+import path from 'node:path';+import { fileURLToPath } from 'node:url';+import { promisify } from 'node:util';+import { once } from 'node:events';+import { createInterface } from 'node:readline';++assert.equal(process.platform, 'darwin');+const exec = promisify(execFile);+const directory = await fs.mkdtemp(path.join(os.tmpdir(), 'cindy-viewer-display-smoke-'));+const source = fileURLToPath(+ new URL('../native/remote-desktop/macos-viewer-display.m', import.meta.url),+);+const binary = path.join(directory, 'display');+const query = `import CoreGraphics+import Foundation+var ids = [CGDirectDisplayID](repeating: 0, count: 32)+var count: UInt32 = 0+CGGetOnlineDisplayList(32, &ids, &count)+let result = ids.prefix(Int(count)).map { id -> [String: Any] in+ let bounds = CGDisplayBounds(id)+ return ["id": id, "width": bounds.width, "height": bounds.height, "main": CGDisplayIsMain(id), "mirror": CGDisplayMirrorsDisplay(id)]+}+print(String(data: try! JSONSerialization.data(withJSONObject: result), encoding: .utf8)!)`;+const displays = async () =>+ JSON.parse((await exec('swift', ['-e', query], { timeout: 30_000 })).stdout);+let child;+try {+ await exec(+ 'clang',+ ['-fobjc-arc', '-framework', 'Foundation', '-framework', 'CoreGraphics', source, '-o', binary],+ { timeout: 60_000 },+ );+ const before = await displays();+ const main = before.find((d) => d.main === 1);+ assert(main && main.mirror === 0);+ child = spawn(binary, [], { stdio: 'pipe' });+ child.stderr.resume();+ const lines = createInterface({ input: child.stdout });+ const change = async (width, height) => {+ const reply = once(lines, 'line', { signal: AbortSignal.timeout(10_000) });+ child.stdin.write(JSON.stringify({ sourceDisplayId: String(main.id), width, height }) + '\n');+ const value = JSON.parse((await reply)[0]);+ assert.equal(value.error, undefined);+ const actual = await displays();+ const mirror = actual.find((d) => d.id === main.id);+ const virtual = actual.find((d) => d.id === value.id);+ assert.equal(mirror.mirror, value.id);+ assert.equal(virtual.width, width);+ assert.equal(virtual.height, height);+ assert.equal(mirror.width, width);+ assert.equal(mirror.height, height);+ console.log(JSON.stringify({ phase: 'resized', width, height, virtualId: value.id }));+ };+ await change(900, 1600);+ await change(1600, 900);+ const exited = once(child, 'exit', { signal: AbortSignal.timeout(10_000) });+ child.stdin.end();+ await exited;+ lines.close();+ child = undefined;+ const after = await displays();+ assert.deepEqual(after, before);+ console.log(JSON.stringify({ phase: 'restored', displays: after }));+} finally {+ if (child) {+ const exited = once(child, 'exit', { signal: AbortSignal.timeout(10_000) }).catch(() => {});+ child.kill('SIGTERM');+ await exited;+ }+ await fs.rm(directory, { recursive: true, force: true });+}diff --git a/apps/desktop/src/main/remote-desktop/__tests__/viewerDisplay.test.ts b/apps/desktop/src/main/remote-desktop/__tests__/viewerDisplay.test.tsnew file mode 100644index 0000000000..27a965cc59--- /dev/null+++ b/apps/desktop/src/main/remote-desktop/__tests__/viewerDisplay.test.ts@@ -0,0 +1,396 @@+import { describe, expect, it, vi } from 'vitest';+import { RemoteDesktopController, type DesktopControllerDeps } from '../controller';+import type { RemoteDesktopLease } from '@cindy/device-link';+import type { ViewerDisplayHandle } from '../viewerDisplay';++function fixture() {+ const handle: ViewerDisplayHandle = {+ displayId: '2',+ resize: vi.fn(async (width, height) => ({ id: '2', name: 'Viewer', width, height })),+ restore: vi.fn(async () => ({ id: '1', name: 'Main', width: 1920, height: 1080 })),+ dispose: vi.fn(),+ };+ const deps: DesktopControllerDeps = {+ authorized: () => true,+ capabilities: async () => ({+ version: 1,+ enabled: true,+ canControl: true,+ platform: 'darwin',+ displays: [{ id: '1', name: 'Main', width: 1920, height: 1080 }],+ }),+ frame: vi.fn(async () => 'jpeg'),+ startInput: vi.fn(async () => {}),+ input: vi.fn(),+ stopInput: vi.fn(),+ stopVideo: vi.fn(),+ offer: vi.fn(async () => 'answer'),+ changed: vi.fn(),+ createViewerDisplay: vi.fn(async () => handle),+ };+ const host = new RemoteDesktopController(deps);+ const start = async (control = true) => {+ const lease = (await host.request('phone', {+ op: 'start',+ displayId: '1',+ })) as RemoteDesktopLease;+ if (control) await host.request('phone', { op: 'control', lease: lease.lease, enabled: true });+ return lease;+ };+ return { host, deps, handle, start };+}++describe('viewer-sized desktop ownership', () => {+ it('hides the helper display during and across stopped-lease restoration', async () => {+ const f = fixture();+ const lease = await f.start();+ await f.host.request('phone', {+ op: 'viewerDisplay',+ lease: lease.lease,+ width: 900,+ height: 1600,+ });+ const caps = await f.deps.capabilities();+ caps.displays.push({ id: '2', name: 'Viewer', width: 900, height: 1600 });+ f.deps.capabilities = async () => caps;+ let finish!: () => void;+ f.handle.restore = () =>+ new Promise((resolve) => {+ finish = () => resolve(lease.display);+ });+ f.host.stop('phone');+ expect(await f.host.request('other', { op: 'capabilities' })).toMatchObject({+ displays: [{ id: '1' }],+ });+ let read!: () => void;+ f.deps.capabilities = () =>+ new Promise((resolve) => {+ read = () => resolve(caps);+ });+ const pending = f.host.request('other', { op: 'capabilities' });+ finish();+ await Promise.resolve();+ await Promise.resolve();+ read();+ expect(await pending).toMatchObject({ displays: [{ id: '1' }] });+ });+ it('retires the handle when the source display was unplugged', async () => {+ const f = fixture(),+ lease = await f.start();+ await f.host.request('phone', {+ op: 'viewerDisplay',+ lease: lease.lease,+ width: 900,+ height: 1600,+ });+ const restore = vi.mocked(f.handle.restore!);+ restore.mockResolvedValue({ id: '1', name: 'Display', width: 0, height: 0 });+ await f.host.request('phone', { op: 'control', lease: lease.lease, enabled: true });+ await f.host.request('phone', { op: 'restoreViewerDisplay', lease: lease.lease });+ expect(restore).toHaveBeenCalledOnce();+ });+ it('enumerates source modes across matching, repeated resizing and restoration', async () => {+ const f = fixture(),+ lease = await f.start();+ const modes = [{ id: '400', width: 3840, height: 2160, current: false }];+ f.deps.displayModes = vi.fn(async (id) => (id === '1' ? modes : []));+ for (const op of ['viewerDisplay', 'viewerDisplay', 'restoreViewerDisplay'] as const) {+ await f.host.request('phone', { op: 'control', lease: lease.lease, enabled: true });+ await f.host.request('phone', { op, lease: lease.lease, width: 900, height: 1600 });+ expect(await f.host.request('phone', { op: 'displayModes', lease: lease.lease })).toEqual(+ modes,+ );+ expect(f.deps.displayModes).toHaveBeenLastCalledWith('1');+ }+ });++ it.each(['success', 'cancel', 'failure'] as const)(+ 'restores before applying a system mode to the source (%s)',+ async (outcome) => {+ const f = fixture(),+ lease = await f.start();+ await f.host.request('phone', {+ op: 'viewerDisplay',+ lease: lease.lease,+ width: 900,+ height: 1600,+ });+ await f.host.request('phone', { op: 'control', lease: lease.lease, enabled: true });+ let settle!: () => void;+ const restoration = new Promise<RemoteDesktopLease['display']>((resolve, reject) => {+ settle = () =>+ outcome === 'failure' ? reject(new Error('RESTORE_FAILED')) : resolve(lease.display);+ });+ f.handle.restore = vi.fn(() => restoration);+ f.deps.resolution = vi.fn(async (_id, _mode, beforeChange) => beforeChange());+ const pending = f.host.request('phone', {+ op: 'resolution',+ lease: lease.lease,+ modeId: '400',+ });+ expect(f.deps.resolution).not.toHaveBeenCalled();+ await expect(+ f.host.request('other', { op: 'start', displayId: '1', takeover: true }),+ ).rejects.toThrow('DESKTOP_BUSY');+ if (outcome === 'cancel') f.host.stop('phone');+ settle();+ if (outcome === 'success') {+ await pending;+ expect(f.deps.resolution).toHaveBeenCalledWith('1', '400', expect.any(Function));+ expect(f.handle.restore).toHaveBeenCalledOnce();+ } else {+ await expect(pending).rejects.toThrow(+ outcome === 'cancel' ? 'DESKTOP_LEASE_EXPIRED' : 'RESTORE_FAILED',+ );+ expect(f.deps.resolution).not.toHaveBeenCalled();+ }+ expect(f.host.state).toBeNull();+ },+ );++ it.each([false, true])(+ 'waits for restored geometry before starting a new lease (takeover=%s)',+ async (takeover) => {+ const f = fixture(),+ lease = await f.start();+ await f.host.request('phone', {+ op: 'viewerDisplay',+ lease: lease.lease,+ width: 900,+ height: 1600,+ });+ let finish!: () => void;+ let restored = false;+ f.deps.capabilities = async () => ({+ version: 1,+ enabled: true,+ canControl: true,+ platform: 'darwin',+ displays: [+ { id: '1', name: 'Main', width: restored ? 1920 : 900, height: restored ? 1080 : 1600 },+ ],+ });+ f.handle.restore = vi.fn(+ () =>+ new Promise<RemoteDesktopLease['display']>((resolve) => {+ finish = () => {+ restored = true;+ resolve({ id: '1', name: 'Main', width: 1920, height: 1080 });+ };+ }),+ );+ if (!takeover) f.host.stop('phone');+ const pending = f.host.request('other', { op: 'start', displayId: '1', takeover });+ await Promise.resolve();+ expect(f.host.state).toBeNull();+ expect(f.handle.restore).toHaveBeenCalledOnce();+ await expect(f.host.request('third', { op: 'start', displayId: '1' })).rejects.toThrow(+ 'DESKTOP_BUSY',+ );+ finish();+ const next = (await pending) as RemoteDesktopLease;+ expect(next.display).toMatchObject({ id: '1', width: 1920, height: 1080 });+ expect(f.host.hasLease(next.lease)).toBe(true);+ },+ );++ it('retains failed restoration for retry and does not accept a stale display', async () => {+ const f = fixture(),+ lease = await f.start();+ await f.host.request('phone', {+ op: 'viewerDisplay',+ lease: lease.lease,+ width: 900,+ height: 1600,+ });+ vi.mocked(f.handle.restore!).mockRejectedValueOnce(new Error('DISPLAY_NOT_RESTORED'));+ f.host.stop();+ await expect(f.start()).rejects.toThrow('DISPLAY_NOT_RESTORED');+ expect(f.host.state).toBeNull();+ await f.start();+ expect(f.handle.restore).toHaveBeenCalledTimes(2);+ });+ it('finishes restoration on exit without waiting for another connection', async () => {+ const f = fixture(),+ lease = await f.start();+ await f.host.request('phone', {+ op: 'viewerDisplay',+ lease: lease.lease,+ width: 900,+ height: 1600,+ });+ f.host.stop();+ await Promise.resolve();+ await Promise.resolve();+ expect(f.handle.restore).toHaveBeenCalledOnce();+ // Later local display changes must not trigger restoration of an already retired handle.+ vi.mocked(f.handle.restore!).mockRejectedValue(new Error('LOCAL_MODE_CHANGED'));+ await f.start();+ expect(f.handle.restore).toHaveBeenCalledOnce();+ });+ it('waits for held inputs to release and cancels before creating a display after disconnect', async () => {+ const f = fixture(),+ lease = await f.start();+ let release!: () => void;+ f.deps.releaseInput = () =>+ new Promise((resolve) => {+ release = resolve;+ });+ const pending = f.host.request('phone', {+ op: 'viewerDisplay',+ lease: lease.lease,+ width: 900,+ height: 1600,+ });+ expect(f.deps.createViewerDisplay).not.toHaveBeenCalled();+ f.host.stop('phone');+ release();+ await expect(pending).rejects.toThrow('DESKTOP_LEASE_EXPIRED');+ expect(f.deps.createViewerDisplay).not.toHaveBeenCalled();+ });+ it('keeps the lease, replaces geometry and requires fresh control before input', async () => {+ const f = fixture(),+ lease = await f.start();+ const result = await f.host.request('phone', {+ op: 'viewerDisplay',+ lease: lease.lease,+ width: 900,+ height: 1600,+ });+ expect(result).toEqual({+ lease: lease.lease,+ display: { id: '2', name: 'Viewer', width: 900, height: 1600 },+ controlling: false,+ });+ expect(f.host.hasLease(lease.lease)).toBe(true);+ await expect(+ f.host.request('phone', {+ op: 'input',+ lease: lease.lease,+ sequence: 1,+ events: [{ kind: 'move', x: 0.5, y: 0.5 }],+ }),+ ).rejects.toThrow('DESKTOP_VIEW_ONLY');+ await f.host.request('phone', { op: 'control', lease: lease.lease, enabled: true });+ expect(f.deps.startInput).toHaveBeenLastCalledWith('2');+ await f.host.request('phone', {+ op: 'viewerDisplay',+ lease: lease.lease,+ width: 1600,+ height: 900,+ });+ expect(f.deps.createViewerDisplay).toHaveBeenCalledTimes(1);+ f.host.stop();+ expect(f.handle.dispose).toHaveBeenCalledOnce();+ });++ it('restores the original display without replacing the lease and can match again', async () => {+ const f = fixture(),+ lease = await f.start();+ await f.host.request('phone', {+ op: 'viewerDisplay',+ lease: lease.lease,+ width: 900,+ height: 1600,+ });+ await expect(+ f.host.request('phone', { op: 'restoreViewerDisplay', lease: lease.lease }),+ ).rejects.toThrow('DESKTOP_VIEW_ONLY');+ await f.host.request('phone', { op: 'control', lease: lease.lease, enabled: true });+ const result = await f.host.request('phone', {+ op: 'restoreViewerDisplay',+ lease: lease.lease,+ });+ expect(result).toEqual({+ lease: lease.lease,+ controlling: false,+ display: { id: '1', name: 'Main', width: 1920, height: 1080 },+ });+ expect(f.handle.restore).toHaveBeenCalledOnce();+ expect(f.host.hasLease(lease.lease)).toBe(true);+ expect(f.host.displayGeometryMatches('1', 1920, 1080)).toBe(true);+ await f.host.request('phone', { op: 'control', lease: lease.lease, enabled: true });+ await f.host.request('phone', {+ op: 'viewerDisplay',+ lease: lease.lease,+ width: 1600,+ height: 900,+ });+ expect(f.deps.createViewerDisplay).toHaveBeenCalledTimes(2);+ });++ it('rejects view-only and foreign peers without changing displays', async () => {+ const f = fixture(),+ lease = await f.start(false);+ const request = { op: 'viewerDisplay', lease: lease.lease, width: 900, height: 1600 };+ await expect(f.host.request('phone', request)).rejects.toThrow('DESKTOP_VIEW_ONLY');+ await expect(f.host.request('other', request)).rejects.toThrow('DESKTOP_LEASE_EXPIRED');+ expect(f.deps.createViewerDisplay).not.toHaveBeenCalled();+ });++ it('disposes a late native handle after disconnect and cannot revive the lease', async () => {+ const f = fixture(),+ lease = await f.start();+ let resolve!: (handle: ViewerDisplayHandle) => void;+ f.deps.createViewerDisplay = () =>+ new Promise((done) => {+ resolve = done;+ });+ const pending = f.host.request('phone', {+ op: 'viewerDisplay',+ lease: lease.lease,+ width: 900,+ height: 1600,+ });+ f.host.stop('phone');+ resolve(f.handle);+ await expect(pending).rejects.toThrow('DESKTOP_LEASE_EXPIRED');+ expect(f.handle.dispose).toHaveBeenCalledOnce();+ expect(f.handle.resize).not.toHaveBeenCalled();+ expect(f.host.state).toBeNull();+ });++ it('drops old geometry input/frames while resizing and bounds concurrent changes', async () => {+ const f = fixture(),+ lease = await f.start();+ let finish!: () => void;+ f.handle.resize = () =>+ new Promise((resolve) => {+ finish = () => resolve({ id: '2', name: 'Viewer', width: 900, height: 1600 });+ });+ const request = { op: 'viewerDisplay', lease: lease.lease, width: 900, height: 1600 };+ const pending = f.host.request('phone', request);+ await Promise.resolve();+ expect(f.host.changingDisplay).toBe(true);+ await expect(f.host.request('phone', request)).rejects.toThrow('DESKTOP_DISPLAY_BUSY');+ await expect(+ f.host.request('other', { op: 'start', displayId: '1', takeover: true }),+ ).rejects.toThrow('DESKTOP_BUSY');+ f.host.input(lease.lease, 1, [{ kind: 'move', x: 0.5, y: 0.5 }]);+ expect(f.deps.input).not.toHaveBeenCalled();+ expect(await f.host.request('phone', { op: 'frame', lease: lease.lease })).toEqual({+ jpeg: null,+ });+ finish();+ await pending;+ expect(f.host.changingDisplay).toBe(false);+ });++ it('ends the lease and releases the temporary display on native failure', async () => {+ const f = fixture(),+ lease = await f.start();+ f.handle.resize = async () => {+ throw new Error('DISPLAY_MIRROR_FAILED');+ };+ await expect(+ f.host.request('phone', {+ op: 'viewerDisplay',+ lease: lease.lease,+ width: 900,+ height: 1600,+ }),+ ).rejects.toThrow();+ expect(f.handle.dispose).toHaveBeenCalledOnce();+ expect(f.host.state).toBeNull();+ });+});diff --git a/apps/desktop/src/main/remote-desktop/__tests__/viewerDisplaySupport.test.ts b/apps/desktop/src/main/remote-desktop/__tests__/viewerDisplaySupport.test.tsnew file mode 100644index 0000000000..3c8d16095f--- /dev/null+++ b/apps/desktop/src/main/remote-desktop/__tests__/viewerDisplaySupport.test.ts@@ -0,0 +1,62 @@+import { afterEach, beforeEach, expect, it, vi } from 'vitest';++const mocks = vi.hoisted(() => ({+ app: { isPackaged: false, getAppPath: () => '/fake/app', getPath: () => '/fake/profile' },+ exec: vi.fn(),+ access: vi.fn(),+}));+vi.mock('electron', () => ({ app: mocks.app, screen: {} }));+vi.mock('node:child_process', () => ({ execFile: vi.fn(), spawn: vi.fn() }));+vi.mock('node:util', () => ({ promisify: () => mocks.exec }));+vi.mock('node:fs/promises', () => ({+ default: {+ readFile: async () => 'source',+ access: mocks.access,+ mkdir: vi.fn(),+ rename: vi.fn(),+ },+}));++beforeEach(() => {+ vi.resetModules();+ mocks.app.isPackaged = false;+ mocks.exec.mockReset().mockResolvedValue({ stdout: '{"available":true}' });+ mocks.access.mockReset().mockResolvedValue(undefined);+ vi.spyOn(process, 'platform', 'get').mockReturnValue('darwin');+});+afterEach(() => vi.restoreAllMocks());++it('probes an existing binary once across concurrent capability requests', async () => {+ const { viewerDisplaySupported } = await import('../viewerDisplay');+ expect(await Promise.all([viewerDisplaySupported(), viewerDisplaySupported()])).toEqual([+ true,+ true,+ ]);+ expect(mocks.exec).toHaveBeenCalledTimes(1);+ expect(mocks.exec).toHaveBeenCalledWith(+ expect.stringContaining('cindy-viewer-display'),+ ['--probe'],+ { timeout: 5000 },+ );+});+it('does not advertise support after a failed SPI probe', async () => {+ mocks.exec.mockRejectedValue(new Error('unsupported SPI'));+ const { viewerDisplaySupported } = await import('../viewerDisplay');+ expect(await viewerDisplaySupported()).toBe(false);+ expect(mocks.exec).toHaveBeenCalledTimes(1);+});+it('does not advertise support when compilation fails', async () => {+ mocks.access.mockRejectedValue(new Error('missing'));+ mocks.exec.mockRejectedValue(new Error('clang unavailable'));+ const { viewerDisplaySupported } = await import('../viewerDisplay');+ expect(await viewerDisplaySupported()).toBe(false);+ expect(mocks.exec).toHaveBeenCalledWith('clang', expect.any(Array), expect.any(Object));+});+it.each(['packaged', 'windows'] as const)('does no helper work for %s builds', async (kind) => {+ if (kind === 'packaged') mocks.app.isPackaged = true;+ else vi.spyOn(process, 'platform', 'get').mockReturnValue('win32');+ const { viewerDisplaySupported } = await import('../viewerDisplay');+ expect(await viewerDisplaySupported()).toBe(false);+ expect(mocks.exec).not.toHaveBeenCalled();+ expect(mocks.access).not.toHaveBeenCalled();+});diff --git a/apps/desktop/src/main/remote-desktop/controller.ts b/apps/desktop/src/main/remote-desktop/controller.tsindex 86b981c4a9..543b7ef7df 100644--- a/apps/desktop/src/main/remote-desktop/controller.ts+++ b/apps/desktop/src/main/remote-desktop/controller.ts@@ -1,5 +1,6 @@import { ClipboardTransfer } from './clipboardTransfer';import { randomUUID } from 'node:crypto';+import type { ViewerDisplayHandle } from './viewerDisplay';import {parseRemoteDesktopRequest,REMOTE_DESKTOP_LEASE_MS,@@ -29,10 +30,14 @@ export interface DesktopControllerDeps {authenticationSession?(peer: string): string | null;capabilities(): Promise<RemoteDesktopCapabilities>;permissions?(action: 'check' | 'guide'): Promise<RemoteDesktopPermissions>;- frame(displayId: string, cursorOverlay?: boolean): Promise<string | RemoteDesktopCursorFrame | null>;+ frame(+ displayId: string,+ cursorOverlay?: boolean,+ ): Promise<string | RemoteDesktopCursorFrame | null>;startInput(displayId: string): Promise<void>;input(events: DesktopInput[]): void;stopInput(): void;+ releaseInput?(): Promise<void>;stopVideo(): void;lockScreen?(isCurrent: () => boolean, signal: AbortSignal): Promise<void>;offer(@@ -45,12 +50,21 @@ export interface DesktopControllerDeps {ice?(request: RemoteDesktopIceRequest): Promise<RemoteDesktopIceReply>;displayModes?(displayId: string): Promise<RemoteDesktopDisplayMode[]>;resolution?(displayId: string, modeId: string, beforeChange: () => void): Promise<void>;+ createViewerDisplay?(+ displayId: string,+ isCurrent: () => boolean,+ onFailure: () => void,+ ): Promise<ViewerDisplayHandle>;clipboard?(action: 'copy' | 'paste',text: string | undefined,isCurrent: () => boolean,): Promise<string | void>;- clipboardContent?(action: 'copy' | 'paste', content: RemoteClipboardContent | undefined, isCurrent: () => boolean): Promise<RemoteClipboardContent | void>;+ clipboardContent?(+ action: 'copy' | 'paste',+ content: RemoteClipboardContent | undefined,+ isCurrent: () => boolean,+ ): Promise<RemoteClipboardContent | void>;changed(): void;now?: () => number;}@@ -60,6 +74,7 @@ export class RemoteDesktopController {private active:| (RemoteDesktopLease & {peer: string;+ readonly sourceDisplayId: string;expires: number;sequence: number;authenticationSession: string | undefined;@@ -67,6 +82,10 @@ export class RemoteDesktopController {})| null = null;private starting = false;+ private viewerDisplay: ViewerDisplayHandle | null = null;+ private displayRestoring: Promise<void> | null = null;+ private displayChanging = false;+ private viewerGeometryManaged = false;private locking = false;private lockAbort: AbortController | null = null;private startingPeer: string | null = null;@@ -86,6 +105,19 @@ export class RemoteDesktopController {get displayId(): string | null {return this.active?.display.id ?? null;}+ /** Expected mirror events must not end the lease while its new geometry is prepared. */+ get changingDisplay(): boolean {+ return this.displayChanging;+ }+ displayGeometryMatches(id: string, width: number, height: number): boolean {+ const display = this.active?.display;+ return (+ this.viewerGeometryManaged &&+ display?.id === id &&+ display.width === width &&+ display.height === height+ );+ }hasLease(lease: string): boolean {this.tick();return this.active?.lease === lease;@@ -114,7 +146,8 @@ export class RemoteDesktopController {tick(): void {if (this.active &&- (this.active.expires <= this.now() || !this.deps.authorized(this.active.peer) ||+ (this.active.expires <= this.now() ||+ !this.deps.authorized(this.active.peer) ||!this.authenticationCurrent(this.active.peer, this.active.authenticationSession)))this.stop();@@ -132,8 +165,30 @@ export class RemoteDesktopController {this.active = null;this.deps.stopInput();this.deps.stopVideo();+ this.viewerDisplay?.dispose();+ // Retain the handle until restoration completes (or can be retried after failure).+ // EOF releases it asynchronously; a new lease must not bind stale geometry.+ if (!this.viewerDisplay?.restore) this.viewerDisplay = null;+ if (this.viewerDisplay) void this.restoreStoppedDisplay().catch(() => {});+ this.viewerGeometryManaged = false;this.deps.changed();}+ private restoreStoppedDisplay(): Promise<void> {+ if (this.displayRestoring) return this.displayRestoring;+ if (this.active || !this.viewerDisplay) return Promise.resolve();+ const handle = this.viewerDisplay;+ const pending = (async () => {+ await handle.restore?.(() => !this.active && this.viewerDisplay === handle);+ if (this.viewerDisplay === handle) this.viewerDisplay = null;+ })();+ this.displayRestoring = pending;+ void pending+ .finally(() => {+ if (this.displayRestoring === pending) this.displayRestoring = null;+ })+ .catch(() => {});+ return pending;+ }/*** Input injection failed while the lease is still valid. Input belongs to the* control bit, so release control and keep everything else: ending the lease@@ -183,6 +238,7 @@ export class RemoteDesktopController {const request = parseRemoteDesktopRequest({ op: 'input', lease, sequence, events });if (request.op !== 'input') return;this.require(active.peer, lease);+ if (this.displayChanging) return; // Discard queued old-geometry input after authenticating it.if (!active.controlling) throw new Error('DESKTOP_VIEW_ONLY');if (sequence <= active.sequence) return; // never replay a click after retries/reconnectactive.sequence = sequence;@@ -196,36 +252,62 @@ export class RemoteDesktopController {// capabilities response includes display information and is post-auth.const session = this.authenticationSession(peer);if (session !== undefined && !this.deps.authorized(peer)) throw new Error('DESKTOP_DISABLED');+ const viewerDisplay = this.viewerDisplay;const caps = await this.deps.capabilities();- if (!this.authenticationCurrent(peer, session)) throw new Error('DESKTOP_AUTHENTICATION_REQUIRED');+ if (!this.authenticationCurrent(peer, session))+ throw new Error('DESKTOP_AUTHENTICATION_REQUIRED');if (session !== undefined && !this.deps.authorized(peer)) throw new Error('DESKTOP_DISABLED');- return { ...caps, automaticReconnect: true, connectionTakeover: true };+ // Keep the handle alive across the read: stop clears the lease before the+ // helper disappears, and restoration can settle while capabilities awaits.+ const publicDisplays = caps.displays.filter(+ (display) =>+ display.id !== viewerDisplay?.displayId && display.id !== this.viewerDisplay?.displayId,+ );+ return {+ ...caps,+ displays: publicDisplays,+ automaticReconnect: true,+ connectionTakeover: true,+ };}if (!this.deps.authorized(peer)) throw new Error('DESKTOP_DISABLED');if (request.op === 'permissions') {const session = this.authenticationSession(peer);if (!this.deps.permissions) throw new Error('DESKTOP_PERMISSIONS_UNAVAILABLE');const permissions = await this.deps.permissions(request.action);- if (!this.authenticationCurrent(peer, session)) throw new Error('DESKTOP_AUTHENTICATION_REQUIRED');+ if (!this.authenticationCurrent(peer, session))+ throw new Error('DESKTOP_AUTHENTICATION_REQUIRED');return permissions;}if (request.op === 'start') {const authenticationSession = this.authenticationSession(peer);if (request.resume && this.userStopped.has(peer)) throw new Error('DESKTOP_STOPPED');- const resumesActive = request.resume && this.active?.peer === peer &&- this.active.display.id === request.displayId;- if (this.locking || this.starting || (this.active && !request.takeover && !resumesActive))+ const resumesActive =+ request.resume &&+ this.active?.peer === peer &&+ this.active.sourceDisplayId === request.displayId;+ if (+ this.locking ||+ this.starting ||+ this.displayChanging ||+ (this.active && !request.takeover && !resumesActive)+ )throw new Error('DESKTOP_BUSY');this.starting = true;this.startingPeer = peer;const generation = this.controlGeneration;try {+ if (!this.active && this.viewerDisplay) await this.restoreStoppedDisplay();const caps = await this.deps.capabilities();if (!this.authenticationCurrent(peer, authenticationSession))throw new Error('DESKTOP_AUTHENTICATION_REQUIRED');- const display = caps.displays.find((d) => d.id === request.displayId);+ let display = caps.displays.find((d) => d.id === request.displayId);if (!display) throw new Error('DESKTOP_DISPLAY_MISSING');- if (this.startingPeer !== peer || generation !== this.controlGeneration || !this.deps.authorized(peer))+ if (+ this.startingPeer !== peer ||+ generation !== this.controlGeneration ||+ !this.deps.authorized(peer)+ )throw new Error('DESKTOP_DISABLED');if (caps.permissions && !desktopPermissionReady(caps.permissions.screenRecording))throw new Error('DESKTOP_SCREEN_PERMISSION_REQUIRED');@@ -234,11 +316,26 @@ export class RemoteDesktopController {// A lost start reply can leave our own lease alive. Rotate it using the// existing cleanup so the new viewer can restart its input sequence at 0.else if (resumesActive) this.stop(peer);+ if (this.viewerDisplay) {+ const restorationGeneration = this.controlGeneration;+ await this.restoreStoppedDisplay();+ const restored = await this.deps.capabilities();+ if (+ this.startingPeer !== peer ||+ restorationGeneration !== this.controlGeneration ||+ !this.deps.authorized(peer) ||+ !this.authenticationCurrent(peer, authenticationSession)+ )+ throw new Error('DESKTOP_LEASE_EXPIRED');+ display = restored.displays.find((d) => d.id === request.displayId);+ if (!display) throw new Error('DESKTOP_DISPLAY_MISSING');+ }if (!request.resume) this.userStopped.delete(peer);const lease: RemoteDesktopLease = { lease: randomUUID(), display, controlling: false };this.active = {...lease,peer,+ sourceDisplayId: display.id,expires: this.now() + REMOTE_DESKTOP_LEASE_MS,sequence: -1,authenticationSession,@@ -251,7 +348,67 @@ export class RemoteDesktopController {}}const active = this.require(peer, request.lease);+ if (this.displayChanging && !['stop', 'heartbeat', 'frame', 'input'].includes(request.op))+ throw new Error('DESKTOP_DISPLAY_BUSY');switch (request.op) {+ case 'restoreViewerDisplay':+ case 'viewerDisplay': {+ if (!active.controlling) throw new Error('DESKTOP_VIEW_ONLY');+ if (this.inputStarting || this.clipboardPending || this.locking || this.starting)+ throw new Error('DESKTOP_DISPLAY_BUSY');+ if (!this.deps.createViewerDisplay) throw new Error('DESKTOP_VIEWER_DISPLAY_UNAVAILABLE');+ const generation = this.controlGeneration;+ const current = () => {+ this.tick();+ return (+ this.active === active && generation === this.controlGeneration && active.controlling+ );+ };+ this.displayChanging = true;+ try {+ // Wait for held keys/buttons to be released in the old coordinate space.+ this.deps.stopInput();+ this.deps.stopVideo();+ if (this.deps.releaseInput) await this.deps.releaseInput();+ if (!current()) throw new Error('DESKTOP_LEASE_EXPIRED');+ if (request.op === 'restoreViewerDisplay' && !this.viewerDisplay?.restore)+ throw new Error('DESKTOP_VIEWER_DISPLAY_UNAVAILABLE');+ if (!this.viewerDisplay) {+ const handle = await this.deps.createViewerDisplay(active.display.id, current, () => {+ if (this.active === active) this.stop(peer);+ });+ if (!current()) {+ handle.dispose();+ throw new Error('DESKTOP_LEASE_EXPIRED');+ }+ this.viewerDisplay = handle;+ }+ const display =+ request.op === 'restoreViewerDisplay'+ ? await this.viewerDisplay.restore!(current)+ : await this.viewerDisplay.resize(request.width, request.height, current);+ if (+ request.op === 'restoreViewerDisplay' &&+ !(await this.deps.capabilities()).displays.some(+ (item) => item.id === active.sourceDisplayId,+ )+ )+ throw new Error('DESKTOP_DISPLAY_MISSING');+ if (request.op === 'restoreViewerDisplay') this.viewerDisplay = null;+ if (!current()) throw new Error('DESKTOP_LEASE_EXPIRED');+ this.viewerGeometryManaged = true;+ active.display = display;+ active.controlling = false;+ this.controlGeneration++;+ this.deps.changed();+ return { lease: active.lease, display, controlling: false };+ } catch (error) {+ if (this.active === active) this.stop(peer);+ throw error;+ } finally {+ this.displayChanging = false;+ }+ }case 'stop': {if (request.lockScreen) {if (!this.deps.lockScreen) throw new Error('DESKTOP_LOCK_UNAVAILABLE');@@ -327,7 +484,8 @@ export class RemoteDesktopController {case 'clipboardContent':case 'clipboard': {if (!active.controlling) throw new Error('DESKTOP_VIEW_ONLY');- if (request.op === 'clipboard' ? !this.deps.clipboard : !this.deps.clipboardContent) throw new Error('DESKTOP_CLIPBOARD_UNAVAILABLE');+ if (request.op === 'clipboard' ? !this.deps.clipboard : !this.deps.clipboardContent)+ throw new Error('DESKTOP_CLIPBOARD_UNAVAILABLE');if (this.clipboardPending) throw new Error('DESKTOP_CLIPBOARD_BUSY');const generation = this.controlGeneration;const isCurrent = () => {@@ -339,8 +497,15 @@ export class RemoteDesktopController {this.clipboardPending = true;try {if (request.op === 'clipboardContent') {- const result = await this.clipboardTransfer.handle(request, isCurrent, this.deps.clipboardContent!);- if (!isCurrent()) { this.clipboardTransfer.reset(); throw new Error('DESKTOP_LEASE_EXPIRED'); }+ const result = await this.clipboardTransfer.handle(+ request,+ isCurrent,+ this.deps.clipboardContent!,+ );+ if (!isCurrent()) {+ this.clipboardTransfer.reset();+ throw new Error('DESKTOP_LEASE_EXPIRED');+ }return result;}const text = await this.deps.clipboard!(@@ -359,6 +524,8 @@ export class RemoteDesktopController {this.input(request.lease, request.sequence, request.events);return { ok: true };case 'frame': {+ if (this.displayChanging) return { jpeg: null };+ const display = active.display;// Compatibility transport: one small frame in flight, no replaying frame queue.if (this.framePending || this.now() - this.lastFrame < 250) return { jpeg: null };this.framePending = true;@@ -366,6 +533,7 @@ export class RemoteDesktopController {try {const jpeg = await this.deps.frame(active.display.id, request.cursorOverlay);this.require(peer, request.lease); // revoke during capture must not leak the result+ if (this.displayChanging || active.display !== display) return { jpeg: null };const frame = typeof jpeg === 'object' && jpeg !== null ? jpeg : { jpeg };// Cursor metadata does not turn a relay frame into a local media frame.if (frame.jpeg && frame.jpeg.length > Math.ceil(REMOTE_DESKTOP_MAX_FRAME_BYTES / 3) * 4)@@ -377,24 +545,62 @@ export class RemoteDesktopController {}case 'displayModes': {if (!this.deps.displayModes) throw new Error('DESKTOP_DISPLAY_MODES_UNAVAILABLE');- const modes = await this.deps.displayModes(active.display.id);+ // The virtual capture display has only its current mode. System mode IDs+ // belong to the monitor selected when this lease began.+ const modes = await this.deps.displayModes(active.sourceDisplayId);this.require(peer, request.lease);return modes;}case 'resolution': {if (!active.controlling) throw new Error('DESKTOP_VIEW_ONLY');if (!this.deps.resolution) throw new Error('DESKTOP_DISPLAY_MODES_UNAVAILABLE');+ if (this.inputStarting || this.clipboardPending || this.locking || this.starting)+ throw new Error('DESKTOP_DISPLAY_BUSY');+ const restoringViewer = this.viewerDisplay !== null;const generation = this.controlGeneration;- const beforeChange = () => {+ const current = () => {this.tick();- if (this.active !== active || !active.controlling || generation !== this.controlGeneration)- throw new Error('DESKTOP_LEASE_EXPIRED');+ return (+ this.active === active && active.controlling && generation === this.controlGeneration+ );+ };+ const beforeChange = () => {+ if (!current()) throw new Error('DESKTOP_LEASE_EXPIRED');// Release old geometry before the native write can emit display events.// Completion must not inspect or stop a replacement lease.this.stop(peer);};- await this.deps.resolution(active.display.id, request.modeId, beforeChange);- return { ok: true };+ try {+ if (this.viewerDisplay) {+ this.displayChanging = true;+ try {+ this.deps.stopInput();+ this.deps.stopVideo();+ if (this.deps.releaseInput) await this.deps.releaseInput();+ if (!current()) throw new Error('DESKTOP_LEASE_EXPIRED');+ if (!this.viewerDisplay.restore)+ throw new Error('DESKTOP_VIEWER_DISPLAY_UNAVAILABLE');+ // Finish the helper's original-mode write before applying a system+ // mode; otherwise delayed cleanup can overwrite the user's choice.+ const restored = await this.viewerDisplay.restore(current);+ if (!current()) throw new Error('DESKTOP_LEASE_EXPIRED');+ if (restored.width <= 0 || restored.height <= 0)+ throw new Error('DESKTOP_DISPLAY_MISSING');+ const restoredCaps = await this.deps.capabilities();+ if (!restoredCaps.displays.some((display) => display.id === active.sourceDisplayId))+ throw new Error('DESKTOP_DISPLAY_MISSING');+ this.viewerDisplay = null;+ active.display = restored;+ } finally {+ this.displayChanging = false;+ }+ }+ await this.deps.resolution(active.sourceDisplayId, request.modeId, beforeChange);+ return { ok: true };+ } catch (error) {+ if (restoringViewer && current()) this.stop(peer);+ throw error;+ }}case 'offer': {const sdp = await this.deps.offer(diff --git a/apps/desktop/src/main/remote-desktop/index.ts b/apps/desktop/src/main/remote-desktop/index.tsindex 89624a869a..52aa5fe12e 100644--- a/apps/desktop/src/main/remote-desktop/index.ts+++ b/apps/desktop/src/main/remote-desktop/index.ts@@ -35,6 +35,7 @@ import { denyAppDesktopCapture } from './capturePermissions';import { readDeviceLinkSettings, writeDeviceLinkSetting } from '../device-link/settings-store';import { throwIpcError } from '../utils/ipcValidate';import { RemoteDesktopController } from './controller';+import { createViewerDisplay, viewerDisplaySupported } from './viewerDisplay';import { desktopCaptureSource, enumerateDesktopSources } from './captureSource';import { encodeDesktopFrame, encodeNativeRelayFrame } from './frame';import { transferDesktopClipboard, transferDesktopClipboardContent } from './clipboard';@@ -67,10 +68,11 @@ const permissions = new RemoteDesktopPermissionsService({if (permission === 'accessibility') await requestDesktopInputPermission(isCurrent, signal);elseawait enumerateDesktopSources(- () => desktopCapturer.getSources({- types: ['screen'],- thumbnailSize: { width: 0, height: 0 },- }),+ () =>+ desktopCapturer.getSources({+ types: ['screen'],+ thumbnailSize: { width: 0, height: 0 },+ }),REMOTE_DESKTOP_OFFER_BUDGET.sourcesMs,);},@@ -183,7 +185,10 @@ async function offer(nativeAvailable ||=process.platform === 'win32' && (await readWindowsDesktopSupport()) === 'ready';try {- const available = await sources(false, nativeAvailable ? 2000 : REMOTE_DESKTOP_OFFER_BUDGET.sourcesMs);+ const available = await sources(+ false,+ nativeAvailable ? 2000 : REMOTE_DESKTOP_OFFER_BUDGET.sourcesMs,+ );source = desktopCaptureSource(available, lease.display.id, screen.getAllDisplays());} catch (error) {// A locked macOS session can reject Chromium's source enumeration even@@ -283,19 +288,41 @@ async function ice(request: RemoteDesktopIceRequest): Promise<RemoteDesktopIceReexport async function requestRemoteDesktop(peer: string, value: unknown): Promise<unknown> {const settings = readDeviceLinkSettings();- if (!settings.remoteControlEnabled || !settings.remoteDesktopEnabled || settings.revokedControllers.includes(peer))+ if (+ !settings.remoteControlEnabled ||+ !settings.remoteDesktopEnabled ||+ settings.revokedControllers.includes(peer)+ )throw new Error('DESKTOP_UNAVAILABLE');- if (process.platform === 'darwin' && value !== null && typeof value === 'object' && 'op' in value && value.op === 'credential' && 'version' in value && value.version === 1 && 'kind' in value) {+ if (+ process.platform === 'darwin' &&+ value !== null &&+ typeof value === 'object' &&+ 'op' in value &&+ value.op === 'credential' &&+ 'version' in value &&+ value.version === 1 &&+ 'kind' in value+ ) {if (value.kind === 'status') return { version: 1, state: await readDesktopLockState() };if (value.kind === 'prepare') {const credentials = remoteCredentialHost.currentToken?.();if (!credentials) throw new Error('CREDENTIAL_INVALID_IDENTITY');- const descriptor = await remoteCredentialHost.configure(credentials.realm, credentials.membership, credentials.authDevice, credentials.token);+ const descriptor = await remoteCredentialHost.configure(+ credentials.realm,+ credentials.membership,+ credentials.authDevice,+ credentials.token,+ );return { version: 1, ready: true, descriptor };}}- return process.platform === 'darwin' && value !== null && typeof value === 'object' && 'op' in value && value.op === 'credential'- ? remoteCredentialHost.request(peer, value, body => remoteDesktop.request(peer, body))+ return process.platform === 'darwin' &&+ value !== null &&+ typeof value === 'object' &&+ 'op' in value &&+ value.op === 'credential'+ ? remoteCredentialHost.request(peer, value, (body) => remoteDesktop.request(peer, body)): remoteDesktop.request(peer, value);}@@ -312,6 +339,7 @@ export const remoteDesktop = new RemoteDesktopController({windowsAvailable = (await readWindowsDesktopSupport()) === 'ready';const settings = readDeviceLinkSettings();const enabled = settings.remoteDesktopEnabled && settings.remoteControlEnabled;+ const viewerDisplay = enabled && (await viewerDisplaySupported());return {version: 1,cursorOverlay: process.platform === 'darwin',@@ -323,6 +351,8 @@ export const remoteDesktop = new RemoteDesktopController({backgroundViewing: true,systemAudio: supportsSystemAudio,displayModes: process.platform === 'darwin',+ viewerDisplay,+ viewerDisplayRestore: viewerDisplay,enabled,canControl: process.platform === 'darwin' || process.platform === 'win32',platform: process.platform,@@ -369,6 +399,7 @@ export const remoteDesktop = new RemoteDesktopController({transferDesktopClipboardContent(action, content, isCurrent, (events) => input.input(events)),displayModes: readDesktopDisplayModes,resolution: setDesktopDisplayMode,+ createViewerDisplay,startInput: (displayId) => input.start(displayId),input: (events) => {try {@@ -383,12 +414,15 @@ export const remoteDesktop = new RemoteDesktopController({}},stopInput: () => input.stop(),- ...(process.platform === 'darwin' ? {- lockScreen: async (isCurrent: () => boolean, signal: AbortSignal) => {- await input.release();- await lockDesktopScreen(isCurrent, signal);- },- } : {}),+ releaseInput: () => input.release(),+ ...(process.platform === 'darwin'+ ? {+ lockScreen: async (isCurrent: () => boolean, signal: AbortSignal) => {+ await input.release();+ await lockDesktopScreen(isCurrent, signal);+ },+ }+ : {}),offer,ice,stopVideo,@@ -404,7 +438,9 @@ export const remoteDesktop = new RemoteDesktopController({},});-export function registerRemoteDesktopIpc(isVoiceInputOwner?: Parameters<typeof denyAppDesktopCapture>[1]): void {+export function registerRemoteDesktopIpc(+ isVoiceInputOwner?: Parameters<typeof denyAppDesktopCapture>[1],+): void {denyAppDesktopCapture(session.defaultSession, isVoiceInputOwner);const timer = setInterval(() => remoteDesktop.tick(), 1000);timer.unref();@@ -414,13 +450,23 @@ export function registerRemoteDesktopIpc(isVoiceInputOwner?: Parameters<typeof dremoteDesktop.stop();});screen.on('display-removed', (_event, display) => {+ if (remoteDesktop.changingDisplay) return;if (String(display.id) === remoteDesktop.displayId) remoteDesktop.stop();});screen.on('display-metrics-changed', (_event, display, metrics) => {+ if (remoteDesktop.changingDisplay) return;// Work-area changes (lock screen, Dock/menu bar, display wake) do not// change whole-screen input coordinates and must not terminate the lease.if (String(display.id) === remoteDesktop.displayId &&+ !(+ metrics.every((metric) => metric === 'bounds' || metric === 'workArea') &&+ remoteDesktop.displayGeometryMatches(+ String(display.id),+ display.size.width,+ display.size.height,+ )+ ) &&metrics.some((metric) => metric === 'bounds' || metric === 'scaleFactor' || metric === 'rotation',)diff --git a/apps/desktop/src/main/remote-desktop/viewerDisplay.ts b/apps/desktop/src/main/remote-desktop/viewerDisplay.tsnew file mode 100644index 0000000000..8d2e89304c--- /dev/null+++ b/apps/desktop/src/main/remote-desktop/viewerDisplay.ts@@ -0,0 +1,199 @@+import { app, screen } from 'electron';+import { execFile, spawn, type ChildProcessWithoutNullStreams } from 'node:child_process';+import { createHash } from 'node:crypto';+import fs from 'node:fs/promises';+import path from 'node:path';+import { promisify } from 'node:util';+import type { RemoteDesktopDisplay } from '@cindy/device-link';++const exec = promisify(execFile);+let build: Promise<string> | undefined;++/** Prototype only: SPI is not shipped until supported OS/signing tests are complete. */+export async function viewerDisplaySupported(): Promise<boolean> {+ try {+ await binary();+ return true;+ } catch {+ return false;+ }+}++async function binary(): Promise<string> {+ if (process.platform !== 'darwin' || app.isPackaged)+ throw new Error('DESKTOP_VIEWER_DISPLAY_UNAVAILABLE');+ if (build) return build;+ build = (async () => {+ const source = path.join(+ app.getAppPath(),+ 'native',+ 'remote-desktop',+ 'macos-viewer-display.m',+ );+ const digest = createHash('sha256')+ .update(await fs.readFile(source))+ .update(process.arch)+ .digest('hex');+ const directory = path.join(app.getPath('userData'), 'remote-desktop', 'native', digest);+ const target = path.join(directory, 'cindy-viewer-display');+ try {+ await fs.access(target);+ return target;+ } catch {+ /* Compile this version. */+ }+ await fs.mkdir(directory, { recursive: true });+ const temporary = `${target}.${process.pid}.tmp`;+ await exec(+ 'clang',+ [+ '-fobjc-arc',+ '-framework',+ 'Foundation',+ '-framework',+ 'CoreGraphics',+ source,+ '-o',+ temporary,+ ],+ { timeout: 60_000 },+ );+ await fs.rename(temporary, target);+ return target;+ })()+ .then(async (target) => {+ await exec(target, ['--probe'], { timeout: 5000 });+ return target;+ })+ .catch(() => {+ build = undefined;+ throw new Error('DESKTOP_VIEWER_DISPLAY_UNAVAILABLE');+ });+ return build;+}++export interface ViewerDisplayHandle {+ readonly displayId?: string;+ resize(width: number, height: number, isCurrent: () => boolean): Promise<RemoteDesktopDisplay>;+ restore?(isCurrent: () => boolean): Promise<RemoteDesktopDisplay>;+ dispose(): void;+}++/** The helper owns one temporary mirror, never a global persistent display preference. */+export async function createViewerDisplay(+ sourceDisplayId: string,+ isCurrent: () => boolean,+ onFailure: () => void,+): Promise<ViewerDisplayHandle> {+ const executable = await binary();+ if (!isCurrent()) throw new Error('DESKTOP_LEASE_EXPIRED');+ const child: ChildProcessWithoutNullStreams = spawn(executable, [], { stdio: 'pipe' });+ const original = screen+ .getAllDisplays()+ .find((display) => String(display.id) === sourceDisplayId);+ let exited = false;+ child.once('exit', () => {+ exited = true;+ });+ let closed = false;+ let virtualDisplayId: number | undefined;+ child.stderr.resume();+ const failed = () => {+ if (!closed) onFailure();+ };+ child.on('error', failed);+ child.on('exit', failed);+ child.stdin.on('error', failed);+ return {+ get displayId() {+ return virtualDisplayId === undefined ? undefined : String(virtualDisplayId);+ },+ async resize(width, height, current) {+ if (closed || !current()) throw new Error('DESKTOP_LEASE_EXPIRED');+ const result = await new Promise<{ id: number }>((resolve, reject) => {+ let text = '';+ const finish = (error?: Error, value?: { id: number }) => {+ clearTimeout(timer);+ child.stdout.off('data', receive);+ child.off('exit', exited);+ child.off('error', exited);+ error ? reject(error) : resolve(value!);+ };+ const exited = () => finish(new Error('DESKTOP_VIEWER_DISPLAY_UNAVAILABLE'));+ const receive = (chunk: Buffer) => {+ text += chunk.toString();+ if (text.length > 4096) return exited();+ if (!text.includes('\n')) return;+ try {+ const value = JSON.parse(text);+ if (+ !Number.isSafeInteger(value.id) ||+ value.id <= 0 ||+ value.width !== width ||+ value.height !== height+ )+ return exited();+ finish(undefined, value);+ } catch {+ exited();+ }+ };+ const timer = setTimeout(exited, 8000);+ child.stdout.on('data', receive);+ child.once('exit', exited);+ child.once('error', exited);+ child.stdin.write(`${JSON.stringify({ sourceDisplayId, width, height })}\n`);+ });+ virtualDisplayId = result.id;+ // CoreGraphics replies before Electron necessarily observes the display change.+ for (let attempt = 0; attempt < 40; attempt++) {+ if (closed || !current()) throw new Error('DESKTOP_LEASE_EXPIRED');+ const actual = screen.getAllDisplays().find((d) => d.id === result.id);+ if (actual?.size.width === width && actual.size.height === height)+ return {+ id: String(actual.id),+ name: actual.label || 'Cindy Remote Desktop',+ width,+ height,+ };+ await new Promise((resolve) => setTimeout(resolve, 50));+ }+ throw new Error('DESKTOP_VIEWER_DISPLAY_UNAVAILABLE');+ },+ async restore(this: ViewerDisplayHandle, current) {+ this.dispose();+ for (let attempt = 0; attempt < 100; attempt++) {+ if (!current()) throw new Error('DESKTOP_LEASE_EXPIRED');+ const displays = screen.getAllDisplays();+ const display = displays.find((item) => String(item.id) === sourceDisplayId);+ if (+ exited &&+ !displays.some((item) => item.id === virtualDisplayId) &&+ // If the source was unplugged, virtual-display cleanup is the only+ // restoration that remains possible; retire the handle so another+ // monitor can be selected on the next lease.+ (!display ||+ !original ||+ (display.size.width === original.size.width &&+ display.size.height === original.size.height))+ )+ return {+ id: sourceDisplayId,+ name: display?.label || original?.label || 'Display',+ width: display?.size.width || original?.size.width || 0,+ height: display?.size.height || original?.size.height || 0,+ };+ await new Promise((resolve) => setTimeout(resolve, 50));+ }+ throw new Error('DESKTOP_VIEWER_DISPLAY_UNAVAILABLE');+ },+ dispose() {+ if (closed) return;+ closed = true;+ child.stdin.end(); // EOF restores the source mode and releases the virtual display.+ const timer = setTimeout(() => child.kill('SIGTERM'), 1500);+ timer.unref();+ child.once('exit', () => clearTimeout(timer));+ },+ };+}diff --git a/apps/desktop/src/renderer/features/remote-desktop/RemoteDesktopViewerWindow.tsx b/apps/desktop/src/renderer/features/remote-desktop/RemoteDesktopViewerWindow.tsxindex e6d318a059..46fd1af534 100644--- a/apps/desktop/src/renderer/features/remote-desktop/RemoteDesktopViewerWindow.tsx+++ b/apps/desktop/src/renderer/features/remote-desktop/RemoteDesktopViewerWindow.tsx@@ -280,6 +280,24 @@ export function RemoteDesktopViewerWindow() {</FormField></>)}+ {state?.caps?.viewerDisplay && (+ <div className="remote-viewer-field">+ <Button+ variant="secondary"+ disabled={!state.controlling || state.controlPending}+ onClick={() => {+ if (!root.current) return;+ void controller.current+ ?.fitDisplay(root.current.clientWidth, root.current.clientHeight)+ .then(() => setModes([]))+ .catch(() => setNotice(t('remoteDesktop.viewer.settingsFailed')));+ }}+ >+ {t('remoteDesktop.viewer.fitViewerDisplay')}+ </Button>+ <p>{t('remoteDesktop.viewer.fitViewerDisplayHint')}</p>+ </div>+ )}{modes.length > 0 && (<FormFieldlabel={t('remoteDesktop.viewer.resolution')}diff --git a/apps/desktop/src/renderer/features/remote-desktop/__tests__/viewerControl.test.ts b/apps/desktop/src/renderer/features/remote-desktop/__tests__/viewerControl.test.tsindex 23bad4eca5..720f7b4457 100644--- a/apps/desktop/src/renderer/features/remote-desktop/__tests__/viewerControl.test.ts+++ b/apps/desktop/src/renderer/features/remote-desktop/__tests__/viewerControl.test.ts@@ -34,6 +34,7 @@ async function fixture(firstControl?: Promise<{ controlling: boolean }>) {if (firstControl) control.mockImplementationOnce(() => firstControl);const heartbeat = vi.fn(async () => ({ controlling: true }));const clipboard = vi.fn(async () => {});+ const resolution = vi.fn(async (_modeId: string) => ({}));const api = {state: async () => ({generation: 1,@@ -57,6 +58,8 @@ async function fixture(firstControl?: Promise<{ controlling: boolean }>) {version: 1,enabled: true,canControl: true,+ viewerDisplay: true,+ viewerDisplayRestore: true,clipboardText: true,automaticReconnect: true,displays: [{ id: 'one', name: 'Display', width: 1280, height: 720 }],@@ -71,6 +74,19 @@ async function fixture(firstControl?: Promise<{ controlling: boolean }>) {return control(request.enabled);case 'heartbeat':return heartbeat();+ case 'displayModes':+ return [+ { id: '640', width: 640, height: 1242, current: false },+ { id: '4k', width: 3840, height: 2160, current: false },+ ];+ case 'resolution':+ return resolution(request.modeId);+ case 'viewerDisplay':+ return {+ lease: 'lease',+ controlling: false,+ display: { id: 'viewer', width: request.width, height: request.height },+ };case 'frame':return { jpeg: null };default:@@ -82,13 +98,53 @@ async function fixture(firstControl?: Promise<{ controlling: boolean }>) {snapshot = state;});await vi.advanceTimersByTimeAsync(0);- return { control, heartbeat, clipboard };+ return { control, heartbeat, clipboard, resolution };}const present = () => runtime.post?.({ type: 'streaming', epoch: 'lease' });const inputEnabled = () =>runtime.receive.mock.calls.filter(([message]) => message.type === 'control').at(-1)?.[0].enabled ?? false;+it('preserves high-resolution system modes through the legacy mode-ID path', async () => {+ const f = await fixture();+ present();+ await controller.resolution('4k');+ expect(f.resolution).toHaveBeenCalledWith('4k');+ expect(runtime.receive.mock.calls.some(([m]) => m.type === 'videoSettings')).toBe(false);+});++it('matches the viewer ratio without replacing the lease or resetting input sequence', async () => {+ const f = await fixture();+ present();+ await controller.fitDisplay(500, 1000);+ expect(snapshot.displayId).toBe('one');+ expect(snapshot.controlling).toBe(true);+ expect(f.control).toHaveBeenLastCalledWith(true);+ expect(runtime.receive).toHaveBeenCalledWith({+ type: 'videoSettings',+ width: 960,+ height: 1920,+ audio: false,+ });+ expect(runtime.receive.mock.calls.filter(([m]) => m.type === 'init')).toHaveLength(1);+});++it('changes portrait resolution using the same temporary screen lease', async () => {+ await fixture();+ present();+ await controller.fitDisplay(500, 1000);+ await controller.resolution('640');+ expect(snapshot.controlling).toBe(true);+ expect(snapshot.displayId).toBe('one');+ expect(runtime.receive).toHaveBeenCalledWith({+ type: 'videoSettings',+ width: 640,+ height: 1242,+ audio: false,+ });+ expect(runtime.receive.mock.calls.filter(([m]) => m.type === 'init')).toHaveLength(1);+});+it('orders quick copy/paste shortcuts and reports transfer failure without reconnecting', async () => {const current = await fixture();present();diff --git a/apps/desktop/src/renderer/features/remote-desktop/viewerController.ts b/apps/desktop/src/renderer/features/remote-desktop/viewerController.tsindex 38384e6f36..20a49f261d 100644--- a/apps/desktop/src/renderer/features/remote-desktop/viewerController.ts+++ b/apps/desktop/src/renderer/features/remote-desktop/viewerController.ts@@ -4,6 +4,7 @@ import {REMOTE_DESKTOP_ICE_SERVERS,REMOTE_DESKTOP_MAX_FRAME_BYTES,RemoteDesktopViewerSession,+ viewerDisplaySize,RemoteDesktopViewerMedia,remoteDesktopFailureKey,isDesktopInput,@@ -181,7 +182,9 @@ export class DesktopViewerController {takeover,isCurrent: () => this.epoch === epoch && !this.disposed && this.scope.active,onCapabilities: (caps) => this.publish({ caps }),- onStart: () => { this.resuming = true; },+ onStart: () => {+ this.resuming = true;+ },});if (epoch !== this.epoch) return;this.publish({ caps, displayId: lease.display.id, status: 'connecting' });@@ -337,11 +340,71 @@ export class DesktopViewerController {async resolution(modeId: string): Promise<void> {const lease = this.session.lease;if (!lease) return;+ if (this.state.caps?.viewerDisplay && this.state.caps.viewerDisplayRestore) {+ const mode = (await this.displayModes()).find((item) => item.id === modeId);+ if (this.session.lease !== lease) return;+ if (!mode) throw new Error('DESKTOP_DISPLAY_MODE_MISSING');+ if ([mode.width, mode.height].every((size) => size >= 320 && size <= 2560)) {+ await this.fitDisplay(mode.width, mode.height, true);+ return;+ }+ }await this.request({ op: 'resolution', lease: lease.lease, modeId });this.cancel(true);this.resuming = false;void this.connect();}+ async fitDisplay(width: number, height: number, exactResolution = false): Promise<void> {+ if (+ exactResolution &&+ ![width, height].every((value) => Number.isInteger(value) && value >= 320 && value <= 2560)+ )+ throw new Error('DESKTOP_DISPLAY_MODE_MISSING');+ const size = exactResolution ? { width, height } : viewerDisplaySize(width, height);+ const lease = this.session.lease;+ if (+ !size ||+ !lease?.controlling ||+ !this.state.caps?.viewerDisplay ||+ this.state.controlPending+ )+ return;+ this.publish({ controlPending: true });+ this.syncControl();+ this.media.reset();+ const sourceDisplayId = this.state.displayId;+ try {+ const next = await this.session.fitDisplay(size.width, size.height);+ if (this.session.lease !== lease) return;+ const caps = this.state.caps;+ this.publish({+ // Keep the physical source as the reconnect target; the temporary+ // display is only the current capture/input surface.+ displayId: sourceDisplayId,+ // The temporary capture surface is lease state, never a reconnectable+ // display choice in the selector.+ caps,+ });+ this.streaming = false;+ this.runtime.receive({+ type: 'videoSettings',+ ...size,+ audio: this.state.settings.audio && this.state.caps?.systemAudio === true,+ });+ await this.session.control(true);+ } catch (error) {+ if (error instanceof Error && error.message === 'INVOKE_TIMEOUT') {+ this.cancel(true);+ void this.connect();+ }+ throw error;+ } finally {+ if (this.session.lease === lease) {+ this.publish({ controlPending: false });+ this.syncControl();+ }+ }+ }private async heartbeat(): Promise<void> {if (!this.scope.active || this.disposed) return;if (!this.session.lease) {diff --git a/apps/desktop/src/renderer/i18n/locales/en/common.json b/apps/desktop/src/renderer/i18n/locales/en/common.jsonindex 5fc4f7a3da..85020a32e8 100644--- a/apps/desktop/src/renderer/i18n/locales/en/common.json+++ b/apps/desktop/src/renderer/i18n/locales/en/common.json@@ -105,6 +105,8 @@"confirmDisconnectHint": "The remote desktop connection will end. Apps on the computer will keep running.","disconnectFailed": "Could not disconnect. Try again.","clipboardShortcutHint": "Use {{modifier}}+C / {{modifier}}+V in the picture to copy and paste text between computers. Images and files are not supported yet.",+ "fitViewerDisplay": "Match Current View",+ "fitViewerDisplayHint": "Temporarily adjusts the computer desktop to this view’s aspect ratio, keeping its contents. Disconnect to restore the original resolution.","resolutionHint": "Resolution changes affect the actual remote monitor."}},diff --git a/apps/desktop/src/renderer/i18n/locales/ja/common.json b/apps/desktop/src/renderer/i18n/locales/ja/common.jsonindex 9e98e4b612..e631c10ea7 100644--- a/apps/desktop/src/renderer/i18n/locales/ja/common.json+++ b/apps/desktop/src/renderer/i18n/locales/ja/common.json@@ -105,6 +105,8 @@"confirmDisconnectHint": "リモートデスクトップ接続を終了します。コンピュータ上のアプリは動作を続けます。","disconnectFailed": "切断できませんでした。もう一度お試しください。","clipboardShortcutHint": "画面内で {{modifier}}+C / {{modifier}}+V を使うと、コンピュータ間でテキストをコピーして貼り付けられます。画像とファイルにはまだ対応していません。",+ "fitViewerDisplay": "現在の画面比率に合わせる",+ "fitViewerDisplayHint": "デスクトップの内容を保ったまま、パソコンの画面比率を一時的に変更します。切断すると元の解像度に戻ります。","resolutionHint": "解像度の変更はリモートの実際のモニターに反映されます。"}},diff --git a/apps/desktop/src/renderer/i18n/locales/ko/common.json b/apps/desktop/src/renderer/i18n/locales/ko/common.jsonindex 87584a1e96..18f9091cbc 100644--- a/apps/desktop/src/renderer/i18n/locales/ko/common.json+++ b/apps/desktop/src/renderer/i18n/locales/ko/common.json@@ -105,6 +105,8 @@"confirmDisconnectHint": "원격 데스크톱 연결이 종료됩니다. 컴퓨터의 앱은 계속 실행됩니다.","disconnectFailed": "연결을 끊지 못했습니다. 다시 시도하세요.","clipboardShortcutHint": "화면 안에서 {{modifier}}+C / {{modifier}}+V로 컴퓨터 간에 텍스트를 복사하고 붙여넣으세요. 이미지와 파일은 아직 지원하지 않습니다.",+ "fitViewerDisplay": "현재 화면 비율에 맞추기",+ "fitViewerDisplayHint": "데스크톱 내용을 유지하면서 컴퓨터 화면 비율을 일시적으로 변경합니다. 연결을 종료하면 원래 해상도로 돌아갑니다.","resolutionHint": "해상도를 변경하면 원격 컴퓨터의 실제 모니터에 적용됩니다."}},diff --git a/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json b/apps/desktop/src/renderer/i18n/locales/zh-CN/common.jsonindex 1f2755e569..51e1652ce6 100644--- a/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json+++ b/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json@@ -105,6 +105,8 @@"confirmDisconnectHint": "将结束远程桌面连接,电脑上的应用仍会继续运行。","disconnectFailed": "未能断开连接,请重试。","clipboardShortcutHint": "在画面内使用 {{modifier}}+C / {{modifier}}+V,即可在两台电脑之间复制粘贴文字。暂不支持图片和文件。",+ "fitViewerDisplay": "按当前画面比例调整",+ "fitViewerDisplayHint": "临时调整电脑桌面比例,保留当前桌面内容。退出连接后恢复原分辨率。","resolutionHint": "分辨率修改会影响远端的实际显示器。"}},diff --git a/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json b/apps/desktop/src/renderer/i18n/locales/zh-TW/common.jsonindex 8728e662f3..3fe57b079a 100644--- a/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json+++ b/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json@@ -105,6 +105,8 @@"confirmDisconnectHint": "將結束遠端桌面連線,電腦上的應用程式仍會繼續執行。","disconnectFailed": "無法中斷連線,請重試。","clipboardShortcutHint": "在畫面內使用 {{modifier}}+C / {{modifier}}+V,即可在兩台電腦之間複製貼上文字。暫不支援圖片和檔案。",+ "fitViewerDisplay": "依目前畫面比例調整",+ "fitViewerDisplayHint": "暫時調整電腦桌面比例,保留目前桌面內容。離開連線後恢復原解析度。","resolutionHint": "解析度修改會影響遠端的實際顯示器。"}},diff --git a/apps/mobile/src/i18n/locales/en/remoteDesktop.json b/apps/mobile/src/i18n/locales/en/remoteDesktop.jsonindex 67ed78f54d..1730e08ef1 100644--- a/apps/mobile/src/i18n/locales/en/remoteDesktop.json+++ b/apps/mobile/src/i18n/locales/en/remoteDesktop.json@@ -112,6 +112,9 @@"loadingSettings": "Loading…","retrySettings": "Could not load. Tap to retry.","followComputer": "Follow computer",+ "restoreViewerDisplay": "Restore Computer Screen Ratio",+ "fitViewerDisplay": "Match Current View",+ "fitViewerDisplayHint": "Temporarily adjusts the computer desktop to this view’s aspect ratio, keeping its contents. Disconnect to restore the original resolution.","resolutionHint": "Changes the computer’s display resolution and window layout, then briefly reconnects.","resolutionControlHint": "Take control to change the resolution.","videoSettingsFailed": "Live video is unavailable. Compatibility viewing does not apply frame rate, quality or sound settings. Select a setting again to retry.",diff --git a/apps/mobile/src/i18n/locales/ja/remoteDesktop.json b/apps/mobile/src/i18n/locales/ja/remoteDesktop.jsonindex 47bf15bea0..6d09e9e66e 100644--- a/apps/mobile/src/i18n/locales/ja/remoteDesktop.json+++ b/apps/mobile/src/i18n/locales/ja/remoteDesktop.json@@ -112,6 +112,9 @@"loadingSettings": "読み込み中…","retrySettings": "読み込み失敗。タップして再試行。","followComputer": "パソコンに合わせる",+ "restoreViewerDisplay": "パソコン本来の画面比率に戻す",+ "fitViewerDisplay": "現在の画面比率に合わせる",+ "fitViewerDisplayHint": "デスクトップの内容を保ったまま、パソコンの画面比率を一時的に変更します。切断すると元の解像度に戻ります。","resolutionHint": "パソコンの実際の解像度とウィンドウ配置を変更し、一時的に再接続します。","resolutionControlHint": "解像度を変更するには操作権を取得してください。","videoSettingsFailed": "リアルタイム映像に接続できません。互換表示ではフレームレート・画質・サウンド設定は適用されません。設定を選び直して再試行できます。",diff --git a/apps/mobile/src/i18n/locales/ko/remoteDesktop.json b/apps/mobile/src/i18n/locales/ko/remoteDesktop.jsonindex 0d0a482601..5dd3faedba 100644--- a/apps/mobile/src/i18n/locales/ko/remoteDesktop.json+++ b/apps/mobile/src/i18n/locales/ko/remoteDesktop.json@@ -112,6 +112,9 @@"loadingSettings": "불러오는 중…","retrySettings": "불러오지 못했습니다. 눌러서 다시 시도하세요.","followComputer": "컴퓨터에 맞춤",+ "restoreViewerDisplay": "컴퓨터의 원래 화면 비율로 복원",+ "fitViewerDisplay": "현재 화면 비율에 맞추기",+ "fitViewerDisplayHint": "데스크톱 내용을 유지하면서 컴퓨터 화면 비율을 일시적으로 변경합니다. 연결을 종료하면 원래 해상도로 돌아갑니다.","resolutionHint": "컴퓨터의 실제 해상도와 창 배치를 변경하고 잠시 다시 연결합니다.","resolutionControlHint": "해상도를 변경하려면 제어권을 가져오세요.","videoSettingsFailed": "실시간 영상에 연결하지 못했습니다. 호환 모드에서는 프레임 속도, 화질, 소리가 적용되지 않습니다. 설정을 다시 선택해 재시도하세요.",diff --git a/apps/mobile/src/i18n/locales/zh-CN/remoteDesktop.json b/apps/mobile/src/i18n/locales/zh-CN/remoteDesktop.jsonindex 1a6e1a7a54..7632a48280 100644--- a/apps/mobile/src/i18n/locales/zh-CN/remoteDesktop.json+++ b/apps/mobile/src/i18n/locales/zh-CN/remoteDesktop.json@@ -113,6 +113,9 @@"retrySettings": "读取失败,轻点重试","followComputer": "跟随电脑","resolutionHint": "更改电脑实际分辨率,会调整电脑上的窗口布局,并短暂重新连接。",+ "restoreViewerDisplay": "恢复电脑原生屏幕比例",+ "fitViewerDisplay": "按当前画面比例调整",+ "fitViewerDisplayHint": "临时调整电脑桌面比例,保留当前桌面内容。退出连接后恢复原分辨率。","resolutionControlHint": "接管操作后可以更改分辨率。","videoSettingsFailed": "实时传输未建立,当前使用兼容画面。帧率、画质和声音暂未生效,可重新选择设置重试。","settingFailed": "设置未能应用,请重试。",diff --git a/apps/mobile/src/i18n/locales/zh-TW/remoteDesktop.json b/apps/mobile/src/i18n/locales/zh-TW/remoteDesktop.jsonindex f74184a24c..73e2c51bd0 100644--- a/apps/mobile/src/i18n/locales/zh-TW/remoteDesktop.json+++ b/apps/mobile/src/i18n/locales/zh-TW/remoteDesktop.json@@ -112,6 +112,9 @@"loadingSettings": "讀取中…","retrySettings": "讀取失敗,輕點重試","followComputer": "跟隨電腦",+ "restoreViewerDisplay": "恢復電腦原生螢幕比例",+ "fitViewerDisplay": "依目前畫面比例調整",+ "fitViewerDisplayHint": "暫時調整電腦桌面比例,保留目前桌面內容。離開連線後恢復原解析度。","resolutionHint": "變更電腦實際解析度,會調整電腦上的視窗配置,並短暫重新連線。","resolutionControlHint": "接管操作後可以變更解析度。","videoSettingsFailed": "即時傳輸未建立,目前使用相容畫面。幀率、畫質與聲音尚未生效,可重新選擇設定重試。",diff --git a/apps/mobile/src/remote-desktop/RemoteDesktopControls.tsx b/apps/mobile/src/remote-desktop/RemoteDesktopControls.tsxindex b124b446f4..87880a1d83 100644--- a/apps/mobile/src/remote-desktop/RemoteDesktopControls.tsx+++ b/apps/mobile/src/remote-desktop/RemoteDesktopControls.tsx@@ -89,6 +89,10 @@ export function RemoteDesktopControls({settings: RemoteDesktopVideoSettings;busy: boolean;modesSupported: boolean;+ displayGeometry?: string;+ viewerDisplaySupported?: boolean;+ viewerDisplayMatched?: boolean;+ onFitDisplay?(): void;notice: string | null;onChange(settings: Partial<RemoteDesktopVideoSettings>): void;readModes(): Promise<RemoteDesktopDisplayMode[]>;diff --git a/apps/mobile/src/remote-desktop/RemoteDesktopDisplaySettings.tsx b/apps/mobile/src/remote-desktop/RemoteDesktopDisplaySettings.tsxindex 4075bc3efa..073ca410ae 100644--- a/apps/mobile/src/remote-desktop/RemoteDesktopDisplaySettings.tsx+++ b/apps/mobile/src/remote-desktop/RemoteDesktopDisplaySettings.tsx@@ -14,6 +14,7 @@ import type {RemoteDesktopVideoSettings,} from "@cindy/device-link";import { Text } from "@/components/AppText";+import { RemoteDesktopActionButton } from "./RemoteDesktopActionButton";import {NativePullDownMenu,usesNativePullDownMenu,@@ -33,6 +34,10 @@ type Props = {settings: RemoteDesktopVideoSettings;busy: boolean;modesSupported: boolean;+ displayGeometry?: string;+ viewerDisplaySupported?: boolean;+ viewerDisplayMatched?: boolean;+ onFitDisplay?(): void;onChange(settings: Partial<RemoteDesktopVideoSettings>): void;readModes(): Promise<RemoteDesktopDisplayMode[]>;onResolution(id: string): Promise<void>;@@ -73,7 +78,7 @@ export function RemoteDesktopDisplaySettings({return () => {active = false;};- }, [connected, video.modesSupported, reload]);+ }, [connected, video.modesSupported, video.displayGeometry, reload]);const disabled = !connected || !video.supported;const title = { color: colors.textPrimary, fontSize: typeScale.body };const hint = { color: colors.textTertiary, fontSize: typeScale.caption };@@ -242,6 +247,33 @@ export function RemoteDesktopDisplaySettings({}}>{displayControl}+ {video.viewerDisplaySupported && (+ <View style={{ gap: spacing.xs }}>+ <RemoteDesktopActionButton+ accessibilityRole="button"+ accessibilityLabel={t(+ video.viewerDisplayMatched+ ? "remoteDesktop.restoreViewerDisplay"+ : "remoteDesktop.fitViewerDisplay",+ )}+ disabled={!connected || !controlling || video.busy}+ onPress={() => video.onFitDisplay?.()}+ style={[+ styles.row,+ { opacity: !connected || !controlling || video.busy ? 0.6 : 1 },+ ]}+ >+ <Text style={title}>+ {t(+ video.viewerDisplayMatched+ ? "remoteDesktop.restoreViewerDisplay"+ : "remoteDesktop.fitViewerDisplay",+ )}+ </Text>+ </RemoteDesktopActionButton>+ <Text style={hint}>{t("remoteDesktop.fitViewerDisplayHint")}</Text>+ </View>+ )}<Viewstyle={{height: StyleSheet.hairlineWidth,diff --git a/apps/mobile/src/remote-desktop/RemoteDesktopScreen.tsx b/apps/mobile/src/remote-desktop/RemoteDesktopScreen.tsxindex 0091daf850..56d28c45bb 100644--- a/apps/mobile/src/remote-desktop/RemoteDesktopScreen.tsx+++ b/apps/mobile/src/remote-desktop/RemoteDesktopScreen.tsx@@ -40,6 +40,7 @@ import { useTranslation } from "react-i18next";import SegmentedControl from "@expo/ui/community/segmented-control";import {RemoteDesktopViewerSession,+ viewerDisplaySize,RemoteDesktopViewerMedia,remoteDesktopFailureKey,REMOTE_DESKTOP_CHANNEL,@@ -235,6 +236,23 @@ export default function RemoteDesktopScreen() {const unlockFrame = useRef<((presented: boolean) => void) | null>(null);const inputBusy = useRef<string | null>(null);const [lease, setLease] = useState<RemoteDesktopLease | null>(null);+ const [viewerDisplayApplied, setViewerDisplayApplied] = useState(false);+ const [viewerViewport, setViewerViewport] = useState({ width: 0, height: 0 });+ const viewportGeneration = useRef(0);+ const matchesViewer = (+ display: { width: number; height: number },+ width: number,+ height: number,+ ) =>+ width > 0 &&+ height > 0 &&+ Math.abs(display.width / display.height - width / height) < 0.003;+ const viewerDisplayMatched = Boolean(+ viewerDisplayApplied &&+ caps?.viewerDisplayRestore &&+ lease &&+ matchesViewer(lease.display, viewerViewport.width, viewerViewport.height),+ );exitLock.current =lockOnExitLoaded && lockOnExit && caps?.lockOnExit === true;const [status, setStatus] = useState("connecting");@@ -491,6 +509,7 @@ export default function RemoteDesktopScreen() {send({ type: "stop", preserveFrame });if (alive.current) {setLease(null);+ setViewerDisplayApplied(false);setFrameReady(false);setControlReady(false);setCanPip(false);@@ -625,8 +644,10 @@ export default function RemoteDesktopScreen() {setKeyboard(false);}}- if (alive.current) setLease({ ...current });}+ // The shared session may already have mutated this lease before returning.+ // Always publish the confirmation to React's separate snapshot as well.+ if (alive.current) setLease({ ...current });send({ type: "control", enabled: controlling });};const requestHostControl = (@@ -688,7 +709,9 @@ export default function RemoteDesktopScreen() {displayId: displayId ?? recovery.current.displayId,resume: resuming,takeover,- onStart: () => { recovery.current.resuming = true; },+ onStart: () => {+ recovery.current.resuming = true;+ },isCurrent: () => current === generation.current && alive.current,onCapabilities: (result) => {mark("capabilities");@@ -769,20 +792,20 @@ export default function RemoteDesktopScreen() {} finally {if (current === generation.current) connecting.current = false;}- },- [- deviceId,- fail,- landscape,- mode,- request,- resolveControlFailure,- send,- stop,- t,- videoPreferencesLoaded,- viewerSession,- ],+ },+ [+ deviceId,+ fail,+ landscape,+ mode,+ request,+ resolveControlFailure,+ send,+ stop,+ t,+ videoPreferencesLoaded,+ viewerSession,+ ],);const connectRef = useRef(connect);connectRef.current = connect;@@ -1121,6 +1144,32 @@ export default function RemoteDesktopScreen() {return;}switch (message.type) {+ case "viewportChanged":+ if (+ typeof message.width === "number" &&+ typeof message.height === "number" &&+ Number.isFinite(message.width) &&+ Number.isFinite(message.height)+ ) {+ viewportGeneration.current += 1;+ setViewerViewport((previous) =>+ previous.width === message.width &&+ previous.height === message.height+ ? previous+ : {+ width: message.width as number,+ height: message.height as number,+ },+ );+ }+ break;+ case "viewportSize":+ if (+ typeof message.width === "number" &&+ typeof message.height === "number"+ )+ void fitViewerDisplay(message.width, message.height);+ break;case "reconnecting":if (message.attemptId === mediaAttempt.current)setStatus("reconnecting");@@ -1436,6 +1485,26 @@ export default function RemoteDesktopScreen() {const changeResolution = async (modeId: string) => {const current = active.current;if (!current?.controlling || settingInFlight.current) return;+ if (caps?.viewerDisplay && caps.viewerDisplayRestore) {+ try {+ const modes = await request<+ import("@cindy/device-link").RemoteDesktopDisplayMode[]+ >({ op: "displayModes", lease: current.lease });+ if (active.current !== current) return;+ const mode = modes.find((item) => item.id === modeId);+ if (!mode) throw new Error("DESKTOP_DISPLAY_MODE_MISSING");+ if (+ [mode.width, mode.height].every((size) => size >= 320 && size <= 2560)+ ) {+ await fitViewerDisplay(mode.width, mode.height, true);+ return;+ }+ } catch {+ if (active.current === current)+ setSettingNotice(t("remoteDesktop.settingFailed"));+ return;+ }+ }settingInFlight.current = true;setSettingBusy(true);setSettingNotice(null);@@ -1455,6 +1524,83 @@ export default function RemoteDesktopScreen() {setSettingBusy(false);}};+ const fitViewerDisplay = async (+ width: number,+ height: number,+ exactResolution = false,+ ) => {+ const current = active.current;+ if (+ exactResolution &&+ ![width, height].every(+ (value) => Number.isInteger(value) && value >= 320 && value <= 2560,+ )+ ) {+ setSettingNotice(t("remoteDesktop.settingUnsupported"));+ return;+ }+ const size = exactResolution+ ? { width, height }+ : viewerDisplaySize(width, height);+ if (+ !current?.controlling ||+ !caps?.viewerDisplay ||+ !size ||+ settingInFlight.current ||+ settingBusy ||+ controlInFlight.current+ )+ return;+ settingInFlight.current = true;+ setSettingBusy(true);+ setSettingNotice(null);+ const sourceDisplayId = recovery.current.displayId || current.display.id;+ send({ type: "control", enabled: false });+ try {+ viewerMedia.reset();+ const restore = Boolean(+ !exactResolution &&+ viewerDisplayApplied &&+ caps.viewerDisplayRestore &&+ matchesViewer(current.display, width, height),+ );+ const requestGeneration = viewportGeneration.current;+ const next = await viewerSession.current.fitDisplay(+ size.width,+ size.height,+ restore,+ );+ if (active.current !== current) return;+ // Reconnect the physical source display after the temporary mirror ends.+ recovery.current.displayId = sourceDisplayId;+ if (!exactResolution && viewportGeneration.current === requestGeneration)+ setViewerViewport({ width, height });+ setViewerDisplayApplied(!restore);+ setLease({ ...next });+ streaming.current = false;+ setCanPip(false);+ send({+ type: "videoSettings",+ width: next.display.width,+ height: next.display.height,+ restore,+ audio: Boolean(caps.systemAudio && videoSettingsRef.current.audio),+ });+ const control = await viewerSession.current.control(true);+ if (active.current === current)+ applyConfirmedControl(current, control.controlling);+ } catch (cause) {+ if (active.current === current) {+ setSettingNotice(t("remoteDesktop.settingFailed"));+ if ((cause as { code?: string })?.code === "INVOKE_TIMEOUT")+ fail(cause);+ else resolveControlFailure(cause);+ }+ } finally {+ settingInFlight.current = false;+ setSettingBusy(false);+ }+ };const shortcut = (keys: string[]) =>send({type: "events",@@ -1784,6 +1930,12 @@ export default function RemoteDesktopScreen() {settings: videoSettings,busy: settingBusy,modesSupported: Boolean(caps?.displayModes),+ displayGeometry: lease+ ? `${lease.display.id}:${lease.display.width}:${lease.display.height}`+ : undefined,+ viewerDisplaySupported: caps?.viewerDisplay === true,+ viewerDisplayMatched,+ onFitDisplay: () => send({ type: "measureViewport" }),notice: audioUnavailable.current? t("remoteDesktop.audioUnavailable"): settingNotice,@@ -1801,7 +1953,7 @@ export default function RemoteDesktopScreen() {}}inputMode={inputMode}displays={caps?.displays ?? []}- displayId={lease?.display.id}+ displayId={recovery.current.displayId ?? lease?.display.id}onViewOnly={() => void toggleControl()}onInputMode={(value) => {setInputMode(value);diff --git a/apps/mobile/src/remote-desktop/__tests__/screen.test.tsx b/apps/mobile/src/remote-desktop/__tests__/screen.test.tsxindex 49dc8050d8..5b9d9b0fc8 100644--- a/apps/mobile/src/remote-desktop/__tests__/screen.test.tsx+++ b/apps/mobile/src/remote-desktop/__tests__/screen.test.tsx@@ -561,6 +561,268 @@ describe("remote desktop controls", () => {act(() => host.querySelector("button")!.click());expect(onChange).toHaveBeenCalledTimes(2);});++ it("offers viewer sizing only with host support and active control", () => {+ const onFitDisplay = vi.fn();+ const renderSettings = (+ supported: boolean,+ controlling: boolean,+ busy = false,+ ) => {+ act(() =>+ root.render(+ <RemoteDesktopDisplaySettings+ connected+ controlling={controlling}+ displayControl={null}+ video={{+ supported: true,+ busy,+ modesSupported: false,+ viewerDisplaySupported: supported,+ onFitDisplay,+ settings: { fps: 30, bitrate: 0, audio: false },+ onChange: vi.fn(),+ readModes: async () => [],+ onResolution: async () => {},+ }}+ />,+ ),+ );+ };+ const button = () =>+ host.querySelector<HTMLButtonElement>(+ '[aria-label="remoteDesktop.fitViewerDisplay"]',+ );+ renderSettings(false, true);+ expect(button()).toBeNull();+ renderSettings(true, false);+ expect(button()?.disabled).toBe(true);+ renderSettings(true, true, true);+ expect(button()?.disabled).toBe(true);+ renderSettings(true, true);+ act(() => button()?.click());+ expect(onFitDisplay).toHaveBeenCalledOnce();+ });+ it("keeps 4K system resolution selectable on viewer-display hosts", async () => {+ const original = fixture.invoke.getMockImplementation()!;+ fixture.invoke.mockImplementation(async (...args) => {+ const request = args[2][0];+ if (request.op === "capabilities")+ return {+ ...(await original(...args)),+ viewerDisplay: true,+ viewerDisplayRestore: true,+ videoSettings: true,+ displayModes: true,+ };+ if (request.op === "displayModes")+ return [{ id: "4k", width: 3840, height: 2160, current: false }];+ return original(...args);+ });+ await connect();+ act(() => button("operations").click());+ await act(async () => button("displaySettings").click());+ act(() => button("resolution").click());+ const mode = Array.from(+ host.querySelectorAll<HTMLButtonElement>("button"),+ ).find((item) => item.textContent === "3840 × 2160")!;+ expect(mode).toBeDefined();+ await act(async () => mode.click());+ expect(requests()).toContainEqual({+ op: "resolution",+ lease: "lease",+ modeId: "4k",+ });+ expect(requests().some((request) => request.op === "viewerDisplay")).toBe(+ false,+ );+ });++ it("changes portrait resolution without stopping the temporary display and stops it on exit", async () => {+ const original = fixture.invoke.getMockImplementation()!;+ fixture.invoke.mockImplementation(async (...args) => {+ const request = args[2][0];+ if (request.op === "capabilities")+ return {+ ...(await original(...args)),+ viewerDisplay: true,+ viewerDisplayRestore: true,+ videoSettings: true,+ displayModes: true,+ };+ if (request.op === "displayModes")+ return [{ id: "640", width: 640, height: 1242, current: false }];+ if (request.op === "viewerDisplay")+ return {+ lease: "lease",+ controlling: false,+ display: {+ ...display,+ id: "virtual",+ width: request.width,+ height: request.height,+ },+ };+ return original(...args);+ });+ await connect();+ act(() => button("operations").click());+ act(() => button("displaySettings").click());+ await act(async () =>+ fixture.message!({+ nativeEvent: {+ data: JSON.stringify({+ type: "viewportSize",+ epoch: "lease",+ width: 390,+ height: 760,+ }),+ },+ }),+ );+ act(() => button("resolution").click());+ const mode = Array.from(+ host.querySelectorAll<HTMLButtonElement>("button"),+ ).find((item) => item.textContent === "640 × 1242")!;+ expect(mode).toBeDefined();+ await act(async () => mode.click());+ expect(+ requests()+ .filter((request) => request.op === "viewerDisplay")+ .at(-1),+ ).toEqual({+ op: "viewerDisplay",+ lease: "lease",+ width: 640,+ height: 1242,+ });+ expect(+ requests().filter(+ (request) =>+ request.op === "resolution" ||+ request.op === "restoreViewerDisplay" ||+ request.op === "stop",+ ),+ ).toHaveLength(0);+ expect(requests().filter((request) => request.op === "start")).toHaveLength(+ 1,+ );+ expect(host.textContent).not.toContain(+ "remoteDesktop.resolutionControlHint",+ );+ await act(async () => root.unmount());+ mounted = false;+ expect(requests().filter((request) => request.op === "stop")).toHaveLength(+ 1,+ );+ });+ it.each([true, false])(+ "keeps matching after rotation and supports restore when advertised: %s",+ async (canRestore) => {+ const original = fixture.invoke.getMockImplementation()!;+ fixture.invoke.mockImplementation(async (...args) => {+ const request = args[2][0];+ if (request.op === "capabilities")+ return {+ ...(await original(...args)),+ viewerDisplay: true,+ viewerDisplayRestore: canRestore,+ videoSettings: true,+ displayModes: true,+ };+ if (request.op === "viewerDisplay")+ return {+ lease: "lease",+ controlling: false,+ display: {+ ...display,+ id: "virtual",+ width: request.width,+ height: request.height,+ },+ };+ if (request.op === "restoreViewerDisplay")+ return { lease: "lease", controlling: false, display };+ if (request.op === "displayModes") return [];+ return original(...args);+ });+ await connect();+ act(() => button("operations").click());+ act(() => button("displaySettings").click());+ for (const [width, height] of [+ [390, 760],+ [760, 390],+ ]) {+ await act(async () =>+ fixture.message!({+ nativeEvent: {+ data: JSON.stringify({+ type: "viewportChanged",+ epoch: "lease",+ width,+ height,+ }),+ },+ }),+ );+ expect(button("fitViewerDisplay").disabled).toBe(false);+ act(() => button("fitViewerDisplay").click());+ expect(sent()).toContainEqual({ type: "measureViewport" });+ await act(async () => {+ fixture.message!({+ nativeEvent: {+ data: JSON.stringify({+ type: "viewportSize",+ epoch: "lease",+ width,+ height,+ }),+ },+ });+ });+ expect(+ button(canRestore ? "restoreViewerDisplay" : "fitViewerDisplay")+ .disabled,+ ).toBe(false);+ expect(host.textContent).not.toContain(+ "remoteDesktop.resolutionControlHint",+ );+ }+ const matches = requests().filter(+ (request) => request.op === "viewerDisplay",+ );+ expect(matches).toHaveLength(2);+ expect(matches[0].height).toBeGreaterThan(matches[0].width);+ expect(matches[1].width).toBeGreaterThan(matches[1].height);+ if (canRestore) {+ act(() => button("restoreViewerDisplay").click());+ await act(async () =>+ fixture.message!({+ nativeEvent: {+ data: JSON.stringify({+ type: "viewportSize",+ epoch: "lease",+ width: 760,+ height: 390,+ }),+ },+ }),+ );+ expect(+ requests().filter((request) => request.op === "restoreViewerDisplay"),+ ).toHaveLength(1);+ expect(button("fitViewerDisplay").disabled).toBe(false);+ expect(sent()).toContainEqual(+ expect.objectContaining({+ type: "videoSettings",+ width: display.width,+ height: display.height,+ }),+ );+ }+ },+ );it.each(["streaming", "fallback"])("coalesces continuous quality choices until %s",async (terminal) => {@@ -1727,7 +1989,9 @@ describe("remote desktop controls", () => {const original = fixture.invoke.getMockImplementation()!;fixture.invoke.mockImplementation((...args) =>args[2][0].op === "input"- ? Promise.reject(Object.assign(new Error("timeout"), { code: "INVOKE_TIMEOUT" }))+ ? Promise.reject(+ Object.assign(new Error("timeout"), { code: "INVOKE_TIMEOUT" }),+ ): original(...args),);await act(async () => {@@ -1792,8 +2056,7 @@ describe("remote desktop controls", () => {return Promise.reject(Object.assign(new Error("timeout"), { code: "INVOKE_TIMEOUT" }),);- if (req.op === "heartbeat")- return Promise.resolve({ controlling: true });+ if (req.op === "heartbeat") return Promise.resolve({ controlling: true });return original(...args);});await act(async () => {@@ -1831,8 +2094,7 @@ describe("remote desktop controls", () => {);return Promise.resolve({ controlling: false });}- if (req.op === "heartbeat")- return Promise.resolve({ controlling: true });+ if (req.op === "heartbeat") return Promise.resolve({ controlling: true });return original(...args);});await act(async () => {@@ -1853,7 +2115,11 @@ describe("remote desktop controls", () => {// Overflow timed out with pending release. Take control must finish that// release (host stopInput) before asking to enable, so the helper restarts.expect(controlOps).toEqual([true, false, false, true]);- expect(sent().filter((m) => m.type === "control").at(-1)).toEqual({+ expect(+ sent()+ .filter((m) => m.type === "control")+ .at(-1),+ ).toEqual({type: "control",enabled: true,});@@ -1871,17 +2137,24 @@ describe("remote desktop controls", () => {return Promise.reject(Object.assign(new Error("timeout"), { code: "INVOKE_TIMEOUT" }),);- if (req.op === "heartbeat")- return Promise.resolve({ controlling: true });+ if (req.op === "heartbeat") return Promise.resolve({ controlling: true });return original(...args);});await act(async () => button("viewOnly").click());- expect(sent().filter((m) => m.type === "control").at(-1)).toEqual({+ expect(+ sent()+ .filter((m) => m.type === "control")+ .at(-1),+ ).toEqual({type: "control",enabled: false,});await act(async () => vi.advanceTimersByTimeAsync(3000));- expect(sent().filter((m) => m.type === "control").at(-1)).toEqual({+ expect(+ sent()+ .filter((m) => m.type === "control")+ .at(-1),+ ).toEqual({type: "control",enabled: true,});@@ -1920,7 +2193,11 @@ describe("remote desktop controls", () => {await act(async () => vi.advanceTimersByTimeAsync(3000));// A heartbeat issued while startInput was settling must not consume the// pending take-control; after the reply is lost, host-true still restores.- expect(sent().filter((m) => m.type === "control").at(-1)).toEqual({+ expect(+ sent()+ .filter((m) => m.type === "control")+ .at(-1),+ ).toEqual({type: "control",enabled: true,});diff --git a/apps/mobile/src/remote-desktop/__tests__/viewer.test.ts b/apps/mobile/src/remote-desktop/__tests__/viewer.test.tsindex 258e3fa8a4..f2aca442f0 100644--- a/apps/mobile/src/remote-desktop/__tests__/viewer.test.ts+++ b/apps/mobile/src/remote-desktop/__tests__/viewer.test.ts@@ -149,6 +149,37 @@ function viewer() {}describe("remote desktop viewport", () => {+ it("measures the usable picture and fits updated geometry above the toolbar", () => {+ const v = viewer();+ v.send({ type: "init", epoch: "one", width: 1920, height: 1080 });+ v.send({ type: "mouseButtons", bottomInset: 100 });+ v.send({ type: "measureViewport" });+ expect(v.messages.find((m) => m.type === "viewportSize")).toMatchObject({+ width: 400,+ height: 500,+ });+ v.send({ type: "videoSettings", width: 800, height: 1000, audio: false });+ expect(v.elements.video.style).toMatchObject({+ width: "400px",+ height: "500px",+ left: "0px",+ top: "0px",+ });+ v.send({ type: "control", enabled: true });+ v.send({ type: "mode", mode: "touch" });+ v.pointer("pointerdown", 1, 200, 250);+ v.pointer("pointerup", 1, 200, 250);+ v.ack();+ v.flush();+ expect(+ v.messages+ .flatMap((m) => m.events ?? [])+ .filter((e) => e.kind === "button"),+ ).toEqual([+ { kind: "button", button: 0, down: true, x: 0.5, y: 0.5 },+ { kind: "button", button: 0, down: false, x: 0.5, y: 0.5 },+ ]);+ });it.each([["left", 0],["right", 2],@@ -238,8 +269,8 @@ describe("remote desktop viewport", () => {v.frame(100);v.mouse("wheel", "pointerup", 100);expect(v.messages.flatMap((m) => m.events ?? [])).toEqual([- { kind: "button", button: 1, down: true, x: .5, y: .5 },- { kind: "button", button: 1, down: false, x: .5, y: .5 },+ { kind: "button", button: 1, down: true, x: 0.5, y: 0.5 },+ { kind: "button", button: 1, down: false, x: 0.5, y: 0.5 },]);});it("keeps scrolling while held, reverses direction and stops on release", () => {@@ -248,10 +279,13 @@ describe("remote desktop viewport", () => {v.send({ type: "mouseButtons", enabled: true });v.mouse("wheel", "pointerdown", 100);v.mouse("wheel", "pointermove", 90);- v.flush(); v.ack();- v.flush(); v.ack(); // No new movement: still scrolls.+ v.flush();+ v.ack();+ v.flush();+ v.ack(); // No new movement: still scrolls.v.mouse("wheel", "pointermove", 124);- v.flush(); v.ack();+ v.flush();+ v.ack();v.mouse("wheel", "pointermove", 100);v.flush(); // Center dead zone stops scrolling.v.mouse("wheel", "pointerup", 100);@@ -261,7 +295,9 @@ describe("remote desktop viewport", () => {{ kind: "scroll", dx: 0, dy: -9 },{ kind: "scroll", dx: 0, dy: 30 },]);- expect(v.elements["mouse-wheel-grip"].style.transform).toBe("translateY(0px)");+ expect(v.elements["mouse-wheel-grip"].style.transform).toBe(+ "translateY(0px)",+ );});it("does not accumulate held wheel ticks while awaiting acknowledgement", () => {const v = viewer();@@ -274,7 +310,9 @@ describe("remote desktop viewport", () => {v.mouse("wheel", "pointermove", y);v.flush();}- expect(v.messages.some((message) => message.type === "inputOverflow")).toBe(false);+ expect(v.messages.some((message) => message.type === "inputOverflow")).toBe(+ false,+ );v.ack();v.flush();expect(v.messages.flatMap((message) => message.events ?? [])).toEqual([@@ -307,51 +345,106 @@ describe("remote desktop viewport", () => {expect(parseFloat(v.elements.image.style.top)).toBeGreaterThan(0);});it.each([- [1, .1], [1, .5], [1, .9], [2, .1], [2, .5], [2, .9],- ])("keeps landscape scale %sx with minimal horizontal movement for cursor %s", (scale, cursorX) => {- const v = viewer();- v.elements.stage.clientWidth = 800;- v.elements.stage.clientHeight = 400;- v.send({ type: "init", epoch: "keyboard", width: 1920, height: 1080, fillHeight: true });- if (scale === 2) {- v.pointer("pointerdown", 1, 200, 200);- v.pointer("pointerdown", 2, 400, 200);- v.pointer("pointermove", 1, 100, 200);- v.pointer("pointermove", 2, 500, 200);- v.frame();- v.frame(40);- v.pointer("pointerup", 1, 100, 200);- v.pointer("pointerup", 2, 500, 200);- }- v.send({ type: "frame", jpeg: "", cursor: {- x: cursorX, y: .85, width: 18, height: 18, hotX: 9, hotY: 9,- visible: true, png: "iVBORw0KGgo=",- } });- v.send({ type: "mouseButtons", keyboardOpen: false, bottomInset: 0, leftInset: 50, rightInset: 80 });- for (let i = 0; i < 30; i++) v.frame();- const width = v.elements.image.style.width;- expect(parseFloat(v.elements.image.style.height)).toBeCloseTo(400 * scale);- // Removing the toolbar before the keyboard has a measured height must not shift the image.- const originalLeft = v.elements.image.style.left;- v.send({ type: "mouseButtons", keyboardOpen: true, bottomInset: 0, leftInset: 50, rightInset: 0 });- expect(parseFloat(v.elements.image.style.left)).toBeCloseTo(parseFloat(originalLeft));- // Header, computer keyboard, phone keyboard, and closing the keyboard.- for (const bottomInset of [60, 260, 300, 0]) {- const previousLeft = parseFloat(v.elements.image.style.left);- const previousCursorX = previousLeft + cursorX * parseFloat(width);- const rightInset = bottomInset > 0 ? 0 : 80;- const expectedCursorX = Math.max(67, Math.min(800 - rightInset - 17, previousCursorX));- v.send({ type: "mouseButtons", keyboardOpen: bottomInset > 0, bottomInset, leftInset: 50, rightInset });- v.blur();+ [1, 0.1],+ [1, 0.5],+ [1, 0.9],+ [2, 0.1],+ [2, 0.5],+ [2, 0.9],+ ])(+ "keeps landscape scale %sx with minimal horizontal movement for cursor %s",+ (scale, cursorX) => {+ const v = viewer();+ v.elements.stage.clientWidth = 800;+ v.elements.stage.clientHeight = 400;+ v.send({+ type: "init",+ epoch: "keyboard",+ width: 1920,+ height: 1080,+ fillHeight: true,+ });+ if (scale === 2) {+ v.pointer("pointerdown", 1, 200, 200);+ v.pointer("pointerdown", 2, 400, 200);+ v.pointer("pointermove", 1, 100, 200);+ v.pointer("pointermove", 2, 500, 200);+ v.frame();+ v.frame(40);+ v.pointer("pointerup", 1, 100, 200);+ v.pointer("pointerup", 2, 500, 200);+ }+ v.send({+ type: "frame",+ jpeg: "",+ cursor: {+ x: cursorX,+ y: 0.85,+ width: 18,+ height: 18,+ hotX: 9,+ hotY: 9,+ visible: true,+ png: "iVBORw0KGgo=",+ },+ });+ v.send({+ type: "mouseButtons",+ keyboardOpen: false,+ bottomInset: 0,+ leftInset: 50,+ rightInset: 80,+ });for (let i = 0; i < 30; i++) v.frame();- const image = v.elements.image.style;- expect(image.width).toBe(width);- expect(parseFloat(image.height)).toBeCloseTo(400 * scale);- expect(parseFloat(image.left) + cursorX * parseFloat(image.width)).toBeCloseTo(expectedCursorX);- expect(parseFloat(image.left)).toBeCloseTo(previousLeft + expectedCursorX - previousCursorX);- expect(parseFloat(image.top) + .85 * parseFloat(image.height)).toBeCloseTo((400 - bottomInset) / 2);- }- });+ const width = v.elements.image.style.width;+ expect(parseFloat(v.elements.image.style.height)).toBeCloseTo(+ 400 * scale,+ );+ // Removing the toolbar before the keyboard has a measured height must not shift the image.+ const originalLeft = v.elements.image.style.left;+ v.send({+ type: "mouseButtons",+ keyboardOpen: true,+ bottomInset: 0,+ leftInset: 50,+ rightInset: 0,+ });+ expect(parseFloat(v.elements.image.style.left)).toBeCloseTo(+ parseFloat(originalLeft),+ );+ // Header, computer keyboard, phone keyboard, and closing the keyboard.+ for (const bottomInset of [60, 260, 300, 0]) {+ const previousLeft = parseFloat(v.elements.image.style.left);+ const previousCursorX = previousLeft + cursorX * parseFloat(width);+ const rightInset = bottomInset > 0 ? 0 : 80;+ const expectedCursorX = Math.max(+ 67,+ Math.min(800 - rightInset - 17, previousCursorX),+ );+ v.send({+ type: "mouseButtons",+ keyboardOpen: bottomInset > 0,+ bottomInset,+ leftInset: 50,+ rightInset,+ });+ v.blur();+ for (let i = 0; i < 30; i++) v.frame();+ const image = v.elements.image.style;+ expect(image.width).toBe(width);+ expect(parseFloat(image.height)).toBeCloseTo(400 * scale);+ expect(+ parseFloat(image.left) + cursorX * parseFloat(image.width),+ ).toBeCloseTo(expectedCursorX);+ expect(parseFloat(image.left)).toBeCloseTo(+ previousLeft + expectedCursorX - previousCursorX,+ );+ expect(+ parseFloat(image.top) + 0.85 * parseFloat(image.height),+ ).toBeCloseTo((400 - bottomInset) / 2);+ }+ },+ );it("does not recenter portrait content for a keyboard overlay message", () => {const v = viewer();const before = { ...v.elements.image.style };@@ -362,12 +455,36 @@ describe("remote desktop viewport", () => {const v = viewer();v.elements.stage.clientWidth = 800;v.elements.stage.clientHeight = 400;- v.send({ type: "init", epoch: "quick-keyboard", width: 1920, height: 1080, fillHeight: true });- v.send({ type: "mouseButtons", keyboardOpen: false, bottomInset: 0, leftInset: 50, rightInset: 80 });+ v.send({+ type: "init",+ epoch: "quick-keyboard",+ width: 1920,+ height: 1080,+ fillHeight: true,+ });+ v.send({+ type: "mouseButtons",+ keyboardOpen: false,+ bottomInset: 0,+ leftInset: 50,+ rightInset: 80,+ });const before = { ...v.elements.image.style };for (let i = 0; i < 2; i++) {- v.send({ type: "mouseButtons", keyboardOpen: true, bottomInset: 0, leftInset: 50, rightInset: 0 });- v.send({ type: "mouseButtons", keyboardOpen: false, bottomInset: 0, leftInset: 50, rightInset: 80 });+ v.send({+ type: "mouseButtons",+ keyboardOpen: true,+ bottomInset: 0,+ leftInset: 50,+ rightInset: 0,+ });+ v.send({+ type: "mouseButtons",+ keyboardOpen: false,+ bottomInset: 0,+ leftInset: 50,+ rightInset: 80,+ });expect(v.elements.image.style).toEqual(before);v.blur();for (let j = 0; j < 30; j++) v.frame();diff --git a/docs/design-previews/remote-desktop-viewer-aspect/index.html b/docs/design-previews/remote-desktop-viewer-aspect/index.htmlnew file mode 100644index 0000000000..9f63b43d91--- /dev/null+++ b/docs/design-previews/remote-desktop-viewer-aspect/index.html@@ -0,0 +1,268 @@+<!doctype html>+<html lang="zh-CN">+ <meta charset="utf-8" />+ <meta name="viewport" content="width=device-width,initial-scale=1" />+ <title>远程桌面 · 控制端比例预览</title>+ <style>+ :root {+ color-scheme: light dark;+ --bg: #fafafa;+ --surface: #fff;+ --chip: #e5e5e5;+ --fg: #262626;+ --muted: #737373;+ --border: #d7d7d4;+ }+ @media (prefers-color-scheme: dark) {+ :root {+ --bg: #202020;+ --surface: #2c2c2a;+ --chip: #3c3c3a;+ --fg: #d4d4d4;+ --muted: #a3a3a3;+ --border: #3c3c3a;+ }+ }+ * {+ box-sizing: border-box;+ }+ body {+ margin: 0;+ background: var(--bg);+ color: var(--fg);+ font:+ 14px/1.5 Inter,+ -apple-system,+ BlinkMacSystemFont,+ sans-serif;+ }+ main {+ max-width: 1100px;+ margin: 32px auto;+ padding: 0 24px;+ }+ h1 {+ font-size: 18px;+ font-weight: 500;+ }+ p {+ color: var(--muted);+ }+ button {+ font: inherit;+ background: var(--chip);+ color: var(--fg);+ border: 1px solid var(--border);+ border-radius: 9999px;+ min-height: 44px;+ padding: 8px 20px;+ cursor: pointer;+ }+ button:focus-visible {+ outline: 2px solid var(--fg);+ outline-offset: 3px;+ }+ button:hover {+ filter: brightness(0.95);+ }+ .layout {+ display: flex;+ gap: 32px;+ align-items: flex-start;+ flex-wrap: wrap;+ }+ .settings {+ width: 300px;+ background: var(--surface);+ border: 1px solid var(--border);+ border-radius: 12px;+ padding: 20px;+ }+ .settings h2 {+ font-size: 14px;+ font-weight: 500;+ margin: 0 0 16px;+ }+ .settings button {+ width: 100%;+ }+ .settings p {+ font-size: 12px;+ }+ .row {+ display: flex;+ justify-content: space-between;+ padding: 12px 0;+ border-top: 1px solid var(--border);+ }+ .phone {+ width: 312px;+ height: 620px;+ border: 1px solid var(--border);+ border-radius: 12px;+ overflow: hidden;+ background: var(--chip);+ display: flex;+ flex-direction: column;+ }+ .phone.landscape {+ width: 580px;+ height: 300px;+ }+ .canvas {+ flex: 1;+ min-height: 0;+ display: flex;+ align-items: center;+ justify-content: center;+ overflow: hidden;+ }+ .desktop {+ background: var(--surface);+ border: 1px solid var(--border);+ flex: none;+ overflow: hidden;+ }+ .menu {+ height: 24px;+ background: var(--chip);+ font-size: 10px;+ padding: 4px 10px;+ }+ .content {+ height: calc(100% - 24px);+ display: flex;+ gap: 8px;+ padding: 10px;+ align-items: stretch;+ }+ .files {+ width: 24%;+ min-width: 38px;+ background: var(--bg);+ font-size: 10px;+ padding: 6px;+ }+ .editor {+ flex: 1;+ min-width: 0;+ border: 1px solid var(--border);+ border-radius: 8px;+ padding: 12px;+ font-size: 11px;+ }+ .editor h3 {+ font-size: 12px;+ font-weight: 500;+ margin: 0 0 10px;+ }+ .editor p {+ margin: 8px 0;+ }+ .toolbar {+ height: 50px;+ flex: none;+ background: var(--surface);+ border-top: 1px solid var(--border);+ display: flex;+ align-items: center;+ justify-content: space-around;+ font-size: 12px;+ }+ .actions {+ display: flex;+ gap: 8px;+ margin-bottom: 20px;+ }+ output {+ font-variant-numeric: tabular-nums;+ }+ .caption {+ font-size: 12px;+ max-width: 580px;+ }+ </style>+ <main>+ <h1>远程桌面 · 按控制端比例调整</h1>+ <p>交互预览,使用示例桌面。这里不连接或修改真实电脑。</p>+ <div class="actions">+ <button id="rotate">切换横竖屏</button+ ><button id="reset">恢复电脑比例</button>+ </div>+ <div class="layout">+ <section>+ <div class="phone" id="phone">+ <div class="canvas" id="canvas">+ <div class="desktop" id="desktop">+ <div class="menu">桌面 文件 编辑 窗口</div>+ <div class="content">+ <aside class="files">+ 文件<br /><br />项目<br /><br />文档+ </aside>+ <article class="editor">+ <h3>今天的工作</h3>+ <p>继续处理电脑上的内容。</p>+ <p>点击右侧按钮,让桌面采用当前画面区域的比例。</p>+ <p>桌面尺寸改变后,内容区域随之变化。</p>+ </article>+ </div>+ </div>+ </div>+ <div class="toolbar">+ <span>返回</span><span>键盘</span><span>操作</span>+ </div>+ </div>+ <p class="caption">+ 旋转后再点一次调整;工具栏和键盘不会自动触发远端切屏。+ </p>+ </section>+ <section class="settings">+ <h2>显示</h2>+ <div class="row">+ <span>电脑分辨率</span><output id="size">2560 × 1440</output>+ </div>+ <button id="match">按当前画面比例调整</button>+ <p>临时调整电脑桌面比例,保留当前桌面内容。退出连接后恢复原分辨率。</p>+ <p id="status" role="status">当前使用电脑比例。</p>+ </section>+ </div>+ </main>+ <script>+ const canvas = document.getElementById("canvas"),+ desktop = document.getElementById("desktop"),+ phone = document.getElementById("phone"),+ size = document.getElementById("size"),+ status = document.getElementById("status");+ let width = 2560,+ height = 1440;+ function render() {+ const scale = Math.min(+ canvas.clientWidth / width,+ canvas.clientHeight / height,+ );+ desktop.style.width = width * scale + "px";+ desktop.style.height = height * scale + "px";+ size.value = width + " × " + height;+ }+ document.getElementById("match").onclick = () => {+ const scale = 1920 / Math.max(canvas.clientWidth, canvas.clientHeight);+ width = Math.round((canvas.clientWidth * scale) / 2) * 2;+ height = Math.round((canvas.clientHeight * scale) / 2) * 2;+ render();+ status.textContent = "已按当前画面区域调整。";+ };+ document.getElementById("rotate").onclick = () => {+ phone.classList.toggle("landscape");+ render();+ status.textContent = "画面方向已变,可再次调整桌面比例。";+ };+ document.getElementById("reset").onclick = () => {+ width = 2560;+ height = 1440;+ render();+ status.textContent = "已恢复电脑比例。";+ };+ new ResizeObserver(render).observe(canvas);+ render();+ </script>+</html>diff --git a/docs/remote-desktop-viewer-aspect-prototype.md b/docs/remote-desktop-viewer-aspect-prototype.mdnew file mode 100644index 0000000000..84d22824d6--- /dev/null+++ b/docs/remote-desktop-viewer-aspect-prototype.md@@ -0,0 +1,105 @@+# Viewer aspect ratio prototype++The new **Match Current View** action uses the controller's usable picture area+to choose independent width/height, with an even-pixel 1920-pixel long-edge limit.+Mobile excludes its toolbar from the measured area. Desktop uses the viewer+content area. Resizing is explicit: rotating the phone or changing window size+does not continually reconfigure the remote computer.++On Mobile, after matching, the action becomes **Restore Computer Screen Ratio**+while the current viewport still matches. Rotating to a different ratio offers+**Match Current View** again. Restore removes the temporary display, waits for+the original monitor mode to return, and renegotiates video/control on the same+lease. This uses the optional `viewerDisplayRestore` capability; older hosts+continue to offer matching only.++## Current scope++- **macOS source-development hosts only** advertise `viewerDisplay`. Packaged+ hosts and Windows/Linux retain the existing display-mode behavior.+- Both Mobile and Desktop viewers expose the action only for capable hosts.+ The same authenticated, controlling lease is required. No new relay kind,+ server change, IPC channel, credential access or Mobile native dependency.+- A temporary virtual display becomes the mirror source for the selected+ existing display, so the current desktop is shown instead of an empty extended+ desktop. This changes the local monitor's layout too; large application windows+ may require resizing. Existing mirror sets are not supported by this prototype.+- The helper retains the virtual display only for the lease. Explicit exit,+ expiry, revocation and helper failure dispose it. EOF/SIGTERM restore the source+ display mode. Original window positions are not snapshotted or restored.+ A new lease waits for the prior temporary display's restoration before reading+ its geometry, including takeover. A failed restoration can be retried by the+ next start; it never creates a lease from unconfirmed geometry.+- Resizing preserves the lease and input sequence; old video/input are stopped,+ queued old-geometry input is discarded, and the viewer negotiates video and+ reacquires control on the confirmed new display. Shared Device Link connections+ are not restarted.+- Failure ends only this remote-desktop lease and restores its temporary display.+ The viewer's existing recovery remains responsible for reconnecting.+- On hosts advertising both viewer-display capabilities, the resolution dropdown+ resizes the temporary display without ending the lease for dimensions within+ 320–2560 per edge. Larger system modes (such as 4K) retain the legacy mode-ID+ path and its existing reconnect/persistent-mode behavior. Exiting a temporary+ display completes before a legacy mode write, so cleanup cannot overwrite it.+ Mode enumeration and native mode-ID writes use the lease's original source+ monitor; video and input use the current capture display. Exiting a temporary+ display restores the original monitor mode. The list still includes all macOS-reported modes,+ including different aspect ratios such as 800 × 600; filtering the list to the+ matched ratio is not implemented in this prototype.++## Native dependency++`CGVirtualDisplay` is a private CoreGraphics interface, resolved at runtime.+Chromium's [virtual display test implementation](https://chromium.googlesource.com/chromium/src/+/HEAD/ui/display/mac/test/virtual_display_util_mac.mm)+is the reference for its interface shape. No new third-party binary or package is+installed. The helper is compiled from source by the development host. Enabling+it in packaged releases requires a separate supported-OS, signing and packaging+decision and validation; this prototype deliberately does not advertise it there.++## Try it++Start the development host from this worktree using the repository's Desktop+development instructions. Connect using this worktree's Mobile or Desktop viewer,+take control, then open **Controls → Display → Match Current View** (Mobile) or+the viewer's controls panel (Desktop). Rotate/resize and apply again to change+the target ratio. Disconnect to restore the original monitor resolution.++The [interactive HTML preview](design-previews/remote-desktop-viewer-aspect/index.html)+uses sample content and does not touch the computer display.++Explicit native integration check (temporarily changes the main monitor layout):++```sh+node apps/desktop/scripts/viewer-display-native-smoke.mjs+```++The check compiles in an OS temporary directory, verifies 900 × 1600 and+1600 × 900 on the same virtual display, ends the helper, and compares the complete+online display list and geometry against the initial snapshot. It does not+capture pixels or inject input, and is not in the ordinary unit suite.++Real Mobile-to-Mac video/input, multiple monitors, HiDPI, lock/sleep and signed+packaged execution still require device testing. Native mode readback and unit+tests are not evidence that those end-to-end paths have passed.++## Validation in this worktree++- 238 focused tests passed across the protocol/session, host lifecycle, Desktop+ viewer, Mobile screen, viewport and RTC suites.+- Desktop and Mobile `typecheck`, and device-link's `build` (`tsc --noEmit`) passed.+- The shared package's full `build` reports eight existing diagnostics in+ `brandIdentity.test.ts`, `composerPalette.test.ts`, `historyView.test.ts` and+ `workRunGrouping.ts`. An unmodified HEAD archive produced the same eight+ diagnostics, with no added or removed errors.+- Native smoke passed both orientations and restoration. A separate real+ Electron process also exercised the production `createViewerDisplay` adapter+ at 888 × 1920 and 1920 × 888, and verified the original Electron screen snapshot+ (bounds, scale factor and display count) after disposal.+- The HTML preview was exercised in Chromium in Light/Dark and both orientations;+ Light/Dark portrait screenshots were visually inspected. This is sample-content+ preview evidence, not native Mobile screenshots.+- Five-language i18n consistency, terminology and generated-viewer synchronization+ checks passed. No release was made.+- The physical iPhone Air now has a separately signed Cindy Aspect test app with+ an embedded JS bundle; app launch was verified. Full video/input acceptance+ remains separate from installation and startup.diff --git a/packages/device-link/src/__tests__/viewerDisplay.test.ts b/packages/device-link/src/__tests__/viewerDisplay.test.tsnew file mode 100644index 0000000000..4b98889626--- /dev/null+++ b/packages/device-link/src/__tests__/viewerDisplay.test.ts@@ -0,0 +1,119 @@+import { describe, expect, it, vi } from "vitest";+import { parseRemoteDesktopRequest } from "../remoteDesktop.js";+import {+ RemoteDesktopViewerSession,+ viewerDisplaySize,+ type DesktopViewerRequest,+} from "../remoteDesktopViewerSession.js";++describe("viewer display dimensions", () => {+ it.each([+ [390, 844, 888, 1920],+ [844, 390, 1920, 888],+ [1000, 1000, 1920, 1920],+ ])(+ "preserves the ratio of %d x %d within an even 1920-pixel bound",+ (width, height, w, h) => {+ expect(viewerDisplaySize(width, height)).toEqual({ width: w, height: h });+ },+ );+ it.each([0, -1, NaN, Infinity, 0.01])(+ "rejects unusable viewport dimensions: %s",+ (width) => {+ expect(viewerDisplaySize(width, 800)).toBeNull();+ },+ );+ it.each([319, 2561, 900.5, NaN, Infinity, "900"])(+ "rejects invalid wire dimensions: %s",+ (width) => {+ expect(() =>+ parseRemoteDesktopRequest({+ op: "viewerDisplay",+ lease: "lease",+ width,+ height: 1600,+ }),+ ).toThrow("INVALID_REQUEST");+ },+ );+ it("accepts authenticated restore requests without a mode identifier", () => {+ expect(+ parseRemoteDesktopRequest({ op: "restoreViewerDisplay", lease: "lease" }),+ ).toEqual({ op: "restoreViewerDisplay", lease: "lease" });+ expect(() =>+ parseRemoteDesktopRequest({ op: "restoreViewerDisplay" }),+ ).toThrow();+ });+ it("accepts a bounded independent width/height without changing mode-ID requests", () => {+ expect(+ parseRemoteDesktopRequest({+ op: "viewerDisplay",+ lease: "lease",+ width: 900,+ height: 1600,+ }),+ ).toEqual({+ op: "viewerDisplay",+ lease: "lease",+ width: 900,+ height: 1600,+ });+ expect(+ parseRemoteDesktopRequest({+ op: "resolution",+ lease: "lease",+ modeId: "123",+ }),+ ).toEqual({ op: "resolution", lease: "lease", modeId: "123" });+ });+ it("updates the existing viewer lease and starts control on the new display", async () => {+ const request = vi.fn(async (value) => {+ if (value.op === "capabilities")+ return { version: 1, enabled: true, displays: [{ id: "1" }] };+ if (value.op === "start")+ return {+ lease: "lease",+ display: { id: "1", width: 1920, height: 1080 },+ controlling: false,+ };+ if (value.op === "control") return { controlling: value.enabled };+ if (value.op === "restoreViewerDisplay")+ return {+ lease: "lease",+ display: { id: "1", width: 2560, height: 1440 },+ controlling: false,+ };+ if (value.op === "viewerDisplay")+ return {+ lease: "lease",+ display: { id: "2", width: value.width, height: value.height },+ controlling: false,+ };+ return {};+ });+ const session = new RemoteDesktopViewerSession(+ request as DesktopViewerRequest,+ );+ await session.connect({ isCurrent: () => true });+ await session.control(true);+ const previous = session.lease;+ await session.fitDisplay(900, 1600);+ expect(session.lease).toBe(previous);+ expect(session.lease?.display.id).toBe("2");+ expect(session.lease?.controlling).toBe(false);+ await session.control(true);+ expect(session.lease?.controlling).toBe(true);+ await session.fitDisplay(900, 1600, true);+ expect(request).toHaveBeenLastCalledWith(+ { op: "restoreViewerDisplay", lease: "lease" },+ expect.any(Function),+ );+ expect(session.lease).toBe(previous);+ expect(session.lease?.display).toEqual({+ id: "1",+ width: 2560,+ height: 1440,+ });+ expect(session.lease?.controlling).toBe(false);+ });+});diff --git a/packages/device-link/src/remoteDesktop.ts b/packages/device-link/src/remoteDesktop.tsindex df025a6274..88ee2b19a3 100644--- a/packages/device-link/src/remoteDesktop.ts+++ b/packages/device-link/src/remoteDesktop.ts@@ -131,6 +131,9 @@ export interface RemoteDesktopCapabilities {trickleIce?: boolean;systemAudio?: boolean;displayModes?: boolean;+ /** Can temporarily lay out the desktop at the viewer's requested dimensions. */+ viewerDisplay?: boolean;+ viewerDisplayRestore?: boolean;backgroundViewing?: boolean;cursorOverlay?: boolean;clipboardText?: boolean;@@ -180,6 +183,8 @@ export type RemoteDesktopRequest =| { op: "clipboard"; lease: string; action: "copy" }| { op: "clipboard"; lease: string; action: "paste"; text: string }| { op: "displayModes"; lease: string }+ | { op: "viewerDisplay"; lease: string; width: number; height: number }+ | { op: "restoreViewerDisplay"; lease: string }| { op: "resolution"; lease: string; modeId: string };export function parseRemoteDesktopRequest(@@ -198,8 +203,10 @@ export function parseRemoteDesktopRequest() {if (v.resume !== undefined && typeof v.resume !== "boolean")throw new Error("INVALID_REQUEST");- if (v.takeover !== undefined && typeof v.takeover !== "boolean") throw new Error("INVALID_REQUEST");- if (v.takeover === true && v.resume === true) throw new Error("INVALID_REQUEST");+ if (v.takeover !== undefined && typeof v.takeover !== "boolean")+ throw new Error("INVALID_REQUEST");+ if (v.takeover === true && v.resume === true)+ throw new Error("INVALID_REQUEST");return {op: v.op,displayId: v.displayId,@@ -225,26 +232,59 @@ export function parseRemoteDesktopRequest(return parseClipboardContentRequest(v, lease);if (v.op === "clipboard") {if (v.action === "copy") return { op: v.op, lease, action: "copy" };- if (v.action === "paste" && typeof v.text === "string" && v.text.length > 0 && v.text.length <= REMOTE_DESKTOP_MAX_CLIPBOARD_CHARS)+ if (+ v.action === "paste" &&+ typeof v.text === "string" &&+ v.text.length > 0 &&+ v.text.length <= REMOTE_DESKTOP_MAX_CLIPBOARD_CHARS+ )return { op: v.op, lease, action: "paste", text: v.text };throw new Error("INVALID_REQUEST");}if (v.op === "frame") {- if (v.cursorOverlay !== undefined && typeof v.cursorOverlay !== "boolean") throw new Error("INVALID_REQUEST");- return { op: v.op, lease, ...(v.cursorOverlay === true ? { cursorOverlay: true } : {}) };+ if (v.cursorOverlay !== undefined && typeof v.cursorOverlay !== "boolean")+ throw new Error("INVALID_REQUEST");+ return {+ op: v.op,+ lease,+ ...(v.cursorOverlay === true ? { cursorOverlay: true } : {}),+ };}if (v.op === "stop") {- if (v.lockScreen !== undefined && typeof v.lockScreen !== "boolean") throw new Error("INVALID_REQUEST");- return { op: v.op, lease, ...(v.lockScreen === true ? { lockScreen: true } : {}) };+ if (v.lockScreen !== undefined && typeof v.lockScreen !== "boolean")+ throw new Error("INVALID_REQUEST");+ return {+ op: v.op,+ lease,+ ...(v.lockScreen === true ? { lockScreen: true } : {}),+ };}- if (v.op === "heartbeat")- return { op: v.op, lease };+ if (v.op === "heartbeat") return { op: v.op, lease };if ((v.op === "control" || v.op === "presentation") &&typeof v.enabled === "boolean")return { op: v.op, lease, enabled: v.enabled };+ if (v.op === "restoreViewerDisplay") return { op: v.op, lease };if (v.op === "displayModes") return { op: v.op, lease };+ if (v.op === "viewerDisplay") {+ if (+ ![v.width, v.height].every(+ (size) =>+ typeof size === "number" &&+ Number.isInteger(size) &&+ size >= 320 &&+ size <= 2560,+ )+ )+ throw new Error("INVALID_REQUEST");+ return {+ op: v.op,+ lease,+ width: v.width as number,+ height: v.height as number,+ };+ }if (v.op === "resolution" &&typeof v.modeId === "string" &&diff --git a/packages/device-link/src/remoteDesktopViewerSession.ts b/packages/device-link/src/remoteDesktopViewerSession.tsindex 6b59ac5e01..be70efd11e 100644--- a/packages/device-link/src/remoteDesktopViewerSession.ts+++ b/packages/device-link/src/remoteDesktopViewerSession.ts@@ -159,6 +159,50 @@ export class RemoteDesktopViewerSession {return { controlling: lease.controlling };}+ /** Keep the lease and input sequence, replacing only its display geometry. */+ async fitDisplay(+ width: number,+ height: number,+ restore = false,+ ): Promise<RemoteDesktopLease> {+ const lease = this.active;+ if (!lease?.controlling) throw new Error("DESKTOP_VIEW_ONLY");+ if (this.controlPending) throw new Error("DESKTOP_INPUT_BUSY");+ this.controlGeneration++;+ const check = () => {+ if (this.active !== lease) throw new Error("DESKTOP_LEASE_EXPIRED");+ };+ const operation = this.request<RemoteDesktopLease>(+ restore+ ? { op: "restoreViewerDisplay", lease: lease.lease }+ : { op: "viewerDisplay", lease: lease.lease, width, height },+ check,+ );+ this.controlPending = operation;+ try {+ const result = await operation;+ check();+ if (+ result.lease !== lease.lease ||+ typeof result.display?.id !== "string" ||+ (!restore &&+ (result.display.width !== width ||+ result.display.height !== height)) ||+ !Number.isFinite(result.display.width) ||+ result.display.width <= 0 ||+ !Number.isFinite(result.display.height) ||+ result.display.height <= 0 ||+ result.controlling !== false+ )+ throw new Error("INVALID_RESPONSE");+ lease.display = result.display;+ lease.controlling = false;+ return lease;+ } finally {+ if (this.controlPending === operation) this.controlPending = null;+ }+ }+stop(lockScreen = false): Promise<unknown> {this.generation++;const lease = this.active;@@ -178,6 +222,26 @@ export class RemoteDesktopViewerSession {}}+/** Layout points determine the ratio; cap the long edge instead of sending phone DPR pixels. */+export function viewerDisplaySize(+ width: number,+ height: number,+): { width: number; height: number } | null {+ if (+ !Number.isFinite(width) ||+ !Number.isFinite(height) ||+ width <= 0 ||+ height <= 0+ )+ return null;+ const scale = 1920 / Math.max(width, height);+ const result = {+ width: Math.round((width * scale) / 2) * 2,+ height: Math.round((height * scale) / 2) * 2,+ };+ return Math.min(result.width, result.height) >= 320 ? result : null;+}+/** Only transient connection errors may restart a viewer. Explicit stop wins. */export function remoteDesktopFailureKey(code: string): string | null {if (/ACCESS_REVOKED/.test(code)) return "accessRevoked";diff --git a/packages/maker-shared/src/remote-desktop/viewerRuntime.js b/packages/maker-shared/src/remote-desktop/viewerRuntime.jsindex 6a6f7124b1..d169e93d4f 100644--- a/packages/maker-shared/src/remote-desktop/viewerRuntime.js+++ b/packages/maker-shared/src/remote-desktop/viewerRuntime.js@@ -107,6 +107,7 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {cursor = find("cursor");let dw = 1920,dh = 1080,+ viewerSized = false,zoom = 1,fx = 0.5,fy = 0.5,@@ -232,6 +233,8 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {// Insets guide centering and pan limits without clipping the full-screen// video surface or adding an opaque strip beside the Dynamic Island.const viewportLeft = () => (fillHeight ? viewportLeftInset : 0);+ const viewportHeight = () =>+ Math.max(1, stage.clientHeight - (viewerSized ? viewportBottomInset : 0));const viewportWidth = () =>Math.max(1,@@ -242,23 +245,23 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {const layout = () => {const r = transform(viewportWidth(),- stage.clientHeight,+ viewportHeight(),dw,dh,zoom,fx,fy,- fillHeight,+ fillHeight && !viewerSized,);return {...r,x: viewportLeft() + viewportWidth() / 2 - fx * r.width,- y: stage.clientHeight / 2 - fy * r.height,+ y: viewportHeight() / 2 - fy * r.height,};};function panBounds(r) {const vw = viewportWidth(),- vh = stage.clientHeight;+ vh = viewportHeight();// Grow extra resting travel continuously from zero at fit to 180 screen// points at maximum zoom. Never count the unused space of a fitting axis.const clearance = (180 * (Math.max(1, Math.min(5, zoom)) - 1)) / 4;@@ -308,7 +311,7 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {}function place(x, y, r) {fx = (viewportLeft() + viewportWidth() / 2 - x) / r.width;- fy = (stage.clientHeight / 2 - y) / r.height;+ fy = (viewportHeight() / 2 - y) / r.height;}function cursorViewport() {const hotX = remoteCursor?.hotX ?? 9,@@ -429,7 +432,9 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {cursor.style.left = r.x + cursorX * r.width + "px";cursor.style.top = r.y + cursorY * r.height + "px";cursor.style.display =- !config.desktop && control && (mode === "pointer" || mode === "touch") ? "block" : "none";+ !config.desktop && control && (mode === "pointer" || mode === "touch")+ ? "block"+ : "none";}if (config.desktop) stage.style.cursor = control ? "none" : "default";}@@ -770,7 +775,7 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {fx =multi.anchor.x +(viewportLeft() + viewportWidth() / 2 - next.x) / r.width;- fy = multi.anchor.y + (stage.clientHeight / 2 - next.y) / r.height;+ fy = multi.anchor.y + (viewportHeight() / 2 - next.y) / r.height;const moved = layout(),b = panBounds(moved);place(@@ -994,7 +999,8 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {if (control &&clipboardShortcuts &&- normalizedKey(e.code) === (clipboardModifier === "meta" ? "MetaLeft" : "ControlLeft")+ normalizedKey(e.code) ===+ (clipboardModifier === "meta" ? "MetaLeft" : "ControlLeft")) {e.preventDefault();if (!hardwareKeys.has(normalizedKey(e.code)))@@ -1004,7 +1010,9 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {if (control &&clipboardShortcuts &&- (clipboardModifier === "meta" ? e.metaKey && !e.ctrlKey : e.ctrlKey && !e.metaKey) &&+ (clipboardModifier === "meta"+ ? e.metaKey && !e.ctrlKey+ : e.ctrlKey && !e.metaKey) &&!e.altKey &&!e.shiftKey &&(e.code === "KeyC" || e.code === "KeyV")@@ -1013,7 +1021,10 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {if (!e.repeat) {release();hardwareKeys.clear();- post({ type: "clipboard", action: e.code === "KeyC" ? "copy" : "paste" });+ post({+ type: "clipboard",+ action: e.code === "KeyC" ? "copy" : "paste",+ });}return;}@@ -1052,7 +1063,15 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {release();settlePan();});+ function reportViewport() {+ post({+ type: "viewportChanged",+ width: viewportWidth(),+ height: Math.max(1, stage.clientHeight - viewportBottomInset),+ });+ }const observer = new ResizeObserver(() => {+ reportViewport();release();settlePan();render();@@ -1493,6 +1512,13 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {return;}switch (message.type) {+ case "measureViewport":+ post({+ type: "viewportSize",+ width: viewportWidth(),+ height: Math.max(1, stage.clientHeight - viewportBottomInset),+ });+ break;case "presentation":try {if (message.enabled) {@@ -1514,6 +1540,24 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {}break;case "videoSettings":+ if (+ Number.isInteger(message.width) &&+ Number.isInteger(message.height) &&+ message.width >= 320 &&+ message.height >= 320 &&+ (message.restore === true ||+ (message.width <= 2560 && message.height <= 2560))+ ) {+ release();+ stopPanAnimation();+ viewerSized = message.restore !== true;+ zoom = 1;+ fx = fy = 0.5;+ followRest = null;+ dw = message.width;+ dh = message.height;+ render();+ }video.muted = !message.audio;retries = 0;connect();@@ -1546,8 +1590,10 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {break;case "mouseButtons": {const before = layout(),+ previousBottomInset = viewportBottomInset,keyboardOpen = fillHeight && message.keyboardOpen === true;- const keepHorizontal = fillHeight && (keyboardOpen || keyboardViewportOpen);+ const keepHorizontal =+ fillHeight && (keyboardOpen || keyboardViewportOpen);// Opening is observable before the native keyboard has a measured height.keyboardViewportOpen = keyboardOpen;let sideInsetsChanged = false;@@ -1601,10 +1647,14 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {}followRest = { fx, fy, zoom };render();- } else if (sideInsetsChanged) {+ } else if (+ sideInsetsChanged ||+ (viewerSized && previousBottomInset !== viewportBottomInset)+ ) {settlePan();render();}+ reportViewport();if (!message.enabled) {for (const button of heldMouse.keys())queue({ kind: "button", button, down: false, x: cx, y: cy });@@ -1621,11 +1671,15 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {showMouseButtons = message.enabled === true;for (const name of ["left", "right", "wheel"])if (typeof message.labels?.[name] === "string")- find("mouse-" + name).setAttribute("aria-label", message.labels[name]);+ find("mouse-" + name).setAttribute(+ "aria-label",+ message.labels[name],+ );updateMouseButtons();break;}case "init":+ viewerSized = false;followRest = null;cursorNeedsEntry = true;stopPanAnimation();@@ -1636,8 +1690,10 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {sending = false;seq = 0;epoch = message.epoch;- clipboardShortcuts = config.desktop && message.clipboardShortcuts === true;- clipboardModifier = message.clipboardModifier === "meta" ? "meta" : "control";+ clipboardShortcuts =+ config.desktop && message.clipboardShortcuts === true;+ clipboardModifier =+ message.clipboardModifier === "meta" ? "meta" : "control";dw = message.width;dh = message.height;fillHeight = message.fillHeight === true;diff --git a/packages/maker-shared/src/remote-desktop/viewerSource.ts b/packages/maker-shared/src/remote-desktop/viewerSource.tsindex d27a3d1733..774cacdb59 100644--- a/packages/maker-shared/src/remote-desktop/viewerSource.ts+++ b/packages/maker-shared/src/remote-desktop/viewerSource.ts@@ -1,2 +1,2 @@// Generated by scripts/sync-remote-desktop-viewer.mjs. Do not edit.-export const DESKTOP_VIEWER_SOURCE = "/** Shared browser viewer. Mobile bundles this exact source; Desktop imports it as a CSP-safe module. */\nfunction mountRemoteDesktopViewer(root, postMessage, config) {\n const cleanups = [];\n const listen = (target, type, fn, options) => {\n target.addEventListener(type, fn, options);\n cleanups.push(() => target.removeEventListener(type, fn, options));\n };\n const find = (id) =>\n root.getElementById\n ? root.getElementById(id)\n : root.querySelector(\"#\" + id);\n const { net, iceServers } = config;\n const validKeys = new Set(config.keyCodes);\n const transform =\n /* BEGIN TRANSFORM */\n function desktopTransform(\n vw,\n vh,\n dw,\n dh,\n zoom,\n fx,\n fy,\n fillHeight = false,\n ) {\n const scale =\n (fillHeight\n ? vh / Math.max(1, dh)\n : Math.min(vw / Math.max(1, dw), vh / Math.max(1, dh))) *\n Math.max(1, Math.min(5, zoom));\n const width = dw * scale;\n const height = dh * scale;\n const x =\n width <= vw\n ? (vw - width) / 2\n : Math.min(0, Math.max(vw - width, vw / 2 - fx * width));\n const y =\n height <= vh\n ? (vh - height) / 2\n : Math.min(0, Math.max(vh - height, vh / 2 - fy * height));\n return { x, y, width, height, scale };\n };\n /* END TRANSFORM */ const networkStats =\n /* BEGIN NETWORK_STATS */\n function networkStats(stats, previous) {\n const rows = [...stats.values()];\n const video = rows.find(\n (s) =>\n s.type === \"inbound-rtp\" &&\n (s.kind === \"video\" || s.mediaType === \"video\"),\n );\n const transport = rows.find(\n (s) => s.type === \"transport\" && s.selectedCandidatePairId,\n );\n const pair = transport\n ? stats.get(transport.selectedCandidatePairId)\n : rows.find(\n (s) =>\n s.type === \"candidate-pair\" &&\n s.state === \"succeeded\" &&\n s.nominated,\n );\n const local = pair && stats.get(pair.localCandidateId),\n remote = pair && stats.get(pair.remoteCandidateId);\n const direct = [\"host\", \"srflx\", \"prflx\"];\n const route =\n local?.candidateType === \"relay\" || remote?.candidateType === \"relay\"\n ? \"relay\"\n : direct.includes(local?.candidateType) &&\n direct.includes(remote?.candidateType)\n ? \"direct\"\n : \"video\";\n const sample =\n video &&\n Number.isFinite(video.bytesReceived) &&\n Number.isFinite(video.timestamp)\n ? {\n id: video.id,\n path: pair?.id,\n bytes: video.bytesReceived,\n time: video.timestamp,\n }\n : null;\n let bytesPerSecond = null;\n if (\n sample &&\n previous &&\n sample.id === previous.id &&\n sample.path === previous.path &&\n sample.time > previous.time &&\n sample.bytes >= previous.bytes\n )\n bytesPerSecond =\n ((sample.bytes - previous.bytes) * 1000) /\n (sample.time - previous.time);\n const latencyMs =\n Number.isFinite(pair?.currentRoundTripTime) &&\n pair.currentRoundTripTime >= 0\n ? pair.currentRoundTripTime * 1000\n : null;\n return { transport: route, bytesPerSecond, latencyMs, sample };\n };\n\n /* END NETWORK_STATS */ const stage = find(\"stage\"),\n image = find(\"image\"),\n video = find(\"video\"),\n cursor = find(\"cursor\");\n let dw = 1920,\n dh = 1080,\n zoom = 1,\n fx = 0.5,\n fy = 0.5,\n mode = \"pointer\",\n control = false,\n clipboardShortcuts = false,\n clipboardModifier = \"control\",\n deferredClipboardModifier = null,\n pc = null,\n dc = null,\n seq = 0,\n generation = 0,\n epoch = null,\n gestureFrame = null;\n let remoteCursor = null,\n lastLocalMove = 0,\n localCursorAwake = false;\n let touchCursor = null,\n touchCursorTimer = null,\n touchCursorVisible = false;\n const reducedMotion = matchMedia(\"(prefers-reduced-motion: reduce)\");\n const cursorImage = find(\"cursor-image\");\n let pending = [],\n sending = false,\n cx = 0.5,\n cy = 0.5,\n drag = false,\n start = null,\n last = null,\n multi = null,\n hold = null,\n moved = false;\n const pointers = new Map();\n const mouseButtons = find(\"mouse-buttons\"),\n wheel = find(\"mouse-wheel\"),\n wheelGrip = find(\"mouse-wheel-grip\");\n const keyboardInput = find(\"keyboard-input\");\n let keyboardEnabled = false,\n composing = false;\n const keyboardSentinel = \"\\u200b\";\n function resetKeyboard() {\n keyboardInput.value = keyboardSentinel;\n keyboardInput.setSelectionRange(1, 1);\n }\n function showKeyboard(enabled) {\n keyboardEnabled = enabled && control;\n if (!keyboardEnabled) {\n composing = false;\n resetKeyboard();\n keyboardInput.blur();\n return;\n }\n if (composing) return;\n if (config.desktop && document.activeElement === keyboardInput) return;\n // Keep focus inside the native evaluateJavaScript call; WebKit can reject\n // keyboard presentation after requestAnimationFrame loses user activation.\n keyboardInput.blur();\n resetKeyboard();\n keyboardInput.focus({ preventScroll: true });\n }\n function keyboardKey(code) {\n if (!keyboardEnabled || !control) return;\n queue({ kind: \"key\", code, down: true });\n queue({ kind: \"key\", code, down: false });\n flush();\n }\n function commitKeyboard() {\n if (!keyboardEnabled || !control || composing) return;\n const text = keyboardInput.value.replace(keyboardSentinel, \"\");\n if (text) {\n for (let i = 0; i < text.length; i += 4096)\n queue({ kind: \"text\", text: text.slice(i, i + 4096) });\n flush();\n }\n resetKeyboard();\n }\n listen(keyboardInput, \"compositionstart\", () => {\n composing = true;\n });\n listen(keyboardInput, \"compositionend\", () => {\n composing = false;\n commitKeyboard();\n });\n listen(keyboardInput, \"input\", (e) => {\n if (!e.isComposing) commitKeyboard();\n });\n listen(keyboardInput, \"beforeinput\", (e) => {\n if (composing || e.isComposing) return;\n if (e.inputType === \"deleteContentBackward\") {\n e.preventDefault();\n keyboardKey(\"Backspace\");\n resetKeyboard();\n } else if (\n e.inputType === \"insertLineBreak\" ||\n e.inputType === \"insertParagraph\"\n ) {\n e.preventDefault();\n keyboardKey(\"Enter\");\n resetKeyboard();\n }\n });\n const heldMouse = new Map();\n let showMouseButtons = false,\n wheelY = null;\n function resetWheel() {\n wheelY = null;\n wheelGrip.style.transform = \"translateY(0px)\";\n }\n let statsTimer = null,\n statsSample = null;\n const post = (message) => postMessage({ ...message, epoch });\n const clamp = (v) => Math.max(0, Math.min(1, v));\n let fillHeight = false;\n let panAnimation = null,\n viewportRightInset = 0,\n viewportLeftInset = 0,\n viewportBottomInset = 0,\n keyboardViewportInset = 0;\n let keyboardViewportOpen = false;\n let cursorNeedsEntry = true,\n manualViewMoved = false,\n followRest = null;\n // Insets guide centering and pan limits without clipping the full-screen\n // video surface or adding an opaque strip beside the Dynamic Island.\n const viewportLeft = () => (fillHeight ? viewportLeftInset : 0);\n const viewportWidth = () =>\n Math.max(\n 1,\n stage.clientWidth -\n viewportLeft() -\n (fillHeight ? viewportRightInset : 0),\n );\n const layout = () => {\n const r = transform(\n viewportWidth(),\n stage.clientHeight,\n dw,\n dh,\n zoom,\n fx,\n fy,\n fillHeight,\n );\n return {\n ...r,\n x: viewportLeft() + viewportWidth() / 2 - fx * r.width,\n y: stage.clientHeight / 2 - fy * r.height,\n };\n };\n function panBounds(r) {\n const vw = viewportWidth(),\n vh = stage.clientHeight;\n // Grow extra resting travel continuously from zero at fit to 180 screen\n // points at maximum zoom. Never count the unused space of a fitting axis.\n const clearance = (180 * (Math.max(1, Math.min(5, zoom)) - 1)) / 4;\n const axis = (viewport, content) =>\n content <= viewport\n ? {\n min: (viewport - content) / 2 - clearance,\n max: (viewport - content) / 2 + clearance,\n }\n : { min: viewport - content - clearance, max: clearance };\n const x = axis(vw, r.width),\n y = axis(vh, r.height);\n const bounds = {\n minX: viewportLeft() + x.min,\n maxX: viewportLeft() + x.max,\n minY: y.min,\n maxY: y.max,\n };\n // A camera position required to expose the cursor is a valid resting point,\n // not elastic overshoot. Preserve it across the next pan/pinch handoff.\n if (followRest) {\n const restX = viewportLeft() + vw / 2 - followRest.fx * r.width,\n restY = vh / 2 - followRest.fy * r.height;\n const weight =\n followRest.zoom > 1 ? clamp((zoom - 1) / (followRest.zoom - 1)) : 1;\n bounds.minX += Math.min(0, restX - bounds.minX) * weight;\n bounds.maxX += Math.max(0, restX - bounds.maxX) * weight;\n bounds.minY += Math.min(0, restY - bounds.minY) * weight;\n bounds.maxY += Math.max(0, restY - bounds.maxY) * weight;\n }\n return bounds;\n }\n const bounded = (v, min, max) => Math.max(min, Math.min(max, v));\n // Convert displayed overshoot back to finger travel before the next delta,\n // so reversing direction unwinds smoothly without an edge jump.\n function rubber(v, min, max, inverse = false) {\n const edge = bounded(v, min, max),\n d = v - edge;\n const reach = 40 + 10 * (Math.max(1, Math.min(5, zoom)) - 1);\n return (\n edge +\n Math.sign(d) *\n (inverse\n ? (reach * Math.abs(d)) / Math.max(1, reach - Math.abs(d))\n : (reach * Math.abs(d)) / (reach + Math.abs(d)))\n );\n }\n function place(x, y, r) {\n fx = (viewportLeft() + viewportWidth() / 2 - x) / r.width;\n fy = (stage.clientHeight / 2 - y) / r.height;\n }\n function cursorViewport() {\n const hotX = remoteCursor?.hotX ?? 9,\n hotY = remoteCursor?.hotY ?? 9;\n const w = remoteCursor?.width ?? 18,\n h = remoteCursor?.height ?? 18;\n const left = viewportLeft(),\n right = left + viewportWidth(),\n bottom = Math.max(1, stage.clientHeight - viewportBottomInset);\n // Keep the entire cursor, including its hotspot offset, clear of chrome.\n const minX = Math.min(right - 1, left + 8 + hotX),\n minY = Math.min(bottom - 1, 8 + hotY);\n return {\n minX,\n maxX: Math.max(minX, right - 8 - (w - hotX)),\n minY,\n maxY: Math.max(minY, bottom - 8 - (h - hotY)),\n };\n }\n function moveTouchpad(dx, dy) {\n stopPanAnimation();\n const r = layout(),\n v = cursorViewport();\n if (cursorNeedsEntry) {\n // Re-enter at the nearest visible edge; do not pull a manually positioned\n // desktop back to the old off-screen cursor before applying this movement.\n cx = clamp((bounded(r.x + cx * r.width, v.minX, v.maxX) - r.x) / r.width);\n cy = clamp(\n (bounded(r.y + cy * r.height, v.minY, v.maxY) - r.y) / r.height,\n );\n cursorNeedsEntry = false;\n }\n cx = clamp(cx + dx / r.width);\n cy = clamp(cy + dy / r.height);\n const x = r.x + cx * r.width,\n y = r.y + cy * r.height;\n if (\n zoom > 1 ||\n r.width > viewportWidth() ||\n r.height > stage.clientHeight - viewportBottomInset\n ) {\n place(\n r.x + bounded(x, v.minX, v.maxX) - x,\n r.y + bounded(y, v.minY, v.maxY) - y,\n r,\n );\n followRest = { fx, fy, zoom };\n }\n manualViewMoved = false;\n }\n function stopPanAnimation() {\n if (panAnimation !== null) cancelAnimationFrame(panAnimation);\n panAnimation = null;\n }\n function settlePan() {\n stopPanAnimation();\n const r = layout(),\n b = panBounds(r),\n x = bounded(r.x, b.minX, b.maxX),\n y = bounded(r.y, b.minY, b.maxY);\n if (x === r.x && y === r.y) return;\n if (reducedMotion.matches) {\n place(x, y, r);\n render();\n return;\n }\n const started = performance.now();\n const tick = (now) => {\n const t = Math.min(1, (now - started) / 280),\n ease = 1 - Math.pow(1 - t, 3);\n place(r.x + (x - r.x) * ease, r.y + (y - r.y) * ease, r);\n render();\n panAnimation = t < 1 ? requestAnimationFrame(tick) : null;\n };\n panAnimation = requestAnimationFrame(tick);\n }\n function render() {\n const r = layout();\n const touchFeedback = control && mode === \"touch\" && touchCursorVisible;\n const cursorX = mode === \"touch\" && touchCursor ? touchCursor.x : cx;\n const cursorY = mode === \"touch\" && touchCursor ? touchCursor.y : cy;\n cursor.style.transition =\n mode === \"touch\" && !touchFeedback && !reducedMotion.matches\n ? \"opacity 120ms ease-out\"\n : \"none\";\n for (const el of [image, video]) {\n el.style.width = r.width + \"px\";\n el.style.height = r.height + \"px\";\n el.style.left = r.x + \"px\";\n el.style.top = r.y + \"px\";\n }\n if (remoteCursor) {\n cursor.style.width = remoteCursor.width + \"px\";\n cursor.style.height = remoteCursor.height + \"px\";\n cursor.style.border = \"0\";\n cursor.style.borderRadius = \"0\";\n cursor.style.transform = \"none\";\n cursor.style.left = r.x + cursorX * r.width - remoteCursor.hotX + \"px\";\n cursor.style.top = r.y + cursorY * r.height - remoteCursor.hotY + \"px\";\n cursor.style.display = \"block\";\n cursor.style.opacity = (\n mode === \"touch\"\n ? touchFeedback\n : remoteCursor.visible ||\n (control && mode === \"pointer\" && localCursorAwake)\n )\n ? \"1\"\n : \"0\";\n } else {\n cursor.style.opacity = (mode === \"touch\" ? !touchFeedback : false)\n ? \"0\"\n : \"1\";\n cursor.style.width = \"18px\";\n cursor.style.height = \"18px\";\n cursor.style.border = \"2px solid var(--foreground)\";\n cursor.style.borderRadius = \"50%\";\n cursor.style.transform = \"translate(-50%,-50%)\";\n cursor.style.left = r.x + cursorX * r.width + \"px\";\n cursor.style.top = r.y + cursorY * r.height + \"px\";\n cursor.style.display =\n !config.desktop && control && (mode === \"pointer\" || mode === \"touch\") ? \"block\" : \"none\";\n }\n if (config.desktop) stage.style.cursor = control ? \"none\" : \"default\";\n }\n // Literal source is required: Hermes function.toString() yields bytecode.\n function validCursor(v) {\n return (\n !!v &&\n typeof v === \"object\" &&\n typeof v.visible === \"boolean\" &&\n [v.x, v.y, v.width, v.height, v.hotX, v.hotY].every(Number.isFinite) &&\n v.x >= 0 &&\n v.x <= 1 &&\n v.y >= 0 &&\n v.y <= 1 &&\n v.width > 0 &&\n v.width <= 256 &&\n v.height > 0 &&\n v.height <= 256 &&\n v.hotX >= 0 &&\n v.hotX <= v.width &&\n v.hotY >= 0 &&\n v.hotY <= v.height &&\n typeof v.png === \"string\" &&\n v.png.length <= 65536 &&\n /^iVBORw0KGgo[A-Za-z0-9+/]*={0,2}$/.test(v.png)\n );\n }\n function receiveCursor(value) {\n if (value === null) {\n if (remoteCursor) {\n remoteCursor = { ...remoteCursor, visible: false };\n render();\n }\n return;\n }\n if (!validCursor(value)) return;\n if (!remoteCursor || remoteCursor.png !== value.png)\n cursorImage.src = \"data:image/png;base64,\" + value.png;\n // Keep delayed host coordinates from pulling the local touchpad backwards.\n if (\n !remoteCursor ||\n !control ||\n mode !== \"pointer\" ||\n (!pointers.size && performance.now() - lastLocalMove > 200)\n ) {\n cx = value.x;\n cy = value.y;\n }\n if (value.visible) localCursorAwake = false;\n remoteCursor = value;\n render();\n }\n function resetCursor() {\n clearTimeout(touchCursorTimer);\n touchCursorVisible = false;\n touchCursor = null;\n localCursorAwake = false;\n remoteCursor = null;\n cursorImage.removeAttribute(\"src\");\n render();\n }\n function queue(event) {\n if (!control) return;\n if (config.desktop && (event.kind === \"button\" || event.kind === \"scroll\"))\n flushClipboardModifier();\n // Remote visibility can remain hidden after synthetic mouse movement. Wake\n // the local touchpad cursor until the host reports a visible cursor again.\n if (event.kind === \"move\" && mode === \"pointer\") localCursorAwake = true;\n if (event.kind === \"key\" || event.kind === \"text\") {\n localCursorAwake = false;\n render();\n }\n if (event.kind === \"move\" || event.kind === \"button\")\n lastLocalMove = performance.now();\n const tail = pending[pending.length - 1];\n if (event.kind === \"move\" && tail?.kind === \"move\")\n pending[pending.length - 1] = event;\n // Coalesce only adjacent scrolls, preserving button/key ordering. Bound the\n // accumulated distance so a delayed ACK cannot replay a large scroll backlog.\n else if (event.kind === \"scroll\" && tail?.kind === \"scroll\") {\n tail.dx = Math.max(-2000, Math.min(2000, tail.dx + event.dx));\n tail.dy = Math.max(-2000, Math.min(2000, tail.dy + event.dy));\n } else pending.push(event);\n if (pending.length > 64) {\n pending = [{ kind: \"release\" }];\n control = false;\n post({ type: \"inputOverflow\" });\n }\n }\n function flush() {\n if (!pending.length || sending) return;\n const events = pending.splice(0, 64),\n sequence = ++seq;\n if (\n pc?.connectionState === \"connected\" &&\n dc &&\n dc.readyState === \"open\" &&\n dc.bufferedAmount < 16384\n ) {\n dc.send(JSON.stringify({ sequence, events }));\n } else {\n sending = true;\n post({ type: \"input\", sequence, events });\n }\n }\n const inputTimer = setInterval(() => {\n // Hold displacement controls speed, even without further pointer moves.\n // Skip missed ticks while awaiting ACK instead of building a scroll backlog.\n if (wheelY?.moved && !sending) {\n const offset = Math.max(-24, Math.min(24, wheelY.y - wheelY.start));\n if (Math.abs(offset) > 4)\n scrollMouse(Math.sign(offset) * (Math.abs(offset) - 4) * 1.5);\n }\n flush();\n }, 33);\n cleanups.push(() => clearInterval(inputTimer));\n function release() {\n deferredClipboardModifier = null;\n clearTimeout(hold);\n if (gestureFrame !== null) cancelAnimationFrame(gestureFrame);\n gestureFrame = null;\n if (control) {\n queue({ kind: \"release\" });\n flush();\n }\n heldMouse.clear();\n resetWheel();\n updateMouseButtons();\n drag = false;\n pointers.clear();\n start = null;\n last = null;\n multi = null;\n }\n function updateMouseButtons() {\n mouseButtons.style.display = showMouseButtons && control ? \"block\" : \"none\";\n for (const [name, button] of [\n [\"left\", 0],\n [\"right\", 2],\n ])\n find(\"mouse-\" + name).setAttribute(\n \"aria-pressed\",\n String(heldMouse.has(button)),\n );\n }\n for (const [name, button] of [\n [\"left\", 0],\n [\"right\", 2],\n ]) {\n const el = find(\"mouse-\" + name);\n listen(el, \"pointerdown\", (e) => {\n e.preventDefault();\n if (!control || !showMouseButtons || heldMouse.has(button)) return;\n clearTimeout(hold);\n el.setPointerCapture(e.pointerId);\n heldMouse.set(button, e.pointerId);\n queue({ kind: \"button\", button, down: true, x: cx, y: cy });\n flush();\n updateMouseButtons();\n });\n const up = (e) => {\n e.preventDefault();\n if (heldMouse.get(button) !== e.pointerId) return;\n heldMouse.delete(button);\n queue({ kind: \"button\", button, down: false, x: cx, y: cy });\n flush();\n updateMouseButtons();\n };\n listen(el, \"pointerup\", up);\n listen(el, \"pointercancel\", up);\n listen(el, \"lostpointercapture\", up);\n listen(el, \"click\", (e) => {\n if (e.detail === 0 && control && showMouseButtons) {\n click(button);\n flush();\n }\n });\n }\n const scrollMouse = (dy) => {\n if (control && showMouseButtons && Number.isFinite(dy)) {\n queue({ kind: \"scroll\", dx: 0, dy: Math.max(-2000, Math.min(2000, dy)) });\n }\n };\n listen(wheel, \"pointerdown\", (e) => {\n e.preventDefault();\n if (!control || !showMouseButtons || wheelY) return;\n wheel.setPointerCapture(e.pointerId);\n wheelY = { id: e.pointerId, y: e.clientY, start: e.clientY, moved: false };\n });\n // A small dead zone preserves middle clicks; displacement drives the timer.\n listen(wheel, \"pointermove\", (e) => {\n if (!wheelY || wheelY.id !== e.pointerId) return;\n e.preventDefault();\n wheelGrip.style.transform =\n \"translateY(\" +\n Math.max(-24, Math.min(24, e.clientY - wheelY.start)) +\n \"px)\";\n wheelY.y = e.clientY;\n if (Math.abs(e.clientY - wheelY.start) > 4) wheelY.moved = true;\n });\n listen(wheel, \"pointerup\", (e) => {\n if (!wheelY || wheelY.id !== e.pointerId) return;\n e.preventDefault();\n if (!wheelY.moved && control && showMouseButtons) {\n click(1);\n flush();\n }\n resetWheel();\n });\n const cancelWheel = (e) => {\n if (wheelY?.id === e.pointerId) resetWheel();\n };\n listen(wheel, \"pointercancel\", cancelWheel);\n listen(wheel, \"lostpointercapture\", cancelWheel);\n listen(wheel, \"click\", (e) => {\n if (e.detail === 0 && control && showMouseButtons) {\n click(1);\n flush();\n }\n });\n listen(wheel, \"keydown\", (e) => {\n if (e.key === \"ArrowUp\" || e.key === \"ArrowDown\") {\n e.preventDefault();\n scrollMouse(e.key === \"ArrowUp\" ? -120 : 120);\n }\n });\n function insideDesktop(p) {\n const r = layout();\n return (\n p.x >= r.x && p.x <= r.x + r.width && p.y >= r.y && p.y <= r.y + r.height\n );\n }\n function point(p) {\n const r = layout();\n return {\n x: clamp((p.x - r.x) / r.width),\n y: clamp((p.y - r.y) / r.height),\n };\n }\n const desktopPoint = (e) => {\n const bounds = stage.getBoundingClientRect();\n return { x: e.clientX - bounds.left, y: e.clientY - bounds.top };\n };\n function mouse(e, down) {\n if (!control || ![0, 1, 2].includes(e.button)) return;\n const local = desktopPoint(e);\n if (down && !insideDesktop(local)) return;\n const p = point(local);\n cx = p.x;\n cy = p.y;\n if (down) {\n showKeyboard(true);\n stage.setPointerCapture(e.pointerId);\n heldMouse.set(e.button, e.pointerId);\n } else if (!heldMouse.delete(e.button)) return;\n queue({ kind: \"button\", button: e.button, down, x: cx, y: cy });\n flush();\n render();\n }\n function click(button = 0) {\n if (control && mode === \"touch\") {\n clearTimeout(touchCursorTimer);\n touchCursor = { x: cx, y: cy };\n touchCursorVisible = true;\n render();\n touchCursorTimer = setTimeout(() => {\n touchCursorVisible = false;\n render();\n }, 450);\n }\n queue({ kind: \"button\", button, down: true, x: cx, y: cy });\n queue({ kind: \"button\", button, down: false, x: cx, y: cy });\n }\n function pan(dx, dy) {\n manualViewMoved = true;\n cursorNeedsEntry = true;\n stopPanAnimation();\n const r = layout(),\n b = panBounds(r);\n place(\n rubber(rubber(r.x, b.minX, b.maxX, true) + dx, b.minX, b.maxX),\n rubber(rubber(r.y, b.minY, b.maxY, true) + dy, b.minY, b.maxY),\n r,\n );\n render();\n }\n function pair() {\n const [a, b] = [...pointers.values()];\n return {\n x: (a.x + b.x) / 2,\n y: (a.y + b.y) / 2,\n d: Math.hypot(a.x - b.x, a.y - b.y),\n };\n }\n function movePair() {\n gestureFrame = null;\n if (pointers.size < 2 || !multi) return;\n const next = pair(),\n span = Math.abs(next.d - multi.d),\n travel = Math.hypot(next.x - multi.x, next.y - multi.y);\n if (multi.kind !== \"pinch\") {\n const pinchSlop = Math.max(6, Math.min(12, multi.d * 0.04));\n if (span >= pinchSlop && span > travel * 0.65) {\n // A parallel scroll may deliver its two pointer updates in different\n // frames. Confirm separation briefly before taking over as a pinch.\n if (!multi.candidate)\n multi.candidate = {\n at: Date.now(),\n d: next.d,\n zoom,\n anchor: point(next),\n };\n if (Date.now() - multi.candidate.at >= 40) {\n if (multi.kind === \"scroll\") {\n multi.d = multi.candidate.d;\n multi.zoom = multi.candidate.zoom;\n multi.anchor = multi.candidate.anchor;\n }\n multi.kind = \"pinch\";\n multi.candidate = null;\n } else {\n gestureFrame = requestAnimationFrame(movePair);\n return;\n }\n } else {\n multi.candidate = null;\n if (!multi.kind && travel > 8 && travel > span) multi.kind = \"scroll\";\n }\n }\n if (multi.kind === \"pinch\") {\n manualViewMoved = true;\n cursorNeedsEntry = true;\n zoom = Math.max(\n 1,\n Math.min(5, (multi.zoom * next.d) / Math.max(1, multi.d)),\n );\n const r = layout();\n fx =\n multi.anchor.x +\n (viewportLeft() + viewportWidth() / 2 - next.x) / r.width;\n fy = multi.anchor.y + (stage.clientHeight / 2 - next.y) / r.height;\n const moved = layout(),\n b = panBounds(moved);\n place(\n rubber(moved.x, b.minX, b.maxX),\n rubber(moved.y, b.minY, b.maxY),\n moved,\n );\n } else if (multi.kind === \"scroll\") {\n const dx = next.x - multi.lastX,\n dy = next.y - multi.lastY;\n if (control && mode !== \"pan\")\n queue({\n kind: \"scroll\",\n dx: Math.max(-2000, Math.min(2000, -dx)),\n dy: Math.max(-2000, Math.min(2000, -dy)),\n });\n else pan(dx, dy);\n }\n if (multi.kind) {\n multi.lastX = next.x;\n multi.lastY = next.y;\n }\n render();\n }\n listen(stage, \"pointerdown\", (e) => {\n if (config.desktop) {\n e.preventDefault();\n mouse(e, true);\n return;\n }\n e.preventDefault();\n stopPanAnimation();\n stage.setPointerCapture(e.pointerId);\n pointers.set(e.pointerId, { x: e.clientX, y: e.clientY });\n if (pointers.size === 1) {\n cursorNeedsEntry = true;\n start = { x: e.clientX, y: e.clientY };\n last = start;\n moved = false;\n hold = setTimeout(() => {\n if (\n control &&\n mode !== \"pan\" &&\n !moved &&\n pointers.size === 1 &&\n !heldMouse.size &&\n (mode !== \"touch\" || insideDesktop(start))\n ) {\n if (mode === \"touch\") {\n const p = point(start);\n cx = p.x;\n cy = p.y;\n }\n queue({ kind: \"button\", button: 0, down: true, x: cx, y: cy });\n drag = true;\n }\n }, 400);\n } else {\n clearTimeout(hold);\n if (drag) queue({ kind: \"release\" });\n drag = false;\n start = null;\n const p = pair();\n multi = {\n ...p,\n lastX: p.x,\n lastY: p.y,\n zoom,\n anchor: point(p),\n kind: null,\n };\n }\n });\n listen(stage, \"pointermove\", (e) => {\n if (config.desktop) {\n if (!control) return;\n const local = desktopPoint(e);\n if (!heldMouse.size && !insideDesktop(local)) return;\n const p = point(local);\n cx = p.x;\n cy = p.y;\n queue({ kind: \"move\", x: cx, y: cy });\n render();\n return;\n }\n if (!pointers.has(e.pointerId)) return;\n e.preventDefault();\n pointers.set(e.pointerId, { x: e.clientX, y: e.clientY });\n if (pointers.size >= 2) {\n if (gestureFrame === null) gestureFrame = requestAnimationFrame(movePair);\n return;\n }\n if (!start || !last) return;\n const now = { x: e.clientX, y: e.clientY },\n dx = now.x - last.x,\n dy = now.y - last.y;\n last = now;\n if (Math.hypot(now.x - start.x, now.y - start.y) > 6) {\n moved = true;\n clearTimeout(hold);\n }\n if (\n !control ||\n mode === \"pan\" ||\n (mode === \"touch\" && !drag && !heldMouse.size)\n ) {\n pan(dx, dy);\n return;\n }\n if (mode === \"pointer\") {\n moveTouchpad(dx, dy);\n } else {\n const p = point(now);\n cx = p.x;\n cy = p.y;\n }\n queue({ kind: \"move\", x: cx, y: cy });\n render();\n });\n function up(e) {\n if (config.desktop) {\n mouse(e, false);\n return;\n }\n if (!pointers.has(e.pointerId)) return;\n clearTimeout(hold);\n if (pointers.size >= 2 && multi) {\n pointers.set(e.pointerId, { x: e.clientX, y: e.clientY });\n if (gestureFrame !== null) cancelAnimationFrame(gestureFrame);\n movePair();\n if (gestureFrame !== null) cancelAnimationFrame(gestureFrame);\n gestureFrame = null;\n }\n pointers.delete(e.pointerId);\n if (drag) {\n queue({ kind: \"button\", button: 0, down: false, x: cx, y: cy });\n drag = false;\n } else if (\n start &&\n !moved &&\n control &&\n mode !== \"pan\" &&\n !heldMouse.size &&\n e.type === \"pointerup\" &&\n (mode !== \"touch\" || insideDesktop({ x: e.clientX, y: e.clientY }))\n ) {\n if (mode === \"touch\") {\n const p = point({ x: e.clientX, y: e.clientY });\n cx = p.x;\n cy = p.y;\n }\n click();\n }\n start = null;\n last = null;\n multi = null;\n render();\n if (!pointers.size && manualViewMoved) settlePan();\n }\n listen(stage, \"pointerup\", up);\n listen(stage, \"pointercancel\", () => {\n release();\n settlePan();\n });\n listen(stage, \"lostpointercapture\", () => {\n if (config.desktop && heldMouse.size) release();\n });\n if (config.desktop)\n listen(\n stage,\n \"wheel\",\n (e) => {\n e.preventDefault();\n if (!control) return;\n const factor =\n e.deltaMode === 1 ? 16 : e.deltaMode === 2 ? stage.clientHeight : 1;\n queue({\n kind: \"scroll\",\n dx: Math.max(-2000, Math.min(2000, e.deltaX * factor)),\n dy: Math.max(-2000, Math.min(2000, e.deltaY * factor)),\n });\n flush();\n },\n { passive: false },\n );\n listen(config.desktop ? stage : document, \"contextmenu\", (e) =>\n e.preventDefault(),\n );\n // Scope selection suppression to the desktop surface, preserving the hidden\n // textarea's native keyboard/IME editing and programmatic selection.\n listen(stage, \"selectstart\", (e) => e.preventDefault());\n listen(stage, \"dragstart\", (e) => e.preventDefault());\n const normalizedKey = (code) =>\n /^(Shift|Control|Alt|Meta)Right$/.test(code)\n ? code.replace(/Right$/, \"Left\")\n : code;\n const hardwareKeys = new Set();\n function flushClipboardModifier() {\n if (!deferredClipboardModifier) return;\n const code = deferredClipboardModifier;\n deferredClipboardModifier = null;\n hardwareKeys.add(code);\n queue({ kind: \"key\", code, down: true });\n }\n listen(document, \"keydown\", (e) => {\n if (config.desktop) {\n if (e.ctrlKey && e.altKey && e.code === \"Escape\") {\n e.preventDefault();\n release();\n showKeyboard(false);\n post({ type: \"releaseFocus\" });\n return;\n }\n if (\n document.activeElement !== keyboardInput ||\n e.isComposing ||\n composing\n )\n return;\n if ((e.metaKey || e.ctrlKey) && e.code === \"KeyW\") return; // always retain a local close shortcut\n if (\n control &&\n clipboardShortcuts &&\n normalizedKey(e.code) === (clipboardModifier === \"meta\" ? \"MetaLeft\" : \"ControlLeft\")\n ) {\n e.preventDefault();\n if (!hardwareKeys.has(normalizedKey(e.code)))\n deferredClipboardModifier = normalizedKey(e.code);\n return;\n }\n if (\n control &&\n clipboardShortcuts &&\n (clipboardModifier === \"meta\" ? e.metaKey && !e.ctrlKey : e.ctrlKey && !e.metaKey) &&\n !e.altKey &&\n !e.shiftKey &&\n (e.code === \"KeyC\" || e.code === \"KeyV\")\n ) {\n e.preventDefault();\n if (!e.repeat) {\n release();\n hardwareKeys.clear();\n post({ type: \"clipboard\", action: e.code === \"KeyC\" ? \"copy\" : \"paste\" });\n }\n return;\n }\n if (\n !e.ctrlKey &&\n !e.metaKey &&\n !e.altKey &&\n (e.key?.length === 1 || e.key === \"Process\" || e.key === \"Dead\")\n )\n return;\n }\n if (control && validKeys.has(normalizedKey(e.code))) {\n e.preventDefault();\n flushClipboardModifier();\n hardwareKeys.add(normalizedKey(e.code));\n queue({ kind: \"key\", code: normalizedKey(e.code), down: true });\n }\n });\n listen(document, \"keyup\", (e) => {\n const code = normalizedKey(e.code);\n if (config.desktop && control && deferredClipboardModifier === code) {\n flushClipboardModifier();\n }\n if (config.desktop && !hardwareKeys.delete(code)) return;\n if (control && validKeys.has(code)) {\n e.preventDefault();\n queue({ kind: \"key\", code, down: false });\n }\n });\n if (config.desktop)\n listen(keyboardInput, \"blur\", () => {\n hardwareKeys.clear();\n release();\n });\n listen(window, \"blur\", () => {\n release();\n settlePan();\n });\n const observer = new ResizeObserver(() => {\n release();\n settlePan();\n render();\n });\n observer.observe(stage);\n cleanups.push(\n () => observer.disconnect(),\n () => stopPanAnimation(),\n () => clearTimeout(touchCursorTimer),\n );\n // Keep one bounded frame in this document only; never persist desktop pixels.\n // Snapshot before detaching the track. A JPEG fallback already lives in image.\n function retainFrame() {\n if (\n !videoPresented ||\n video.style.display !== \"block\" ||\n video.readyState < 2 ||\n !video.videoWidth ||\n !video.videoHeight\n )\n return;\n const canvas = document.createElement(\"canvas\");\n try {\n const scale = Math.min(\n 1,\n 1280 / Math.max(video.videoWidth, video.videoHeight),\n );\n canvas.width = Math.max(1, Math.round(video.videoWidth * scale));\n canvas.height = Math.max(1, Math.round(video.videoHeight * scale));\n const context = canvas.getContext(\"2d\");\n if (context) {\n context.drawImage(video, 0, 0, canvas.width, canvas.height);\n image.src = canvas.toDataURL(\"image/jpeg\", 0.7);\n }\n } catch {\n /* Keep the previous compatibility frame if WebKit cannot read video. */\n } finally {\n canvas.width = 0;\n canvas.height = 0;\n }\n }\n /* BEGIN RTC */\n\n let trickleIce = false,\n attemptId = null,\n retries = 0,\n exchangeId = 0;\n let configPending = false;\n let videoPresented = false,\n videoFrameCallback = null,\n videoPaintCallback = null;\n function cancelVideoHandoff() {\n if (videoFrameCallback !== null)\n video.cancelVideoFrameCallback?.(videoFrameCallback);\n if (videoPaintCallback !== null) cancelAnimationFrame(videoPaintCallback);\n videoFrameCallback = videoPaintCallback = null;\n }\n function revealVideo(g) {\n post({ type: \"videoFrameReady\", attemptId });\n // Keep the JPEG decoded and painted as a backing layer. Promote the video\n // only after its first frame; removing the image exposes WebKit repaint gaps.\n videoPaintCallback = requestAnimationFrame(() => {\n if (g !== generation) return;\n videoPaintCallback = requestAnimationFrame(() => {\n if (g !== generation) return;\n videoPaintCallback = null;\n videoPresented = true;\n video.style.zIndex = \"2\";\n post({ type: \"streaming\", attemptId });\n post({\n type: \"pipCapability\",\n supported: !!(\n video.webkitSupportsPresentationMode?.(\"picture-in-picture\") ||\n document.pictureInPictureEnabled\n ),\n });\n });\n });\n }\n function waitForVideoFrame(g) {\n if (g !== generation) return;\n if (\n video.readyState >= 2 &&\n video.videoWidth > 0 &&\n video.videoHeight > 0\n ) {\n revealVideo(g);\n return;\n }\n videoPaintCallback = requestAnimationFrame(() => waitForVideoFrame(g));\n }\n let retryTimer = null,\n deadlineTimer = null,\n disconnectTimer = null,\n stableTimer = null,\n iceTimer = null,\n gatherTimer = null,\n gatherDone = null;\n let localCandidates = [],\n localAck = 0,\n remoteAfter = 0,\n icePending = null,\n remoteSeen = new Set(),\n exchangeUntil = 0,\n remoteComplete = false;\n function clearRtcTimers() {\n for (const timer of [\n retryTimer,\n deadlineTimer,\n disconnectTimer,\n stableTimer,\n iceTimer,\n gatherTimer,\n ])\n clearTimeout(timer);\n retryTimer =\n deadlineTimer =\n disconnectTimer =\n stableTimer =\n iceTimer =\n gatherTimer =\n null;\n if (gatherDone) {\n gatherDone();\n gatherDone = null;\n }\n }\n function closeRtc(preserveFrame = true) {\n configPending = false;\n if (preserveFrame) retainFrame();\n generation++;\n clearRtcTimers();\n cancelVideoHandoff();\n videoPresented = false;\n localCandidates = [];\n localAck = remoteAfter = 0;\n icePending = null;\n remoteSeen = new Set();\n attemptId = null;\n clearInterval(statsTimer);\n statsTimer = null;\n statsSample = null;\n if (dc) dc.close();\n if (pc) pc.close();\n pc = null;\n dc = null;\n video.onplaying = null;\n video.srcObject = null;\n video.style.display = \"none\";\n video.style.zIndex = \"0\";\n image.style.display = \"block\";\n if (!preserveFrame) image.removeAttribute(\"src\");\n }\n function failRtc(reason, canRetry = true) {\n const failedAttempt = attemptId;\n release();\n closeRtc();\n post({ type: \"fallback\", attemptId: failedAttempt, reason });\n if (canRetry && epoch && retries < net.retryMs.length) {\n const delay = net.retryMs[retries++];\n retryTimer = setTimeout(() => {\n retryTimer = null;\n if (epoch) connect();\n }, delay);\n }\n }\n function pollIce() {\n if (\n !pc ||\n !trickleIce ||\n !pc.remoteDescription ||\n icePending ||\n Date.now() >= exchangeUntil\n )\n return;\n const candidates = localCandidates.slice(\n localAck,\n localAck + net.batchSize,\n );\n icePending = {\n after: remoteAfter,\n sent: candidates.length,\n exchangeId: ++exchangeId,\n };\n post({\n type: \"ice\",\n attemptId,\n candidates,\n after: remoteAfter,\n exchangeId,\n });\n iceTimer = setTimeout(() => {\n icePending = null;\n pollIce();\n }, 4500);\n }\n async function receiveIce(message) {\n if (\n !pc ||\n !icePending ||\n message.attemptId !== attemptId ||\n message.exchangeId !== icePending.exchangeId\n )\n return;\n const rtc = pc,\n g = generation,\n batch = icePending;\n clearTimeout(iceTimer);\n try {\n if (message.error) {\n icePending = null;\n iceTimer = setTimeout(pollIce, 1000);\n return;\n }\n if (\n !Array.isArray(message.candidates) ||\n message.candidates.length > net.batchSize ||\n message.next !== batch.after + message.candidates.length ||\n message.next > net.maxCandidates\n )\n throw new Error();\n for (const candidate of message.candidates) {\n if (g !== generation) return;\n const key = JSON.stringify(candidate);\n if (remoteSeen.has(key)) continue;\n if (remoteSeen.size >= net.maxCandidates) throw new Error();\n await rtc.addIceCandidate(candidate);\n if (g !== generation) return;\n remoteSeen.add(key);\n }\n if (g !== generation) return;\n localAck += batch.sent;\n remoteAfter = message.next;\n remoteComplete = message.complete === true;\n icePending = null;\n if (!(\n remoteComplete &&\n rtc.iceGatheringState === \"complete\" &&\n localAck === localCandidates.length\n ))\n iceTimer = setTimeout(pollIce, net.pollMs);\n } catch {\n if (g === generation) failRtc(\"candidates\");\n }\n }\n async function receiveAnswer(message) {\n if (!pc || message.attemptId !== attemptId) return;\n const rtc = pc,\n g = generation;\n try {\n await rtc.setRemoteDescription({ type: \"answer\", sdp: message.sdp });\n if (g !== generation) return;\n clearTimeout(deadlineTimer);\n deadlineTimer = setTimeout(() => {\n if (g === generation && rtc.connectionState !== \"connected\")\n failRtc(\"connect-timeout\");\n }, net.connectMs);\n exchangeUntil = Date.now() + net.exchangeMs;\n pollIce();\n } catch {\n if (g === generation) failRtc(\"answer\");\n }\n }\n async function connect() {\n closeRtc();\n const g = generation;\n attemptId = String(g);\n if (!window.RTCPeerConnection) {\n failRtc(\"unsupported\", false);\n return;\n }\n configPending = true;\n deadlineTimer = setTimeout(() => {\n if (g === generation && configPending) {\n configPending = false;\n void startRtc(g, iceServers);\n }\n }, 3500);\n return post({ type: \"iceConfig\", attemptId });\n }\n async function receiveIceConfig(message) {\n if (!epoch || !configPending || message.attemptId !== attemptId) return;\n configPending = false;\n clearTimeout(deadlineTimer);\n deadlineTimer = null;\n await startRtc(generation, message.iceServers);\n }\n async function startRtc(g, servers) {\n try {\n const rtc = new RTCPeerConnection({ iceServers: servers });\n pc = rtc;\n dc = rtc.createDataChannel(\"input-v1\");\n rtc.addTransceiver(\"video\", { direction: \"recvonly\" });\n rtc.addTransceiver(\"audio\", { direction: \"recvonly\" });\n rtc.onicecandidate = ({ candidate }) => {\n if (g !== generation || !candidate?.candidate || !trickleIce) return;\n if (localCandidates.length >= net.maxCandidates) {\n failRtc(\"candidate-limit\", false);\n return;\n }\n localCandidates.push({\n candidate: candidate.candidate,\n sdpMid: candidate.sdpMid,\n sdpMLineIndex: candidate.sdpMLineIndex,\n ...(candidate.usernameFragment\n ? { usernameFragment: candidate.usernameFragment }\n : {}),\n });\n // Gathering continues after the offer; the serial exchange flushes late addresses.\n };\n dc.onmessage = (e) => {\n try {\n if (g !== generation || pc !== rtc) return;\n if (typeof e.data !== \"string\" || e.data.length > 80000) return;\n const message = JSON.parse(e.data);\n if (message.type === \"cursor\") receiveCursor(message.cursor);\n if (\n (video.webkitPresentationMode === \"picture-in-picture\" ||\n document.pictureInPictureElement === video) &&\n message.type === \"viewPing\" &&\n typeof message.challenge === \"string\" &&\n message.challenge.length <= 64 &&\n dc?.readyState === \"open\"\n )\n dc.send(message.challenge);\n } catch {}\n };\n let readingStats = false;\n statsTimer = setInterval(async () => {\n if (readingStats || g !== generation) return;\n readingStats = true;\n try {\n const stats = await rtc.getStats();\n if (g !== generation || pc !== rtc) return;\n const result = networkStats(stats, statsSample);\n statsSample = result.sample;\n post({\n type: \"network\",\n transport: result.transport,\n bytesPerSecond: result.bytesPerSecond,\n latencyMs: result.latencyMs,\n });\n } catch {\n } finally {\n readingStats = false;\n }\n }, 1000);\n rtc.ontrack = (e) => {\n if (g !== generation) return;\n video.srcObject = e.streams[0] || new MediaStream([e.track]);\n video.play().catch(() => {});\n };\n video.onplaying = () => {\n if (\n g !== generation ||\n videoPresented ||\n videoFrameCallback !== null ||\n videoPaintCallback !== null\n )\n return;\n video.style.display = \"block\";\n render();\n if (typeof video.requestVideoFrameCallback === \"function\") {\n videoFrameCallback = video.requestVideoFrameCallback(() => {\n if (g !== generation) return;\n videoFrameCallback = null;\n revealVideo(g);\n });\n } else waitForVideoFrame(g);\n };\n rtc.onconnectionstatechange = () => {\n if (g !== generation) return;\n if ([\"failed\", \"closed\"].includes(rtc.connectionState)) {\n failRtc(\"transport\");\n return;\n }\n if (rtc.connectionState === \"disconnected\") {\n clearTimeout(stableTimer);\n stableTimer = null;\n if (!disconnectTimer) {\n release();\n post({ type: \"reconnecting\", attemptId });\n disconnectTimer = setTimeout(() => {\n if (g === generation) failRtc(\"disconnected\");\n }, net.disconnectedMs);\n }\n } else if (rtc.connectionState === \"connected\") {\n clearTimeout(disconnectTimer);\n disconnectTimer = null;\n clearTimeout(deadlineTimer);\n if (!stableTimer)\n stableTimer = setTimeout(() => {\n if (g === generation) retries = 0;\n }, net.stableMs);\n if (videoPresented) post({ type: \"streaming\", attemptId });\n }\n };\n await rtc.setLocalDescription(await rtc.createOffer());\n if (!trickleIce)\n await new Promise((resolve) => {\n if (rtc.iceGatheringState === \"complete\") return resolve();\n gatherDone = resolve;\n gatherTimer = setTimeout(resolve, net.legacyGatherMs);\n rtc.onicegatheringstatechange = () => {\n if (rtc.iceGatheringState === \"complete\") {\n clearTimeout(gatherTimer);\n resolve();\n }\n };\n });\n if (g !== generation) return;\n post({ type: \"offer\", attemptId, sdp: rtc.localDescription.sdp });\n deadlineTimer = setTimeout(() => {\n if (g === generation) failRtc(\"answer-timeout\");\n }, net.answerMs);\n } catch {\n if (g === generation) failRtc(\"setup\");\n }\n }\n\n /* END RTC */\n function reportPresentation() {\n post({\n type: \"presentation\",\n active:\n video.webkitPresentationMode === \"picture-in-picture\" ||\n document.pictureInPictureElement === video,\n });\n }\n listen(video, \"webkitpresentationmodechanged\", reportPresentation);\n listen(video, \"enterpictureinpicture\", reportPresentation);\n listen(video, \"leavepictureinpicture\", reportPresentation);\n function receive(event) {\n let message;\n try {\n message = JSON.parse(event.data);\n } catch {\n return;\n }\n switch (message.type) {\n case \"presentation\":\n try {\n if (message.enabled) {\n release();\n showKeyboard(false);\n if (video.webkitSupportsPresentationMode?.(\"picture-in-picture\"))\n video.webkitSetPresentationMode(\"picture-in-picture\");\n else if (video.requestPictureInPicture)\n video\n .requestPictureInPicture()\n .catch(() => post({ type: \"presentationFailed\" }));\n else post({ type: \"presentationFailed\" });\n } else if (video.webkitSetPresentationMode)\n video.webkitSetPresentationMode(\"inline\");\n else if (document.pictureInPictureElement)\n document.exitPictureInPicture().catch(() => {});\n } catch {\n post({ type: \"presentationFailed\" });\n }\n break;\n case \"videoSettings\":\n video.muted = !message.audio;\n retries = 0;\n connect();\n break;\n case \"keyboard\":\n showKeyboard(message.enabled === true);\n break;\n case \"resume\":\n if (video.srcObject) video.play().catch(() => {});\n break;\n case \"releaseInput\":\n release();\n break;\n case \"theme\":\n if (/^#[0-9a-f]{3,8}$/i.test(message.surface)) {\n document.body.style.background = message.surface;\n document.documentElement.style.background = message.surface;\n document.documentElement.style.setProperty(\n \"--surface\",\n message.surface,\n );\n }\n if (/^#[0-9a-f]{3,8}$/i.test(message.foreground)) {\n cursor.style.borderColor = message.foreground;\n document.documentElement.style.setProperty(\n \"--foreground\",\n message.foreground,\n );\n }\n break;\n case \"mouseButtons\": {\n const before = layout(),\n keyboardOpen = fillHeight && message.keyboardOpen === true;\n const keepHorizontal = fillHeight && (keyboardOpen || keyboardViewportOpen);\n // Opening is observable before the native keyboard has a measured height.\n keyboardViewportOpen = keyboardOpen;\n let sideInsetsChanged = false;\n if (Number.isFinite(message.bottomInset))\n viewportBottomInset = Math.max(\n 0,\n Math.min(stage.clientHeight - 1, message.bottomInset),\n );\n if (Number.isFinite(message.leftInset)) {\n const inset = Math.max(\n 0,\n Math.min(stage.clientWidth - 1, message.leftInset),\n );\n sideInsetsChanged = sideInsetsChanged || inset !== viewportLeftInset;\n viewportLeftInset = inset;\n }\n if (Number.isFinite(message.rightInset)) {\n const inset = Math.max(\n 0,\n Math.min(stage.clientWidth - 1, message.rightInset),\n );\n sideInsetsChanged = sideInsetsChanged || inset !== viewportRightInset;\n viewportRightInset = inset;\n }\n // Hiding the side toolbar changes the viewport center. Keep the image's\n // actual horizontal position instead of following that center sideways.\n if (keepHorizontal) {\n stopPanAnimation();\n const r = layout();\n place(before.x, r.y, r);\n }\n const keyboardInset = keyboardOpen ? viewportBottomInset : 0;\n if (keyboardInset !== keyboardViewportInset) {\n const wasOpen = keyboardViewportInset > 0;\n keyboardViewportInset = keyboardInset;\n if (fillHeight && (keyboardInset > 0 || wasOpen)) {\n const r = layout(),\n v = cursorViewport();\n place(r.x, (v.minY + v.maxY) / 2 - cy * r.height, r);\n cursorNeedsEntry = false;\n manualViewMoved = false;\n }\n }\n if (keepHorizontal) {\n // Restoring the toolbar can cover the cursor even after a zero-height opening.\n if (!keyboardOpen || keyboardInset > 0) {\n const r = layout(),\n v = cursorViewport(),\n x = r.x + cx * r.width;\n place(r.x + bounded(x, v.minX, v.maxX) - x, r.y, r);\n }\n followRest = { fx, fy, zoom };\n render();\n } else if (sideInsetsChanged) {\n settlePan();\n render();\n }\n if (!message.enabled) {\n for (const button of heldMouse.keys())\n queue({ kind: \"button\", button, down: false, x: cx, y: cy });\n heldMouse.clear();\n resetWheel();\n flush();\n }\n for (const [edge, value] of [\n [\"bottom\", message.bottomInset],\n [\"right\", message.rightInset],\n ])\n if (Number.isFinite(value) && value >= 0 && value <= 4096)\n mouseButtons.style[edge] = value + \"px\";\n showMouseButtons = message.enabled === true;\n for (const name of [\"left\", \"right\", \"wheel\"])\n if (typeof message.labels?.[name] === \"string\")\n find(\"mouse-\" + name).setAttribute(\"aria-label\", message.labels[name]);\n updateMouseButtons();\n break;\n }\n case \"init\":\n followRest = null;\n cursorNeedsEntry = true;\n stopPanAnimation();\n resetCursor();\n control = false;\n release();\n pending = [];\n sending = false;\n seq = 0;\n epoch = message.epoch;\n clipboardShortcuts = config.desktop && message.clipboardShortcuts === true;\n clipboardModifier = message.clipboardModifier === \"meta\" ? \"meta\" : \"control\";\n dw = message.width;\n dh = message.height;\n fillHeight = message.fillHeight === true;\n video.muted = !message.audio;\n trickleIce = message.trickleIce === true;\n retries = 0;\n render();\n connect();\n break;\n case \"viewport\":\n fillHeight = message.fillHeight === true;\n release();\n render();\n break;\n case \"answer\":\n if (message.epoch === epoch) void receiveAnswer(message);\n break;\n case \"iceConfig\":\n if (message.epoch === epoch) void receiveIceConfig(message);\n break;\n case \"ice\":\n if (message.epoch === epoch) void receiveIce(message);\n break;\n case \"fallback\":\n if (message.epoch === epoch && message.attemptId === attemptId)\n failRtc(\"host\", message.retry !== false);\n break;\n case \"frame\": {\n if (\"cursor\" in message) receiveCursor(message.cursor);\n else resetCursor();\n const frameEpoch = epoch;\n image.onload = () => {\n if (epoch === frameEpoch) post({ type: \"framePresented\" });\n };\n image.src = \"data:image/jpeg;base64,\" + message.jpeg;\n break;\n }\n case \"control\":\n release();\n control = message.enabled;\n if (!control) showKeyboard(false);\n pending = [];\n updateMouseButtons();\n render();\n break;\n case \"mode\":\n release();\n if (message.mode !== \"pointer\") followRest = null;\n settlePan();\n clearTimeout(touchCursorTimer);\n touchCursorVisible = false;\n mode = message.mode;\n render();\n break;\n case \"fit\":\n followRest = null;\n cursorNeedsEntry = true;\n manualViewMoved = false;\n stopPanAnimation();\n zoom = 1;\n fx = 0.5;\n fy = 0.5;\n render();\n break;\n case \"rightClick\":\n click(2);\n break;\n case \"events\":\n for (const e of message.events) queue(e);\n flush();\n break;\n case \"ack\":\n if (message.epoch === epoch && message.sequence === seq)\n sending = false;\n break;\n case \"stop\":\n showKeyboard(false);\n control = false;\n release();\n pending = [];\n sending = false;\n epoch = null;\n closeRtc(message.preserveFrame === true);\n updateMouseButtons();\n render();\n break;\n }\n }\n render();\n post({ type: \"ready\" });\n return {\n receive: (message) => receive({ data: JSON.stringify(message) }),\n dispose: () => {\n release();\n epoch = null;\n closeRtc(false);\n for (const cleanup of cleanups) cleanup();\n },\n };\n}\n";+export const DESKTOP_VIEWER_SOURCE = "/** Shared browser viewer. Mobile bundles this exact source; Desktop imports it as a CSP-safe module. */\nfunction mountRemoteDesktopViewer(root, postMessage, config) {\n const cleanups = [];\n const listen = (target, type, fn, options) => {\n target.addEventListener(type, fn, options);\n cleanups.push(() => target.removeEventListener(type, fn, options));\n };\n const find = (id) =>\n root.getElementById\n ? root.getElementById(id)\n : root.querySelector(\"#\" + id);\n const { net, iceServers } = config;\n const validKeys = new Set(config.keyCodes);\n const transform =\n /* BEGIN TRANSFORM */\n function desktopTransform(\n vw,\n vh,\n dw,\n dh,\n zoom,\n fx,\n fy,\n fillHeight = false,\n ) {\n const scale =\n (fillHeight\n ? vh / Math.max(1, dh)\n : Math.min(vw / Math.max(1, dw), vh / Math.max(1, dh))) *\n Math.max(1, Math.min(5, zoom));\n const width = dw * scale;\n const height = dh * scale;\n const x =\n width <= vw\n ? (vw - width) / 2\n : Math.min(0, Math.max(vw - width, vw / 2 - fx * width));\n const y =\n height <= vh\n ? (vh - height) / 2\n : Math.min(0, Math.max(vh - height, vh / 2 - fy * height));\n return { x, y, width, height, scale };\n };\n /* END TRANSFORM */ const networkStats =\n /* BEGIN NETWORK_STATS */\n function networkStats(stats, previous) {\n const rows = [...stats.values()];\n const video = rows.find(\n (s) =>\n s.type === \"inbound-rtp\" &&\n (s.kind === \"video\" || s.mediaType === \"video\"),\n );\n const transport = rows.find(\n (s) => s.type === \"transport\" && s.selectedCandidatePairId,\n );\n const pair = transport\n ? stats.get(transport.selectedCandidatePairId)\n : rows.find(\n (s) =>\n s.type === \"candidate-pair\" &&\n s.state === \"succeeded\" &&\n s.nominated,\n );\n const local = pair && stats.get(pair.localCandidateId),\n remote = pair && stats.get(pair.remoteCandidateId);\n const direct = [\"host\", \"srflx\", \"prflx\"];\n const route =\n local?.candidateType === \"relay\" || remote?.candidateType === \"relay\"\n ? \"relay\"\n : direct.includes(local?.candidateType) &&\n direct.includes(remote?.candidateType)\n ? \"direct\"\n : \"video\";\n const sample =\n video &&\n Number.isFinite(video.bytesReceived) &&\n Number.isFinite(video.timestamp)\n ? {\n id: video.id,\n path: pair?.id,\n bytes: video.bytesReceived,\n time: video.timestamp,\n }\n : null;\n let bytesPerSecond = null;\n if (\n sample &&\n previous &&\n sample.id === previous.id &&\n sample.path === previous.path &&\n sample.time > previous.time &&\n sample.bytes >= previous.bytes\n )\n bytesPerSecond =\n ((sample.bytes - previous.bytes) * 1000) /\n (sample.time - previous.time);\n const latencyMs =\n Number.isFinite(pair?.currentRoundTripTime) &&\n pair.currentRoundTripTime >= 0\n ? pair.currentRoundTripTime * 1000\n : null;\n return { transport: route, bytesPerSecond, latencyMs, sample };\n };\n\n /* END NETWORK_STATS */ const stage = find(\"stage\"),\n image = find(\"image\"),\n video = find(\"video\"),\n cursor = find(\"cursor\");\n let dw = 1920,\n dh = 1080,\n viewerSized = false,\n zoom = 1,\n fx = 0.5,\n fy = 0.5,\n mode = \"pointer\",\n control = false,\n clipboardShortcuts = false,\n clipboardModifier = \"control\",\n deferredClipboardModifier = null,\n pc = null,\n dc = null,\n seq = 0,\n generation = 0,\n epoch = null,\n gestureFrame = null;\n let remoteCursor = null,\n lastLocalMove = 0,\n localCursorAwake = false;\n let touchCursor = null,\n touchCursorTimer = null,\n touchCursorVisible = false;\n const reducedMotion = matchMedia(\"(prefers-reduced-motion: reduce)\");\n const cursorImage = find(\"cursor-image\");\n let pending = [],\n sending = false,\n cx = 0.5,\n cy = 0.5,\n drag = false,\n start = null,\n last = null,\n multi = null,\n hold = null,\n moved = false;\n const pointers = new Map();\n const mouseButtons = find(\"mouse-buttons\"),\n wheel = find(\"mouse-wheel\"),\n wheelGrip = find(\"mouse-wheel-grip\");\n const keyboardInput = find(\"keyboard-input\");\n let keyboardEnabled = false,\n composing = false;\n const keyboardSentinel = \"\\u200b\";\n function resetKeyboard() {\n keyboardInput.value = keyboardSentinel;\n keyboardInput.setSelectionRange(1, 1);\n }\n function showKeyboard(enabled) {\n keyboardEnabled = enabled && control;\n if (!keyboardEnabled) {\n composing = false;\n resetKeyboard();\n keyboardInput.blur();\n return;\n }\n if (composing) return;\n if (config.desktop && document.activeElement === keyboardInput) return;\n // Keep focus inside the native evaluateJavaScript call; WebKit can reject\n // keyboard presentation after requestAnimationFrame loses user activation.\n keyboardInput.blur();\n resetKeyboard();\n keyboardInput.focus({ preventScroll: true });\n }\n function keyboardKey(code) {\n if (!keyboardEnabled || !control) return;\n queue({ kind: \"key\", code, down: true });\n queue({ kind: \"key\", code, down: false });\n flush();\n }\n function commitKeyboard() {\n if (!keyboardEnabled || !control || composing) return;\n const text = keyboardInput.value.replace(keyboardSentinel, \"\");\n if (text) {\n for (let i = 0; i < text.length; i += 4096)\n queue({ kind: \"text\", text: text.slice(i, i + 4096) });\n flush();\n }\n resetKeyboard();\n }\n listen(keyboardInput, \"compositionstart\", () => {\n composing = true;\n });\n listen(keyboardInput, \"compositionend\", () => {\n composing = false;\n commitKeyboard();\n });\n listen(keyboardInput, \"input\", (e) => {\n if (!e.isComposing) commitKeyboard();\n });\n listen(keyboardInput, \"beforeinput\", (e) => {\n if (composing || e.isComposing) return;\n if (e.inputType === \"deleteContentBackward\") {\n e.preventDefault();\n keyboardKey(\"Backspace\");\n resetKeyboard();\n } else if (\n e.inputType === \"insertLineBreak\" ||\n e.inputType === \"insertParagraph\"\n ) {\n e.preventDefault();\n keyboardKey(\"Enter\");\n resetKeyboard();\n }\n });\n const heldMouse = new Map();\n let showMouseButtons = false,\n wheelY = null;\n function resetWheel() {\n wheelY = null;\n wheelGrip.style.transform = \"translateY(0px)\";\n }\n let statsTimer = null,\n statsSample = null;\n const post = (message) => postMessage({ ...message, epoch });\n const clamp = (v) => Math.max(0, Math.min(1, v));\n let fillHeight = false;\n let panAnimation = null,\n viewportRightInset = 0,\n viewportLeftInset = 0,\n viewportBottomInset = 0,\n keyboardViewportInset = 0;\n let keyboardViewportOpen = false;\n let cursorNeedsEntry = true,\n manualViewMoved = false,\n followRest = null;\n // Insets guide centering and pan limits without clipping the full-screen\n // video surface or adding an opaque strip beside the Dynamic Island.\n const viewportLeft = () => (fillHeight ? viewportLeftInset : 0);\n const viewportHeight = () =>\n Math.max(1, stage.clientHeight - (viewerSized ? viewportBottomInset : 0));\n const viewportWidth = () =>\n Math.max(\n 1,\n stage.clientWidth -\n viewportLeft() -\n (fillHeight ? viewportRightInset : 0),\n );\n const layout = () => {\n const r = transform(\n viewportWidth(),\n viewportHeight(),\n dw,\n dh,\n zoom,\n fx,\n fy,\n fillHeight && !viewerSized,\n );\n return {\n ...r,\n x: viewportLeft() + viewportWidth() / 2 - fx * r.width,\n y: viewportHeight() / 2 - fy * r.height,\n };\n };\n function panBounds(r) {\n const vw = viewportWidth(),\n vh = viewportHeight();\n // Grow extra resting travel continuously from zero at fit to 180 screen\n // points at maximum zoom. Never count the unused space of a fitting axis.\n const clearance = (180 * (Math.max(1, Math.min(5, zoom)) - 1)) / 4;\n const axis = (viewport, content) =>\n content <= viewport\n ? {\n min: (viewport - content) / 2 - clearance,\n max: (viewport - content) / 2 + clearance,\n }\n : { min: viewport - content - clearance, max: clearance };\n const x = axis(vw, r.width),\n y = axis(vh, r.height);\n const bounds = {\n minX: viewportLeft() + x.min,\n maxX: viewportLeft() + x.max,\n minY: y.min,\n maxY: y.max,\n };\n // A camera position required to expose the cursor is a valid resting point,\n // not elastic overshoot. Preserve it across the next pan/pinch handoff.\n if (followRest) {\n const restX = viewportLeft() + vw / 2 - followRest.fx * r.width,\n restY = vh / 2 - followRest.fy * r.height;\n const weight =\n followRest.zoom > 1 ? clamp((zoom - 1) / (followRest.zoom - 1)) : 1;\n bounds.minX += Math.min(0, restX - bounds.minX) * weight;\n bounds.maxX += Math.max(0, restX - bounds.maxX) * weight;\n bounds.minY += Math.min(0, restY - bounds.minY) * weight;\n bounds.maxY += Math.max(0, restY - bounds.maxY) * weight;\n }\n return bounds;\n }\n const bounded = (v, min, max) => Math.max(min, Math.min(max, v));\n // Convert displayed overshoot back to finger travel before the next delta,\n // so reversing direction unwinds smoothly without an edge jump.\n function rubber(v, min, max, inverse = false) {\n const edge = bounded(v, min, max),\n d = v - edge;\n const reach = 40 + 10 * (Math.max(1, Math.min(5, zoom)) - 1);\n return (\n edge +\n Math.sign(d) *\n (inverse\n ? (reach * Math.abs(d)) / Math.max(1, reach - Math.abs(d))\n : (reach * Math.abs(d)) / (reach + Math.abs(d)))\n );\n }\n function place(x, y, r) {\n fx = (viewportLeft() + viewportWidth() / 2 - x) / r.width;\n fy = (viewportHeight() / 2 - y) / r.height;\n }\n function cursorViewport() {\n const hotX = remoteCursor?.hotX ?? 9,\n hotY = remoteCursor?.hotY ?? 9;\n const w = remoteCursor?.width ?? 18,\n h = remoteCursor?.height ?? 18;\n const left = viewportLeft(),\n right = left + viewportWidth(),\n bottom = Math.max(1, stage.clientHeight - viewportBottomInset);\n // Keep the entire cursor, including its hotspot offset, clear of chrome.\n const minX = Math.min(right - 1, left + 8 + hotX),\n minY = Math.min(bottom - 1, 8 + hotY);\n return {\n minX,\n maxX: Math.max(minX, right - 8 - (w - hotX)),\n minY,\n maxY: Math.max(minY, bottom - 8 - (h - hotY)),\n };\n }\n function moveTouchpad(dx, dy) {\n stopPanAnimation();\n const r = layout(),\n v = cursorViewport();\n if (cursorNeedsEntry) {\n // Re-enter at the nearest visible edge; do not pull a manually positioned\n // desktop back to the old off-screen cursor before applying this movement.\n cx = clamp((bounded(r.x + cx * r.width, v.minX, v.maxX) - r.x) / r.width);\n cy = clamp(\n (bounded(r.y + cy * r.height, v.minY, v.maxY) - r.y) / r.height,\n );\n cursorNeedsEntry = false;\n }\n cx = clamp(cx + dx / r.width);\n cy = clamp(cy + dy / r.height);\n const x = r.x + cx * r.width,\n y = r.y + cy * r.height;\n if (\n zoom > 1 ||\n r.width > viewportWidth() ||\n r.height > stage.clientHeight - viewportBottomInset\n ) {\n place(\n r.x + bounded(x, v.minX, v.maxX) - x,\n r.y + bounded(y, v.minY, v.maxY) - y,\n r,\n );\n followRest = { fx, fy, zoom };\n }\n manualViewMoved = false;\n }\n function stopPanAnimation() {\n if (panAnimation !== null) cancelAnimationFrame(panAnimation);\n panAnimation = null;\n }\n function settlePan() {\n stopPanAnimation();\n const r = layout(),\n b = panBounds(r),\n x = bounded(r.x, b.minX, b.maxX),\n y = bounded(r.y, b.minY, b.maxY);\n if (x === r.x && y === r.y) return;\n if (reducedMotion.matches) {\n place(x, y, r);\n render();\n return;\n }\n const started = performance.now();\n const tick = (now) => {\n const t = Math.min(1, (now - started) / 280),\n ease = 1 - Math.pow(1 - t, 3);\n place(r.x + (x - r.x) * ease, r.y + (y - r.y) * ease, r);\n render();\n panAnimation = t < 1 ? requestAnimationFrame(tick) : null;\n };\n panAnimation = requestAnimationFrame(tick);\n }\n function render() {\n const r = layout();\n const touchFeedback = control && mode === \"touch\" && touchCursorVisible;\n const cursorX = mode === \"touch\" && touchCursor ? touchCursor.x : cx;\n const cursorY = mode === \"touch\" && touchCursor ? touchCursor.y : cy;\n cursor.style.transition =\n mode === \"touch\" && !touchFeedback && !reducedMotion.matches\n ? \"opacity 120ms ease-out\"\n : \"none\";\n for (const el of [image, video]) {\n el.style.width = r.width + \"px\";\n el.style.height = r.height + \"px\";\n el.style.left = r.x + \"px\";\n el.style.top = r.y + \"px\";\n }\n if (remoteCursor) {\n cursor.style.width = remoteCursor.width + \"px\";\n cursor.style.height = remoteCursor.height + \"px\";\n cursor.style.border = \"0\";\n cursor.style.borderRadius = \"0\";\n cursor.style.transform = \"none\";\n cursor.style.left = r.x + cursorX * r.width - remoteCursor.hotX + \"px\";\n cursor.style.top = r.y + cursorY * r.height - remoteCursor.hotY + \"px\";\n cursor.style.display = \"block\";\n cursor.style.opacity = (\n mode === \"touch\"\n ? touchFeedback\n : remoteCursor.visible ||\n (control && mode === \"pointer\" && localCursorAwake)\n )\n ? \"1\"\n : \"0\";\n } else {\n cursor.style.opacity = (mode === \"touch\" ? !touchFeedback : false)\n ? \"0\"\n : \"1\";\n cursor.style.width = \"18px\";\n cursor.style.height = \"18px\";\n cursor.style.border = \"2px solid var(--foreground)\";\n cursor.style.borderRadius = \"50%\";\n cursor.style.transform = \"translate(-50%,-50%)\";\n cursor.style.left = r.x + cursorX * r.width + \"px\";\n cursor.style.top = r.y + cursorY * r.height + \"px\";\n cursor.style.display =\n !config.desktop && control && (mode === \"pointer\" || mode === \"touch\")\n ? \"block\"\n : \"none\";\n }\n if (config.desktop) stage.style.cursor = control ? \"none\" : \"default\";\n }\n // Literal source is required: Hermes function.toString() yields bytecode.\n function validCursor(v) {\n return (\n !!v &&\n typeof v === \"object\" &&\n typeof v.visible === \"boolean\" &&\n [v.x, v.y, v.width, v.height, v.hotX, v.hotY].every(Number.isFinite) &&\n v.x >= 0 &&\n v.x <= 1 &&\n v.y >= 0 &&\n v.y <= 1 &&\n v.width > 0 &&\n v.width <= 256 &&\n v.height > 0 &&\n v.height <= 256 &&\n v.hotX >= 0 &&\n v.hotX <= v.width &&\n v.hotY >= 0 &&\n v.hotY <= v.height &&\n typeof v.png === \"string\" &&\n v.png.length <= 65536 &&\n /^iVBORw0KGgo[A-Za-z0-9+/]*={0,2}$/.test(v.png)\n );\n }\n function receiveCursor(value) {\n if (value === null) {\n if (remoteCursor) {\n remoteCursor = { ...remoteCursor, visible: false };\n render();\n }\n return;\n }\n if (!validCursor(value)) return;\n if (!remoteCursor || remoteCursor.png !== value.png)\n cursorImage.src = \"data:image/png;base64,\" + value.png;\n // Keep delayed host coordinates from pulling the local touchpad backwards.\n if (\n !remoteCursor ||\n !control ||\n mode !== \"pointer\" ||\n (!pointers.size && performance.now() - lastLocalMove > 200)\n ) {\n cx = value.x;\n cy = value.y;\n }\n if (value.visible) localCursorAwake = false;\n remoteCursor = value;\n render();\n }\n function resetCursor() {\n clearTimeout(touchCursorTimer);\n touchCursorVisible = false;\n touchCursor = null;\n localCursorAwake = false;\n remoteCursor = null;\n cursorImage.removeAttribute(\"src\");\n render();\n }\n function queue(event) {\n if (!control) return;\n if (config.desktop && (event.kind === \"button\" || event.kind === \"scroll\"))\n flushClipboardModifier();\n // Remote visibility can remain hidden after synthetic mouse movement. Wake\n // the local touchpad cursor until the host reports a visible cursor again.\n if (event.kind === \"move\" && mode === \"pointer\") localCursorAwake = true;\n if (event.kind === \"key\" || event.kind === \"text\") {\n localCursorAwake = false;\n render();\n }\n if (event.kind === \"move\" || event.kind === \"button\")\n lastLocalMove = performance.now();\n const tail = pending[pending.length - 1];\n if (event.kind === \"move\" && tail?.kind === \"move\")\n pending[pending.length - 1] = event;\n // Coalesce only adjacent scrolls, preserving button/key ordering. Bound the\n // accumulated distance so a delayed ACK cannot replay a large scroll backlog.\n else if (event.kind === \"scroll\" && tail?.kind === \"scroll\") {\n tail.dx = Math.max(-2000, Math.min(2000, tail.dx + event.dx));\n tail.dy = Math.max(-2000, Math.min(2000, tail.dy + event.dy));\n } else pending.push(event);\n if (pending.length > 64) {\n pending = [{ kind: \"release\" }];\n control = false;\n post({ type: \"inputOverflow\" });\n }\n }\n function flush() {\n if (!pending.length || sending) return;\n const events = pending.splice(0, 64),\n sequence = ++seq;\n if (\n pc?.connectionState === \"connected\" &&\n dc &&\n dc.readyState === \"open\" &&\n dc.bufferedAmount < 16384\n ) {\n dc.send(JSON.stringify({ sequence, events }));\n } else {\n sending = true;\n post({ type: \"input\", sequence, events });\n }\n }\n const inputTimer = setInterval(() => {\n // Hold displacement controls speed, even without further pointer moves.\n // Skip missed ticks while awaiting ACK instead of building a scroll backlog.\n if (wheelY?.moved && !sending) {\n const offset = Math.max(-24, Math.min(24, wheelY.y - wheelY.start));\n if (Math.abs(offset) > 4)\n scrollMouse(Math.sign(offset) * (Math.abs(offset) - 4) * 1.5);\n }\n flush();\n }, 33);\n cleanups.push(() => clearInterval(inputTimer));\n function release() {\n deferredClipboardModifier = null;\n clearTimeout(hold);\n if (gestureFrame !== null) cancelAnimationFrame(gestureFrame);\n gestureFrame = null;\n if (control) {\n queue({ kind: \"release\" });\n flush();\n }\n heldMouse.clear();\n resetWheel();\n updateMouseButtons();\n drag = false;\n pointers.clear();\n start = null;\n last = null;\n multi = null;\n }\n function updateMouseButtons() {\n mouseButtons.style.display = showMouseButtons && control ? \"block\" : \"none\";\n for (const [name, button] of [\n [\"left\", 0],\n [\"right\", 2],\n ])\n find(\"mouse-\" + name).setAttribute(\n \"aria-pressed\",\n String(heldMouse.has(button)),\n );\n }\n for (const [name, button] of [\n [\"left\", 0],\n [\"right\", 2],\n ]) {\n const el = find(\"mouse-\" + name);\n listen(el, \"pointerdown\", (e) => {\n e.preventDefault();\n if (!control || !showMouseButtons || heldMouse.has(button)) return;\n clearTimeout(hold);\n el.setPointerCapture(e.pointerId);\n heldMouse.set(button, e.pointerId);\n queue({ kind: \"button\", button, down: true, x: cx, y: cy });\n flush();\n updateMouseButtons();\n });\n const up = (e) => {\n e.preventDefault();\n if (heldMouse.get(button) !== e.pointerId) return;\n heldMouse.delete(button);\n queue({ kind: \"button\", button, down: false, x: cx, y: cy });\n flush();\n updateMouseButtons();\n };\n listen(el, \"pointerup\", up);\n listen(el, \"pointercancel\", up);\n listen(el, \"lostpointercapture\", up);\n listen(el, \"click\", (e) => {\n if (e.detail === 0 && control && showMouseButtons) {\n click(button);\n flush();\n }\n });\n }\n const scrollMouse = (dy) => {\n if (control && showMouseButtons && Number.isFinite(dy)) {\n queue({ kind: \"scroll\", dx: 0, dy: Math.max(-2000, Math.min(2000, dy)) });\n }\n };\n listen(wheel, \"pointerdown\", (e) => {\n e.preventDefault();\n if (!control || !showMouseButtons || wheelY) return;\n wheel.setPointerCapture(e.pointerId);\n wheelY = { id: e.pointerId, y: e.clientY, start: e.clientY, moved: false };\n });\n // A small dead zone preserves middle clicks; displacement drives the timer.\n listen(wheel, \"pointermove\", (e) => {\n if (!wheelY || wheelY.id !== e.pointerId) return;\n e.preventDefault();\n wheelGrip.style.transform =\n \"translateY(\" +\n Math.max(-24, Math.min(24, e.clientY - wheelY.start)) +\n \"px)\";\n wheelY.y = e.clientY;\n if (Math.abs(e.clientY - wheelY.start) > 4) wheelY.moved = true;\n });\n listen(wheel, \"pointerup\", (e) => {\n if (!wheelY || wheelY.id !== e.pointerId) return;\n e.preventDefault();\n if (!wheelY.moved && control && showMouseButtons) {\n click(1);\n flush();\n }\n resetWheel();\n });\n const cancelWheel = (e) => {\n if (wheelY?.id === e.pointerId) resetWheel();\n };\n listen(wheel, \"pointercancel\", cancelWheel);\n listen(wheel, \"lostpointercapture\", cancelWheel);\n listen(wheel, \"click\", (e) => {\n if (e.detail === 0 && control && showMouseButtons) {\n click(1);\n flush();\n }\n });\n listen(wheel, \"keydown\", (e) => {\n if (e.key === \"ArrowUp\" || e.key === \"ArrowDown\") {\n e.preventDefault();\n scrollMouse(e.key === \"ArrowUp\" ? -120 : 120);\n }\n });\n function insideDesktop(p) {\n const r = layout();\n return (\n p.x >= r.x && p.x <= r.x + r.width && p.y >= r.y && p.y <= r.y + r.height\n );\n }\n function point(p) {\n const r = layout();\n return {\n x: clamp((p.x - r.x) / r.width),\n y: clamp((p.y - r.y) / r.height),\n };\n }\n const desktopPoint = (e) => {\n const bounds = stage.getBoundingClientRect();\n return { x: e.clientX - bounds.left, y: e.clientY - bounds.top };\n };\n function mouse(e, down) {\n if (!control || ![0, 1, 2].includes(e.button)) return;\n const local = desktopPoint(e);\n if (down && !insideDesktop(local)) return;\n const p = point(local);\n cx = p.x;\n cy = p.y;\n if (down) {\n showKeyboard(true);\n stage.setPointerCapture(e.pointerId);\n heldMouse.set(e.button, e.pointerId);\n } else if (!heldMouse.delete(e.button)) return;\n queue({ kind: \"button\", button: e.button, down, x: cx, y: cy });\n flush();\n render();\n }\n function click(button = 0) {\n if (control && mode === \"touch\") {\n clearTimeout(touchCursorTimer);\n touchCursor = { x: cx, y: cy };\n touchCursorVisible = true;\n render();\n touchCursorTimer = setTimeout(() => {\n touchCursorVisible = false;\n render();\n }, 450);\n }\n queue({ kind: \"button\", button, down: true, x: cx, y: cy });\n queue({ kind: \"button\", button, down: false, x: cx, y: cy });\n }\n function pan(dx, dy) {\n manualViewMoved = true;\n cursorNeedsEntry = true;\n stopPanAnimation();\n const r = layout(),\n b = panBounds(r);\n place(\n rubber(rubber(r.x, b.minX, b.maxX, true) + dx, b.minX, b.maxX),\n rubber(rubber(r.y, b.minY, b.maxY, true) + dy, b.minY, b.maxY),\n r,\n );\n render();\n }\n function pair() {\n const [a, b] = [...pointers.values()];\n return {\n x: (a.x + b.x) / 2,\n y: (a.y + b.y) / 2,\n d: Math.hypot(a.x - b.x, a.y - b.y),\n };\n }\n function movePair() {\n gestureFrame = null;\n if (pointers.size < 2 || !multi) return;\n const next = pair(),\n span = Math.abs(next.d - multi.d),\n travel = Math.hypot(next.x - multi.x, next.y - multi.y);\n if (multi.kind !== \"pinch\") {\n const pinchSlop = Math.max(6, Math.min(12, multi.d * 0.04));\n if (span >= pinchSlop && span > travel * 0.65) {\n // A parallel scroll may deliver its two pointer updates in different\n // frames. Confirm separation briefly before taking over as a pinch.\n if (!multi.candidate)\n multi.candidate = {\n at: Date.now(),\n d: next.d,\n zoom,\n anchor: point(next),\n };\n if (Date.now() - multi.candidate.at >= 40) {\n if (multi.kind === \"scroll\") {\n multi.d = multi.candidate.d;\n multi.zoom = multi.candidate.zoom;\n multi.anchor = multi.candidate.anchor;\n }\n multi.kind = \"pinch\";\n multi.candidate = null;\n } else {\n gestureFrame = requestAnimationFrame(movePair);\n return;\n }\n } else {\n multi.candidate = null;\n if (!multi.kind && travel > 8 && travel > span) multi.kind = \"scroll\";\n }\n }\n if (multi.kind === \"pinch\") {\n manualViewMoved = true;\n cursorNeedsEntry = true;\n zoom = Math.max(\n 1,\n Math.min(5, (multi.zoom * next.d) / Math.max(1, multi.d)),\n );\n const r = layout();\n fx =\n multi.anchor.x +\n (viewportLeft() + viewportWidth() / 2 - next.x) / r.width;\n fy = multi.anchor.y + (viewportHeight() / 2 - next.y) / r.height;\n const moved = layout(),\n b = panBounds(moved);\n place(\n rubber(moved.x, b.minX, b.maxX),\n rubber(moved.y, b.minY, b.maxY),\n moved,\n );\n } else if (multi.kind === \"scroll\") {\n const dx = next.x - multi.lastX,\n dy = next.y - multi.lastY;\n if (control && mode !== \"pan\")\n queue({\n kind: \"scroll\",\n dx: Math.max(-2000, Math.min(2000, -dx)),\n dy: Math.max(-2000, Math.min(2000, -dy)),\n });\n else pan(dx, dy);\n }\n if (multi.kind) {\n multi.lastX = next.x;\n multi.lastY = next.y;\n }\n render();\n }\n listen(stage, \"pointerdown\", (e) => {\n if (config.desktop) {\n e.preventDefault();\n mouse(e, true);\n return;\n }\n e.preventDefault();\n stopPanAnimation();\n stage.setPointerCapture(e.pointerId);\n pointers.set(e.pointerId, { x: e.clientX, y: e.clientY });\n if (pointers.size === 1) {\n cursorNeedsEntry = true;\n start = { x: e.clientX, y: e.clientY };\n last = start;\n moved = false;\n hold = setTimeout(() => {\n if (\n control &&\n mode !== \"pan\" &&\n !moved &&\n pointers.size === 1 &&\n !heldMouse.size &&\n (mode !== \"touch\" || insideDesktop(start))\n ) {\n if (mode === \"touch\") {\n const p = point(start);\n cx = p.x;\n cy = p.y;\n }\n queue({ kind: \"button\", button: 0, down: true, x: cx, y: cy });\n drag = true;\n }\n }, 400);\n } else {\n clearTimeout(hold);\n if (drag) queue({ kind: \"release\" });\n drag = false;\n start = null;\n const p = pair();\n multi = {\n ...p,\n lastX: p.x,\n lastY: p.y,\n zoom,\n anchor: point(p),\n kind: null,\n };\n }\n });\n listen(stage, \"pointermove\", (e) => {\n if (config.desktop) {\n if (!control) return;\n const local = desktopPoint(e);\n if (!heldMouse.size && !insideDesktop(local)) return;\n const p = point(local);\n cx = p.x;\n cy = p.y;\n queue({ kind: \"move\", x: cx, y: cy });\n render();\n return;\n }\n if (!pointers.has(e.pointerId)) return;\n e.preventDefault();\n pointers.set(e.pointerId, { x: e.clientX, y: e.clientY });\n if (pointers.size >= 2) {\n if (gestureFrame === null) gestureFrame = requestAnimationFrame(movePair);\n return;\n }\n if (!start || !last) return;\n const now = { x: e.clientX, y: e.clientY },\n dx = now.x - last.x,\n dy = now.y - last.y;\n last = now;\n if (Math.hypot(now.x - start.x, now.y - start.y) > 6) {\n moved = true;\n clearTimeout(hold);\n }\n if (\n !control ||\n mode === \"pan\" ||\n (mode === \"touch\" && !drag && !heldMouse.size)\n ) {\n pan(dx, dy);\n return;\n }\n if (mode === \"pointer\") {\n moveTouchpad(dx, dy);\n } else {\n const p = point(now);\n cx = p.x;\n cy = p.y;\n }\n queue({ kind: \"move\", x: cx, y: cy });\n render();\n });\n function up(e) {\n if (config.desktop) {\n mouse(e, false);\n return;\n }\n if (!pointers.has(e.pointerId)) return;\n clearTimeout(hold);\n if (pointers.size >= 2 && multi) {\n pointers.set(e.pointerId, { x: e.clientX, y: e.clientY });\n if (gestureFrame !== null) cancelAnimationFrame(gestureFrame);\n movePair();\n if (gestureFrame !== null) cancelAnimationFrame(gestureFrame);\n gestureFrame = null;\n }\n pointers.delete(e.pointerId);\n if (drag) {\n queue({ kind: \"button\", button: 0, down: false, x: cx, y: cy });\n drag = false;\n } else if (\n start &&\n !moved &&\n control &&\n mode !== \"pan\" &&\n !heldMouse.size &&\n e.type === \"pointerup\" &&\n (mode !== \"touch\" || insideDesktop({ x: e.clientX, y: e.clientY }))\n ) {\n if (mode === \"touch\") {\n const p = point({ x: e.clientX, y: e.clientY });\n cx = p.x;\n cy = p.y;\n }\n click();\n }\n start = null;\n last = null;\n multi = null;\n render();\n if (!pointers.size && manualViewMoved) settlePan();\n }\n listen(stage, \"pointerup\", up);\n listen(stage, \"pointercancel\", () => {\n release();\n settlePan();\n });\n listen(stage, \"lostpointercapture\", () => {\n if (config.desktop && heldMouse.size) release();\n });\n if (config.desktop)\n listen(\n stage,\n \"wheel\",\n (e) => {\n e.preventDefault();\n if (!control) return;\n const factor =\n e.deltaMode === 1 ? 16 : e.deltaMode === 2 ? stage.clientHeight : 1;\n queue({\n kind: \"scroll\",\n dx: Math.max(-2000, Math.min(2000, e.deltaX * factor)),\n dy: Math.max(-2000, Math.min(2000, e.deltaY * factor)),\n });\n flush();\n },\n { passive: false },\n );\n listen(config.desktop ? stage : document, \"contextmenu\", (e) =>\n e.preventDefault(),\n );\n // Scope selection suppression to the desktop surface, preserving the hidden\n // textarea's native keyboard/IME editing and programmatic selection.\n listen(stage, \"selectstart\", (e) => e.preventDefault());\n listen(stage, \"dragstart\", (e) => e.preventDefault());\n const normalizedKey = (code) =>\n /^(Shift|Control|Alt|Meta)Right$/.test(code)\n ? code.replace(/Right$/, \"Left\")\n : code;\n const hardwareKeys = new Set();\n function flushClipboardModifier() {\n if (!deferredClipboardModifier) return;\n const code = deferredClipboardModifier;\n deferredClipboardModifier = null;\n hardwareKeys.add(code);\n queue({ kind: \"key\", code, down: true });\n }\n listen(document, \"keydown\", (e) => {\n if (config.desktop) {\n if (e.ctrlKey && e.altKey && e.code === \"Escape\") {\n e.preventDefault();\n release();\n showKeyboard(false);\n post({ type: \"releaseFocus\" });\n return;\n }\n if (\n document.activeElement !== keyboardInput ||\n e.isComposing ||\n composing\n )\n return;\n if ((e.metaKey || e.ctrlKey) && e.code === \"KeyW\") return; // always retain a local close shortcut\n if (\n control &&\n clipboardShortcuts &&\n normalizedKey(e.code) ===\n (clipboardModifier === \"meta\" ? \"MetaLeft\" : \"ControlLeft\")\n ) {\n e.preventDefault();\n if (!hardwareKeys.has(normalizedKey(e.code)))\n deferredClipboardModifier = normalizedKey(e.code);\n return;\n }\n if (\n control &&\n clipboardShortcuts &&\n (clipboardModifier === \"meta\"\n ? e.metaKey && !e.ctrlKey\n : e.ctrlKey && !e.metaKey) &&\n !e.altKey &&\n !e.shiftKey &&\n (e.code === \"KeyC\" || e.code === \"KeyV\")\n ) {\n e.preventDefault();\n if (!e.repeat) {\n release();\n hardwareKeys.clear();\n post({\n type: \"clipboard\",\n action: e.code === \"KeyC\" ? \"copy\" : \"paste\",\n });\n }\n return;\n }\n if (\n !e.ctrlKey &&\n !e.metaKey &&\n !e.altKey &&\n (e.key?.length === 1 || e.key === \"Process\" || e.key === \"Dead\")\n )\n return;\n }\n if (control && validKeys.has(normalizedKey(e.code))) {\n e.preventDefault();\n flushClipboardModifier();\n hardwareKeys.add(normalizedKey(e.code));\n queue({ kind: \"key\", code: normalizedKey(e.code), down: true });\n }\n });\n listen(document, \"keyup\", (e) => {\n const code = normalizedKey(e.code);\n if (config.desktop && control && deferredClipboardModifier === code) {\n flushClipboardModifier();\n }\n if (config.desktop && !hardwareKeys.delete(code)) return;\n if (control && validKeys.has(code)) {\n e.preventDefault();\n queue({ kind: \"key\", code, down: false });\n }\n });\n if (config.desktop)\n listen(keyboardInput, \"blur\", () => {\n hardwareKeys.clear();\n release();\n });\n listen(window, \"blur\", () => {\n release();\n settlePan();\n });\n function reportViewport() {\n post({\n type: \"viewportChanged\",\n width: viewportWidth(),\n height: Math.max(1, stage.clientHeight - viewportBottomInset),\n });\n }\n const observer = new ResizeObserver(() => {\n reportViewport();\n release();\n settlePan();\n render();\n });\n observer.observe(stage);\n cleanups.push(\n () => observer.disconnect(),\n () => stopPanAnimation(),\n () => clearTimeout(touchCursorTimer),\n );\n // Keep one bounded frame in this document only; never persist desktop pixels.\n // Snapshot before detaching the track. A JPEG fallback already lives in image.\n function retainFrame() {\n if (\n !videoPresented ||\n video.style.display !== \"block\" ||\n video.readyState < 2 ||\n !video.videoWidth ||\n !video.videoHeight\n )\n return;\n const canvas = document.createElement(\"canvas\");\n try {\n const scale = Math.min(\n 1,\n 1280 / Math.max(video.videoWidth, video.videoHeight),\n );\n canvas.width = Math.max(1, Math.round(video.videoWidth * scale));\n canvas.height = Math.max(1, Math.round(video.videoHeight * scale));\n const context = canvas.getContext(\"2d\");\n if (context) {\n context.drawImage(video, 0, 0, canvas.width, canvas.height);\n image.src = canvas.toDataURL(\"image/jpeg\", 0.7);\n }\n } catch {\n /* Keep the previous compatibility frame if WebKit cannot read video. */\n } finally {\n canvas.width = 0;\n canvas.height = 0;\n }\n }\n /* BEGIN RTC */\n\n let trickleIce = false,\n attemptId = null,\n retries = 0,\n exchangeId = 0;\n let configPending = false;\n let videoPresented = false,\n videoFrameCallback = null,\n videoPaintCallback = null;\n function cancelVideoHandoff() {\n if (videoFrameCallback !== null)\n video.cancelVideoFrameCallback?.(videoFrameCallback);\n if (videoPaintCallback !== null) cancelAnimationFrame(videoPaintCallback);\n videoFrameCallback = videoPaintCallback = null;\n }\n function revealVideo(g) {\n post({ type: \"videoFrameReady\", attemptId });\n // Keep the JPEG decoded and painted as a backing layer. Promote the video\n // only after its first frame; removing the image exposes WebKit repaint gaps.\n videoPaintCallback = requestAnimationFrame(() => {\n if (g !== generation) return;\n videoPaintCallback = requestAnimationFrame(() => {\n if (g !== generation) return;\n videoPaintCallback = null;\n videoPresented = true;\n video.style.zIndex = \"2\";\n post({ type: \"streaming\", attemptId });\n post({\n type: \"pipCapability\",\n supported: !!(\n video.webkitSupportsPresentationMode?.(\"picture-in-picture\") ||\n document.pictureInPictureEnabled\n ),\n });\n });\n });\n }\n function waitForVideoFrame(g) {\n if (g !== generation) return;\n if (\n video.readyState >= 2 &&\n video.videoWidth > 0 &&\n video.videoHeight > 0\n ) {\n revealVideo(g);\n return;\n }\n videoPaintCallback = requestAnimationFrame(() => waitForVideoFrame(g));\n }\n let retryTimer = null,\n deadlineTimer = null,\n disconnectTimer = null,\n stableTimer = null,\n iceTimer = null,\n gatherTimer = null,\n gatherDone = null;\n let localCandidates = [],\n localAck = 0,\n remoteAfter = 0,\n icePending = null,\n remoteSeen = new Set(),\n exchangeUntil = 0,\n remoteComplete = false;\n function clearRtcTimers() {\n for (const timer of [\n retryTimer,\n deadlineTimer,\n disconnectTimer,\n stableTimer,\n iceTimer,\n gatherTimer,\n ])\n clearTimeout(timer);\n retryTimer =\n deadlineTimer =\n disconnectTimer =\n stableTimer =\n iceTimer =\n gatherTimer =\n null;\n if (gatherDone) {\n gatherDone();\n gatherDone = null;\n }\n }\n function closeRtc(preserveFrame = true) {\n configPending = false;\n if (preserveFrame) retainFrame();\n generation++;\n clearRtcTimers();\n cancelVideoHandoff();\n videoPresented = false;\n localCandidates = [];\n localAck = remoteAfter = 0;\n icePending = null;\n remoteSeen = new Set();\n attemptId = null;\n clearInterval(statsTimer);\n statsTimer = null;\n statsSample = null;\n if (dc) dc.close();\n if (pc) pc.close();\n pc = null;\n dc = null;\n video.onplaying = null;\n video.srcObject = null;\n video.style.display = \"none\";\n video.style.zIndex = \"0\";\n image.style.display = \"block\";\n if (!preserveFrame) image.removeAttribute(\"src\");\n }\n function failRtc(reason, canRetry = true) {\n const failedAttempt = attemptId;\n release();\n closeRtc();\n post({ type: \"fallback\", attemptId: failedAttempt, reason });\n if (canRetry && epoch && retries < net.retryMs.length) {\n const delay = net.retryMs[retries++];\n retryTimer = setTimeout(() => {\n retryTimer = null;\n if (epoch) connect();\n }, delay);\n }\n }\n function pollIce() {\n if (\n !pc ||\n !trickleIce ||\n !pc.remoteDescription ||\n icePending ||\n Date.now() >= exchangeUntil\n )\n return;\n const candidates = localCandidates.slice(\n localAck,\n localAck + net.batchSize,\n );\n icePending = {\n after: remoteAfter,\n sent: candidates.length,\n exchangeId: ++exchangeId,\n };\n post({\n type: \"ice\",\n attemptId,\n candidates,\n after: remoteAfter,\n exchangeId,\n });\n iceTimer = setTimeout(() => {\n icePending = null;\n pollIce();\n }, 4500);\n }\n async function receiveIce(message) {\n if (\n !pc ||\n !icePending ||\n message.attemptId !== attemptId ||\n message.exchangeId !== icePending.exchangeId\n )\n return;\n const rtc = pc,\n g = generation,\n batch = icePending;\n clearTimeout(iceTimer);\n try {\n if (message.error) {\n icePending = null;\n iceTimer = setTimeout(pollIce, 1000);\n return;\n }\n if (\n !Array.isArray(message.candidates) ||\n message.candidates.length > net.batchSize ||\n message.next !== batch.after + message.candidates.length ||\n message.next > net.maxCandidates\n )\n throw new Error();\n for (const candidate of message.candidates) {\n if (g !== generation) return;\n const key = JSON.stringify(candidate);\n if (remoteSeen.has(key)) continue;\n if (remoteSeen.size >= net.maxCandidates) throw new Error();\n await rtc.addIceCandidate(candidate);\n if (g !== generation) return;\n remoteSeen.add(key);\n }\n if (g !== generation) return;\n localAck += batch.sent;\n remoteAfter = message.next;\n remoteComplete = message.complete === true;\n icePending = null;\n if (!(\n remoteComplete &&\n rtc.iceGatheringState === \"complete\" &&\n localAck === localCandidates.length\n ))\n iceTimer = setTimeout(pollIce, net.pollMs);\n } catch {\n if (g === generation) failRtc(\"candidates\");\n }\n }\n async function receiveAnswer(message) {\n if (!pc || message.attemptId !== attemptId) return;\n const rtc = pc,\n g = generation;\n try {\n await rtc.setRemoteDescription({ type: \"answer\", sdp: message.sdp });\n if (g !== generation) return;\n clearTimeout(deadlineTimer);\n deadlineTimer = setTimeout(() => {\n if (g === generation && rtc.connectionState !== \"connected\")\n failRtc(\"connect-timeout\");\n }, net.connectMs);\n exchangeUntil = Date.now() + net.exchangeMs;\n pollIce();\n } catch {\n if (g === generation) failRtc(\"answer\");\n }\n }\n async function connect() {\n closeRtc();\n const g = generation;\n attemptId = String(g);\n if (!window.RTCPeerConnection) {\n failRtc(\"unsupported\", false);\n return;\n }\n configPending = true;\n deadlineTimer = setTimeout(() => {\n if (g === generation && configPending) {\n configPending = false;\n void startRtc(g, iceServers);\n }\n }, 3500);\n return post({ type: \"iceConfig\", attemptId });\n }\n async function receiveIceConfig(message) {\n if (!epoch || !configPending || message.attemptId !== attemptId) return;\n configPending = false;\n clearTimeout(deadlineTimer);\n deadlineTimer = null;\n await startRtc(generation, message.iceServers);\n }\n async function startRtc(g, servers) {\n try {\n const rtc = new RTCPeerConnection({ iceServers: servers });\n pc = rtc;\n dc = rtc.createDataChannel(\"input-v1\");\n rtc.addTransceiver(\"video\", { direction: \"recvonly\" });\n rtc.addTransceiver(\"audio\", { direction: \"recvonly\" });\n rtc.onicecandidate = ({ candidate }) => {\n if (g !== generation || !candidate?.candidate || !trickleIce) return;\n if (localCandidates.length >= net.maxCandidates) {\n failRtc(\"candidate-limit\", false);\n return;\n }\n localCandidates.push({\n candidate: candidate.candidate,\n sdpMid: candidate.sdpMid,\n sdpMLineIndex: candidate.sdpMLineIndex,\n ...(candidate.usernameFragment\n ? { usernameFragment: candidate.usernameFragment }\n : {}),\n });\n // Gathering continues after the offer; the serial exchange flushes late addresses.\n };\n dc.onmessage = (e) => {\n try {\n if (g !== generation || pc !== rtc) return;\n if (typeof e.data !== \"string\" || e.data.length > 80000) return;\n const message = JSON.parse(e.data);\n if (message.type === \"cursor\") receiveCursor(message.cursor);\n if (\n (video.webkitPresentationMode === \"picture-in-picture\" ||\n document.pictureInPictureElement === video) &&\n message.type === \"viewPing\" &&\n typeof message.challenge === \"string\" &&\n message.challenge.length <= 64 &&\n dc?.readyState === \"open\"\n )\n dc.send(message.challenge);\n } catch {}\n };\n let readingStats = false;\n statsTimer = setInterval(async () => {\n if (readingStats || g !== generation) return;\n readingStats = true;\n try {\n const stats = await rtc.getStats();\n if (g !== generation || pc !== rtc) return;\n const result = networkStats(stats, statsSample);\n statsSample = result.sample;\n post({\n type: \"network\",\n transport: result.transport,\n bytesPerSecond: result.bytesPerSecond,\n latencyMs: result.latencyMs,\n });\n } catch {\n } finally {\n readingStats = false;\n }\n }, 1000);\n rtc.ontrack = (e) => {\n if (g !== generation) return;\n video.srcObject = e.streams[0] || new MediaStream([e.track]);\n video.play().catch(() => {});\n };\n video.onplaying = () => {\n if (\n g !== generation ||\n videoPresented ||\n videoFrameCallback !== null ||\n videoPaintCallback !== null\n )\n return;\n video.style.display = \"block\";\n render();\n if (typeof video.requestVideoFrameCallback === \"function\") {\n videoFrameCallback = video.requestVideoFrameCallback(() => {\n if (g !== generation) return;\n videoFrameCallback = null;\n revealVideo(g);\n });\n } else waitForVideoFrame(g);\n };\n rtc.onconnectionstatechange = () => {\n if (g !== generation) return;\n if ([\"failed\", \"closed\"].includes(rtc.connectionState)) {\n failRtc(\"transport\");\n return;\n }\n if (rtc.connectionState === \"disconnected\") {\n clearTimeout(stableTimer);\n stableTimer = null;\n if (!disconnectTimer) {\n release();\n post({ type: \"reconnecting\", attemptId });\n disconnectTimer = setTimeout(() => {\n if (g === generation) failRtc(\"disconnected\");\n }, net.disconnectedMs);\n }\n } else if (rtc.connectionState === \"connected\") {\n clearTimeout(disconnectTimer);\n disconnectTimer = null;\n clearTimeout(deadlineTimer);\n if (!stableTimer)\n stableTimer = setTimeout(() => {\n if (g === generation) retries = 0;\n }, net.stableMs);\n if (videoPresented) post({ type: \"streaming\", attemptId });\n }\n };\n await rtc.setLocalDescription(await rtc.createOffer());\n if (!trickleIce)\n await new Promise((resolve) => {\n if (rtc.iceGatheringState === \"complete\") return resolve();\n gatherDone = resolve;\n gatherTimer = setTimeout(resolve, net.legacyGatherMs);\n rtc.onicegatheringstatechange = () => {\n if (rtc.iceGatheringState === \"complete\") {\n clearTimeout(gatherTimer);\n resolve();\n }\n };\n });\n if (g !== generation) return;\n post({ type: \"offer\", attemptId, sdp: rtc.localDescription.sdp });\n deadlineTimer = setTimeout(() => {\n if (g === generation) failRtc(\"answer-timeout\");\n }, net.answerMs);\n } catch {\n if (g === generation) failRtc(\"setup\");\n }\n }\n\n /* END RTC */\n function reportPresentation() {\n post({\n type: \"presentation\",\n active:\n video.webkitPresentationMode === \"picture-in-picture\" ||\n document.pictureInPictureElement === video,\n });\n }\n listen(video, \"webkitpresentationmodechanged\", reportPresentation);\n listen(video, \"enterpictureinpicture\", reportPresentation);\n listen(video, \"leavepictureinpicture\", reportPresentation);\n function receive(event) {\n let message;\n try {\n message = JSON.parse(event.data);\n } catch {\n return;\n }\n switch (message.type) {\n case \"measureViewport\":\n post({\n type: \"viewportSize\",\n width: viewportWidth(),\n height: Math.max(1, stage.clientHeight - viewportBottomInset),\n });\n break;\n case \"presentation\":\n try {\n if (message.enabled) {\n release();\n showKeyboard(false);\n if (video.webkitSupportsPresentationMode?.(\"picture-in-picture\"))\n video.webkitSetPresentationMode(\"picture-in-picture\");\n else if (video.requestPictureInPicture)\n video\n .requestPictureInPicture()\n .catch(() => post({ type: \"presentationFailed\" }));\n else post({ type: \"presentationFailed\" });\n } else if (video.webkitSetPresentationMode)\n video.webkitSetPresentationMode(\"inline\");\n else if (document.pictureInPictureElement)\n document.exitPictureInPicture().catch(() => {});\n } catch {\n post({ type: \"presentationFailed\" });\n }\n break;\n case \"videoSettings\":\n if (\n Number.isInteger(message.width) &&\n Number.isInteger(message.height) &&\n message.width >= 320 &&\n message.height >= 320 &&\n (message.restore === true ||\n (message.width <= 2560 && message.height <= 2560))\n ) {\n release();\n stopPanAnimation();\n viewerSized = message.restore !== true;\n zoom = 1;\n fx = fy = 0.5;\n followRest = null;\n dw = message.width;\n dh = message.height;\n render();\n }\n video.muted = !message.audio;\n retries = 0;\n connect();\n break;\n case \"keyboard\":\n showKeyboard(message.enabled === true);\n break;\n case \"resume\":\n if (video.srcObject) video.play().catch(() => {});\n break;\n case \"releaseInput\":\n release();\n break;\n case \"theme\":\n if (/^#[0-9a-f]{3,8}$/i.test(message.surface)) {\n document.body.style.background = message.surface;\n document.documentElement.style.background = message.surface;\n document.documentElement.style.setProperty(\n \"--surface\",\n message.surface,\n );\n }\n if (/^#[0-9a-f]{3,8}$/i.test(message.foreground)) {\n cursor.style.borderColor = message.foreground;\n document.documentElement.style.setProperty(\n \"--foreground\",\n message.foreground,\n );\n }\n break;\n case \"mouseButtons\": {\n const before = layout(),\n previousBottomInset = viewportBottomInset,\n keyboardOpen = fillHeight && message.keyboardOpen === true;\n const keepHorizontal =\n fillHeight && (keyboardOpen || keyboardViewportOpen);\n // Opening is observable before the native keyboard has a measured height.\n keyboardViewportOpen = keyboardOpen;\n let sideInsetsChanged = false;\n if (Number.isFinite(message.bottomInset))\n viewportBottomInset = Math.max(\n 0,\n Math.min(stage.clientHeight - 1, message.bottomInset),\n );\n if (Number.isFinite(message.leftInset)) {\n const inset = Math.max(\n 0,\n Math.min(stage.clientWidth - 1, message.leftInset),\n );\n sideInsetsChanged = sideInsetsChanged || inset !== viewportLeftInset;\n viewportLeftInset = inset;\n }\n if (Number.isFinite(message.rightInset)) {\n const inset = Math.max(\n 0,\n Math.min(stage.clientWidth - 1, message.rightInset),\n );\n sideInsetsChanged = sideInsetsChanged || inset !== viewportRightInset;\n viewportRightInset = inset;\n }\n // Hiding the side toolbar changes the viewport center. Keep the image's\n // actual horizontal position instead of following that center sideways.\n if (keepHorizontal) {\n stopPanAnimation();\n const r = layout();\n place(before.x, r.y, r);\n }\n const keyboardInset = keyboardOpen ? viewportBottomInset : 0;\n if (keyboardInset !== keyboardViewportInset) {\n const wasOpen = keyboardViewportInset > 0;\n keyboardViewportInset = keyboardInset;\n if (fillHeight && (keyboardInset > 0 || wasOpen)) {\n const r = layout(),\n v = cursorViewport();\n place(r.x, (v.minY + v.maxY) / 2 - cy * r.height, r);\n cursorNeedsEntry = false;\n manualViewMoved = false;\n }\n }\n if (keepHorizontal) {\n // Restoring the toolbar can cover the cursor even after a zero-height opening.\n if (!keyboardOpen || keyboardInset > 0) {\n const r = layout(),\n v = cursorViewport(),\n x = r.x + cx * r.width;\n place(r.x + bounded(x, v.minX, v.maxX) - x, r.y, r);\n }\n followRest = { fx, fy, zoom };\n render();\n } else if (\n sideInsetsChanged ||\n (viewerSized && previousBottomInset !== viewportBottomInset)\n ) {\n settlePan();\n render();\n }\n reportViewport();\n if (!message.enabled) {\n for (const button of heldMouse.keys())\n queue({ kind: \"button\", button, down: false, x: cx, y: cy });\n heldMouse.clear();\n resetWheel();\n flush();\n }\n for (const [edge, value] of [\n [\"bottom\", message.bottomInset],\n [\"right\", message.rightInset],\n ])\n if (Number.isFinite(value) && value >= 0 && value <= 4096)\n mouseButtons.style[edge] = value + \"px\";\n showMouseButtons = message.enabled === true;\n for (const name of [\"left\", \"right\", \"wheel\"])\n if (typeof message.labels?.[name] === \"string\")\n find(\"mouse-\" + name).setAttribute(\n \"aria-label\",\n message.labels[name],\n );\n updateMouseButtons();\n break;\n }\n case \"init\":\n viewerSized = false;\n followRest = null;\n cursorNeedsEntry = true;\n stopPanAnimation();\n resetCursor();\n control = false;\n release();\n pending = [];\n sending = false;\n seq = 0;\n epoch = message.epoch;\n clipboardShortcuts =\n config.desktop && message.clipboardShortcuts === true;\n clipboardModifier =\n message.clipboardModifier === \"meta\" ? \"meta\" : \"control\";\n dw = message.width;\n dh = message.height;\n fillHeight = message.fillHeight === true;\n video.muted = !message.audio;\n trickleIce = message.trickleIce === true;\n retries = 0;\n render();\n connect();\n break;\n case \"viewport\":\n fillHeight = message.fillHeight === true;\n release();\n render();\n break;\n case \"answer\":\n if (message.epoch === epoch) void receiveAnswer(message);\n break;\n case \"iceConfig\":\n if (message.epoch === epoch) void receiveIceConfig(message);\n break;\n case \"ice\":\n if (message.epoch === epoch) void receiveIce(message);\n break;\n case \"fallback\":\n if (message.epoch === epoch && message.attemptId === attemptId)\n failRtc(\"host\", message.retry !== false);\n break;\n case \"frame\": {\n if (\"cursor\" in message) receiveCursor(message.cursor);\n else resetCursor();\n const frameEpoch = epoch;\n image.onload = () => {\n if (epoch === frameEpoch) post({ type: \"framePresented\" });\n };\n image.src = \"data:image/jpeg;base64,\" + message.jpeg;\n break;\n }\n case \"control\":\n release();\n control = message.enabled;\n if (!control) showKeyboard(false);\n pending = [];\n updateMouseButtons();\n render();\n break;\n case \"mode\":\n release();\n if (message.mode !== \"pointer\") followRest = null;\n settlePan();\n clearTimeout(touchCursorTimer);\n touchCursorVisible = false;\n mode = message.mode;\n render();\n break;\n case \"fit\":\n followRest = null;\n cursorNeedsEntry = true;\n manualViewMoved = false;\n stopPanAnimation();\n zoom = 1;\n fx = 0.5;\n fy = 0.5;\n render();\n break;\n case \"rightClick\":\n click(2);\n break;\n case \"events\":\n for (const e of message.events) queue(e);\n flush();\n break;\n case \"ack\":\n if (message.epoch === epoch && message.sequence === seq)\n sending = false;\n break;\n case \"stop\":\n showKeyboard(false);\n control = false;\n release();\n pending = [];\n sending = false;\n epoch = null;\n closeRtc(message.preserveFrame === true);\n updateMouseButtons();\n render();\n break;\n }\n }\n render();\n post({ type: \"ready\" });\n return {\n receive: (message) => receive({ data: JSON.stringify(message) }),\n dispose: () => {\n release();\n epoch = null;\n closeRtc(false);\n for (const cleanup of cleanups) cleanup();\n },\n };\n}\n";