Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

feat(remote-desktop): 补齐桌面连接能力并优化窗口交互 (#4715)

Dasheb1fb2ffe30a
diff --git a/apps/desktop/forge.config.ts b/apps/desktop/forge.config.ts
index b06d3222be..39c897a006 100644
--- a/apps/desktop/forge.config.ts
+++ b/apps/desktop/forge.config.ts
@@ -1239,6 +1239,8 @@ function buildRemoteDesktopInput(platform: ForgePlatform, arch: ForgeArch): void
if (location.error || location.status !== 0)
throw new Error('Remote credentials output unavailable');
outputs.push(path.join(location.stdout.trim(), 'cindy-macos-remote-credentials'));
+ fs.cpSync(path.join(location.stdout.trim(), 'CindyRemoteCredentials_CindyRemoteCredentials.bundle'),
+ path.join(destDir, 'CindyRemoteCredentials_CindyRemoteCredentials.bundle'), { recursive: true });
}
const credential = path.join(destDir, 'cindy-macos-remote-credentials');
if (outputs.length === 1) fs.copyFileSync(outputs[0], credential);
diff --git a/apps/desktop/native/remote-desktop/macos-capture.m b/apps/desktop/native/remote-desktop/macos-capture.m
index bf37b2c60a..ea1742bb01 100644
--- a/apps/desktop/native/remote-desktop/macos-capture.m
+++ b/apps/desktop/native/remote-desktop/macos-capture.m
@@ -37,6 +37,35 @@ typedef CGError (*StartStream)(CFTypeRef);
- (void)stream:(SCStream *)stream didStopWithError:(NSError *)error { _exit(5); }
@end
+static NSString *standardCursorShape(NSCursor *cursor) {
+ if (!cursor) return @"default";
+ // Some AppKit standard cursors are unavailable in capture-only processes.
+ // A C array preserves nil entries (and their shape indices); NSArray would
+ // throw before we could skip them, terminating the screen capture process.
+ NSCursor *cursors[] = {
+ NSCursor.arrowCursor, NSCursor.IBeamCursor, NSCursor.pointingHandCursor,
+ NSCursor.crosshairCursor, NSCursor.openHandCursor, NSCursor.closedHandCursor,
+ NSCursor.resizeLeftRightCursor, NSCursor.resizeUpDownCursor,
+ NSCursor.operationNotAllowedCursor, NSCursor.dragCopyCursor,
+ NSCursor.dragLinkCursor, NSCursor.IBeamCursorForVerticalLayout
+ };
+ NSArray<NSString *> *shapes = @[
+ @"default", @"text", @"pointer", @"crosshair", @"grab", @"grabbing",
+ @"ew-resize", @"ns-resize", @"not-allowed", @"copy", @"alias", @"vertical-text"
+ ];
+ NSData *raster = nil;
+ for (NSUInteger i = 0; i < sizeof(cursors) / sizeof(cursors[0]); i++) {
+ NSCursor *candidate = cursors[i];
+ if (!candidate) continue;
+ if ([cursor isEqual:candidate]) return shapes[i];
+ if (!NSEqualSizes(cursor.image.size, candidate.image.size) ||
+ !NSEqualPoints(cursor.hotSpot, candidate.hotSpot)) continue;
+ if (!raster) raster = cursor.image.TIFFRepresentation;
+ if (raster && [raster isEqualToData:candidate.image.TIFFRepresentation]) return shapes[i];
+ }
+ return @"default";
+}
+
static NSDictionary *readCursor(CGDirectDisplayID display) {
// Read on the main queue. currentSystemCursor returns the actual global shape,
// including cursors supplied by other applications (not this helper's cursor).
@@ -67,7 +96,8 @@ static NSDictionary *readCursor(CGDirectDisplayID display) {
@"width": @(size.width), @"height": @(size.height),
@"hotX": @(fmax(0, fmin(size.width, hot.x))),
@"hotY": @(fmax(0, fmin(size.height, hot.y))),
- @"png": [png base64EncodedStringWithOptions:0]
+ @"png": [png base64EncodedStringWithOptions:0],
+ @"shape": standardCursorShape(cursor)
};
}
diff --git a/apps/desktop/native/remote-desktop/macos-capture.test.m b/apps/desktop/native/remote-desktop/macos-capture.test.m
new file mode 100644
index 0000000000..a7f1514e35
--- /dev/null
+++ b/apps/desktop/native/remote-desktop/macos-capture.test.m
@@ -0,0 +1,54 @@
+// Standalone native regression: compile with the same frameworks as macos-capture.m.
+// Including production code keeps this test on the actual shape-recognition path.
+#define main captureHelperMain
+#include "macos-capture.m"
+#undef main
+#import <objc/runtime.h>
+#include <assert.h>
+
+static NSCursor *unavailableCursor(id receiver, SEL selector) { return nil; }
+static NSCursor *testHand;
+static NSCursor *availableHand(id receiver, SEL selector) { return testHand; }
+
+int main(void) {
+ @autoreleasepool {
+ testHand = [[NSCursor alloc] initWithImage:[[NSImage alloc] initWithSize:NSMakeSize(16, 16)]
+ hotSpot:NSZeroPoint];
+ assert(testHand);
+ SEL selectors[] = {
+ @selector(arrowCursor), @selector(IBeamCursor), @selector(pointingHandCursor),
+ @selector(crosshairCursor), @selector(openHandCursor), @selector(closedHandCursor),
+ @selector(resizeLeftRightCursor), @selector(resizeUpDownCursor),
+ @selector(operationNotAllowedCursor), @selector(dragCopyCursor),
+ @selector(dragLinkCursor), @selector(IBeamCursorForVerticalLayout)
+ };
+ const size_t count = sizeof(selectors) / sizeof(selectors[0]);
+ IMP original[sizeof(selectors) / sizeof(selectors[0])];
+ for (size_t i = 0; i < count; i++) {
+ Method method = class_getClassMethod(NSCursor.class, selectors[i]);
+ original[i] = method_getImplementation(method);
+ }
+ @try {
+ // A missing entry must neither abort capture nor shift later mappings.
+ for (size_t i = 0; i < count; i++) {
+ method_setImplementation(class_getClassMethod(NSCursor.class, selectors[i]), (IMP)unavailableCursor);
+ }
+ method_setImplementation(class_getClassMethod(NSCursor.class, selectors[2]), (IMP)availableHand);
+ assert([standardCursorShape(testHand) isEqualToString:@"pointer"]);
+ assert([standardCursorShape(nil) isEqualToString:@"default"]);
+ for (size_t i = 0; i < count; i++) {
+ method_setImplementation(class_getClassMethod(NSCursor.class, selectors[i]), (IMP)unavailableCursor);
+ }
+ assert([standardCursorShape(testHand) isEqualToString:@"default"]);
+ } @catch (NSException *exception) {
+ fprintf(stderr, "FAIL cursor recognition: %s\n", exception.name.UTF8String);
+ return 1;
+ } @finally {
+ for (size_t i = 0; i < count; i++) {
+ method_setImplementation(class_getClassMethod(NSCursor.class, selectors[i]), original[i]);
+ }
+ }
+ puts("PASS cursor recognition: unavailable candidate, stable mapping, nil input, fallback");
+ }
+ return 0;
+}
diff --git a/apps/desktop/native/remote-desktop/macos-input.swift b/apps/desktop/native/remote-desktop/macos-input.swift
index 8d153f0666..eb2da0ec3a 100644
--- a/apps/desktop/native/remote-desktop/macos-input.swift
+++ b/apps/desktop/native/remote-desktop/macos-input.swift
@@ -3,19 +3,51 @@ import ApplicationServices
import AppKit
import IOKit.graphics
import Security
+import Darwin
#if !DESKTOP_INPUT_TEST
// This guard precedes every entrypoint, including selection and permission UI.
guard let inputCaller = DesktopInputCaller.authenticate() else { exit(77) }
#endif
+// Both one-shot and persistent queries recheck the OS session on every read.
+func clipboardCounter(portable: Bool) -> String {
+ let session = CGSessionCopyCurrentDictionary() as? [String: Any]
+ guard let session = session, !(session["CGSSessionScreenIsLocked"] as? Bool ?? false) else { return "unavailable" }
+ if portable && (NSPasteboard.general.pasteboardItems?.count ?? 0) > 1 { return "unsupported" }
+ return String(NSPasteboard.general.changeCount)
+}
+#if !DESKTOP_INPUT_TEST
+if CommandLine.arguments == [CommandLine.arguments[0], "--clipboard-counter"] {
+ // Bounded input and idle exit also release the process if Main disappears.
+ var bytes: [UInt8] = []
+ while true {
+ var descriptor = pollfd(fd: STDIN_FILENO, events: Int16(POLLIN), revents: 0)
+ guard poll(&descriptor, 1, 5000) > 0 else { exit(0) }
+ var byte: UInt8 = 0
+ guard read(STDIN_FILENO, &byte, 1) == 1 else { exit(0) }
+ if byte != 10 {
+ bytes.append(byte)
+ if bytes.count > 32 { exit(2) }
+ continue
+ }
+ autoreleasepool {
+ guard inputCaller.code() != nil else { exit(77) }
+ let parts = String(bytes: bytes, encoding: .utf8)?.split(separator: " ") ?? []
+ guard parts.count == 2, parts[0].count <= 16, UInt64(parts[0]) != nil,
+ parts[1] == "0" || parts[1] == "1" else { exit(2) }
+ print("\(parts[0]) \(clipboardCounter(portable: parts[1] == "1"))"); fflush(stdout)
+ }
+ bytes.removeAll(keepingCapacity: true)
+ }
+}
+#endif
// Expose only the clipboard change counter, never clipboard content on stdout.
if CommandLine.arguments.count == 2 && ["--clipboard-version", "--clipboard-content-version"].contains(CommandLine.arguments[1]) {
- let session = CGSessionCopyCurrentDictionary() as? [String: Any]
- guard let session = session, !(session["CGSSessionScreenIsLocked"] as? Bool ?? false) else { exit(2) }
- if CommandLine.arguments[1] == "--clipboard-content-version",
- (NSPasteboard.general.pasteboardItems?.count ?? 0) > 1 { exit(3) }
- print(NSPasteboard.general.changeCount); exit(0)
+ let value = clipboardCounter(portable: CommandLine.arguments[1] == "--clipboard-content-version")
+ if value == "unavailable" { exit(2) }
+ if value == "unsupported" { exit(3) }
+ print(value); exit(0)
}
// Read the actual accessibility selection instead of attributing arbitrary
diff --git a/apps/desktop/native/remote-desktop/macos-viewer-display.m b/apps/desktop/native/remote-desktop/macos-viewer-display.m
index 476d6692d2..6af9a2004e 100644
--- a/apps/desktop/native/remote-desktop/macos-viewer-display.m
+++ b/apps/desktop/native/remote-desktop/macos-viewer-display.m
@@ -119,7 +119,9 @@ int main(int argc, const char *argv[]) {
if (CGCompleteDisplayConfiguration(config, kCGConfigureForSession) != kCGErrorSuccess) {
emit(@{@"error": @"DISPLAY_MIRROR_FAILED"}); return;
}
- emit(@{@"id": @([display displayID]), @"width": @(width), @"height": @(height)});
+ CGRect bounds = CGDisplayBounds([display displayID]);
+ emit(@{@"id": @([display displayID]), @"width": @(width), @"height": @(height),
+ @"logicalWidth": @(bounds.size.width), @"logicalHeight": @(bounds.size.height)});
} @catch (NSException *exception) {
emit(@{@"error": @"DISPLAY_UNAVAILABLE"});
}
diff --git a/apps/desktop/native/remote-desktop/windows-host/src/cursor.rs b/apps/desktop/native/remote-desktop/windows-host/src/cursor.rs
index 83906e3482..8f75614bf7 100644
--- a/apps/desktop/native/remote-desktop/windows-host/src/cursor.rs
+++ b/apps/desktop/native/remote-desktop/windows-host/src/cursor.rs
@@ -203,6 +203,19 @@ unsafe fn raster(handle: HCURSOR) -> Option<(i32, i32, u32, u32, Vec<u8>)> {
))
}
+fn standard_shape(handle: HCURSOR) -> &'static str {
+ for (id, shape) in [
+ (IDC_ARROW, "default"), (IDC_IBEAM, "text"), (IDC_HAND, "pointer"),
+ (IDC_CROSS, "crosshair"), (IDC_WAIT, "wait"), (IDC_APPSTARTING, "progress"),
+ (IDC_SIZEWE, "ew-resize"), (IDC_SIZENS, "ns-resize"),
+ (IDC_SIZENWSE, "nwse-resize"), (IDC_SIZENESW, "nesw-resize"),
+ (IDC_SIZEALL, "move"), (IDC_NO, "not-allowed"), (IDC_HELP, "help"),
+ ] {
+ if unsafe { LoadCursorW(ptr::null_mut(), id) } == handle { return shape; }
+ }
+ "default"
+}
+
pub fn read(rect: [i32; 4]) -> Option<serde_json::Value> {
unsafe {
let mut cursor: CURSORINFO = mem::zeroed();
@@ -218,7 +231,8 @@ pub fn read(rect: [i32; 4]) -> Option<serde_json::Value> {
"visible": cursor.flags == CURSOR_SHOWING && (0.0..1.0).contains(&cx) && (0.0..1.0).contains(&cy),
"x": cx.clamp(0.0, 1.0), "y": cy.clamp(0.0, 1.0),
"width": width, "height": height, "hotX": hot_x, "hotY": hot_y,
- "bgra": base64::engine::general_purpose::STANDARD.encode(pixels)
+ "bgra": base64::engine::general_purpose::STANDARD.encode(pixels),
+ "shape": standard_shape(cursor.hCursor)
}))
}
}
@@ -227,6 +241,16 @@ pub fn read(rect: [i32; 4]) -> Option<serde_json::Value> {
mod tests {
use super::*;
+ #[test]
+ fn identifies_standard_native_shapes() {
+ for (id, expected) in [(IDC_ARROW, "default"), (IDC_IBEAM, "text"),
+ (IDC_HAND, "pointer"), (IDC_SIZEWE, "ew-resize")] {
+ let handle = unsafe { LoadCursorW(ptr::null_mut(), id) };
+ assert!(!handle.is_null());
+ assert_eq!(standard_shape(handle), expected);
+ }
+ }
+
#[test]
fn preserves_transparency_opaque_color_and_premultiplied_alpha() {
let black = [0, 0, 0, 0, 20, 30, 40, 0, 10, 20, 30, 0];
diff --git a/apps/desktop/native/remote-desktop/windows-input/src/main.rs b/apps/desktop/native/remote-desktop/windows-input/src/main.rs
index cd5b9091da..ed40c4e89f 100644
--- a/apps/desktop/native/remote-desktop/windows-input/src/main.rs
+++ b/apps/desktop/native/remote-desktop/windows-input/src/main.rs
@@ -136,7 +136,48 @@ fn release(keys: &mut HashSet<u16>, buttons: &mut HashSet<u64>) {
}
}
}
+fn clipboard_counter() -> Result<u32, ()> {
+ use windows_sys::Win32::System::{DataExchange::*, StationsAndDesktops::*};
+ unsafe {
+ let desktop = OpenInputDesktop(0, 0, DESKTOP_READOBJECTS);
+ if desktop.is_null() { return Err(()); }
+ let mut name = [0u16; 256];
+ let mut needed = 0;
+ let ok = GetUserObjectInformationW(desktop, UOI_NAME, name.as_mut_ptr().cast(), 512, &mut needed);
+ CloseDesktop(desktop);
+ let length = name.iter().position(|c| *c == 0).unwrap_or(name.len());
+ if ok == 0 || String::from_utf16_lossy(&name[..length]) != "Default" { return Err(()); }
+ Ok(GetClipboardSequenceNumber())
+ }
+}
+fn counter_session() {
+ let (tx, rx) = mpsc::sync_channel(1);
+ std::thread::spawn(move || {
+ let mut input = io::stdin().lock();
+ loop {
+ let mut line = Vec::new();
+ match input.by_ref().take(34).read_until(b'\n', &mut line) {
+ Ok(0) | Err(_) => break,
+ Ok(_) if line.len() > 33 || line.last() != Some(&b'\n') => break,
+ Ok(_) => if tx.send(line).is_err() { break; },
+ }
+ }
+ });
+ while let Ok(line) = rx.recv_timeout(Duration::from_secs(5)) {
+ let Ok(line) = std::str::from_utf8(&line) else { break; };
+ let parts: Vec<_> = line.trim_end().split(' ').collect();
+ if parts.len() != 2 || parts[0].len() > 16 || parts[0].parse::<u64>().is_err()
+ || !["0", "1"].contains(&parts[1]) { break; }
+ let value = clipboard_counter().map(|v| v.to_string()).unwrap_or_else(|_| "unavailable".into());
+ println!("{} {}", parts[0], value);
+ if io::stdout().flush().is_err() { break; }
+ }
+}
fn main() {
+ if std::env::args().nth(1).as_deref() == Some("--clipboard-counter") {
+ counter_session();
+ return;
+ }
if std::env::args().nth(1).as_deref() == Some("--privacy-input") {
privacy::run();
return;
@@ -150,27 +191,9 @@ fn main() {
}
if std::env::args().nth(1).as_deref() == Some("--clipboard-version") {
// The user-session clipboard must never masquerade as secure-desktop data.
- use windows_sys::Win32::System::{DataExchange::*, StationsAndDesktops::*};
- unsafe {
- let desktop = OpenInputDesktop(0, 0, DESKTOP_READOBJECTS);
- if desktop.is_null() {
- std::process::exit(2);
- }
- let mut name = [0u16; 256];
- let mut needed = 0;
- let ok = GetUserObjectInformationW(
- desktop,
- UOI_NAME,
- name.as_mut_ptr().cast(),
- 512,
- &mut needed,
- );
- CloseDesktop(desktop);
- let length = name.iter().position(|c| *c == 0).unwrap_or(name.len());
- if ok == 0 || String::from_utf16_lossy(&name[..length]) != "Default" {
- std::process::exit(2);
- }
- println!("{}", GetClipboardSequenceNumber());
+ match clipboard_counter() {
+ Ok(value) => println!("{value}"),
+ Err(_) => std::process::exit(2),
}
return;
}
diff --git a/apps/desktop/src/main/remote-desktop-viewer/__tests__/connection.test.ts b/apps/desktop/src/main/remote-desktop-viewer/__tests__/connection.test.ts
index c11e3c2617..f356ccda71 100644
--- a/apps/desktop/src/main/remote-desktop-viewer/__tests__/connection.test.ts
+++ b/apps/desktop/src/main/remote-desktop-viewer/__tests__/connection.test.ts
@@ -1,6 +1,7 @@
import { describe, it, expect, vi } from 'vitest';
import { RemoteViewerConnection } from '../connection';
-import type { RemoteDesktopRequest } from '@cindy/device-link';
+import { ClipboardSync, type RemoteDesktopRequest } from '@cindy/device-link';
+import { DEFAULT_VIEWER_PREFERENCES } from '../../../shared/remoteDesktopViewer';
function fixture() {
let owner = 'account-a:1';
@@ -33,6 +34,58 @@ function fixture() {
};
}
describe('standalone remote viewer authority', () => {
+ it('stops counters on blur and orders remote disable after an in-flight enable', async () => {
+ let focused = true;
+ let finish!: () => void;
+ const stop = vi.fn();
+ const tick = vi.spyOn(ClipboardSync.prototype, 'tick').mockResolvedValue(undefined);
+ const request = vi.fn(async (_target, message, check) => {
+ check();
+ if (message.op === 'capabilities') return { clipboardSync: true };
+ if (message.op === 'start') return { lease: 'lease', controlling: false };
+ if (message.op === 'control') return { controlling: true };
+ if (message.op === 'clipboardSync' && message.enabled && !finish)
+ await new Promise<void>((resolve) => {
+ finish = resolve;
+ });
+ return { enabled: message.enabled };
+ });
+ const connection = new RemoteViewerConnection({
+ owner: () => 'owner',
+ request,
+ focused: () => focused,
+ readClipboard: () => '',
+ writeClipboard: () => {},
+ preferences: () => ({ ...DEFAULT_VIEWER_PREFERENCES, clipboardSync: true }),
+ clipboard: { stop, version: async () => '1', read: async () => '', write: async () => '1' },
+ });
+ try {
+ connection.bind({ deviceId: 'target', name: 'Target' });
+ connection.setActive(true);
+ const generation = connection.generation;
+ await connection.request(generation, { op: 'capabilities' });
+ await connection.request(generation, { op: 'start', displayId: '1' });
+ await connection.request(generation, { op: 'control', lease: 'lease', enabled: true });
+ const pending = connection.safety(generation);
+ await vi.waitFor(() => expect(finish).toBeTypeOf('function'));
+ focused = false;
+ const paused = connection.focusChanged();
+ expect(stop).toHaveBeenCalledOnce();
+ finish();
+ await Promise.all([pending, paused]);
+ expect(
+ request.mock.calls.filter(([, m]) => m.op === 'clipboardSync').map(([, m]) => m.enabled),
+ ).toEqual([true, false]);
+ expect(tick).not.toHaveBeenCalled();
+ focused = true;
+ await connection.safety(generation);
+ expect(tick).toHaveBeenCalledOnce();
+ connection.deactivate();
+ expect(stop).toHaveBeenCalledTimes(2);
+ } finally {
+ tick.mockRestore();
+ }
+ });
it.each(['target', 'owner'])(
'does not block a new %s behind the old scope cleanup',
async (changed) => {
@@ -227,3 +280,114 @@ describe('standalone remote viewer authority', () => {
).toHaveLength(1);
});
});
+
+it.each([true, false])(
+ 'explicit close honors lock-on-exit capability %s and cancels authentication before stopping',
+ async (supported) => {
+ const dispose = vi.fn();
+ let finish!: () => void;
+ const request = vi.fn(
+ async (
+ _target: string,
+ message: RemoteDesktopRequest,
+ check: () => void,
+ ): Promise<unknown> => {
+ check();
+ if (message.op === 'capabilities') return { platform: 'darwin', lockOnExit: supported };
+ if (message.op === 'start') return { lease: 'lease', controlling: false };
+ if (message.op === 'stop') {
+ expect(dispose).toHaveBeenCalledOnce();
+ return new Promise<void>((resolve) => {
+ finish = resolve;
+ });
+ }
+ return {};
+ },
+ );
+ const connection = new RemoteViewerConnection({
+ owner: () => 'owner',
+ request,
+ readClipboard: () => '',
+ writeClipboard: () => {},
+ preferences: () => ({
+ audio: true,
+ privacyScreen: false,
+ hostMute: false,
+ clipboardSync: false,
+ lockOnExit: true,
+ }),
+ credentials: { dispose, run: vi.fn() },
+ });
+ connection.bind({ deviceId: 'target', name: 'Target' });
+ connection.setActive(true);
+ dispose.mockClear();
+ const generation = connection.generation;
+ await connection.request(generation, { op: 'capabilities' });
+ await connection.request(generation, { op: 'start', displayId: 'screen' });
+ const closed = connection.close(generation);
+ expect(dispose).toHaveBeenCalledOnce();
+ expect(request.mock.calls.at(-1)?.[1]).toEqual({
+ op: 'stop',
+ lease: 'lease',
+ ...(supported ? { lockScreen: true } : {}),
+ });
+ finish();
+ await closed;
+ },
+);
+
+it.each([false, true])(
+ 'keeps rich clipboard payloads in Main and cancels writes after control release %s',
+ async (release) => {
+ const content = JSON.stringify({ text: 'synthetic text', html: '<b>synthetic text</b>' });
+ let finish!: (value: unknown) => void;
+ const write = vi.fn(
+ async (_json: string, _version: string | undefined, current: () => boolean) => {
+ if (!current()) throw new Error('DESKTOP_STOPPED');
+ return 'new-version';
+ },
+ );
+ const request = vi.fn(
+ async (
+ _target: string,
+ message: RemoteDesktopRequest,
+ check: () => void,
+ ): Promise<unknown> => {
+ check();
+ if (message.op === 'capabilities') return { clipboardContent: true };
+ if (message.op === 'start') return { lease: 'lease', controlling: false };
+ if (message.op === 'control') return { controlling: message.enabled };
+ if (message.op === 'clipboardContent' && message.action === 'copy')
+ return { id: 'transfer', length: content.length };
+ if (message.op === 'clipboardContent' && message.action === 'read')
+ return new Promise((resolve) => {
+ finish = resolve;
+ });
+ return {};
+ },
+ );
+ const connection = new RemoteViewerConnection({
+ owner: () => 'owner',
+ request,
+ readClipboard: () => '',
+ writeClipboard: vi.fn(),
+ clipboard: { version: async () => 'version', read: async () => content, write },
+ });
+ connection.bind({ deviceId: 'target', name: 'Target' });
+ connection.setActive(true);
+ const generation = connection.generation;
+ await connection.request(generation, { op: 'capabilities' });
+ await connection.request(generation, { op: 'start', displayId: 'screen' });
+ await connection.request(generation, { op: 'control', lease: 'lease', enabled: true });
+ const copy = connection.clipboard(generation, 'copy');
+ await vi.waitFor(() => expect(finish).toBeTypeOf('function'));
+ if (release)
+ await connection.request(generation, { op: 'control', lease: 'lease', enabled: false });
+ finish({ data: content });
+ expect(await copy).toEqual(
+ release ? { ok: false, code: 'DESKTOP_VIEW_ONLY' } : { ok: true, result: null },
+ );
+ if (release) expect(write).not.toHaveBeenCalled();
+ else expect(write).toHaveBeenCalledWith(content, undefined, expect.any(Function));
+ },
+);
diff --git a/apps/desktop/src/main/remote-desktop-viewer/__tests__/credentials.test.ts b/apps/desktop/src/main/remote-desktop-viewer/__tests__/credentials.test.ts
new file mode 100644
index 0000000000..ab1126270d
--- /dev/null
+++ b/apps/desktop/src/main/remote-desktop-viewer/__tests__/credentials.test.ts
@@ -0,0 +1,146 @@
+import { beforeEach, expect, it, vi } from 'vitest';
+import { ViewerCredentials } from '../credentials';
+
+const native = vi.hoisted(() => ({ call: vi.fn(), dispose: vi.fn() }));
+vi.mock('../../remote-desktop/credentialHost', () => ({
+ RemoteCredentialHost: class {
+ viewerCall = native.call;
+ dispose = native.dispose;
+ },
+ remoteCredentialHost: {
+ currentToken: () => ({
+ realm: 'global',
+ membership: 'owner',
+ authDevice: 'local',
+ token: 'test-token',
+ }),
+ },
+}));
+vi.mock('../../i18n', () => ({ getResolvedMainLocale: () => 'en' }));
+vi.mock('electron', () => ({ nativeTheme: { shouldUseDarkColors: false } }));
+beforeEach(() => {
+ vi.resetAllMocks();
+});
+const mac = it.runIf(process.platform === 'darwin');
+mac.each(['unlocked', 'unknown'])('does not authenticate when host state is %s', async (state) => {
+ native.call.mockResolvedValue({ autoUnlock: true });
+ const request = vi.fn(async () => ({ version: 1, state })) as any;
+ const credentials = new ViewerCredentials({ request });
+ await credentials.run('target', 'darwin', 'unlock', undefined, () => {});
+ expect(request).toHaveBeenCalledTimes(1);
+ expect(native.call.mock.calls.map((call) => call[1])).toEqual(['viewerSettings']);
+});
+mac(
+ 'preserves a disabled biometric preference when reenabling and never receives a password',
+ async () => {
+ let receives = 0;
+ native.call.mockImplementation(async (_realm, method, args) => {
+ if (method === 'viewerSettings')
+ return { autoUnlock: false, biometricAvailable: true, biometricPreferred: false };
+ if (method === 'viewerBegin') {
+ expect(args.biometric).toBe(false);
+ return { handle: 'local', offer: 'encrypted-offer', descriptor: 'public-local' };
+ }
+ if (method === 'viewerReceive')
+ return JSON.stringify(
+ ++receives === 1 ? { kind: 'ready' } : { kind: 'authenticated', accepted: true },
+ );
+ if (method === 'viewerPassword') return 'ciphertext-only';
+ if (method === 'viewerEnd') return '';
+ return 'encrypted-ready';
+ });
+ const request = vi.fn(async (message: any) => {
+ if (message.kind === 'prepare') {
+ expect(message.setup).toBe(true);
+ return { version: 1, ready: true, descriptor: 'public-host' };
+ }
+ if (message.kind === 'open') return { handle: 'remote', offer: 'encrypted-offer' };
+ return { ciphertext: 'encrypted-reply' };
+ }) as any;
+ await new ViewerCredentials({ request }).run('target', 'darwin', 'enable', undefined, () => {});
+ expect(
+ request.mock.calls.some(([message]: any[]) => message.ciphertext === 'ciphertext-only'),
+ ).toBe(true);
+ expect(native.call.mock.calls.filter((call) => call[1] === 'viewerPassword')).toHaveLength(1);
+ },
+);
+mac('cancels native preparation before it can send a late credential offer', async () => {
+ let finish!: () => void;
+ native.call.mockImplementation(async (_realm, method) =>
+ method === 'viewerSettings'
+ ? {}
+ : new Promise<void>((resolve) => {
+ finish = resolve;
+ }),
+ );
+ const request = vi.fn(async () => ({
+ version: 1,
+ ready: true,
+ descriptor: 'public-host',
+ })) as any;
+ const credentials = new ViewerCredentials({ request });
+ const pending = credentials.run('target', 'darwin', 'enable', undefined, () => {});
+ const rejected = expect(pending).rejects.toThrow('CREDENTIAL_CANCELLED');
+ await vi.waitFor(() => expect(finish).toBeTypeOf('function'));
+ credentials.dispose();
+ finish();
+ await rejected;
+ expect(request).toHaveBeenCalledTimes(1);
+ expect(native.dispose).toHaveBeenCalledOnce();
+});
+
+mac.each(['status', 'prepare', 'open', 'exchange', 'cleanup'])(
+ 'releases cancelled authentication during %s without releasing its replacement',
+ async (stage) => {
+ let finishOld!: (value: unknown) => void;
+ let finishNew!: (value: unknown) => void;
+ let cleanup = false;
+ native.call.mockImplementation(async (_realm, method, args) => {
+ if (method === 'viewerSettings') {
+ if (args.target === 'replacement')
+ return new Promise((resolve) => {
+ finishNew = resolve;
+ });
+ return { autoUnlock: true };
+ }
+ if (method === 'viewerBegin')
+ return { handle: 'local', offer: 'offer', descriptor: 'descriptor' };
+ if (method === 'viewerReceive') return JSON.stringify({ kind: 'ready' });
+ if (method === 'viewerPassword') throw new Error('CREDENTIAL_PASSWORD_REJECTED');
+ if (method === 'viewerEnd') cleanup = true;
+ return 'ciphertext';
+ });
+ const request = vi.fn(async (message: any) => {
+ if (message.kind === stage || (stage === 'cleanup' && cleanup))
+ return new Promise((resolve) => {
+ finishOld = resolve;
+ });
+ if (message.kind === 'status') return { version: 1, state: 'locked' };
+ if (message.kind === 'prepare') return { version: 1, ready: true, descriptor: 'host' };
+ if (message.kind === 'open') return { handle: 'remote', offer: 'offer' };
+ return { ciphertext: 'reply' };
+ }) as any;
+ const credentials = new ViewerCredentials({ request });
+ const old = credentials.run('old', 'darwin', 'unlock', undefined, () => {});
+ const oldRejected = expect(old).rejects.toThrow(
+ stage === 'cleanup' ? 'CREDENTIAL_PASSWORD_REJECTED' : 'CREDENTIAL_CANCELLED',
+ );
+ await vi.waitFor(() => expect(finishOld).toBeTypeOf('function'));
+ credentials.dispose();
+ const replacement = credentials.run('replacement', 'darwin', 'settings', undefined, () => {});
+ await vi.waitFor(() => expect(finishNew).toBeTypeOf('function'));
+ const nativeCalls = native.call.mock.calls.length;
+ finishOld({});
+ await oldRejected;
+ expect(native.call).toHaveBeenCalledTimes(nativeCalls);
+ expect(native.dispose).toHaveBeenCalledOnce();
+ await expect(
+ credentials.run('third', 'darwin', 'settings', undefined, () => {}),
+ ).rejects.toThrow('CREDENTIAL_AUTHENTICATION_BUSY');
+ finishNew({ autoUnlock: true });
+ await expect(replacement).resolves.toMatchObject({ available: true, autoUnlock: true });
+ await expect(
+ credentials.run('third', 'darwin', 'settings', undefined, () => {}),
+ ).resolves.toMatchObject({ available: true });
+ },
+);
diff --git a/apps/desktop/src/main/remote-desktop-viewer/__tests__/safety.test.ts b/apps/desktop/src/main/remote-desktop-viewer/__tests__/safety.test.ts
new file mode 100644
index 0000000000..734b6843ae
--- /dev/null
+++ b/apps/desktop/src/main/remote-desktop-viewer/__tests__/safety.test.ts
@@ -0,0 +1,96 @@
+import { expect, it, vi } from 'vitest';
+import { ClipboardSync, type RemoteDesktopCapabilities } from '@cindy/device-link';
+import { DEFAULT_VIEWER_PREFERENCES } from '../../../shared/remoteDesktopViewer';
+import { ViewerSafety } from '../safety';
+
+function fixture() {
+ const safety = new ViewerSafety();
+ const request = vi.fn(async () => ({ enabled: true })) as any;
+ const options = {
+ lease: 'lease',
+ caps: { privacyScreen: true, hostMute: true, clipboardSync: true } as RemoteDesktopCapabilities,
+ preferences: { ...DEFAULT_VIEWER_PREFERENCES, privacyScreen: true, hostMute: true },
+ current: () => true,
+ clipboardCurrent: () => false,
+ request,
+ };
+ return { safety, options, request };
+}
+it('applies preferences once and prioritizes privacy failures over mute failures', async () => {
+ const { safety, options, request } = fixture();
+ request.mockRejectedValue(new Error('DESKTOP_UNAVAILABLE'));
+ expect((await safety.tick(options)).notice).toBe('privacyFailed');
+ const count = request.mock.calls.length;
+ await safety.tick(options);
+ expect(request.mock.calls.length).toBe(count);
+ safety.invalidate();
+ request.mockResolvedValue({ enabled: true });
+ expect(await safety.tick(options)).toMatchObject({ privacyActive: true, notice: null });
+});
+it('does not apply opt-ins without confirmed control', async () => {
+ const { safety, options, request } = fixture();
+ await safety.tick({ ...options, current: () => false });
+ expect(request).not.toHaveBeenCalled();
+});
+it('focus pause preserves privacy state and does not retry failed mute settings', async () => {
+ const { safety, options, request } = fixture();
+ request.mockImplementation(async (message: { op: string }) => {
+ if (message.op === 'hostMute') throw new Error('DESKTOP_UNAVAILABLE');
+ return { enabled: true };
+ });
+ await safety.tick(options);
+ const before = safety.snapshot();
+ await safety.pauseClipboard();
+ expect(safety.snapshot()).toEqual(before);
+ await safety.tick(options);
+ expect(
+ request.mock.calls.filter(([message]: [{ op: string }]) => message.op === 'privacyScreen'),
+ ).toHaveLength(1);
+ expect(
+ request.mock.calls.filter(([message]: [{ op: string }]) => message.op === 'hostMute'),
+ ).toHaveLength(1);
+});
+it('pauses until old work settles and discards its late success', async () => {
+ const { safety, options, request } = fixture();
+ let finish!: (value: unknown) => void;
+ request.mockImplementationOnce(
+ () =>
+ new Promise((resolve) => {
+ finish = resolve;
+ }),
+ );
+ const pending = safety.tick(options);
+ let paused = false;
+ const pause = safety.pause().then(() => {
+ paused = true;
+ });
+ await Promise.resolve();
+ expect(paused).toBe(false);
+ finish({ enabled: true });
+ await Promise.all([pending, pause]);
+ expect(safety.snapshot()).toMatchObject({ privacyActive: false, notice: null });
+ expect(request).toHaveBeenCalledTimes(1);
+});
+
+it('keeps conflict feedback until content is actually synchronized', async () => {
+ const { safety, options } = fixture();
+ const now = vi.spyOn(Date, 'now').mockReturnValue(10000);
+ const tick = vi
+ .spyOn(ClipboardSync.prototype, 'tick')
+ .mockRejectedValueOnce(new Error('CLIPBOARD_CONFLICT'))
+ .mockResolvedValue(undefined);
+ const sync = {
+ ...options,
+ preferences: { ...options.preferences, clipboardSync: true },
+ clipboardCurrent: () => true,
+ clipboard: { version: async () => 'v', read: async () => '{}', write: async () => 'v' },
+ };
+ try {
+ expect((await safety.tick(sync)).notice).toBe('clipboardSyncConflict');
+ now.mockReturnValue(12000);
+ expect((await safety.tick(sync)).notice).toBe('clipboardSyncConflict');
+ } finally {
+ now.mockRestore();
+ tick.mockRestore();
+ }
+});
diff --git a/apps/desktop/src/main/remote-desktop-viewer/__tests__/windows.test.ts b/apps/desktop/src/main/remote-desktop-viewer/__tests__/windows.test.ts
index 449c717499..5d60b2d6b4 100644
--- a/apps/desktop/src/main/remote-desktop-viewer/__tests__/windows.test.ts
+++ b/apps/desktop/src/main/remote-desktop-viewer/__tests__/windows.test.ts
@@ -8,6 +8,7 @@ const fixture = vi.hoisted(() => ({
}));
vi.mock('electron', () => ({
BrowserWindow: { fromWebContents: () => null },
+ screen: { getDisplayMatching: () => ({ workArea: { x: 0, y: 0, width: 1920, height: 1080 } }) },
clipboard: { readText: () => '', writeText: vi.fn() },
ipcMain: { handle: (name: string, fn: Function) => fixture.handlers.set(name, fn) },
}));
@@ -21,6 +22,23 @@ vi.mock('../../appSessionState', () => ({
vi.mock('../../appCapabilities', () => ({
getAppCapabilities: () => ({ canUseDeviceLink: true }),
}));
+vi.mock('../preferences', () => ({
+ readViewerPreferences: () => ({
+ audio: true,
+ privacyScreen: false,
+ hostMute: false,
+ clipboardSync: false,
+ lockOnExit: false,
+ }),
+ writeViewerPreferences: async (_target: string, patch: object) => ({
+ audio: true,
+ privacyScreen: false,
+ hostMute: false,
+ clipboardSync: false,
+ lockOnExit: false,
+ ...patch,
+ }),
+}));
vi.mock('../../i18n', () => ({ t: (s: string) => s }));
vi.mock('../../device-link/index', () => ({
getSelfDeviceId: () => 'local',
@@ -62,6 +80,12 @@ vi.mock('../../resource-usage-window/window', () => ({
isFullScreen: () => false,
setTitle: vi.fn(),
setFullScreen: vi.fn(),
+ isMaximized: () => false,
+ unmaximize: vi.fn(),
+ getBounds: () => ({ x: 100, y: 100, width: 1000, height: 700 }),
+ getContentBounds: () => ({ x: 100, y: 100, width: 1000, height: 680 }),
+ getMinimumSize: () => [720, 420],
+ setBounds: vi.fn(),
restore: vi.fn(),
focus: vi.fn(),
isFocused: () => visible,
@@ -95,7 +119,7 @@ afterEach(() => {
const event = (win: any) => ({ sender: win.webContents, senderFrame: win.webContents.mainFrame });
const call = (channel: string, win: any, ...args: unknown[]) =>
fixture.handlers.get(channel)!(event(win), ...args);
-it('routes native close and the close shortcut to confirmation without ending the lease', () => {
+it('routes native close and the close shortcut to confirmation without ending the lease', async () => {
const sender: any = { id: 100 };
manager = new RemoteDesktopViewerWindows((value) => value === sender);
manager.register();
@@ -122,12 +146,12 @@ it('routes native close and the close shortcut to confirmation without ending th
);
expect(win.isVisible()).toBe(true);
expect(call(REMOTE_VIEWER.STATE, win).active).toBe(true);
- call(REMOTE_VIEWER.CLOSE, win, state.generation - 1);
+ await call(REMOTE_VIEWER.CLOSE, win, state.generation - 1);
expect(win.isVisible()).toBe(true);
- call(REMOTE_VIEWER.CLOSE, win, state.generation);
+ await call(REMOTE_VIEWER.CLOSE, win, state.generation);
expect(win.isVisible()).toBe(false);
manager.open(sender, { deviceId: 'a', name: 'A' });
- call(REMOTE_VIEWER.CLOSE, win, state.generation);
+ await call(REMOTE_VIEWER.CLOSE, win, state.generation);
expect(call(REMOTE_VIEWER.STATE, win).active).toBe(true);
expect(win.isVisible()).toBe(true);
});
@@ -180,3 +204,50 @@ it('rejects subframes and stale window/account generations even with a valid cha
).rejects.toThrow('DESKTOP_STOPPED');
expect(fixture.calls).toHaveLength(0);
});
+
+it('resizes only the bound active viewer and clamps actual size to the display work area', () => {
+ const sender: any = { id: 100 };
+ manager = new RemoteDesktopViewerWindows((value) => value === sender);
+ manager.register();
+ manager.open(sender, { deviceId: 'a', name: 'A' });
+ const win = fixture.windows[0];
+ const { generation } = call(REMOTE_VIEWER.STATE, win);
+ call(REMOTE_VIEWER.RESIZE, win, generation, 1280, 780);
+ expect(win.setBounds).toHaveBeenLastCalledWith({ x: 0, y: 50, width: 1280, height: 800 });
+ call(REMOTE_VIEWER.RESIZE, win, generation, 3840, 2220);
+ expect(win.setBounds).toHaveBeenLastCalledWith({ x: 0, y: 0, width: 1920, height: 1080 });
+ expect(() => call(REMOTE_VIEWER.RESIZE, win, generation, NaN, 600)).toThrow(
+ 'Invalid viewer size',
+ );
+ expect(() => call(REMOTE_VIEWER.RESIZE, win, generation - 1, 800, 600)).toThrow(
+ 'DESKTOP_STOPPED',
+ );
+ expect(() =>
+ fixture.handlers.get(REMOTE_VIEWER.RESIZE)!(
+ { sender: win.webContents, senderFrame: {} },
+ generation,
+ 800,
+ 600,
+ ),
+ ).toThrow('Invalid viewer');
+});
+
+it('waits for fullscreen exit and rejects delayed resize after a session replacement', () => {
+ const sender: any = { id: 100 };
+ manager = new RemoteDesktopViewerWindows((value) => value === sender);
+ manager.register();
+ manager.open(sender, { deviceId: 'a', name: 'A' });
+ const win = fixture.windows[0];
+ const { generation } = call(REMOTE_VIEWER.STATE, win);
+ win.isFullScreen = () => true;
+ call(REMOTE_VIEWER.RESIZE, win, generation, 1280, 780);
+ expect(win.setFullScreen).toHaveBeenCalledWith(false);
+ expect(win.setBounds).not.toHaveBeenCalled();
+ win.emit('leave-full-screen');
+ expect(win.setBounds).toHaveBeenCalledOnce();
+ call(REMOTE_VIEWER.RESIZE, win, generation, 1280, 780);
+ manager.reset();
+ win.setBounds.mockClear();
+ win.emit('leave-full-screen');
+ expect(win.setBounds).not.toHaveBeenCalled();
+});
diff --git a/apps/desktop/src/main/remote-desktop-viewer/connection.ts b/apps/desktop/src/main/remote-desktop-viewer/connection.ts
index a5eb32e8fd..86b69f6bcd 100644
--- a/apps/desktop/src/main/remote-desktop-viewer/connection.ts
+++ b/apps/desktop/src/main/remote-desktop-viewer/connection.ts
@@ -4,12 +4,21 @@ import {
REMOTE_DESKTOP_MAX_CLIPBOARD_CHARS,
type RemoteDesktopRequest,
type RemoteDesktopLease,
+ type RemoteDesktopCapabilities,
+ transferClipboardContent,
} from '@cindy/device-link';
import type {
RemoteViewerReply,
RemoteViewerTarget,
RemoteViewerState,
} from '../../shared/remoteDesktopViewer.js';
+import {
+ DEFAULT_VIEWER_PREFERENCES,
+ type RemoteViewerPreferences,
+ type RemoteViewerSafety,
+} from '../../shared/remoteDesktopViewer.js';
+import { ViewerSafety, type ViewerClipboard } from './safety.js';
+import type { ViewerCredentials } from './credentials.js';
/** Main owns the target, account generation and lease for one lightweight viewer.
* A destroyed/hidden window cannot leave late starts or inputs alive. No closeLink.
@@ -30,12 +39,25 @@ export class RemoteViewerConnection {
private controlPending = false;
private clipboardPending: object | null = null;
private lifecycle: Promise<void> | null = null;
+ private caps: RemoteDesktopCapabilities | null = null;
+ private preferencesValue = { ...DEFAULT_VIEWER_PREFERENCES };
+ private safetyState = new ViewerSafety();
+ private clipboardProgress: number | null = null;
+ private closing = false;
constructor(
private readonly deps: {
owner(): string;
request(deviceId: string, request: RemoteDesktopRequest, check: () => void): Promise<unknown>;
readClipboard(): string;
writeClipboard(value: string): void;
+ clipboard?: ViewerClipboard;
+ credentials?: Pick<ViewerCredentials, 'run' | 'dispose'>;
+ focused?(): boolean;
+ preferences?(device: string): RemoteViewerPreferences;
+ savePreferences?(
+ device: string,
+ patch: Partial<RemoteViewerPreferences>,
+ ): Promise<RemoteViewerPreferences>;
},
) {}
bind(target: RemoteViewerTarget): void {
@@ -46,6 +68,12 @@ export class RemoteViewerConnection {
this.owner = owner;
this.target = target;
this.attempted = false;
+ this.caps = null;
+ this.closing = false;
+ this.preferencesValue = this.deps.preferences?.(target.deviceId) ?? {
+ ...DEFAULT_VIEWER_PREFERENCES,
+ };
+ this.safetyState.invalidate(true);
}
snapshot(): RemoteViewerState {
return {
@@ -102,6 +130,10 @@ export class RemoteViewerConnection {
return pending;
}
deactivate(): void {
+ this.closing = false;
+ this.safetyState.invalidate();
+ this.clipboardProgress = null;
+ this.deps.credentials?.dispose();
this.generation++;
this.active = false;
const lease = this.lease,
@@ -129,6 +161,7 @@ export class RemoteViewerConnection {
): Promise<RemoteViewerReply> {
try {
this.check(generation);
+ if (this.closing) throw new Error('DESKTOP_STOPPED');
const request = parseRemoteDesktopRequest(value);
const check = () =>
request.op === 'offer' || request.op === 'ice'
@@ -138,7 +171,7 @@ export class RemoteViewerConnection {
if ('lease' in request && request.lease !== this.lease)
throw new Error('DESKTOP_LEASE_EXPIRED');
if (request.op === 'start' && this.starting) throw new Error('DESKTOP_BUSY');
- // The renderer has no generic local clipboard API, password bridge, or lock-on-exit policy.
+ // Clipboard contents and credentials are handled only by dedicated Main adapters.
if (
request.op === 'clipboard' ||
request.op === 'clipboardContent' ||
@@ -152,6 +185,7 @@ export class RemoteViewerConnection {
const isStart = request.op === 'start';
const isControl = request.op === 'control';
if (isControl || request.op === 'stop' || isStart) {
+ this.safetyState.invalidate();
this.controlGeneration++;
this.wantsControl = isControl && request.enabled;
this.controlling = false;
@@ -171,6 +205,10 @@ export class RemoteViewerConnection {
throw new Error('DESKTOP_LEASE_EXPIRED');
if (isStart) this.attempted = true;
const result = await this.deps.request(target.deviceId, request, check);
+ if (request.op === 'capabilities') {
+ check();
+ this.caps = result as RemoteDesktopCapabilities;
+ }
if (isStart) {
const lease = result as RemoteDesktopLease;
if (!lease || typeof lease.lease !== 'string') throw new Error('DESKTOP_UNAVAILABLE');
@@ -191,12 +229,14 @@ export class RemoteViewerConnection {
(isControl ||
(request.op === 'heartbeat' && !controlPendingAtStart && !this.controlPending))
) {
+ const wasControlling = this.controlling;
this.controlling =
this.wantsControl &&
!!result &&
typeof result === 'object' &&
'controlling' in result &&
result.controlling === true;
+ if (wasControlling && !this.controlling) this.safetyState.invalidate();
}
if (request.op === 'stop' && this.lease === request.lease) this.lease = null;
return result;
@@ -230,7 +270,40 @@ export class RemoteViewerConnection {
if (!this.controlling || controlGeneration !== this.controlGeneration)
throw new Error('DESKTOP_VIEW_ONLY');
};
- if (action === 'copy') {
+ if (action !== 'copy' && action !== 'paste') throw new Error('INVALID_REQUEST');
+ if (this.caps?.clipboardContent && this.deps.clipboard) {
+ const current = () => {
+ try {
+ check();
+ return true;
+ } catch {
+ return false;
+ }
+ };
+ await this.safetyState.pause();
+ check();
+ this.clipboardProgress = 0;
+ await transferClipboardContent(
+ action,
+ lease,
+ async (message) => {
+ check();
+ const result = await this.deps.request(this.target!.deviceId, message, check);
+ check();
+ return result as never;
+ },
+ check,
+ {
+ read: () => this.deps.clipboard!.read(current),
+ write: async (json) => {
+ await this.deps.clipboard!.write(json, undefined, current);
+ },
+ progress: (done, total) => {
+ if (current()) this.clipboardProgress = done / total;
+ },
+ },
+ );
+ } else if (action === 'copy') {
const result = (await this.deps.request(
this.target!.deviceId,
{ op: 'clipboard', lease, action: 'copy' },
@@ -259,7 +332,113 @@ export class RemoteViewerConnection {
} catch (error) {
return viewerFailure(error);
} finally {
- if (this.clipboardPending === transfer) this.clipboardPending = null;
+ if (this.clipboardPending === transfer) {
+ this.clipboardPending = null;
+ this.clipboardProgress = null;
+ }
+ }
+ }
+ async preferences(
+ generation: number,
+ patch?: Partial<RemoteViewerPreferences>,
+ ): Promise<RemoteViewerPreferences> {
+ this.check(generation);
+ if (patch !== undefined) {
+ if (!this.deps.savePreferences) throw new Error('DESKTOP_UNAVAILABLE');
+ const value = await this.deps.savePreferences(this.target!.deviceId, patch);
+ this.check(generation);
+ const reset = value.clipboardSync !== this.preferencesValue.clipboardSync;
+ this.preferencesValue = value;
+ this.safetyState.invalidate(reset);
+ }
+ return { ...this.preferencesValue };
+ }
+ async focusChanged(): Promise<void> {
+ const generation = this.generation;
+ // Cancel local reads immediately, then reconcile after any old enable has
+ // settled. Use current focus when reconciling (blur/focus may race).
+ await this.safetyState.pauseClipboard();
+ if (!this.active || generation !== this.generation) return;
+ await this.safety(generation).catch(() => {});
+ }
+ async safety(generation: number, retry = false): Promise<RemoteViewerSafety> {
+ this.check(generation);
+ if (retry) this.safetyState.invalidate(true);
+ const lease = this.lease,
+ revision = this.controlGeneration;
+ const current = () => {
+ try {
+ this.check(generation);
+ return this.controlling && this.lease === lease && this.controlGeneration === revision;
+ } catch {
+ return false;
+ }
+ };
+ const value =
+ lease && this.caps
+ ? await this.safetyState.tick({
+ lease,
+ caps: this.caps,
+ preferences: {
+ ...this.preferencesValue,
+ clipboardSync: this.preferencesValue.clipboardSync && (this.deps.focused?.() ?? true),
+ },
+ current,
+ clipboardCurrent: () =>
+ current() && !this.clipboardPending && (this.deps.focused?.() ?? true),
+ clipboard: this.deps.clipboard,
+ request: async (message, check) => {
+ check();
+ const result = await this.deps.request(this.target!.deviceId, message, check);
+ check();
+ return result as never;
+ },
+ })
+ : this.safetyState.snapshot();
+ this.check(generation);
+ return { ...value, clipboardProgress: this.clipboardProgress };
+ }
+ async credential(generation: number, action: unknown, enabled: unknown) {
+ this.check(generation);
+ if (this.closing) throw new Error('DESKTOP_STOPPED');
+ if (!this.deps.credentials) throw new Error('CREDENTIAL_UNAVAILABLE');
+ return this.deps.credentials.run(
+ this.target!.deviceId,
+ this.caps?.platform,
+ action,
+ enabled,
+ () => {
+ this.check(generation);
+ if (this.closing) throw new Error('DESKTOP_STOPPED');
+ },
+ );
+ }
+ async close(generation: number): Promise<void> {
+ this.check(generation);
+ if (this.closing) throw new Error('DESKTOP_INPUT_BUSY');
+ this.closing = true;
+ this.deps.credentials?.dispose();
+ const lease = this.lease;
+ if (!lease) return;
+ const lockScreen = this.preferencesValue.lockOnExit && this.caps?.lockOnExit === true;
+ this.safetyState.invalidate();
+ this.controlling = false;
+ this.controlGeneration++;
+ try {
+ await this.serializeLifecycle(async () => {
+ this.check(generation);
+ if (this.lease !== lease) return;
+ await this.deps.request(
+ this.target!.deviceId,
+ { op: 'stop', lease, ...(lockScreen ? { lockScreen: true } : {}) },
+ () => this.check(generation),
+ );
+ this.check(generation);
+ if (this.lease === lease) this.lease = null;
+ });
+ } catch (error) {
+ if (generation === this.generation) this.closing = false;
+ throw error;
}
}
}
diff --git a/apps/desktop/src/main/remote-desktop-viewer/credentials.ts b/apps/desktop/src/main/remote-desktop-viewer/credentials.ts
new file mode 100644
index 0000000000..2826b12175
--- /dev/null
+++ b/apps/desktop/src/main/remote-desktop-viewer/credentials.ts
@@ -0,0 +1,180 @@
+import type { RemoteCredentialRequest } from '@cindy/device-link';
+import type { RemoteViewerCredentialState } from '../../shared/remoteDesktopViewer';
+import { RemoteCredentialHost, remoteCredentialHost } from '../remote-desktop/credentialHost';
+import { getResolvedMainLocale } from '../i18n';
+import { nativeTheme } from 'electron';
+
+const unavailable: RemoteViewerCredentialState = {
+ available: false,
+ autoUnlock: false,
+ biometricAvailable: false,
+ biometricVerification: false,
+};
+
+/** Password entry, encryption and storage stay in the signed native helper. */
+export class ViewerCredentials {
+ private native = new RemoteCredentialHost();
+ private pending = false;
+ private revision = 0;
+ constructor(
+ private readonly deps: {
+ request<T>(
+ message:
+ | RemoteCredentialRequest
+ | { op: 'credential'; version: 1; kind: 'status' | 'prepare'; setup?: boolean },
+ check: () => void,
+ ): Promise<T>;
+ },
+ ) {}
+ dispose(): void {
+ this.revision++;
+ this.pending = false;
+ this.native.dispose();
+ }
+ async run(
+ target: string,
+ hostPlatform: string | undefined,
+ action: unknown,
+ enabled: unknown,
+ check: () => void,
+ ): Promise<RemoteViewerCredentialState> {
+ check();
+ if (process.platform !== 'darwin' || hostPlatform !== 'darwin') return { ...unavailable };
+ if (
+ !['settings', 'enable', 'disable', 'unlock', 'biometric'].includes(String(action)) ||
+ (action === 'biometric' && typeof enabled !== 'boolean')
+ )
+ throw new Error('CREDENTIAL_INVALID_MESSAGE');
+ if (this.pending) throw new Error('CREDENTIAL_AUTHENTICATION_BUSY');
+ this.pending = true;
+ const revision = this.revision;
+ const current = () => {
+ check();
+ if (revision !== this.revision) throw new Error('CREDENTIAL_CANCELLED');
+ };
+ const credentials = remoteCredentialHost.currentToken?.();
+ if (!credentials) {
+ this.pending = false;
+ throw new Error('CREDENTIAL_INVALID_IDENTITY');
+ }
+ const args = { ...credentials, target };
+ const call = async <T>(method: string, values: Record<string, unknown> = {}): Promise<T> => {
+ current();
+ const result = await this.native.viewerCall(credentials.realm, method, values);
+ current();
+ return result as T;
+ };
+ let local: { handle: string; offer: string; descriptor: string } | undefined;
+ let host: { handle: string; offer: string } | undefined;
+ try {
+ let biometricPreferred = true;
+ const settings = async (): Promise<RemoteViewerCredentialState> => {
+ const value = await call<Record<string, boolean>>('viewerSettings', args);
+ biometricPreferred = value.biometricPreferred !== false;
+ return {
+ available: true,
+ autoUnlock: value.autoUnlock === true,
+ biometricAvailable: value.biometricAvailable === true,
+ biometricVerification: value.biometricVerification === true,
+ };
+ };
+ if (action === 'disable') {
+ await call('viewerForget', args);
+ return settings();
+ }
+ if (action === 'biometric') {
+ await call('viewerBiometric', { ...args, enabled, locale: getResolvedMainLocale() });
+ return settings();
+ }
+ const state = await settings();
+ if (action === 'settings' || (action === 'unlock' && !state.autoUnlock)) return state;
+ if (action === 'unlock') {
+ const status = await this.deps.request<{ version: number; state: string }>(
+ { op: 'credential', version: 1, kind: 'status' },
+ current,
+ );
+ current();
+ if (status.version !== 1 || status.state !== 'locked') return state;
+ }
+ const [configured, preparation] = await Promise.allSettled([
+ call('viewerConfigure', args),
+ this.deps.request<{ version: number; ready: boolean; descriptor: string }>(
+ { op: 'credential', version: 1, kind: 'prepare', setup: action === 'enable' },
+ current,
+ ),
+ ]);
+ current();
+ if (configured.status === 'rejected') throw configured.reason;
+ if (preparation.status === 'rejected') throw preparation.reason;
+ const prepared = preparation.value;
+ if (prepared.version !== 1 || !prepared.ready || typeof prepared.descriptor !== 'string')
+ throw new Error('CREDENTIAL_UNAVAILABLE');
+ local = await call<{ handle: string; offer: string; descriptor: string }>('viewerBegin', {
+ target,
+ descriptor: prepared.descriptor,
+ setup: action === 'enable',
+ biometric: biometricPreferred && state.biometricAvailable,
+ });
+ host = await this.deps.request<{ handle: string; offer: string }>(
+ {
+ op: 'credential',
+ version: 1,
+ kind: 'open',
+ offer: local.offer,
+ descriptor: local.descriptor,
+ },
+ current,
+ );
+ current();
+ const exchange = async (ciphertext: string) => {
+ const reply = await this.deps.request<{ ciphertext: string }>(
+ { op: 'credential', version: 1, kind: 'exchange', handle: host!.handle, ciphertext },
+ current,
+ );
+ current();
+ const json = await call<string>('viewerReceive', {
+ handle: local!.handle,
+ ciphertext: reply.ciphertext,
+ });
+ return JSON.parse(json) as { kind: string; saved?: boolean; accepted?: boolean };
+ };
+ const ready = await exchange(
+ await call<string>('viewerAccept', { handle: local.handle, offer: host.offer }),
+ );
+ if (ready.kind !== 'ready') throw new Error('CREDENTIAL_INVALID_MESSAGE');
+ const ciphertext = await call<string>('viewerPassword', {
+ handle: local.handle,
+ saved: action !== 'enable' && ready.saved === true,
+ locale: getResolvedMainLocale(),
+ theme: nativeTheme.shouldUseDarkColors ? 'dark' : 'light',
+ });
+ let result;
+ try {
+ result = await exchange(ciphertext);
+ } catch (error) {
+ if (!(error instanceof Error) || error.message !== 'INVOKE_TIMEOUT') throw error;
+ // Never replay a password after an uncertain response.
+ result = await exchange(
+ await call<string>('viewerAuthenticationStatus', { handle: local.handle }),
+ );
+ }
+ if (result.kind !== 'authenticated' || !result.accepted)
+ throw new Error('CREDENTIAL_PASSWORD_REJECTED');
+ return await settings();
+ } finally {
+ if (local && revision === this.revision) {
+ try {
+ const ciphertext = await call<string>('viewerEnd', { handle: local.handle });
+ if (ciphertext && host)
+ await this.deps.request(
+ { op: 'credential', version: 1, kind: 'exchange', handle: host.handle, ciphertext },
+ current,
+ );
+ } catch {
+ /* Lease and ordinary viewing do not depend on optional authentication. */
+ }
+ }
+ if (revision === this.revision) this.pending = false;
+ }
+ }
+}
diff --git a/apps/desktop/src/main/remote-desktop-viewer/preferences.ts b/apps/desktop/src/main/remote-desktop-viewer/preferences.ts
new file mode 100644
index 0000000000..5ce3cabb0a
--- /dev/null
+++ b/apps/desktop/src/main/remote-desktop-viewer/preferences.ts
@@ -0,0 +1,69 @@
+import { createHash } from 'node:crypto';
+import { activeOwnerScopeKey, ownerScopedUserDataPath } from '../appSessionState';
+import { createOverrideSettingsFile } from '../maker-host/override-settings-file';
+import { createLogger } from '../logger';
+import {
+ DEFAULT_VIEWER_PREFERENCES,
+ type RemoteViewerPreferences,
+} from '../../shared/remoteDesktopViewer';
+
+type Overrides = Record<string, Partial<RemoteViewerPreferences>>;
+const store = createOverrideSettingsFile<Overrides>({
+ filePath: () => ownerScopedUserDataPath('remote-desktop-viewer.json'),
+ scopeKey: activeOwnerScopeKey,
+ defaults: {},
+ normalize: (raw) => {
+ const result: Overrides = {};
+ if (!raw || typeof raw !== 'object') return result;
+ for (const [device, value] of Object.entries(raw)) {
+ if (!/^[a-f0-9]{64}$/.test(device) || !value || typeof value !== 'object') continue;
+ const patch: Partial<RemoteViewerPreferences> = {};
+ for (const key of Object.keys(
+ DEFAULT_VIEWER_PREFERENCES,
+ ) as (keyof RemoteViewerPreferences)[]) {
+ if (key in value && typeof value[key] === 'boolean') patch[key] = value[key];
+ }
+ result[device] = patch;
+ }
+ return result;
+ },
+ maxBytes: 256 * 1024,
+ preserveUnreadableFile: true,
+ logLoadedValue: false,
+ logReadErrorDetails: false,
+ log: createLogger('remote-viewer-preferences'),
+ label: 'remote viewer',
+});
+const keyFor = (device: string) => createHash('sha256').update(device).digest('hex');
+export function readViewerPreferences(device: string): RemoteViewerPreferences {
+ store.invalidateIfChanged();
+ return { ...DEFAULT_VIEWER_PREFERENCES, ...store.read()[keyFor(device)] };
+}
+export async function writeViewerPreferences(
+ device: string,
+ patch: Partial<RemoteViewerPreferences>,
+): Promise<RemoteViewerPreferences> {
+ if (
+ !patch ||
+ typeof patch !== 'object' ||
+ Array.isArray(patch) ||
+ Object.entries(patch).some(
+ ([key, value]) =>
+ !Object.hasOwn(DEFAULT_VIEWER_PREFERENCES, key) || typeof value !== 'boolean',
+ )
+ )
+ throw new Error('INVALID_REQUEST');
+ const key = keyFor(device);
+ const result = await store.updateAtomic(({ value }) => {
+ const next = { ...DEFAULT_VIEWER_PREFERENCES, ...value[key], ...patch };
+ return {
+ [key]: Object.fromEntries(
+ Object.entries(next).filter(
+ ([name, value]) =>
+ value !== DEFAULT_VIEWER_PREFERENCES[name as keyof RemoteViewerPreferences],
+ ),
+ ),
+ };
+ });
+ return { ...DEFAULT_VIEWER_PREFERENCES, ...result[key] };
+}
diff --git a/apps/desktop/src/main/remote-desktop-viewer/safety.ts b/apps/desktop/src/main/remote-desktop-viewer/safety.ts
new file mode 100644
index 0000000000..aa4b5a217d
--- /dev/null
+++ b/apps/desktop/src/main/remote-desktop-viewer/safety.ts
@@ -0,0 +1,192 @@
+import { createHash } from 'node:crypto';
+import {
+ ClipboardSync,
+ clipboardSyncFailure,
+ parseClipboardContent,
+ type ClipboardSyncBaseline,
+ type LocalClipboardReadCache,
+ type RemoteDesktopCapabilities,
+ type RemoteDesktopRequest,
+} from '@cindy/device-link';
+import type { RemoteViewerPreferences, RemoteViewerSafety } from '../../shared/remoteDesktopViewer';
+
+export interface ViewerClipboard {
+ version(current: () => boolean): Promise<string>;
+ stop?(): void;
+ read(current: () => boolean): Promise<string>;
+ write(json: string, version: string | undefined, current: () => boolean): Promise<string>;
+}
+
+/** One lease's opt-ins. Clipboard payloads stay in Main; callers only receive status. */
+export class ViewerSafety {
+ private clipboard: ViewerClipboard | undefined;
+ private revision = 0;
+ private clipboardRevision = 0;
+ private pending = false;
+ private settled: Promise<void> = Promise.resolve();
+ async pause(): Promise<void> {
+ this.invalidate();
+ await this.settled;
+ }
+ async pauseClipboard(): Promise<void> {
+ this.clipboardRevision++;
+ this.clipboard?.stop?.();
+ await this.settled;
+ // Focus changes must not retry privacy/mute failures or clear their status.
+ this.applied.delete('clipboardSync');
+ this.retryAt = 0;
+ }
+ private baseline: ClipboardSyncBaseline = { local: '', remote: '' };
+ private localReadCache: LocalClipboardReadCache = {};
+ private applied = new Map<string, boolean>();
+ private notices = new Map<string, string>();
+ private retryAt = 0;
+ private failures = 0;
+ private privacyActive = false;
+ invalidate(resetBaseline = false): void {
+ this.revision++;
+ this.clipboard?.stop?.();
+ this.applied.clear();
+ this.notices.clear();
+ this.privacyActive = false;
+ this.retryAt = 0;
+ this.failures = 0;
+ if (resetBaseline) {
+ this.baseline = { local: '', remote: '' };
+ this.localReadCache = {};
+ }
+ }
+ snapshot(): RemoteViewerSafety {
+ return {
+ privacyActive: this.privacyActive,
+ notice:
+ this.notices.get('privacyScreen') ??
+ this.notices.get('hostMute') ??
+ this.notices.get('clipboardSync') ??
+ null,
+ clipboardProgress: null,
+ };
+ }
+ async tick(options: {
+ lease: string;
+ caps: RemoteDesktopCapabilities;
+ preferences: RemoteViewerPreferences;
+ current(): boolean;
+ clipboardCurrent(): boolean;
+ clipboard?: ViewerClipboard;
+ request<T>(request: RemoteDesktopRequest, check: () => void): Promise<T>;
+ }): Promise<RemoteViewerSafety> {
+ if (this.pending || !options.current()) return this.snapshot();
+ this.clipboard = options.clipboard;
+ this.pending = true;
+ let settle!: () => void;
+ this.settled = new Promise((resolve) => {
+ settle = resolve;
+ });
+ const revision = this.revision;
+ const clipboardRevision = this.clipboardRevision;
+ const valid = () => revision === this.revision && options.current();
+ const check = () => {
+ if (!valid()) throw new Error('DESKTOP_STOPPED');
+ };
+ const request = <T>(message: RemoteDesktopRequest) => options.request<T>(message, check);
+ const { caps, lease, preferences } = options;
+ try {
+ for (const op of ['privacyScreen', 'hostMute', 'clipboardSync'] as const) {
+ if (op === 'clipboardSync' && Date.now() < this.retryAt) continue;
+ if (!caps[op] || this.applied.get(op) === preferences[op]) continue;
+ try {
+ check();
+ const result = await request<{ enabled?: boolean }>({
+ op,
+ lease,
+ enabled: preferences[op],
+ ...(op === 'privacyScreen' ? { lockOnExit: preferences.lockOnExit } : {}),
+ });
+ check();
+ this.applied.set(op, preferences[op]);
+ if (op === 'privacyScreen') this.privacyActive = result.enabled === true;
+ this.notices.delete(op);
+ } catch (error) {
+ if (!valid()) return this.snapshot();
+ this.notices.set(
+ op,
+ op === 'privacyScreen'
+ ? 'privacyFailed'
+ : op === 'hostMute'
+ ? 'hostMuteFailed'
+ : 'clipboardSyncFailed',
+ );
+ // Retry is explicit for safety failures; synchronization follows its existing backoff below.
+ if (op === 'clipboardSync') {
+ const failure = clipboardSyncFailure(error, this.failures++);
+ this.retryAt = failure.delay === null ? Infinity : Date.now() + failure.delay;
+ this.notices.set(op, failure.notice);
+ } else this.applied.set(op, preferences[op]);
+ }
+ }
+ if (
+ !preferences.clipboardSync ||
+ !caps.clipboardSync ||
+ !options.clipboard ||
+ this.applied.get('clipboardSync') !== true ||
+ Date.now() < this.retryAt ||
+ !options.clipboardCurrent()
+ )
+ return this.snapshot();
+ const current = () =>
+ valid() && clipboardRevision === this.clipboardRevision && options.clipboardCurrent();
+ const clipboardCheck = () => {
+ if (!current()) throw new Error('DESKTOP_STOPPED');
+ };
+ const engine = new ClipboardSync(
+ {
+ lease,
+ current,
+ inline: caps.clipboardInline === true,
+ request: (message) => options.request(message, clipboardCheck),
+ localVersion: () => options.clipboard!.version(current),
+ readLocal: () => options.clipboard!.read(current),
+ writeLocal: (json, version) => options.clipboard!.write(json, version, current),
+ localReadCache: this.localReadCache,
+ trace: (stage) => {
+ if (!valid()) return;
+ if (stage === 'content-skipped')
+ this.notices.set('clipboardSync', 'clipboardSyncSkipped');
+ else if (['phone-to-computer-complete', 'computer-to-phone-complete'].includes(stage))
+ this.notices.delete('clipboardSync');
+ },
+ digest: (json) => {
+ const c = parseClipboardContent(json);
+ return createHash('sha256')
+ .update(JSON.stringify([c.text, c.html, c.rtf, c.url, c.png]))
+ .digest('hex');
+ },
+ },
+ this.baseline,
+ );
+ try {
+ await engine.tick();
+ if (valid()) {
+ if (
+ ['clipboardSyncFailed', 'clipboardSyncPermission'].includes(
+ this.notices.get('clipboardSync') ?? '',
+ )
+ )
+ this.notices.delete('clipboardSync');
+ this.failures = 0;
+ }
+ } catch (error) {
+ if (!valid()) return this.snapshot();
+ const failure = clipboardSyncFailure(error, this.failures++);
+ if (failure.code === 'DESKTOP_CLIPBOARD_UNAVAILABLE') this.applied.delete('clipboardSync');
+ this.notices.set('clipboardSync', failure.notice);
+ this.retryAt = failure.delay === null ? Infinity : Date.now() + failure.delay;
+ }
+ return this.snapshot();
+ } finally {
+ this.pending = false;
+ settle();
+ }
+ }
+}
diff --git a/apps/desktop/src/main/remote-desktop-viewer/windows.ts b/apps/desktop/src/main/remote-desktop-viewer/windows.ts
index eeb49de5b7..fcdca41a42 100644
--- a/apps/desktop/src/main/remote-desktop-viewer/windows.ts
+++ b/apps/desktop/src/main/remote-desktop-viewer/windows.ts
@@ -2,10 +2,11 @@ import {
BrowserWindow,
clipboard,
ipcMain,
+ screen,
type IpcMainInvokeEvent,
type WebContents,
} from 'electron';
-import { REMOTE_DESKTOP_CHANNEL, DeviceLinkError } from '@cindy/device-link';
+import { REMOTE_DESKTOP_CHANNEL, DeviceLinkError, parseClipboardContent } from '@cindy/device-link';
import { REMOTE_VIEWER, type RemoteViewerTarget } from '../../shared/remoteDesktopViewer.js';
import { activeOwnerScopeKey, isAppSessionBoundaryPending } from '../appSessionState.js';
import { getAppCapabilities } from '../appCapabilities.js';
@@ -24,6 +25,40 @@ import { t } from '../i18n.js';
import { markRemoteDesktopViewer } from './registry.js';
import { extractIpcError } from '../../shared/ipcError.js';
import { RemoteViewerConnection } from './connection.js';
+import { ViewerCredentials } from './credentials.js';
+import { readViewerPreferences, writeViewerPreferences } from './preferences.js';
+import { resolveDesktopInputBinary } from '../remote-desktop/inputHost.js';
+import { ClipboardCounter } from '../remote-desktop/clipboardCounter.js';
+import { transferDesktopClipboardContent } from '../remote-desktop/clipboard.js';
+
+async function requestRemote<T>(device: string, request: unknown, check: () => void): Promise<T> {
+ check();
+ if (!getAppCapabilities().canUseDeviceLink || isAppSessionBoundaryPending())
+ throw new Error('DESKTOP_STOPPED');
+ try {
+ const result = await remoteInvoke(device, REMOTE_DESKTOP_CHANNEL, [request], {
+ preSend: () => {
+ check();
+ if (!getAppCapabilities().canUseDeviceLink || isAppSessionBoundaryPending())
+ throw new Error('DESKTOP_STOPPED');
+ },
+ });
+ if (!result.ok)
+ throw new Error(result.error.code === 'IPC_ERROR' ? result.error.message : result.error.code);
+ return result.result as T;
+ } catch (error) {
+ if (error instanceof DeviceLinkError) throw new Error(error.code);
+ const parsed = extractIpcError(error);
+ if (parsed?.code === 'DEVICE_LINK_CONTROL_DISABLED') throw new Error('REMOTE_DISABLED');
+ if (parsed)
+ throw new Error(
+ /^(DESKTOP|CLIPBOARD|CREDENTIAL)_[A-Z_]+$/.test(parsed.message)
+ ? parsed.message
+ : parsed.code,
+ );
+ throw error;
+ }
+}
type Entry = {
window: BrowserWindow | null;
@@ -88,38 +123,43 @@ export class RemoteDesktopViewerWindows {
win.webContents.send(REMOTE_VIEWER.CLOSE_REQUESTED, entry.connection.generation);
}
private create(): Entry {
- const connection = new RemoteViewerConnection({
+ const counter = new ClipboardCounter(resolveDesktopInputBinary);
+ const credentials: ViewerCredentials = new ViewerCredentials({
+ request: (message, check) => requestRemote(connection.target!.deviceId, message, check),
+ });
+ const connection: RemoteViewerConnection = new RemoteViewerConnection({
owner: activeOwnerScopeKey,
readClipboard: () => clipboard.readText(),
writeClipboard: (value) => clipboard.writeText(value),
- request: async (device, request, check) => {
- check();
- if (!getAppCapabilities().canUseDeviceLink || isAppSessionBoundaryPending())
- throw new Error('DESKTOP_STOPPED');
- try {
- const result = await remoteInvoke(device, REMOTE_DESKTOP_CHANNEL, [request], {
- preSend: () => {
- check();
- if (!getAppCapabilities().canUseDeviceLink || isAppSessionBoundaryPending())
- throw new Error('DESKTOP_STOPPED');
- },
- });
- if (!result.ok)
- throw new Error(
- result.error.code === 'IPC_ERROR' ? result.error.message : result.error.code,
- );
- return result.result;
- } catch (error) {
- if (error instanceof DeviceLinkError) throw new Error(error.code);
- const parsed = extractIpcError(error);
- if (parsed?.code === 'DEVICE_LINK_CONTROL_DISABLED') throw new Error('REMOTE_DISABLED');
- if (parsed)
- throw new Error(
- /^(DESKTOP|CLIPBOARD)_[A-Z_]+$/.test(parsed.message) ? parsed.message : parsed.code,
- );
- throw error;
- }
+ preferences: readViewerPreferences,
+ savePreferences: writeViewerPreferences,
+ focused: () => entry.window?.isFocused() === true,
+ clipboard: {
+ version: (current) => {
+ if (!current()) return Promise.reject(new Error('DESKTOP_STOPPED'));
+ return counter.read(true);
+ },
+ stop: () => counter.stop(),
+ read: async (current) =>
+ JSON.stringify(
+ await transferDesktopClipboardContent('copy', undefined, current, () => {}, {
+ sync: true,
+ }),
+ ),
+ write: async (json, version, current) => {
+ const result = await transferDesktopClipboardContent(
+ 'paste',
+ parseClipboardContent(json),
+ current,
+ () => {},
+ { sync: true, version },
+ );
+ if (!result || !('version' in result)) throw new Error('DESKTOP_CLIPBOARD_WRITE_FAILED');
+ return result.version;
+ },
},
+ request: requestRemote,
+ credentials,
});
const entry: Entry = { window: null, connection, controller: null! };
entry.controller = new ResourceUsageWindowController({
@@ -149,6 +189,9 @@ export class RemoteDesktopViewerWindows {
register: markRemoteDesktopViewer,
});
entry.window = win;
+ win.on('blur', () => {
+ void connection.focusChanged();
+ });
// Local navigation/reloads/crashes immediately retire authority, including in-flight starts.
win.webContents.on('did-start-navigation', (_event, _url, isInPlace, isMainFrame) => {
if (isMainFrame && !isInPlace) connection.deactivate();
@@ -190,11 +233,33 @@ export class RemoteDesktopViewerWindows {
ipcMain.handle(REMOTE_VIEWER.PRESENTED, (event) => {
this.entry(event).controller.markPresentationReady(event.sender);
});
- ipcMain.handle(REMOTE_VIEWER.CLOSE, (event, generation) => {
+ ipcMain.handle(REMOTE_VIEWER.CLOSE, async (event, generation) => {
const entry = this.entry(event);
if (generation !== entry.connection.generation) return;
+ try {
+ await entry.connection.close(generation);
+ } catch {
+ throwIpcError('PRECONDITION_FAILED', 'DESKTOP_STOP_FAILED');
+ }
+ if (generation !== entry.connection.generation) return;
entry.controller.close(event.sender);
});
+ ipcMain.handle(REMOTE_VIEWER.PREFERENCES, async (event, generation, patch) => {
+ try {
+ return await this.entry(event).connection.preferences(generation, patch);
+ } catch {
+ throwIpcError('PRECONDITION_FAILED', 'DESKTOP_SETTINGS_FAILED');
+ }
+ });
+ ipcMain.handle(REMOTE_VIEWER.SAFETY, async (event, generation, retry) => {
+ if (retry !== undefined && typeof retry !== 'boolean')
+ throwIpcError('INVALID_PARAMS', 'Invalid retry');
+ try {
+ return await this.entry(event).connection.safety(generation, retry);
+ } catch {
+ throwIpcError('PRECONDITION_FAILED', 'DESKTOP_STOPPED');
+ }
+ });
ipcMain.handle(REMOTE_VIEWER.REQUEST, async (event, generation, request, attempt) => {
const result = await this.entry(event).connection.request(generation, request, attempt);
if (!result.ok) throwIpcError('PRECONDITION_FAILED', result.code);
@@ -204,6 +269,18 @@ export class RemoteDesktopViewerWindows {
const result = await this.entry(event).connection.clipboard(generation, action);
if (!result.ok) throwIpcError('PRECONDITION_FAILED', result.code);
});
+ ipcMain.handle(REMOTE_VIEWER.CREDENTIAL, async (event, generation, action, enabled) => {
+ try {
+ return await this.entry(event).connection.credential(generation, action, enabled);
+ } catch (error) {
+ throwIpcError(
+ 'PRECONDITION_FAILED',
+ error instanceof Error && /^CREDENTIAL_[A-Z_]+$/.test(error.message)
+ ? error.message
+ : 'CREDENTIAL_UNAVAILABLE',
+ );
+ }
+ });
ipcMain.handle(REMOTE_VIEWER.ICE, async (event, generation, attempt) => {
const entry = this.entry(event);
try {
@@ -219,6 +296,59 @@ export class RemoteDesktopViewerWindows {
}
return result;
});
+ ipcMain.handle(REMOTE_VIEWER.RESIZE, (event, generation, width, height) => {
+ const entry = this.entry(event);
+ entry.connection.check(generation);
+ if (![width, height].every((value) => Number.isInteger(value) && value > 0 && value <= 32768))
+ throwIpcError('INVALID_PARAMS', 'Invalid viewer size');
+ const win = entry.window!;
+ const resize = () => {
+ if (win.isDestroyed()) return;
+ try {
+ entry.connection.check(generation);
+ } catch {
+ return;
+ }
+ const bounds = win.getBounds();
+ const content = win.getContentBounds();
+ const area = screen.getDisplayMatching(bounds).workArea;
+ const [minWidth, minHeight] = win.getMinimumSize();
+ const w = Math.min(area.width, Math.max(minWidth, width + bounds.width - content.width));
+ const h = Math.min(
+ area.height,
+ Math.max(minHeight, height + bounds.height - content.height),
+ );
+ win.setBounds({
+ width: w,
+ height: h,
+ x: Math.round(
+ Math.max(area.x, Math.min(area.x + area.width - w, bounds.x + (bounds.width - w) / 2)),
+ ),
+ y: Math.round(
+ Math.max(
+ area.y,
+ Math.min(area.y + area.height - h, bounds.y + (bounds.height - h) / 2),
+ ),
+ ),
+ });
+ };
+ const restore = () => {
+ if (win.isDestroyed()) return;
+ try {
+ entry.connection.check(generation);
+ } catch {
+ return;
+ }
+ if (win.isMaximized()) {
+ win.once('unmaximize', resize);
+ win.unmaximize();
+ } else resize();
+ };
+ if (win.isFullScreen()) {
+ win.once('leave-full-screen', restore);
+ win.setFullScreen(false);
+ } else restore();
+ });
ipcMain.handle(REMOTE_VIEWER.FULLSCREEN, (event) => {
const entry = this.entry(event);
const win = entry.window!;
diff --git a/apps/desktop/src/main/remote-desktop/__tests__/captureBoundary.test.ts b/apps/desktop/src/main/remote-desktop/__tests__/captureBoundary.test.ts
index f3c9092c61..2f1044e8d7 100644
--- a/apps/desktop/src/main/remote-desktop/__tests__/captureBoundary.test.ts
+++ b/apps/desktop/src/main/remote-desktop/__tests__/captureBoundary.test.ts
@@ -189,6 +189,7 @@ vi.mock('../nativeCapture', () => ({
},
}));
vi.mock('../inputHost', () => ({
+ resolveDesktopInputBinary: vi.fn(async () => '/fake/desktop-input'),
DesktopInputHost: class {
constructor(onFailure: () => void) {
h.inputFailure = onFailure;
diff --git a/apps/desktop/src/main/remote-desktop/__tests__/clipboardCounter.test.ts b/apps/desktop/src/main/remote-desktop/__tests__/clipboardCounter.test.ts
new file mode 100644
index 0000000000..9a1266b92a
--- /dev/null
+++ b/apps/desktop/src/main/remote-desktop/__tests__/clipboardCounter.test.ts
@@ -0,0 +1,126 @@
+import { afterEach, expect, it, vi } from 'vitest';
+import { EventEmitter } from 'node:events';
+import { PassThrough } from 'node:stream';
+import type { ChildProcessWithoutNullStreams } from 'node:child_process';
+import { ClipboardCounter } from '../clipboardCounter';
+import { linuxClipboardVersion } from '../linuxClipboard';
+
+vi.mock('../linuxClipboard', () => ({ linuxClipboardVersion: vi.fn() }));
+const platform = process.platform;
+
+function fixture(binary = async () => '/fake/helper') {
+ Object.defineProperty(process, 'platform', { value: 'darwin' });
+ vi.useFakeTimers();
+ const children: Array<ChildProcessWithoutNullStreams> = [];
+ const launch = vi.fn(() => {
+ const child = Object.assign(new EventEmitter(), {
+ stdin: new PassThrough(),
+ stdout: new PassThrough(),
+ stderr: new PassThrough(),
+ kill: vi.fn(() => true),
+ }) as unknown as ChildProcessWithoutNullStreams;
+ children.push(child);
+ return child;
+ });
+ const counter = new ClipboardCounter(binary, launch);
+ return { counter, launch, children };
+}
+afterEach(() => {
+ vi.useRealTimers();
+ Object.defineProperty(process, 'platform', { value: platform });
+});
+
+it('keeps Linux selection versions and failures on the existing Wayland adapter', async () => {
+ const binary = vi.fn(async () => '/fake/helper');
+ const { counter, launch } = fixture(binary);
+ Object.defineProperty(process, 'platform', { value: 'linux' });
+ vi.mocked(linuxClipboardVersion).mockResolvedValueOnce('opaque-linux-version');
+ expect(await counter.read(true)).toBe('opaque-linux-version');
+ vi.mocked(linuxClipboardVersion).mockRejectedValueOnce(
+ new Error('DESKTOP_CLIPBOARD_UNAVAILABLE'),
+ );
+ await expect(counter.read()).rejects.toThrow('DESKTOP_CLIPBOARD_UNAVAILABLE');
+ counter.stop();
+ expect(binary).not.toHaveBeenCalled();
+ expect(launch).not.toHaveBeenCalled();
+});
+
+it('reuses one process but queries fresh counters, including concurrent portable reads', async () => {
+ const { counter, children, launch } = fixture();
+ const first = counter.read();
+ const second = counter.read(true);
+ await vi.advanceTimersByTimeAsync(0);
+ const child = children[0];
+ expect((child.stdin as PassThrough).read()?.toString()).toBe('1 0\n2 1\n');
+ child.stdout.emit('data', '1 10\n2 11\n');
+ expect(await first).toBe('10');
+ expect(await second).toBe('11');
+ await vi.advanceTimersByTimeAsync(1500);
+ const next = counter.read();
+ await vi.advanceTimersByTimeAsync(0);
+ child.stdout.emit('data', '3 12\n');
+ expect(await next).toBe('12');
+ expect(launch).toHaveBeenCalledOnce();
+ await vi.advanceTimersByTimeAsync(4000);
+ expect(child.kill).toHaveBeenCalledWith('SIGKILL');
+});
+
+it('cancels startup before a late binary resolution can spawn', async () => {
+ let finish!: (value: string) => void;
+ const { counter, launch } = fixture(
+ () =>
+ new Promise((resolve) => {
+ finish = resolve;
+ }),
+ );
+ const result = expect(counter.read()).rejects.toThrow('DESKTOP_CLIPBOARD_UNAVAILABLE');
+ counter.stop();
+ await result;
+ finish('/fake/helper');
+ await vi.advanceTimersByTimeAsync(0);
+ expect(launch).not.toHaveBeenCalled();
+});
+
+it.each(['exit', 'error', 'timeout', 'stop', 'write', 'read', 'stderr', 'malformed'])(
+ 'rejects on %s and isolates late events from the replacement process',
+ async (failure) => {
+ const { counter, children } = fixture();
+ const rejected = expect(counter.read()).rejects.toThrow('DESKTOP_CLIPBOARD_UNAVAILABLE');
+ await vi.advanceTimersByTimeAsync(0);
+ const old = children[0];
+ if (failure === 'timeout') await vi.advanceTimersByTimeAsync(2000);
+ else if (failure === 'stop') counter.stop();
+ else if (failure === 'write') old.stdin.emit('error', new Error('broken pipe'));
+ else if (failure === 'read') old.stdout.emit('error', new Error('broken pipe'));
+ else if (failure === 'stderr') old.stderr.emit('error', new Error('broken pipe'));
+ else if (failure === 'malformed') old.stdout.emit('data', 'not a counter\n');
+ else old.emit(failure, new Error('exit'));
+ await rejected;
+ const next = counter.read();
+ await vi.advanceTimersByTimeAsync(0);
+ old.stdout.emit('data', '1 900\n');
+ old.emit('exit', 0);
+ expect(children[1].kill).not.toHaveBeenCalled();
+ children[1].stdout.emit('data', '2 12\n');
+ expect(await next).toBe('12');
+ counter.stop();
+ },
+);
+
+it('preserves permission and portable-format refusal without caching it', async () => {
+ const { counter, children, launch } = fixture();
+ const unsupported = expect(counter.read(true)).rejects.toThrow('CLIPBOARD_UNSUPPORTED');
+ await vi.advanceTimersByTimeAsync(0);
+ children[0].stdout.emit('data', '1 unsupported\n');
+ await unsupported;
+ const denied = expect(counter.read()).rejects.toThrow('DESKTOP_CLIPBOARD_UNAVAILABLE');
+ await vi.advanceTimersByTimeAsync(0);
+ children[0].stdout.emit('data', '2 unavailable\n');
+ await denied;
+ const next = counter.read();
+ await vi.advanceTimersByTimeAsync(0);
+ children[0].stdout.emit('data', '3 15\n');
+ expect(await next).toBe('15');
+ expect(launch).toHaveBeenCalledOnce();
+ counter.stop();
+});
diff --git a/apps/desktop/src/main/remote-desktop/__tests__/controller.test.ts b/apps/desktop/src/main/remote-desktop/__tests__/controller.test.ts
index b89f56efff..71a98b6170 100644
--- a/apps/desktop/src/main/remote-desktop/__tests__/controller.test.ts
+++ b/apps/desktop/src/main/remote-desktop/__tests__/controller.test.ts
@@ -198,7 +198,11 @@ describe('remote desktop authority and lifecycle', () => {
expect(foreground.deps.stopVideo).toHaveBeenCalledOnce();
const revoked = harness();
const presentation = await revoked.start();
- await revoked.controller.request('phone', { op: 'presentation', lease: presentation.lease, enabled: true });
+ await revoked.controller.request('phone', {
+ op: 'presentation',
+ lease: presentation.lease,
+ enabled: true,
+ });
revoked.revoke();
revoked.controller.signalingLost('phone');
revoked.controller.viewHeartbeat(presentation.lease);
@@ -213,7 +217,9 @@ describe('remote desktop authority and lifecycle', () => {
h.controller.stopByUser();
h.controller.viewHeartbeat(lease);
expect(h.deps.stopVideo).toHaveBeenCalledOnce();
- await expect(h.controller.request('phone', { op: 'heartbeat', lease })).rejects.toThrow('DESKTOP_STOPPED');
+ await expect(h.controller.request('phone', { op: 'heartbeat', lease })).rejects.toThrow(
+ 'DESKTOP_STOPPED',
+ );
});
it('does not publish selected geometry or resume control before it is observed', async () => {
const h = harness();
@@ -504,6 +510,7 @@ describe('remote desktop authority and lifecycle', () => {
const h = harness(),
{ lease } = await h.start();
let finish!: (version: string) => void;
+ h.deps.stopClipboardVersion = vi.fn();
h.deps.clipboardVersion = () =>
new Promise((resolve) => {
finish = resolve;
@@ -518,9 +525,11 @@ describe('remote desktop authority and lifecycle', () => {
);
const read = h.controller.request('phone', { op: 'clipboardVersion', lease });
await h.controller.request('phone', { op: 'clipboardSync', lease, enabled: false });
+ expect(h.deps.stopClipboardVersion).toHaveBeenCalledOnce();
finish('12');
await expect(read).rejects.toThrow('DESKTOP_LEASE_EXPIRED');
h.controller.stop();
+ expect(h.deps.stopClipboardVersion).toHaveBeenCalledTimes(2);
});
it('restores privacy on lease expiry and invalidates an older enable after disable', async () => {
const h = harness(),
diff --git a/apps/desktop/src/main/remote-desktop/__tests__/viewerDisplay.test.ts b/apps/desktop/src/main/remote-desktop/__tests__/viewerDisplay.test.ts
index b636d1f38b..4752983fa8 100644
--- a/apps/desktop/src/main/remote-desktop/__tests__/viewerDisplay.test.ts
+++ b/apps/desktop/src/main/remote-desktop/__tests__/viewerDisplay.test.ts
@@ -349,6 +349,7 @@ describe('viewer-sized desktop ownership', () => {
lease: lease.lease,
display: { id: '2', name: 'Viewer', width: 900, height: 1600 },
controlling: false,
+ viewerDisplayRequest: { width: 900, height: 1600 },
});
expect(f.host.hasLease(lease.lease)).toBe(true);
await expect(
@@ -483,3 +484,26 @@ describe('viewer-sized desktop ownership', () => {
expect(f.host.state).toBeNull();
});
});
+
+it('acknowledges requested dimensions separately from actual host geometry', async () => {
+ const f = fixture();
+ const { lease } = await f.start();
+ vi.mocked(f.handle.resize).mockResolvedValue({
+ id: '2',
+ name: 'Viewer',
+ width: 960,
+ height: 710,
+ });
+ await expect(
+ f.host.request('phone', { op: 'viewerDisplay', lease, width: 1920, height: 1420 }),
+ ).resolves.toEqual({
+ lease,
+ controlling: false,
+ display: { id: '2', name: 'Viewer', width: 960, height: 710 },
+ viewerDisplayRequest: { width: 1920, height: 1420 },
+ });
+ expect(f.host.hasLease(lease)).toBe(true);
+ await f.host.request('phone', { op: 'control', lease, enabled: true });
+ expect(f.deps.startInput).toHaveBeenLastCalledWith('2');
+ f.host.stop('phone');
+});
diff --git a/apps/desktop/src/main/remote-desktop/__tests__/viewerDisplaySupport.test.ts b/apps/desktop/src/main/remote-desktop/__tests__/viewerDisplaySupport.test.ts
index 18bdcbc186..5ec027af0b 100644
--- a/apps/desktop/src/main/remote-desktop/__tests__/viewerDisplaySupport.test.ts
+++ b/apps/desktop/src/main/remote-desktop/__tests__/viewerDisplaySupport.test.ts
@@ -1,14 +1,17 @@
+import { EventEmitter } from 'node:events';
+import { PassThrough } from 'node:stream';
import { afterEach, beforeEach, expect, it, vi } from 'vitest';
const mocks = vi.hoisted(() => ({
app: { isPackaged: false, getAppPath: () => '/fake/app', getPath: () => '/fake/profile' },
exec: vi.fn(),
+ spawn: vi.fn(),
access: vi.fn(),
accessSync: vi.fn(),
displays: vi.fn(),
}));
vi.mock('electron', () => ({ app: mocks.app, screen: { getAllDisplays: mocks.displays } }));
-vi.mock('node:child_process', () => ({ execFile: vi.fn(), spawn: vi.fn() }));
+vi.mock('node:child_process', () => ({ execFile: vi.fn(), spawn: mocks.spawn }));
vi.mock('node:util', () => ({ promisify: () => mocks.exec }));
vi.mock('node:fs', () => ({ accessSync: mocks.accessSync, constants: { X_OK: 1, R_OK: 4 } }));
vi.mock('node:fs/promises', () => ({
@@ -135,3 +138,43 @@ it.each(['packaged', 'windows'] as const)('does no helper work for %s builds', a
expect(mocks.exec).not.toHaveBeenCalled();
expect(mocks.access).not.toHaveBeenCalled();
});
+
+it.each([false, true])(
+ 'uses OS logical dimensions while matching the request (stale=%s)',
+ async (stale) => {
+ const child = Object.assign(new EventEmitter(), {
+ stdin: new PassThrough(),
+ stdout: new PassThrough(),
+ stderr: new PassThrough(),
+ kill: vi.fn(),
+ });
+ child.stdin.on('data', () =>
+ queueMicrotask(() =>
+ child.stdout.write(
+ JSON.stringify({
+ id: 9,
+ width: stale ? 1280 : 1920,
+ height: 1420,
+ logicalWidth: 960,
+ logicalHeight: 710,
+ }) + '\n',
+ ),
+ ),
+ );
+ child.stdin.on('finish', () => queueMicrotask(() => child.emit('exit', 0)));
+ mocks.spawn.mockReturnValue(child);
+ mocks.displays.mockReturnValue([
+ { id: 4, size: { width: 2560, height: 1440 } },
+ { id: 9, size: { width: 960, height: 710 } },
+ ]);
+ const { createViewerDisplay } = await import('../viewerDisplay');
+ const handle = await createViewerDisplay('4', () => true, vi.fn());
+ try {
+ const result = handle.resize(1920, 1420, () => true);
+ if (stale) await expect(result).rejects.toThrow('DESKTOP_VIEWER_DISPLAY_UNAVAILABLE');
+ else await expect(result).resolves.toMatchObject({ id: '9', width: 960, height: 710 });
+ } finally {
+ handle.dispose();
+ }
+ },
+);
diff --git a/apps/desktop/src/main/remote-desktop/__tests__/windowsCursorFrame.test.ts b/apps/desktop/src/main/remote-desktop/__tests__/windowsCursorFrame.test.ts
index 8249479074..9cb7a3470f 100644
--- a/apps/desktop/src/main/remote-desktop/__tests__/windowsCursorFrame.test.ts
+++ b/apps/desktop/src/main/remote-desktop/__tests__/windowsCursorFrame.test.ts
@@ -18,6 +18,17 @@ const cursor = {
const frame = (value: unknown = cursor) => JSON.stringify({ jpeg: 'anBlZw==', cursor: value });
describe('Windows local cursor frames', () => {
+ it('preserves optional native shape hints without requiring them from older helpers', () => {
+ expect(
+ decodeWindowsCursorFrame(frame({ ...cursor, shape: 'text' }), 2, () => png),
+ ).toMatchObject({ cursor: { shape: 'text' } });
+ expect(
+ decodeWindowsCursorFrame(frame({ ...cursor, shape: 'future-shape' }), 2, () => png),
+ ).toMatchObject({ cursor: { shape: 'future-shape' } });
+ expect(
+ decodeWindowsCursorFrame(frame({ ...cursor, shape: 'x'.repeat(33) }), 2, () => png),
+ ).toMatchObject({ cursor: { png: png.toString('base64') } });
+ });
it.each([1, 1.25, 1.5, 2])(
'preserves raster and position while converting %s DPI geometry to points',
(scale) => {
diff --git a/apps/desktop/src/main/remote-desktop/clipboardCounter.ts b/apps/desktop/src/main/remote-desktop/clipboardCounter.ts
new file mode 100644
index 0000000000..103c6cc12c
--- /dev/null
+++ b/apps/desktop/src/main/remote-desktop/clipboardCounter.ts
@@ -0,0 +1,115 @@
+import { spawn, type ChildProcessWithoutNullStreams } from 'node:child_process';
+import { linuxClipboardVersion } from './linuxClipboard';
+
+type Pending = {
+ resolve(value: string): void;
+ reject(error: Error): void;
+ timer: ReturnType<typeof setTimeout>;
+};
+type Session = {
+ child?: ChildProcessWithoutNullStreams;
+ ready: Promise<void>;
+ pending: Map<number, Pending>;
+ buffer: string;
+ idle?: ReturnType<typeof setTimeout>;
+};
+const unavailable = () => new Error('DESKTOP_CLIPBOARD_UNAVAILABLE');
+
+/** Counter-only pipe, owned by a viewer/host. No content cache or automatic replay. */
+export class ClipboardCounter {
+ private session: Session | undefined;
+ private sequence = 0;
+ constructor(
+ private readonly binary: () => Promise<string>,
+ private readonly launch = (binary: string) =>
+ spawn(binary, ['--clipboard-counter'], { stdio: 'pipe', windowsHide: true }),
+ ) {}
+
+ stop(): void {
+ if (this.session) this.retire(this.session);
+ }
+ private retire(session: Session): void {
+ if (this.session === session) this.session = undefined;
+ clearTimeout(session.idle);
+ for (const pending of session.pending.values()) {
+ clearTimeout(pending.timer);
+ pending.reject(unavailable());
+ }
+ session.pending.clear();
+ // Immediate termination also bounds a helper stuck in an OS API. Its exit
+ // handler owns only this session and cannot retire a replacement process.
+ session.child?.stdin.destroy();
+ session.child?.kill('SIGKILL');
+ }
+ private idle(session: Session): void {
+ clearTimeout(session.idle);
+ if (!session.pending.size && this.session === session)
+ session.idle = setTimeout(() => this.retire(session), 4000);
+ }
+ private start(): Session {
+ const session: Session = { ready: Promise.resolve(), pending: new Map(), buffer: '' };
+ this.session = session;
+ session.ready = this.binary().then((binary) => {
+ if (this.session !== session) throw unavailable();
+ const child = this.launch(binary);
+ session.child = child;
+ child.stderr.resume();
+ child.once('error', () => this.retire(session));
+ child.once('exit', () => this.retire(session));
+ child.stdin.on('error', () => this.retire(session));
+ child.stdout.on('error', () => this.retire(session));
+ child.stderr.on('error', () => this.retire(session));
+ child.stdout.setEncoding('utf8');
+ child.stdout.on('data', (chunk: string) => {
+ if (this.session !== session) return;
+ session.buffer += chunk;
+ if (session.buffer.length > 4096) return this.retire(session);
+ let end: number;
+ while ((end = session.buffer.indexOf('\n')) !== -1) {
+ const line = session.buffer.slice(0, end);
+ session.buffer = session.buffer.slice(end + 1);
+ const match = /^(\d{1,16}) (\d{1,128}|unavailable|unsupported)$/.exec(line);
+ if (!match) return this.retire(session);
+ const id = Number(match[1]);
+ const pending = session.pending.get(id);
+ if (!pending) return this.retire(session);
+ session.pending.delete(id);
+ clearTimeout(pending.timer);
+ if (match[2] === 'unsupported') pending.reject(new Error('CLIPBOARD_UNSUPPORTED'));
+ else if (match[2] === 'unavailable') pending.reject(unavailable());
+ else pending.resolve(match[2]);
+ }
+ this.idle(session);
+ });
+ });
+ // Each read handles the same startup failure; no background retry or spawn.
+ return session;
+ }
+ async read(portable = false): Promise<string> {
+ // Linux owns its Wayland selection adapter; its input helper does not
+ // implement the macOS/Windows counter-only pipe.
+ if (process.platform === 'linux') return linuxClipboardVersion();
+ const session = this.session ?? this.start();
+ if (session.pending.size >= 32) throw unavailable();
+ clearTimeout(session.idle);
+ const id = ++this.sequence;
+ return new Promise<string>((resolve, reject) => {
+ const pending: Pending = {
+ resolve,
+ reject,
+ timer: setTimeout(() => this.retire(session), 125_000),
+ };
+ session.pending.set(id, pending);
+ void session.ready
+ .then(() => {
+ if (this.session !== session || !session.pending.has(id)) throw unavailable();
+ clearTimeout(pending.timer);
+ pending.timer = setTimeout(() => this.retire(session), 2000);
+ session.child!.stdin.write(`${id} ${portable ? 1 : 0}\n`, (error) => {
+ if (error) this.retire(session);
+ });
+ })
+ .catch(() => this.retire(session));
+ });
+ }
+}
diff --git a/apps/desktop/src/main/remote-desktop/controller.ts b/apps/desktop/src/main/remote-desktop/controller.ts
index 9fa6c40ffc..d884da3444 100644
--- a/apps/desktop/src/main/remote-desktop/controller.ts
+++ b/apps/desktop/src/main/remote-desktop/controller.ts
@@ -94,6 +94,7 @@ export interface DesktopControllerDeps {
options?: { sync?: boolean; version?: string },
): Promise<RemoteClipboardContent | { version: string } | void>;
clipboardVersion?(): Promise<string>;
+ stopClipboardVersion?(): void;
privacyScreen?(enabled: boolean, isCurrent: () => boolean): Promise<void>;
stopPrivacyScreen?(): void;
hostMute?(enabled: boolean): Promise<void>;
@@ -142,6 +143,7 @@ export class RemoteDesktopController {
this.privacyGeneration++;
this.privacyOperation = null;
this.syncGeneration++;
+ this.deps.stopClipboardVersion?.();
if (this.active) this.active.clipboardSync = false;
try {
this.deps.stopPrivacyScreen?.();
@@ -228,7 +230,12 @@ export class RemoteDesktopController {
signalingLost(peer?: string): void {
this.tick();
const active = this.active;
- if (active && (!peer || active.peer === peer) && active.backgroundViewing && !active.controlling)
+ if (
+ active &&
+ (!peer || active.peer === peer) &&
+ active.backgroundViewing &&
+ !active.controlling
+ )
return;
this.stop(peer);
}
@@ -608,6 +615,7 @@ export class RemoteDesktopController {
this.clipboardTransfer.resetSync();
}
active.clipboardSync = request.enabled;
+ if (!request.enabled) this.deps.stopClipboardVersion?.();
return { enabled: request.enabled };
}
case 'hostMute': {
@@ -698,7 +706,16 @@ export class RemoteDesktopController {
active.controlling = false;
this.controlGeneration++;
this.deps.changed();
- return { lease: active.lease, display, controlling: false };
+ return {
+ lease: active.lease,
+ display,
+ controlling: false,
+ ...(request.op === 'viewerDisplay'
+ ? {
+ viewerDisplayRequest: { width: request.width, height: request.height },
+ }
+ : {}),
+ };
} catch (error) {
if (this.active === active) this.stop(peer);
throw error;
diff --git a/apps/desktop/src/main/remote-desktop/credentialHost.ts b/apps/desktop/src/main/remote-desktop/credentialHost.ts
index 3f253d3e2f..f4d65fdf55 100644
--- a/apps/desktop/src/main/remote-desktop/credentialHost.ts
+++ b/apps/desktop/src/main/remote-desktop/credentialHost.ts
@@ -28,7 +28,7 @@ async function resolveBinary(): Promise<string> {
const hash = createHash('sha256')
.update(process.execPath)
.update(process.arch)
- .update('credential-host-signed-v1')
+ .update('credential-host-signed-v2-resources')
.update(signingIdentity);
async function digest(directory: string): Promise<void> {
for (const entry of (await fs.readdir(directory, { withFileTypes: true })).sort((a, b) =>
@@ -51,6 +51,7 @@ async function resolveBinary(): Promise<string> {
const binary = path.join(directory, binaryName);
try {
await fs.access(binary);
+ await fs.access(path.join(directory, 'CindyRemoteCredentials_CindyRemoteCredentials.bundle'));
await exec('/usr/bin/codesign', credentialVerificationArguments(binary, signingIdentity));
return binary;
} catch {
@@ -88,7 +89,21 @@ async function resolveBinary(): Promise<string> {
{ timeout: 240_000, maxBuffer: 1024 * 1024 },
);
const temporary = `${binary}.${process.pid}.tmp`;
- await fs.copyFile(path.join(source, '.build/release', binaryName), temporary);
+ const location = await exec('swift', [
+ 'build',
+ '--package-path',
+ source,
+ '-c',
+ 'release',
+ '--show-bin-path',
+ ]);
+ const outputDirectory = location.stdout.trim();
+ await fs.copyFile(path.join(outputDirectory, binaryName), temporary);
+ await fs.cp(
+ path.join(outputDirectory, 'CindyRemoteCredentials_CindyRemoteCredentials.bundle'),
+ path.join(directory, 'CindyRemoteCredentials_CindyRemoteCredentials.bundle'),
+ { recursive: true },
+ );
await fs.chmod(temporary, 0o755);
await exec('/usr/bin/codesign', credentialSigningArguments(signingIdentity, temporary));
await exec('/usr/bin/codesign', credentialVerificationArguments(temporary, signingIdentity));
@@ -109,7 +124,7 @@ type Pending = {
timer: ReturnType<typeof setTimeout>;
};
/** Stdio is authenticated in native code. This class never receives a password. */
-class RemoteCredentialHost {
+export class RemoteCredentialHost {
private child: ChildProcessWithoutNullStreams | null = null;
private starting: Promise<void> | null = null;
private pending = new Map<string, Pending>();
@@ -131,6 +146,26 @@ class RemoteCredentialHost {
| undefined;
onInvalidated: (() => void) | undefined;
+ /** Dedicated controller instances use the same signed pipe, never the host singleton. */
+ async viewerCall(
+ realm: 'global' | 'cn',
+ method: string,
+ args: Record<string, unknown> = {},
+ ): Promise<unknown> {
+ if (
+ !/^viewer(?:Settings|Configure|Begin|Accept|Receive|Password|AuthenticationStatus|Forget|Biometric|End|Reset)$/.test(
+ method,
+ )
+ )
+ throw new Error('CREDENTIAL_INVALID_MESSAGE');
+ this.configuredRealm = realm;
+ return this.call(
+ method,
+ args,
+ ['viewerPassword', 'viewerBiometric'].includes(method) ? 120_000 : 50_000,
+ );
+ }
+
authenticationSession(peer: string): string | null {
return performance.now() < this.validUntil ? (this.sessions[peer] ?? null) : null;
}
diff --git a/apps/desktop/src/main/remote-desktop/index.ts b/apps/desktop/src/main/remote-desktop/index.ts
index f40dc1f2fe..9de712db61 100644
--- a/apps/desktop/src/main/remote-desktop/index.ts
+++ b/apps/desktop/src/main/remote-desktop/index.ts
@@ -37,6 +37,7 @@ import { denyAppDesktopCapture } from './capturePermissions';
import { readDeviceLinkSettings, writeDeviceLinkSetting } from '../device-link/settings-store';
import { throwIpcError } from '../utils/ipcValidate';
import { RemoteDesktopController } from './controller';
+import { ClipboardCounter } from './clipboardCounter';
import { onQuit } from '../lifecycle';
import {
createViewerDisplay,
@@ -78,7 +79,7 @@ import {
readDesktopDisplayModes,
setDesktopDisplayMode,
readDesktopInputPermission,
- readDesktopClipboardVersion,
+ resolveDesktopInputBinary,
readDesktopLockState,
lockDesktopScreen,
requestDesktopInputPermission,
@@ -218,6 +219,7 @@ let pending: {
// A dead input helper or a refused injection is an input failure, not a session
// failure: release control and keep the lease, capture and media running.
const input = new DesktopInputHost(() => remoteDesktop.releaseControl());
+const clipboardCounter = new ClipboardCounter(resolveDesktopInputBinary);
function stopVideo(): void {
offerGeneration++;
portalReady = null;
@@ -633,7 +635,8 @@ export const remoteDesktop: RemoteDesktopController = new RemoteDesktopControlle
options,
),
// Poll counters even for nonportable items; the content read owns format validation.
- clipboardVersion: () => readDesktopClipboardVersion(),
+ clipboardVersion: () => clipboardCounter.read(),
+ stopClipboardVersion: () => clipboardCounter.stop(),
privacyScreen: async (enabled, current) => {
if (process.platform === 'linux') return linuxPrivacy.set(enabled, current);
if (!supportsPrivacyScreen) throw new Error('DESKTOP_PRIVACY_UNAVAILABLE');
diff --git a/apps/desktop/src/main/remote-desktop/viewerDisplay.ts b/apps/desktop/src/main/remote-desktop/viewerDisplay.ts
index daafbb3c86..e9fd38766c 100644
--- a/apps/desktop/src/main/remote-desktop/viewerDisplay.ts
+++ b/apps/desktop/src/main/remote-desktop/viewerDisplay.ts
@@ -143,9 +143,10 @@ export async function createViewerDisplay(
},
async resize(width, height, current) {
if (closed || !current()) throw new Error('DESKTOP_LEASE_EXPIRED');
- const result = await new Promise<{ id: number }>((resolve, reject) => {
+ type DisplayReply = { id: number; logicalWidth: number; logicalHeight: number };
+ const result = await new Promise<DisplayReply>((resolve, reject) => {
let text = '';
- const finish = (error?: Error, value?: { id: number }) => {
+ const finish = (error?: Error, value?: DisplayReply) => {
clearTimeout(timer);
child.stdout.off('data', receive);
child.off('exit', exited);
@@ -163,7 +164,15 @@ export async function createViewerDisplay(
!Number.isSafeInteger(value.id) ||
value.id <= 0 ||
value.width !== width ||
- value.height !== height
+ value.height !== height ||
+ !Number.isSafeInteger(value.logicalWidth) ||
+ value.logicalWidth <= 0 ||
+ value.logicalWidth > 4096 ||
+ !Number.isSafeInteger(value.logicalHeight) ||
+ value.logicalHeight <= 0 ||
+ value.logicalHeight > 4096 ||
+ Math.abs(value.logicalWidth * height - value.logicalHeight * width) >
+ Math.max(width, height)
)
return exited();
finish(undefined, value);
@@ -182,12 +191,15 @@ export async function createViewerDisplay(
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)
+ if (
+ actual?.size.width === result.logicalWidth &&
+ actual.size.height === result.logicalHeight
+ )
return {
id: String(actual.id),
name: actual.label || 'Cindy Remote Desktop',
- width,
- height,
+ width: actual.size.width,
+ height: actual.size.height,
};
await new Promise((resolve) => setTimeout(resolve, 50));
}
diff --git a/apps/desktop/src/main/remote-desktop/windowsCursorFrame.ts b/apps/desktop/src/main/remote-desktop/windowsCursorFrame.ts
index de29d906c4..36880ed4b2 100644
--- a/apps/desktop/src/main/remote-desktop/windowsCursorFrame.ts
+++ b/apps/desktop/src/main/remote-desktop/windowsCursorFrame.ts
@@ -68,6 +68,7 @@ export function decodeWindowsCursorFrame(
hotX: hotX / scale,
hotY: hotY / scale,
png: png.toString('base64'),
+ ...(typeof raw.shape === 'string' && raw.shape.length <= 32 ? { shape: raw.shape } : {}),
};
if (isRemoteDesktopCursor(cursor)) result.cursor = cursor;
} catch {
diff --git a/apps/desktop/src/preload/remoteDesktopViewerPreload.ts b/apps/desktop/src/preload/remoteDesktopViewerPreload.ts
index 073a73eed8..9455ab3a32 100644
--- a/apps/desktop/src/preload/remoteDesktopViewerPreload.ts
+++ b/apps/desktop/src/preload/remoteDesktopViewerPreload.ts
@@ -85,6 +85,8 @@ contextBridge.exposeInMainWorld('electronAPI', {
ipcRenderer.invoke(REMOTE_VIEWER.CLIPBOARD, generation, action),
close: (generation) => ipcRenderer.invoke(REMOTE_VIEWER.CLOSE, generation),
fullscreen: () => ipcRenderer.invoke(REMOTE_VIEWER.FULLSCREEN),
+ resize: (generation, width, height) =>
+ ipcRenderer.invoke(REMOTE_VIEWER.RESIZE, generation, width, height),
rendererReady: () => ipcRenderer.invoke(REMOTE_VIEWER.READY),
presentationReady: () => ipcRenderer.invoke(REMOTE_VIEWER.PRESENTED),
onActive: (cb) => onPayload(REMOTE_VIEWER.ACTIVE, cb),
@@ -92,5 +94,10 @@ contextBridge.exposeInMainWorld('electronAPI', {
onCloseRequested: (cb) => onPayload(REMOTE_VIEWER.CLOSE_REQUESTED, cb),
inputFocus: (generation, focused) =>
ipcRenderer.invoke(REMOTE_VIEWER.INPUT_FOCUS, generation, focused),
+ preferences: (generation, patch) =>
+ ipcRenderer.invoke(REMOTE_VIEWER.PREFERENCES, generation, patch),
+ safety: (generation, retry) => ipcRenderer.invoke(REMOTE_VIEWER.SAFETY, generation, retry),
+ credential: (generation, action, enabled) =>
+ ipcRenderer.invoke(REMOTE_VIEWER.CREDENTIAL, generation, action, enabled),
} satisfies RemoteDesktopViewerApi,
});
diff --git a/apps/desktop/src/renderer/__tests__/mainListModel.test.ts b/apps/desktop/src/renderer/__tests__/mainListModel.test.ts
index 0d94f81751..34a8a9ea36 100644
--- a/apps/desktop/src/renderer/__tests__/mainListModel.test.ts
+++ b/apps/desktop/src/renderer/__tests__/mainListModel.test.ts
@@ -18,6 +18,7 @@ import {
sessionPriorityRank,
sortSessionsForMainList,
partitionCindyMakeSessions,
+ onlineDeviceSectionIds,
splitEntriesByDevice,
type MainListEntry,
} from '../features/cc-agent/lib/mainListModel';
@@ -318,10 +319,7 @@ describe('buildMainListEntries — 混排(recency)', () => {
};
const entries = buildMainListEntries({
- projects: [
- project('/old-repo', [olderRun]),
- project('/new-repo', [newerRun]),
- ],
+ projects: [project('/old-repo', [olderRun]), project('/new-repo', [newerRun])],
dialogues: [],
groupBy: 'flat',
groupDialogue: false,
@@ -455,8 +453,14 @@ describe('buildMainListEntries — 混排(recency)', () => {
section.entries.flatMap((entry) => getMainListEntrySessions(entry).map((item) => item.id)),
),
).toEqual([
- runs.slice(0, 2).map((run) => run.id).reverse(),
- runs.slice(2).map((run) => run.id).reverse(),
+ runs
+ .slice(0, 2)
+ .map((run) => run.id)
+ .reverse(),
+ runs
+ .slice(2)
+ .map((run) => run.id)
+ .reverse(),
]);
});
});
@@ -697,12 +701,14 @@ describe('buildMainListEntries — 排序口径', () => {
},
});
expect(labels(afterLeave)).toEqual(['s:needs-input', 's:just-read', 's:older-rest']);
- expect(sessionPriorityRank(unread, {
- runningSessionIds: new Set<string>(),
- attentionSessionIds: new Set([waiting.id]),
- waitingSessionIds: new Set([waiting.id]),
- recentlyViewedAtMs: left.recentlyViewedAtMs,
- })).toBe(LIVE_TASK_PRIORITY.rest);
+ expect(
+ sessionPriorityRank(unread, {
+ runningSessionIds: new Set<string>(),
+ attentionSessionIds: new Set([waiting.id]),
+ waitingSessionIds: new Set([waiting.id]),
+ recentlyViewedAtMs: left.recentlyViewedAtMs,
+ }),
+ ).toBe(LIVE_TASK_PRIORITY.rest);
const sunkWithoutViewedAt = buildMainListEntries({
projects: [],
@@ -1020,6 +1026,49 @@ describe('buildMainListEntries — 排序口径', () => {
});
describe('splitEntriesByDevice — 拆段后按本段重排', () => {
+ it('retains online empty devices in device order and removes them after going offline', () => {
+ const devices = new Map([
+ ['online', { online: true }],
+ ['offline', { online: false }],
+ ]);
+ const split = () =>
+ splitEntriesByDevice([], [...devices.keys()], {
+ onlineDeviceIds: onlineDeviceSectionIds(devices, 'all'),
+ });
+ expect(split()).toEqual([
+ { deviceId: null, entries: [] },
+ { deviceId: 'online', entries: [] },
+ ]);
+ devices.set('online', { online: false });
+ expect(split()).toEqual([{ deviceId: null, entries: [] }]);
+ expect(splitEntriesByDevice([], [...devices.keys()])).toEqual([]);
+ });
+
+ it('only retains empty online devices inside the selected machine scope', () => {
+ const devices = new Map([
+ ['a', { online: true }],
+ ['b', { online: true }],
+ ['offline', { online: false }],
+ ]);
+ expect(onlineDeviceSectionIds(devices, ['b', 'offline'])).toEqual(['b']);
+ expect(onlineDeviceSectionIds(devices, ['local', 'a'])).toEqual([null, 'a']);
+ });
+
+ it('keeps offline tasks and avoids duplicating populated online device sections', () => {
+ const entries: MainListEntry[] = ['online', 'offline'].map((deviceLinkDeviceId) => ({
+ kind: 'session',
+ session: session({ deviceLinkDeviceId, updatedAt: '2026-09-19T00:00:00Z' }),
+ }));
+ const sections = splitEntriesByDevice(entries, ['online', 'offline'], {
+ onlineDeviceIds: [null, 'online'],
+ });
+ expect(sections.map(({ deviceId, entries }) => [deviceId, entries.length])).toEqual([
+ [null, 0],
+ ['online', 1],
+ ['offline', 1],
+ ]);
+ });
+
it('re-sorts each device section by its own activity after splitting a dialogue group', () => {
const localOld = session({
updatedAt: '2026-08-01T00:00:00Z',
@@ -1073,32 +1122,49 @@ describe('splitEntriesByDevice — 拆段后按本段重排', () => {
});
describe('creation-time ordering', () => {
- it.each(['flat', 'project'] as const)('keeps %s tasks and groups in place after activity changes', (groupBy) => {
- const older = session({ id: 'older', title: 'older', createdAt: '2026-08-01T00:00:00Z', updatedAt: '2026-08-20T00:00:00Z' });
- const newer = session({ id: 'newer', title: 'newer', createdAt: '2026-08-10T00:00:00Z', updatedAt: '2026-08-10T00:00:00Z' });
- const middle = session({ title: 'middle', createdAt: '2026-08-05T00:00:00Z', updatedAt: '2026-08-21T00:00:00Z' });
- const input = {
- projects: [project('alpha', [older, newer])],
- dialogues: [middle],
- groupBy,
- groupDialogue: false,
- sortBy: 'created' as const,
- manualProjectOrder: [],
- };
- const before = buildMainListEntries(input);
- expect(labels(before)).toEqual(
- groupBy === 'flat' ? ['s:newer', 's:middle', 's:older'] : ['p:alpha', 's:middle'],
- );
- if (before[0].kind === 'project') {
- expect(before[0].project.sessions.map((s) => s.id)).toEqual(['newer', 'older']);
- }
- older.updatedAt = '2026-09-09T00:00:00Z';
- older.userSendAt = '2026-09-09T00:00:00Z';
- expect(labels(buildMainListEntries(input))).toEqual(labels(before));
- expect(labels(buildMainListEntries({ ...input, sortBy: 'recency' }))).toEqual(
- groupBy === 'flat' ? ['s:older', 's:middle', 's:newer'] : ['p:alpha', 's:middle'],
- );
- });
+ it.each(['flat', 'project'] as const)(
+ 'keeps %s tasks and groups in place after activity changes',
+ (groupBy) => {
+ const older = session({
+ id: 'older',
+ title: 'older',
+ createdAt: '2026-08-01T00:00:00Z',
+ updatedAt: '2026-08-20T00:00:00Z',
+ });
+ const newer = session({
+ id: 'newer',
+ title: 'newer',
+ createdAt: '2026-08-10T00:00:00Z',
+ updatedAt: '2026-08-10T00:00:00Z',
+ });
+ const middle = session({
+ title: 'middle',
+ createdAt: '2026-08-05T00:00:00Z',
+ updatedAt: '2026-08-21T00:00:00Z',
+ });
+ const input = {
+ projects: [project('alpha', [older, newer])],
+ dialogues: [middle],
+ groupBy,
+ groupDialogue: false,
+ sortBy: 'created' as const,
+ manualProjectOrder: [],
+ };
+ const before = buildMainListEntries(input);
+ expect(labels(before)).toEqual(
+ groupBy === 'flat' ? ['s:newer', 's:middle', 's:older'] : ['p:alpha', 's:middle'],
+ );
+ if (before[0].kind === 'project') {
+ expect(before[0].project.sessions.map((s) => s.id)).toEqual(['newer', 'older']);
+ }
+ older.updatedAt = '2026-09-09T00:00:00Z';
+ older.userSendAt = '2026-09-09T00:00:00Z';
+ expect(labels(buildMainListEntries(input))).toEqual(labels(before));
+ expect(labels(buildMainListEntries({ ...input, sortBy: 'recency' }))).toEqual(
+ groupBy === 'flat' ? ['s:older', 's:middle', 's:newer'] : ['p:alpha', 's:middle'],
+ );
+ },
+ );
it('orders dialogue groups and cached remote device sections by creation time', () => {
const old = session({
@@ -1114,8 +1180,12 @@ describe('creation-time ordering', () => {
deviceLinkDeviceId: 'new-device',
});
const entries = buildMainListEntries({
- projects: [], dialogues: [old, recent], groupBy: 'flat', groupDialogue: true,
- sortBy: 'created', manualProjectOrder: [],
+ projects: [],
+ dialogues: [old, recent],
+ groupBy: 'flat',
+ groupDialogue: true,
+ sortBy: 'created',
+ manualProjectOrder: [],
});
expect(getMainListEntrySessions(entries[0]).map((s) => s.title)).toEqual(['recent', 'old']);
const sections = splitEntriesByDevice(entries, [], { sortBy: 'created' });
@@ -1123,22 +1193,29 @@ describe('creation-time ordering', () => {
});
});
-
it('keeps manual project order while creation-time tasks stay stable inside each project', () => {
const old = session({
- id: 'old', title: 'old',
- createdAt: '2026-08-01T00:00:00Z', updatedAt: '2026-08-20T00:00:00Z',
+ id: 'old',
+ title: 'old',
+ createdAt: '2026-08-01T00:00:00Z',
+ updatedAt: '2026-08-20T00:00:00Z',
});
const recent = session({
- id: 'recent', title: 'recent',
- createdAt: '2026-08-10T00:00:00Z', updatedAt: '2026-08-10T00:00:00Z',
+ id: 'recent',
+ title: 'recent',
+ createdAt: '2026-08-10T00:00:00Z',
+ updatedAt: '2026-08-10T00:00:00Z',
});
const alpha = project('alpha', [old, recent]);
const beta = project('beta', [session({ updatedAt: '2026-07-01T00:00:00Z' })]);
const input = {
- projects: [alpha, beta], dialogues: [], groupBy: 'project' as const,
- groupDialogue: false, sortBy: 'created' as const,
- projectOrder: 'custom' as const, manualProjectOrder: ['local:beta', 'local:alpha'],
+ projects: [alpha, beta],
+ dialogues: [],
+ groupBy: 'project' as const,
+ groupDialogue: false,
+ sortBy: 'created' as const,
+ projectOrder: 'custom' as const,
+ manualProjectOrder: ['local:beta', 'local:alpha'],
};
const before = buildMainListEntries(input);
expect(labels(before)).toEqual(['p:beta', 'p:alpha']);
@@ -1198,7 +1275,15 @@ describe('per-sort numeric keys', () => {
const rows = Array.from({ length: 100 }, (_, i) => {
const row = session({ id: String(i), updatedAt: new Date(i * 1000).toISOString() });
let value = row.updatedAt;
- Object.defineProperty(row, 'updatedAt', { get: () => { reads++; return value; }, set: (v) => { value = v; } });
+ Object.defineProperty(row, 'updatedAt', {
+ get: () => {
+ reads++;
+ return value;
+ },
+ set: (v) => {
+ value = v;
+ },
+ });
return row;
});
expect(sortSessionsForMainList(rows, 'recency')[0].id).toBe('99');
@@ -1209,9 +1294,17 @@ describe('per-sort numeric keys', () => {
it('preserves stable ties, created-id ties and invalid userSendAt semantics', () => {
const a = session({ id: 'z', updatedAt: '2026-01-01T00:00:00Z' });
const b = session({ id: 'a', updatedAt: a.updatedAt });
- const invalid = session({ id: 'invalid', updatedAt: '2030-01-01T00:00:00Z', userSendAt: 'invalid' });
- expect(sortSessionsForMainList([a, b, invalid], 'recency').map(x => x.id)).toEqual(['z', 'a', 'invalid']);
- expect(sortSessionsForMainList([a, b], 'created').map(x => x.id)).toEqual(['a', 'z']);
- expect(sortSessionsForMainList([a, b], 'priority').map(x => x.id)).toEqual(['z', 'a']);
+ const invalid = session({
+ id: 'invalid',
+ updatedAt: '2030-01-01T00:00:00Z',
+ userSendAt: 'invalid',
+ });
+ expect(sortSessionsForMainList([a, b, invalid], 'recency').map((x) => x.id)).toEqual([
+ 'z',
+ 'a',
+ 'invalid',
+ ]);
+ expect(sortSessionsForMainList([a, b], 'created').map((x) => x.id)).toEqual(['a', 'z']);
+ expect(sortSessionsForMainList([a, b], 'priority').map((x) => x.id)).toEqual(['z', 'a']);
});
});
diff --git a/apps/desktop/src/renderer/__tests__/onlineDeviceHeaders.test.tsx b/apps/desktop/src/renderer/__tests__/onlineDeviceHeaders.test.tsx
new file mode 100644
index 0000000000..8de8b95061
--- /dev/null
+++ b/apps/desktop/src/renderer/__tests__/onlineDeviceHeaders.test.tsx
@@ -0,0 +1,155 @@
+// @vitest-environment jsdom
+
+import type { ReactNode } from 'react';
+import { cleanup, fireEvent, render, screen } from '@testing-library/react';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import {
+ ProjectsSection,
+ type ProjectsSectionProps,
+} from '../features/cc-agent/sidebar/sections/ProjectsSection';
+
+vi.mock('react-i18next', () => ({ useTranslation: () => ({ t: (key: string) => key }) }));
+vi.mock('@/components/ui/tooltip', () => ({
+ Tip: ({ children }: { children: ReactNode }) => children,
+}));
+vi.mock('@/features/device-link/useMachineSwitcher', () => ({
+ useEffectiveSelectedMachineId: () => 'all',
+}));
+vi.mock('@/hooks/useReducedMotion', () => ({ useReducedMotion: () => true }));
+vi.mock('@/hooks/useSidebarCardMode', () => ({ useSidebarMainViewMode: () => ({ mode: 'text' }) }));
+vi.mock('../features/cc-agent/hooks/useRemoteHostProjectOrders', () => ({
+ projectOrderWriteScopeForSelection: () => ({ kind: 'viewer' }),
+ useLocalHostProjectOrder: () => ({ snapshot: { manualProjectOrder: [] } }),
+ useRemoteHostProjectOrders: () => ({ orders: new Map() }),
+}));
+vi.mock('../features/cc-agent/sidebar/MainListScopeHeader', () => ({
+ MainListScopeHeader: ({ fold }: { fold: { label: string; onClick: () => void } | null }) =>
+ fold ? <button onClick={fold.onClick}>{fold.label}</button> : null,
+}));
+vi.mock('@/components/sidebar/SortableList', () => ({ SortableList: () => null }));
+vi.mock('../features/cc-agent/sidebar/sections/ProjectNode', () => ({ ProjectNode: () => null }));
+// Keep device grouping real; the header bridge is outside this rendering test.
+vi.mock('../features/cc-agent/sidebar/DeviceSectionHeader', () => ({
+ DeviceSectionHeader: ({ children }: { children: ReactNode }) => <>{children}</>,
+}));
+vi.mock('../features/cc-agent/sidebar/sections/UnclassifiedSection', () => ({
+ UnclassifiedSection: () => null,
+}));
+vi.mock('@/features/bots/BotAvatar', () => ({ BotAvatar: () => <span>Bot avatar</span> }));
+vi.mock('../features/cc-agent/sidebar/SessionItem', () => ({ SessionItem: () => null }));
+vi.mock('../features/cc-agent/sidebar/SessionCard', () => ({ SessionCard: () => null }));
+vi.mock('../features/cc-agent/sidebar/AutomationSessionGroupItem', () => ({
+ AutomationSessionGroupItem: () => null,
+}));
+
+function props(groupDevice: boolean): ProjectsSectionProps {
+ return {
+ unclassified: [],
+ projects: [],
+ dialogues: [],
+ allKnownProjects: [],
+ allProjectKeysForOrder: [],
+ bots: [],
+ filter: {
+ groupBy: 'project',
+ groupDialogue: true,
+ groupDevice,
+ sortBy: 'recency',
+ projectOrder: 'activity',
+ manualProjectOrder: [],
+ projects: 'all',
+ status: 'active',
+ isFilterActive: false,
+ projectsAsSet: null,
+ isSessionContentFiltered: false,
+ vendor: 'all',
+ lastActivity: 'all',
+ manualPinnedOrder: [],
+ setStatus: vi.fn(),
+ toggleProject: vi.fn(),
+ ensureProjectIncluded: vi.fn(),
+ setProjectsAll: vi.fn(),
+ gc: vi.fn(),
+ setVendor: vi.fn(),
+ setLastActivity: vi.fn(),
+ setGroupBy: vi.fn(),
+ setGroupDialogue: vi.fn(),
+ setGroupDevice: vi.fn(),
+ setSortBy: vi.fn(),
+ setProjectOrder: vi.fn(),
+ resetContentFilters: vi.fn(),
+ setManualProjectOrder: vi.fn(),
+ setManualPinnedOrder: vi.fn(),
+ promotePin: vi.fn(),
+ removePin: vi.fn(),
+ },
+ collapsed: new Set(),
+ isAllCollapsed: false,
+ runningSessionIds: new Set(),
+ attachedSessionIds: new Set(),
+ notifications: new Set(),
+ scheduleSessionIndex: new Map(),
+ remoteDeviceIndex: new Map([['remote', { name: 'Remote device', online: true }]]),
+ onSessionClick: vi.fn(),
+ onAction: vi.fn(),
+ onRename: vi.fn(),
+ onTogglePin: vi.fn(),
+ onScheduleAction: vi.fn(),
+ onToggleProject: vi.fn(),
+ onToggleProjectPin: vi.fn(),
+ onRenameProject: vi.fn(),
+ onRemoveFromSidebar: vi.fn(),
+ onCollapseAll: vi.fn(),
+ onExpandAll: vi.fn(),
+ onCreateInProject: vi.fn(),
+ onOpenConversationSearch: vi.fn(),
+ onOpenInExplorer: vi.fn(),
+ onLinkCodexProject: vi.fn(),
+ linkingCodexProject: null,
+ onBrowseFiles: vi.fn(),
+ onArchiveAll: vi.fn(),
+ onCreateDialogue: vi.fn(),
+ };
+}
+
+beforeEach(() => {
+ localStorage.clear();
+ localStorage.setItem('sidebar.collapse.projectSessionLimit', '1');
+ // ProjectsSection 渲染时读 window.electronAPI.platform 做项目键比较(main 897de9031);
+ // jsdom 没有 preload 桥,这里只补 platform 字段,与 windowCloseBehavior 用例同法。
+ Object.defineProperty(window, 'electronAPI', {
+ configurable: true,
+ value: { platform: 'darwin' } as unknown as Window['electronAPI'],
+ });
+});
+afterEach(() => {
+ cleanup();
+ localStorage.clear();
+});
+
+describe('Online device headers without tasks', () => {
+ it('renders and folds an empty online device, then removes it when offline', () => {
+ const p = { ...props(true), bots: [], allProjectKeysForOrder: [] };
+ const view = render(<ProjectsSection {...p} />);
+ const header = screen.getByText('Remote device').closest('button')!;
+ expect(header.getAttribute('aria-expanded')).toBe('true');
+ fireEvent.click(header);
+ expect(header.getAttribute('aria-expanded')).toBe('false');
+ view.rerender(
+ <ProjectsSection
+ {...p}
+ remoteDeviceIndex={new Map([['remote', { name: 'Remote device', online: false }]])}
+ />,
+ );
+ expect(screen.queryByText('Remote device')).toBeNull();
+ view.rerender(<ProjectsSection {...p} />);
+ expect(screen.getByText('Remote device').closest('button')!.getAttribute('aria-expanded')).toBe(
+ 'false',
+ );
+ });
+
+ it('does not show device headers when device grouping is disabled', () => {
+ render(<ProjectsSection {...props(false)} bots={[]} allProjectKeysForOrder={[]} />);
+ expect(screen.queryByText('Remote device')).toBeNull();
+ });
+});
diff --git a/apps/desktop/src/renderer/features/cc-agent/CCAgentSidebarUpper.tsx b/apps/desktop/src/renderer/features/cc-agent/CCAgentSidebarUpper.tsx
index bfabfde974..2724249d6b 100644
--- a/apps/desktop/src/renderer/features/cc-agent/CCAgentSidebarUpper.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/CCAgentSidebarUpper.tsx
@@ -174,6 +174,7 @@ import {
import { PinnedSection, type PinnedSidebarEntry } from './sidebar/sections/PinnedSection';
import { ProjectNode as ProjectNodeView } from './sidebar/sections/ProjectNode';
import { compareDialogueSessions, type DialogueSortBy } from './sidebar/sections/DialogueSection';
+import { onlineDeviceSectionIds } from './lib/mainListModel';
import { sidebarPriorityContext } from './lib/sidebarPriorityContext';
import {
holdSidebarViewedPriority,
@@ -1888,17 +1889,21 @@ function ExpandedView({
);
// D 期:按日期分组已删除(visibleDateSessions 随 DateGroupedSessionsSection 一并下线)。
- const hasVisibleSidebarContent =
- visiblePinnedEntries.length > 0 ||
- visibleUnclassified.length > 0 ||
- visibleProjectsWithVendor.length > 0 ||
- visibleDialogues.length > 0;
// 与 ProjectsSection.deviceGroupingAvailable 同一门控:范围收窄到单台机器时
// 「按设备分组」选项隐藏。占位分支也要挂范围标题,不能各写一份。
const deviceGroupingAvailable =
(remoteDeviceIndex?.size ?? 0) > 0 &&
!(selectedMachineId !== MACHINE_ALL && selectedMachineId.length === 1);
+ const hasVisibleSidebarContent =
+ (deviceGroupingAvailable &&
+ filter.groupDevice &&
+ onlineDeviceSectionIds(remoteDeviceIndex, selectedMachineId).length > 0) ||
+ visiblePinnedEntries.length > 0 ||
+ visibleUnclassified.length > 0 ||
+ visibleProjectsWithVendor.length > 0 ||
+ visibleDialogues.length > 0;
+
const [selectedSessionIds, setSelectedSessionIds] = useState<Set<string>>(() => new Set());
const [selectionAnchorSessionId, setSelectionAnchorSessionId] = useState<string | null>(null);
// 这几个值 handleSessionClick 只在「点击那一刻」读一次。留在它的 deps 里会让
diff --git a/apps/desktop/src/renderer/features/cc-agent/lib/mainListModel.ts b/apps/desktop/src/renderer/features/cc-agent/lib/mainListModel.ts
index a959c7f7ad..7886bb82ed 100644
--- a/apps/desktop/src/renderer/features/cc-agent/lib/mainListModel.ts
+++ b/apps/desktop/src/renderer/features/cc-agent/lib/mainListModel.ts
@@ -25,6 +25,11 @@
import type { Session } from '@/lib/ccAgent.types';
import { isCindyMakeFamilySource } from '../../../../shared/cindyMakeMerge';
+import {
+ MACHINE_ALL,
+ MACHINE_LOCAL,
+ type MachineSelection,
+} from '@/features/device-link/selectedMachineStore';
import { LIVE_TASK_PRIORITY, liveTaskPriorityRank } from '../../../../shared/liveTaskPriority';
import type { FilterProjectOrder, FilterSortBy } from '../hooks/helpers/sidebarFilterCore';
@@ -488,6 +493,19 @@ export interface MainListDeviceSection {
entries: MainListEntry[];
}
+/** Online device headers remain visible independently of task filters. */
+export function onlineDeviceSectionIds(
+ devices: ReadonlyMap<string, { online: boolean }> | null | undefined,
+ selection: MachineSelection,
+): Array<string | null> {
+ const ids: Array<string | null> = [];
+ if (selection === MACHINE_ALL || selection.includes(MACHINE_LOCAL)) ids.push(null);
+ for (const [id, device] of devices ?? []) {
+ if (device.online && (selection === MACHINE_ALL || selection.includes(id))) ids.push(id);
+ }
+ return ids;
+}
+
function entryDeviceId(entry: MainListEntry): string | null {
if (entry.kind === 'project') return entry.project.deviceLinkDeviceId ?? null;
if (entry.kind === 'session') return entry.session.deviceLinkDeviceId ?? null;
@@ -514,6 +532,7 @@ export function splitEntriesByDevice(
entries: readonly MainListEntry[],
deviceOrder: readonly string[],
options: {
+ onlineDeviceIds?: readonly (string | null)[];
sortBy?: FilterSortBy;
projectOrder?: FilterProjectOrder;
manualProjectOrder?: readonly string[];
@@ -545,7 +564,9 @@ export function splitEntriesByDevice(
}
}
- const sections = new Map<string | null, MainListEntry[]>();
+ const sections = new Map<string | null, MainListEntry[]>(
+ (options.onlineDeviceIds ?? []).map((id) => [id, []]),
+ );
for (const entry of flattened) {
const key = entryDeviceId(entry);
const list = sections.get(key);
@@ -557,7 +578,7 @@ export function splitEntriesByDevice(
const result: MainListDeviceSection[] = [];
for (const id of orderedIds) {
const sectionEntries = sections.get(id);
- if (sectionEntries && sectionEntries.length > 0) {
+ if (sectionEntries) {
result.push({
deviceId: id,
entries: sortSectionEntries(sectionEntries, options),
diff --git a/apps/desktop/src/renderer/features/cc-agent/sidebar/DeviceSectionHeader.tsx b/apps/desktop/src/renderer/features/cc-agent/sidebar/DeviceSectionHeader.tsx
index 3f92cb40d8..0e980896d6 100644
--- a/apps/desktop/src/renderer/features/cc-agent/sidebar/DeviceSectionHeader.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/sidebar/DeviceSectionHeader.tsx
@@ -7,17 +7,9 @@ import { cn } from '@/lib/utils';
import { toast } from '@/lib/toast';
import { useRemoteDesktopAvailability } from '@/features/remote-desktop/useRemoteDesktopAvailability';
-function RemoteDesktopShortcut({
- deviceId,
- name,
- active,
-}: {
- deviceId: string;
- name: string;
- active: boolean;
-}) {
+function RemoteDesktopShortcut({ deviceId, name }: { deviceId: string; name: string }) {
const { t } = useTranslation();
- const availability = useRemoteDesktopAvailability(deviceId, active);
+ const availability = useRemoteDesktopAvailability(deviceId);
const [opening, setOpening] = useState(false);
const openingRef = useRef(false);
const generation = useRef(0);
@@ -54,17 +46,22 @@ function RemoteDesktopShortcut({
<button
type="button"
aria-label={label}
- aria-disabled={!availability.available || opening}
+ aria-disabled={busy}
aria-busy={busy || undefined}
className={cn(
SIDEBAR_RAIL_ICON_BUTTON_CLASS,
'h-6 w-6 aria-disabled:opacity-50 aria-disabled:hover:bg-transparent',
'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--focus-ring)]',
- active && busy && 'motion-safe:[&_svg]:animate-spin',
+ !availability.available && 'opacity-50',
+ busy && 'motion-safe:[&_svg]:animate-spin',
)}
onClick={(event) => {
event.stopPropagation();
- if (!availability.available || openingRef.current) return;
+ if (busy || openingRef.current) return;
+ if (!availability.available) {
+ toast.info(label, { duration: 8000 });
+ return;
+ }
const current = generation.current;
openingRef.current = true;
setOpening(true);
@@ -98,22 +95,10 @@ export function DeviceSectionHeader({
name: string;
children: ReactNode;
}) {
- const [hovered, setHovered] = useState(false);
- const [focused, setFocused] = useState(false);
return (
- <div
- className="group/device-header flex min-w-0 items-center gap-1"
- onMouseEnter={() => setHovered(true)}
- onMouseLeave={() => setHovered(false)}
- onFocusCapture={(event) => setFocused(event.target.matches(':focus-visible'))}
- onBlurCapture={(event) => {
- if (!event.currentTarget.contains(event.relatedTarget as Node | null)) setFocused(false);
- }}
- >
+ <div className="group/device-header flex min-w-0 items-center gap-1">
{children}
- {deviceId && (
- <RemoteDesktopShortcut deviceId={deviceId} name={name} active={hovered || focused} />
- )}
+ {deviceId && <RemoteDesktopShortcut deviceId={deviceId} name={name} />}
</div>
);
}
diff --git a/apps/desktop/src/renderer/features/cc-agent/sidebar/__tests__/DeviceSectionHeader.test.tsx b/apps/desktop/src/renderer/features/cc-agent/sidebar/__tests__/DeviceSectionHeader.test.tsx
index 4e650c5d96..6c12c49fca 100644
--- a/apps/desktop/src/renderer/features/cc-agent/sidebar/__tests__/DeviceSectionHeader.test.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/sidebar/__tests__/DeviceSectionHeader.test.tsx
@@ -12,7 +12,7 @@ vi.mock('@/features/device-link/useDeviceLinkDeviceList', () => ({
useDeviceLinkDeviceList: () => fixture.devices,
useDeviceLinkDeviceListRequestState: () => ({ status: 'ready', error: null }),
}));
-vi.mock('@/lib/toast', () => ({ toast: { error: vi.fn() } }));
+vi.mock('@/lib/toast', () => ({ toast: { error: vi.fn(), info: vi.fn() } }));
const capabilities = {
version: 1,
@@ -72,11 +72,12 @@ async function hover() {
}
function unavailable() {
const button = screen.getByRole('button', { name: /^Remote desktop unavailable:/ });
- expect(button.getAttribute('aria-disabled')).toBe('true');
+ expect(button.getAttribute('aria-disabled')).toBe('false');
expect(button.querySelector('.lucide-monitor-off')).not.toBeNull();
fireEvent.click(button);
expect(open).not.toHaveBeenCalled();
expect(toggle).not.toHaveBeenCalled();
+ expect(toast.info).toHaveBeenCalledWith(button.getAttribute('aria-label'), { duration: 8000 });
return button;
}
@@ -87,13 +88,14 @@ it('does not add a remote shortcut or probe to the local machine', async () => {
expect(invoke).not.toHaveBeenCalled();
});
-it('checks on hover without starting a desktop, then opens the right machine without toggling it', async () => {
+it('checks automatically while revealing the shortcut only on hover or keyboard focus', async () => {
render(header());
expect(invoke).not.toHaveBeenCalled();
const action = screen.getByRole('button', { name: 'Checking remote desktop…' });
+ expect(action.parentElement!.className).toContain('opacity-0');
expect(action.parentElement!.className).toContain('group-hover/device-header:opacity-100');
expect(action.parentElement!.className).toContain('has-[:focus-visible]:opacity-100');
- await hover();
+ await act(async () => {});
expect(invoke).toHaveBeenCalledExactlyOnceWith('remote', REMOTE_DESKTOP_CHANNEL, [
{ op: 'capabilities' },
]);
@@ -113,7 +115,10 @@ it.each([
{ remoteControlEnabled: false },
i18n.getResource('en', 'common', 'remoteDesktop.remoteDisabled'),
],
- [{ controlEnabled: false }, 'Allow control of this computer in Remote control settings.'],
+ [
+ { controlEnabled: false },
+ i18n.getResource('en', 'common', 'remoteDesktop.shortcut.controlDisabled'),
+ ],
[{ platform: 'ios' }, 'This device does not support remote desktop.'],
[{ isSelf: true }, 'This device does not support remote desktop.'],
] satisfies [Partial<DeviceLinkDeviceView>, string][])(
@@ -181,19 +186,19 @@ it.each(['CHANNEL_NOT_ALLOWED', 'ACCESS_REVOKED', 'INVOKE_TIMEOUT'])(
},
);
-it('debounces quick passes, refreshes on re-entry, and never polls while hovering', async () => {
+it('remembers the automatic check across pointer re-entry without polling', async () => {
render(header());
fireEvent.mouseEnter(row());
fireEvent.mouseLeave(row());
await act(async () => vi.advanceTimersByTimeAsync(200));
- expect(invoke).not.toHaveBeenCalled();
+ expect(invoke).toHaveBeenCalledOnce();
await hover();
fireEvent.mouseLeave(row());
invoke.mockResolvedValue({ ...capabilities, enabled: false });
await hover();
- unavailable();
+ expect(screen.getByRole('button', { name: 'Open remote desktop' })).toBeTruthy();
await act(async () => vi.advanceTimersByTimeAsync(60000));
- expect(invoke).toHaveBeenCalledTimes(2);
+ expect(invoke).toHaveBeenCalledOnce();
});
it('ignores a late capability reply after the machine goes offline', async () => {
@@ -245,3 +250,59 @@ it('turns a failed open into an unavailable icon and coalesces repeated clicks',
).not.toBeNull();
expect(toast.error).toHaveBeenCalledWith(i18n.t('remoteDesktop.connectionError'));
});
+
+it('retains an in-flight result after pointer leave and a routine directory refresh', async () => {
+ let resolve!: (value: unknown) => void;
+ invoke.mockReturnValue(
+ new Promise((done) => {
+ resolve = done;
+ }),
+ );
+ const view = render(header());
+ await hover();
+ fireEvent.mouseLeave(row());
+ fixture.devices = [{ ...device(), busy: false, name: 'Updated name' }];
+ view.rerender(header());
+ await act(async () => resolve(capabilities));
+ await hover();
+ expect(screen.getByRole('button', { name: 'Open remote desktop' })).toBeTruthy();
+ expect(invoke).toHaveBeenCalledOnce();
+});
+
+it('checks again after an offline/online transition, but not on repeated hover', async () => {
+ const view = render(header());
+ await hover();
+ fixture.devices = [{ ...device(), online: false }];
+ view.rerender(header());
+ unavailable();
+ fixture.devices = [device()];
+ invoke.mockResolvedValue({ ...capabilities, enabled: false });
+ view.rerender(header());
+ await act(async () => vi.advanceTimersByTimeAsync(151));
+ unavailable();
+ fireEvent.mouseLeave(row());
+ await hover();
+ unavailable();
+ expect(invoke).toHaveBeenCalledTimes(2);
+});
+
+it('waits until the device is online, then probes without a pointer interaction', async () => {
+ fixture.devices = [{ ...device(), online: false }];
+ const view = render(header());
+ await act(async () => {});
+ expect(invoke).not.toHaveBeenCalled();
+ fixture.devices = [device()];
+ view.rerender(header());
+ await act(async () => {});
+ expect(invoke).toHaveBeenCalledOnce();
+ expect(screen.getByRole('button', { name: 'Open remote desktop' })).toBeTruthy();
+});
+
+it('explains how to grant screen recording on the remote Mac when clicked', async () => {
+ invoke.mockResolvedValue({ ...capabilities, permissions: { screenRecording: 'missing' } });
+ render(header());
+ await act(async () => {});
+ const action = unavailable();
+ expect(action.getAttribute('aria-label')).toContain('On the remote Mac, open System Settings');
+ expect(action.getAttribute('aria-label')).not.toContain('accessibility');
+});
diff --git a/apps/desktop/src/renderer/features/cc-agent/sidebar/sections/ProjectsSection.tsx b/apps/desktop/src/renderer/features/cc-agent/sidebar/sections/ProjectsSection.tsx
index f64e6b59d3..0c295a732a 100644
--- a/apps/desktop/src/renderer/features/cc-agent/sidebar/sections/ProjectsSection.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/sidebar/sections/ProjectsSection.tsx
@@ -73,6 +73,7 @@ import {
CINDY_MAKE_GROUP_KEY,
getMainListEntrySessions,
holdViewedPriorityRank,
+ onlineDeviceSectionIds,
splitEntriesByDevice,
type MainListDeviceSection,
type MainListEntry,
@@ -662,6 +663,7 @@ export function ProjectsSection({
// review P1:此前先全局折叠再切段,排在前 N 名之外的设备连段头一起消失,
// 看起来像"这台设备没有任务"——设备是最外层层级,折叠只能发生在段内)。
return splitEntriesByDevice(mixedEntries, [...(remoteDeviceIndex?.keys() ?? [])], {
+ onlineDeviceIds: onlineDeviceSectionIds(remoteDeviceIndex, selectedMachineId),
sortBy: filter.sortBy,
projectOrder: filter.projectOrder,
manualProjectOrder: filter.manualProjectOrder,
@@ -672,6 +674,7 @@ export function ProjectsSection({
visibleMixedEntries,
mixedEntries,
remoteDeviceIndex,
+ selectedMachineId,
filter.sortBy,
filter.projectOrder,
filter.manualProjectOrder,
@@ -843,6 +846,7 @@ export function ProjectsSection({
// 时仍画范围标题行(2026-08-13 第 4 轮 review P1:段头恒在),不画列表树。
// 早退必须在全部 hooks 之后——rules of hooks。
const hasMainListContent =
+ (deviceGroupingActive && deviceSections.length > 0) ||
allProjectKeysForOrder.length > 0 ||
unclassified.length > 0 ||
dialogues.length > 0 ||
diff --git a/apps/desktop/src/renderer/features/remote-desktop/RemoteDesktopViewerWindow.tsx b/apps/desktop/src/renderer/features/remote-desktop/RemoteDesktopViewerWindow.tsx
index 46fd1af534..fcf8d36d2b 100644
--- a/apps/desktop/src/renderer/features/remote-desktop/RemoteDesktopViewerWindow.tsx
+++ b/apps/desktop/src/renderer/features/remote-desktop/RemoteDesktopViewerWindow.tsx
@@ -1,15 +1,15 @@
-import { useCallback, useEffect, useRef, useState } from 'react';
+import { useCallback, useEffect, useRef, useState, type ReactNode } from 'react';
import { useTranslation } from 'react-i18next';
import {
- Maximize2,
- Minimize2,
- Volume2,
- VolumeX,
- Settings2,
- LogOut,
+ SlidersHorizontal,
+ Clipboard,
+ Shield,
+ AppWindow,
+ LayoutGrid,
+ ZoomIn,
+ ZoomOut,
+ X,
Monitor,
- Eye,
- MousePointer2,
} from 'lucide-react';
import type { RemoteDesktopDisplayMode } from '@cindy/device-link';
import { WindowControls } from '@/components/title-bar/WindowControls';
@@ -20,6 +20,9 @@ import { Select } from '@/components/ui/select';
import { Button } from '@/components/ui/button';
import { FormField } from '@/components/ui/form-field';
import { ConfirmDialog } from '@/components/ui/confirm-dialog';
+import { Switch } from '@/components/ui/switch';
+import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
+import { Tip } from '@/components/ui/tooltip';
/** A clean, standalone remote desktop surface. No App, router, agent or task providers. */
export function RemoteDesktopViewerWindow() {
@@ -29,26 +32,41 @@ export function RemoteDesktopViewerWindow() {
const root = useRef<HTMLDivElement>(null),
controller = useRef<DesktopViewerController | null>(null);
const [state, setState] = useState<ViewerSnapshot | null>(null);
- const [settings, setSettings] = useState(false),
+ const [settings, setSettings] = useState<'display' | 'clipboard' | 'security' | null>(null),
[selectOpen, setSelectOpen] = useState(false),
[notice, setNotice] = useState<string | null>(null),
[closeGeneration, setCloseGeneration] = useState<number | null>(null);
const [modes, setModes] = useState<RemoteDesktopDisplayMode[]>([]);
+ const [modesStatus, setModesStatus] = useState<'idle' | 'loading' | 'failed'>('idle');
const generation = useRef(-1);
+ const latestState = useRef<ViewerSnapshot | null>(null);
+ const activePanel = useRef(settings);
+ activePanel.current = settings;
const requestClose = useCallback(() => {
controller.current?.releaseInput();
- setSettings(false);
+ setSettings(null);
setSelectOpen(false);
+ if (!latestState.current?.ready) {
+ const owner = generation.current;
+ void controller.current?.close().catch(() => {
+ if (owner !== generation.current) return;
+ setNotice(i18n.t('remoteDesktop.viewer.disconnectFailed'));
+ });
+ return;
+ }
setCloseGeneration(generation.current);
}, []);
useEffect(() => {
if (!root.current) return;
- controller.current = new DesktopViewerController(api, root.current, setState);
+ controller.current = new DesktopViewerController(api, root.current, (snapshot) => {
+ latestState.current = snapshot;
+ setState(snapshot);
+ });
const off = api.onActive((value) => {
if (value.generation !== generation.current || !value.active) setCloseGeneration(null);
generation.current = value.generation;
if (!value.active) {
- setSettings(false);
+ setSettings(null);
setSelectOpen(false);
setNotice(null);
setModes([]);
@@ -95,14 +113,24 @@ export function RemoteDesktopViewerWindow() {
controller.current = null;
};
}, [api, requestClose]);
- const toggleSettings = () => {
+ const loadModes = () => {
+ const owner = generation.current;
+ setModesStatus('loading');
+ void controller.current
+ ?.displayModes()
+ .then((value) => {
+ if (owner !== generation.current) return;
+ setModes(value);
+ setModesStatus('idle');
+ })
+ .catch(() => {
+ if (owner === generation.current) setModesStatus('failed');
+ });
+ };
+ const openPanel = (panel: 'display' | 'clipboard' | 'security', open: boolean) => {
controller.current?.releaseInput();
- setSettings((value) => !value);
- if (state?.caps?.displayModes)
- void controller.current
- ?.displayModes()
- .then(setModes)
- .catch(() => setModes([]));
+ setSettings((current) => (open ? panel : current === panel ? null : current));
+ if (open && panel === 'display' && state?.caps?.displayModes) loadModes();
};
const onSelectOpenChange = (open: boolean) => {
setSelectOpen(open);
@@ -120,16 +148,85 @@ export function RemoteDesktopViewerWindow() {
? 'remoteDesktop.directConnection'
: 'remoteDesktop.live',
)}
+ <span>
+ {' '}
+ ·{' '}
+ {state.receiveRate == null
+ ? '— KB/s'
+ : state.receiveRate >= 1_000_000
+ ? `${(state.receiveRate / 1_000_000).toFixed(1)} MB/s`
+ : `${Math.round(state.receiveRate / 1000)} KB/s`}
+ </span>
{state.latency !== null && (
<span className="remote-viewer-latency"> · {Math.round(state.latency)} ms</span>
)}
</span>
);
+ const controlPrompt = !state?.controlling && (
+ <div className="flex flex-col gap-2" role="status">
+ <p>
+ {t(
+ !state?.ready
+ ? 'remoteDesktop.connecting'
+ : state.controlPending
+ ? 'remoteDesktop.viewer.controlPending'
+ : 'remoteDesktop.viewer.controlRequired',
+ )}
+ </p>
+ <Button
+ variant="secondary"
+ disabled={!state?.ready || !state.caps?.canControl || state.controlPending}
+ loading={state?.controlPending}
+ onClick={() => void controller.current?.setControl(true)}
+ >
+ {t('remoteDesktop.takeControl')}
+ </Button>
+ </div>
+ );
+ const preference = (key: 'privacyScreen' | 'hostMute' | 'clipboardSync' | 'lockOnExit') =>
+ state && (
+ <div className="remote-viewer-preference" key={key}>
+ <label htmlFor={`viewer-${key}`}>
+ <span>{t(`remoteDesktop.${key}`)}</span>
+ <Switch
+ id={`viewer-${key}`}
+ checked={state.preferences[key]}
+ disabled={
+ !state.ready ||
+ state.closing ||
+ (!state.caps?.[key] && !state.preferences[key]) ||
+ (key !== 'lockOnExit' && !state.controlling && !state.preferences[key])
+ }
+ onCheckedChange={(enabled) => void controller.current?.preference({ [key]: enabled })}
+ />
+ </label>
+ <p>
+ {t(
+ !state.ready
+ ? 'remoteDesktop.loadingSettings'
+ : !state.caps?.[key]
+ ? 'remoteDesktop.settingUnsupported'
+ : key === 'privacyScreen' && state.safety.privacyActive
+ ? 'remoteDesktop.privacyActive'
+ : `remoteDesktop.${key}Hint`,
+ )}
+ </p>
+ </div>
+ );
return (
- <div className={`remote-viewer-window ${isFullscreen ? 'remote-viewer-fullscreen' : ''}`}>
+ <div
+ className={`remote-viewer-window ${isFullscreen ? 'remote-viewer-fullscreen' : ''}`}
+ onPointerDownCapture={(event) => {
+ if (settings && root.current?.contains(event.target as Node)) {
+ event.preventDefault();
+ event.stopPropagation();
+ setSettings(null);
+ }
+ }}
+ >
<header
className="remote-viewer-toolbar"
- data-settings-open={settings || undefined}
+ data-settings-open={!!settings || undefined}
data-select-open={selectOpen || undefined}
style={{ paddingLeft: isMac && !isFullscreen ? 82 : 12 }}
>
@@ -140,99 +237,122 @@ export function RemoteDesktopViewerWindow() {
</span>
<div className="remote-viewer-status">
<span>
- {state?.ready
- ? t(state.controlling ? 'remoteDesktop.controlling' : 'remoteDesktop.viewOnly')
- : t('remoteDesktop.connecting')}
+ {state?.closing
+ ? t(
+ state.preferences.lockOnExit && state.caps?.lockOnExit
+ ? 'remoteDesktop.lockingOnExit'
+ : 'remoteDesktop.disconnecting',
+ )
+ : state?.ready
+ ? t(state.controlling ? 'remoteDesktop.controlling' : 'remoteDesktop.viewOnly')
+ : t('remoteDesktop.connecting')}
</span>
- {network && <span aria-hidden="true">·</span>}
- {network}
+ {state?.ready && (
+ <span className="remote-viewer-toolbar-network">
+ <span aria-hidden="true"> · </span>
+ {t(
+ state.transport === 'screenshots'
+ ? 'remoteDesktop.screenshotRelay'
+ : state.transport === 'relay'
+ ? 'remoteDesktop.videoRelay'
+ : state.transport === 'direct'
+ ? 'remoteDesktop.directConnection'
+ : 'remoteDesktop.live',
+ )}
+ </span>
+ )}
</div>
</div>
- {(state?.caps?.displays.length ?? 0) > 1 && (
- <Select
- label={t('remoteDesktop.display')}
- className="remote-viewer-display-select"
- value={state?.displayId ?? ''}
- options={
- state?.caps?.displays.map((display) => ({
- value: display.id,
- label: display.name,
- })) ?? []
- }
- onValueChange={(value) => controller.current?.selectDisplay(value)}
- onOpenChange={onSelectOpenChange}
- />
- )}
- <Button
- variant="secondary"
- className={action}
- disabled={!state?.ready || !state.caps?.canControl || state.controlPending}
- title={t(
- state?.controlling ? 'remoteDesktop.releaseControl' : 'remoteDesktop.takeControl',
- )}
- aria-label={t(
- state?.controlling ? 'remoteDesktop.releaseControl' : 'remoteDesktop.takeControl',
- )}
- onClick={() => void controller.current?.setControl(!state?.controlling)}
+ <div
+ className="remote-viewer-toolgroup"
+ role="group"
+ aria-label={t('remoteDesktop.viewer.controlGroup')}
>
- {state?.controlling ? <MousePointer2 size={16} /> : <Eye size={16} />}
- </Button>
- {state?.caps?.systemAudio && (
- <Button
- variant="secondary"
- className={action}
- aria-label={t('remoteDesktop.viewer.sound')}
- aria-pressed={state.settings.audio}
- onClick={() => controller.current?.settings({ audio: !state.settings.audio })}
+ <ViewerTool
+ label={t('remoteDesktop.allWindows')}
+ disabled={!state?.controlling || state.controlPending}
+ onClick={() =>
+ controller.current?.keys(
+ state?.caps?.platform === 'darwin'
+ ? ['ControlLeft', 'ArrowUp']
+ : ['MetaLeft', 'Tab'],
+ )
+ }
>
- {state.settings.audio ? <Volume2 size={16} /> : <VolumeX size={16} />}
- </Button>
- )}
- <Button
- variant="secondary"
- className={action}
- aria-label={t(
- isFullscreen
- ? 'remoteDesktop.viewer.exitFullscreen'
- : 'remoteDesktop.viewer.fullscreen',
- )}
- onClick={() => void api.fullscreen()}
- >
- {isFullscreen ? <Minimize2 size={16} /> : <Maximize2 size={16} />}
- </Button>
- <Button
- variant="secondary"
- className={action}
+ <LayoutGrid size={16} />
+ </ViewerTool>
+ <ViewerTool
+ label={t('remoteDesktop.showDesktop')}
+ disabled={!state?.controlling || state.controlPending}
+ onClick={() =>
+ controller.current?.keys(
+ state?.caps?.platform === 'darwin' ? ['MetaLeft', 'F3'] : ['MetaLeft', 'KeyD'],
+ )
+ }
+ >
+ <AppWindow size={16} />
+ </ViewerTool>
+ </div>
+ <div className="remote-viewer-zoom-group" role="group" aria-label={t('remoteDesktop.fit')}>
+ <ViewerTool
+ label={t('remoteDesktop.zoomOut')}
+ disabled={!state?.ready}
+ onClick={() => controller.current?.zoom('out')}
+ >
+ <ZoomOut size={18} />
+ </ViewerTool>
+ <ViewerTool
+ label={t('remoteDesktop.fit')}
+ pressed={(state?.scaleMode ?? 'fit') === 'fit'}
+ disabled={!state?.ready}
+ onClick={() => controller.current?.fit()}
+ >
+ <ZoomModeIcon />
+ </ViewerTool>
+ <ViewerTool
+ label={t('remoteDesktop.actualSize')}
+ pressed={state?.scaleMode === 'actual'}
+ disabled={!state?.ready}
+ onClick={() => controller.current?.actualSize()}
+ >
+ <ZoomModeIcon actual />
+ </ViewerTool>
+ <span className="remote-viewer-zoom-divider" aria-hidden="true" />
+ <ViewerTool
+ label={t('remoteDesktop.zoomIn')}
+ disabled={!state?.ready}
+ onClick={() => controller.current?.zoom('in')}
+ >
+ <ZoomIn size={18} />
+ </ViewerTool>
+ </div>
+ <div
+ className="remote-viewer-panels"
+ role="group"
aria-label={t('remoteDesktop.viewer.settings')}
- aria-expanded={settings}
- onClick={toggleSettings}
- >
- <Settings2 size={16} />
- </Button>
- <Button
- variant="secondary"
- className={action}
- aria-label={t('remoteDesktop.disconnect')}
- onClick={requestClose}
>
- <LogOut size={16} />
- </Button>
- {!isMac && <WindowControls onClose={requestClose} />}
- {settings && (
- <aside className="remote-viewer-settings" aria-label={t('remoteDesktop.viewer.settings')}>
- <div className="flex items-center justify-between">
- <strong>{t('remoteDesktop.viewer.settings')}</strong>
- <Button variant="secondary" className={action} onClick={() => setSettings(false)}>
- {t('remoteDesktop.closePermissionGuide')}
- </Button>
- </div>
- <Button
- variant="secondary"
- className={action}
- onClick={() => controller.current?.fit()}
- >
- {t('remoteDesktop.fit')}
- </Button>
+ <ViewerPanel
+ label={t('remoteDesktop.viewer.displayPanel')}
+ icon={<SlidersHorizontal size={16} />}
+ open={settings === 'display'}
+ restoreFocus={() => activePanel.current === null}
+ onOpenChange={(open) => openPanel('display', open)}
+ >
+ {(state?.caps?.displays.length ?? 0) > 1 && (
+ <Select
+ label={t('remoteDesktop.display')}
+ className="remote-viewer-display-select"
+ value={state?.displayId ?? ''}
+ options={
+ state?.caps?.displays.map((display) => ({
+ value: display.id,
+ label: display.name,
+ })) ?? []
+ }
+ onValueChange={(value) => controller.current?.selectDisplay(value)}
+ onOpenChange={onSelectOpenChange}
+ />
+ )}
{state?.caps?.videoSettings && (
<>
<FormField label={t('remoteDesktop.viewer.fps')} className="remote-viewer-field">
@@ -281,7 +401,7 @@ export function RemoteDesktopViewerWindow() {
</>
)}
{state?.caps?.viewerDisplay && (
- <div className="remote-viewer-field">
+ <div className="flex flex-col gap-2">
<Button
variant="secondary"
disabled={!state.controlling || state.controlPending}
@@ -289,7 +409,7 @@ export function RemoteDesktopViewerWindow() {
if (!root.current) return;
void controller.current
?.fitDisplay(root.current.clientWidth, root.current.clientHeight)
- .then(() => setModes([]))
+ .then(loadModes)
.catch(() => setNotice(t('remoteDesktop.viewer.settingsFailed')));
}}
>
@@ -298,6 +418,17 @@ export function RemoteDesktopViewerWindow() {
<p>{t('remoteDesktop.viewer.fitViewerDisplayHint')}</p>
</div>
)}
+ {modesStatus === 'loading' && (
+ <p role="status">{t('remoteDesktop.loadingDisplayModes')}</p>
+ )}
+ {modesStatus === 'failed' && (
+ <div role="status">
+ <p>{t('remoteDesktop.displayModesFailed')}</p>
+ <Button variant="secondary" onClick={loadModes}>
+ {t('remoteDesktop.retry')}
+ </Button>
+ </div>
+ )}
{modes.length > 0 && (
<FormField
label={t('remoteDesktop.viewer.resolution')}
@@ -312,12 +443,12 @@ export function RemoteDesktopViewerWindow() {
value={modes.find((mode) => mode.current)?.id ?? ''}
options={modes.map((mode) => ({
value: mode.id,
- label: `${mode.width} × ${mode.height}`,
+ label: `${mode.width} × ${mode.height}${mode.native ? ` · ${t('remoteDesktop.nativeResolution')}` : ''}`,
}))}
onValueChange={(value) =>
void controller.current
?.resolution(value)
- .then(() => setModes([]))
+ .then(loadModes)
.catch(() => setNotice(t('remoteDesktop.viewer.settingsFailed')))
}
onOpenChange={onSelectOpenChange}
@@ -325,68 +456,155 @@ export function RemoteDesktopViewerWindow() {
)}
</FormField>
)}
- {!state?.controlling && (
- <div className="flex flex-col gap-2" role="status">
+
+ {state?.caps?.displayModes && <p>{t('remoteDesktop.viewer.resolutionHint')}</p>}
+ <div className="remote-viewer-panel-section">
+ <label className="remote-viewer-toggle-row" htmlFor="viewer-audio">
+ <span>{t('remoteDesktop.viewer.sound')}</span>
+ <Switch
+ id="viewer-audio"
+ checked={state?.settings.audio === true}
+ disabled={!state?.caps?.systemAudio || !state.ready}
+ onCheckedChange={(audio) => controller.current?.settings({ audio })}
+ />
+ </label>
+ {!state?.caps?.systemAudio && (
<p>
{t(
- !state?.ready
- ? 'remoteDesktop.connecting'
- : state.controlPending
- ? 'remoteDesktop.viewer.controlPending'
- : 'remoteDesktop.viewer.controlRequired',
+ state?.ready
+ ? 'remoteDesktop.settingUnsupported'
+ : 'remoteDesktop.loadingSettings',
)}
</p>
- <Button
- variant="secondary"
- disabled={!state?.ready || !state.caps?.canControl || state.controlPending}
- loading={state?.controlPending}
- onClick={() => void controller.current?.setControl(true)}
- >
- {t('remoteDesktop.takeControl')}
- </Button>
- </div>
- )}
- <Button
- variant="secondary"
- className={action}
- disabled={!state?.controlling || state.controlPending}
- onClick={() =>
- controller.current?.keys(
- state?.caps?.platform === 'darwin' ? ['MetaLeft', 'F3'] : ['MetaLeft', 'KeyD'],
- )
- }
- >
- {t('remoteDesktop.showDesktop')}
- </Button>
- <Button
- variant="secondary"
- className={action}
- disabled={!state?.controlling || state.controlPending}
- onClick={() =>
- controller.current?.keys(
- state?.caps?.platform === 'darwin'
- ? ['ControlLeft', 'ArrowUp']
- : ['MetaLeft', 'Tab'],
- )
- }
- >
- {t('remoteDesktop.allWindows')}
- </Button>
- <p>{t('remoteDesktop.viewer.inputHint')}</p>
- {state?.caps?.clipboardText && (
- <p>
- {t('remoteDesktop.viewer.clipboardShortcutHint', {
- modifier: isMac ? '⌘' : 'Ctrl',
- })}
- </p>
+ )}
+ {preference('hostMute')}
+ </div>
+ {controlPrompt}
+ {network && <div className="remote-viewer-panel-section">{network}</div>}
+ </ViewerPanel>
+ <ViewerPanel
+ label={t('remoteDesktop.viewer.clipboardPanel')}
+ icon={<Clipboard size={16} />}
+ active={state?.preferences.clipboardSync}
+ open={settings === 'clipboard'}
+ restoreFocus={() => activePanel.current === null}
+ onOpenChange={(open) => openPanel('clipboard', open)}
+ >
+ {preference('clipboardSync')}
+ {state && (
+ <>
+ {(state.caps?.clipboardText || state.caps?.clipboardContent) && (
+ <div className="flex gap-2">
+ {(['copy', 'paste'] as const).map((action) => (
+ <Button
+ key={action}
+ variant="secondary"
+ disabled={!state.controlling || state.closing}
+ onClick={() =>
+ void controller.current
+ ?.clipboard(action)
+ .catch(() => setNotice(t('remoteDesktop.viewer.clipboardFailed')))
+ }
+ >
+ {t(`remoteDesktop.${action}`)}
+ </Button>
+ ))}
+ </div>
+ )}
+ {state.safety.clipboardProgress !== null && (
+ <progress
+ aria-label={t('remoteDesktop.clipboardSync')}
+ max={1}
+ value={state.safety.clipboardProgress}
+ />
+ )}
+ </>
)}
- {state?.caps?.displayModes && <p>{t('remoteDesktop.viewer.resolutionHint')}</p>}
- {notice && <p role="status">{notice}</p>}
- </aside>
- )}
+ <p>
+ {t('remoteDesktop.viewer.clipboardShortcutHint', { modifier: isMac ? '⌘' : 'Ctrl' })}
+ </p>
+ {controlPrompt}
+ </ViewerPanel>
+ <ViewerPanel
+ label={t('remoteDesktop.viewer.securityPanel')}
+ icon={<Shield size={16} />}
+ active={state?.safety.privacyActive}
+ open={settings === 'security'}
+ restoreFocus={() => activePanel.current === null}
+ onOpenChange={(open) => openPanel('security', open)}
+ >
+ {preference('privacyScreen')}
+ {preference('lockOnExit')}
+ <div className="remote-viewer-panel-section">
+ {state && (
+ <>
+ {isMac && state.caps?.platform === 'darwin' && (
+ <div className="flex flex-col gap-2">
+ <label
+ className="flex items-center justify-between gap-3"
+ htmlFor="viewer-autoUnlock"
+ >
+ <span>{t('remoteDesktop.autoUnlock')}</span>
+ <Switch
+ id="viewer-autoUnlock"
+ checked={state.credential?.autoUnlock === true}
+ disabled={!state.ready || state.closing || state.credentialBusy}
+ onCheckedChange={(enabled) =>
+ void controller.current?.credential(enabled ? 'enable' : 'disable')
+ }
+ />
+ </label>
+ <p>{t('remoteDesktop.autoUnlockHint')}</p>
+ {state.credential?.autoUnlock && (
+ <label
+ className="flex items-center justify-between gap-3"
+ htmlFor="viewer-biometric"
+ >
+ <span>{t('remoteDesktop.biometricVerification')}</span>
+ <Switch
+ id="viewer-biometric"
+ checked={state.credential.biometricVerification}
+ disabled={
+ state.credentialBusy ||
+ (!state.credential.biometricAvailable &&
+ !state.credential.biometricVerification)
+ }
+ onCheckedChange={(enabled) =>
+ void controller.current?.credential('biometric', enabled)
+ }
+ />
+ </label>
+ )}
+ <p>{t('remoteDesktop.autoUnlockStorageHint')}</p>
+ {state.credentialBusy && (
+ <p role="status">{t('remoteDesktop.loadingSettings')}</p>
+ )}
+ {state.credentialNotice && (
+ <div role="alert">
+ <p>{t(`remoteDesktop.${state.credentialNotice}`)}</p>
+ <Button
+ variant="secondary"
+ onClick={() => controller.current?.retryCredential()}
+ >
+ {t('remoteDesktop.retry')}
+ </Button>
+ </div>
+ )}
+ </div>
+ )}
+ </>
+ )}
+ </div>
+ {controlPrompt}
+ </ViewerPanel>
+ </div>
+ {!isMac && <WindowControls onClose={requestClose} />}
</header>
<div ref={root} className="remote-viewer-content">
<div id="stage" tabIndex={0} aria-label={t('remoteDesktop.title')}>
+ <div id="bg" aria-hidden="true">
+ <canvas id="bg-canvas" />
+ </div>
<img id="image" alt="" />
<video id="video" autoPlay muted playsInline />
<div id="cursor">
@@ -408,7 +626,7 @@ export function RemoteDesktopViewerWindow() {
<span id="mouse-wheel-grip" />
</Button>
</div>
- {(!state?.ready || state?.error || state?.status === 'reconnecting') && (
+ {!state?.closing && (!state?.ready || state?.error || state?.status === 'reconnecting') && (
<div className="remote-viewer-connection" role="status">
<span>
{state?.error
@@ -445,7 +663,7 @@ export function RemoteDesktopViewerWindow() {
?.permissionGuide()
.then(() => setNotice(t('remoteDesktop.permissionGuideOpened')))
.catch(() => setNotice(t('remoteDesktop.permissionActionFailed')));
- setSettings(true);
+ setSettings('security');
}}
>
{t('remoteDesktop.openGuideOnComputer')}
@@ -454,6 +672,33 @@ export function RemoteDesktopViewerWindow() {
</div>
)}
{isFullscreen && network && <div className="remote-viewer-network-overlay">{network}</div>}
+ {(notice || state?.safety.notice || state?.safety.privacyActive) && (
+ <div className="remote-viewer-feedback" role="status">
+ {state?.safety.privacyActive && <Shield size={14} aria-hidden="true" />}
+ <span>
+ {notice ??
+ (state?.safety.notice
+ ? t(`remoteDesktop.${state.safety.notice}`)
+ : t('remoteDesktop.privacyActive'))}
+ </span>
+ {state?.safety.notice && (
+ <Button
+ variant="secondary"
+ onClick={() => void controller.current?.refreshSafety(true)}
+ >
+ {t('remoteDesktop.retry')}
+ </Button>
+ )}
+ {notice && (
+ <ViewerTool
+ label={t('remoteDesktop.closePermissionGuide')}
+ onClick={() => setNotice(null)}
+ >
+ <X size={14} />
+ </ViewerTool>
+ )}
+ </div>
+ )}
{state?.clipboardError && (
<div className="remote-viewer-notice" role="status">
{t('remoteDesktop.viewer.clipboardFailed')}
@@ -467,17 +712,150 @@ export function RemoteDesktopViewerWindow() {
if (!open) setCloseGeneration(null);
}}
title={t('remoteDesktop.viewer.confirmDisconnect')}
- description={t('remoteDesktop.viewer.confirmDisconnectHint')}
+ description={
+ t('remoteDesktop.viewer.confirmDisconnectHint') +
+ (state?.preferences.lockOnExit && state.caps?.lockOnExit
+ ? ` ${t('remoteDesktop.lockOnExitHint')}`
+ : '')
+ }
confirmText={t('remoteDesktop.disconnect')}
onConfirm={() => {
if (closeGeneration === null || closeGeneration !== generation.current) return;
- void api.close(closeGeneration).catch(() => {
+ void controller.current?.close().catch(() => {
if (closeGeneration !== generation.current) return;
- setNotice(t('remoteDesktop.viewer.disconnectFailed'));
- setSettings(true);
+ setNotice(
+ t(
+ state?.preferences.lockOnExit && state.caps?.lockOnExit
+ ? 'remoteDesktop.lockOnExitFailed'
+ : 'remoteDesktop.viewer.disconnectFailed',
+ ),
+ );
+ setSettings('security');
});
}}
/>
</div>
);
}
+
+function ZoomModeIcon({ actual = false }: { actual?: boolean }) {
+ return (
+ <svg
+ width="18"
+ height="18"
+ viewBox="0 0 24 24"
+ fill="none"
+ stroke="currentColor"
+ strokeWidth="2"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ aria-hidden="true"
+ >
+ <circle cx="11" cy="11" r="8" />
+ <path d="m21 21-4.3-4.3" />
+ {actual ? (
+ <path d="m9 9 2-2v8m-2 0h4" />
+ ) : (
+ <path
+ fill="currentColor"
+ stroke="none"
+ d="M11 5.5 8.5 8.5h5ZM16.5 11l-3-2.5v5ZM11 16.5l2.5-3h-5ZM5.5 11l3 2.5v-5Z"
+ />
+ )}
+ </svg>
+ );
+}
+
+function ViewerTool({
+ label,
+ children,
+ onClick,
+ disabled,
+ pressed,
+}: {
+ label: string;
+ children: ReactNode;
+ onClick(): void;
+ disabled?: boolean;
+ pressed?: boolean;
+}) {
+ const { t } = useTranslation();
+ return (
+ <Tip text={disabled ? t('remoteDesktop.viewer.controlRequired') : label} side="bottom">
+ <Button
+ variant="secondary"
+ className="remote-viewer-icon"
+ aria-label={label}
+ aria-pressed={pressed}
+ disabled={disabled}
+ onClick={onClick}
+ >
+ {children}
+ </Button>
+ </Tip>
+ );
+}
+
+function ViewerPanel({
+ label,
+ icon,
+ active,
+ open,
+ restoreFocus,
+ onOpenChange,
+ children,
+}: {
+ label: string;
+ icon: ReactNode;
+ active?: boolean;
+ open: boolean;
+ restoreFocus(): boolean;
+ onOpenChange(open: boolean): void;
+ children: ReactNode;
+}) {
+ const { t } = useTranslation();
+ const content = useRef<HTMLDivElement>(null);
+ return (
+ <Popover open={open} onOpenChange={onOpenChange}>
+ <PopoverTrigger asChild>
+ <Tip text={label} side="bottom">
+ <Button
+ variant={open ? 'primary' : 'secondary'}
+ className="remote-viewer-panel-trigger"
+ aria-label={label}
+ aria-pressed={open}
+ >
+ {icon}
+ {active && <span className="remote-viewer-active-dot" aria-hidden="true" />}
+ </Button>
+ </Tip>
+ </PopoverTrigger>
+ <PopoverContent
+ ref={content}
+ align="end"
+ sideOffset={10}
+ collisionPadding={12}
+ aria-label={label}
+ className="remote-viewer-popover rounded-xl shadow-none"
+ onOpenAutoFocus={(event) => {
+ event.preventDefault();
+ content.current?.focus();
+ }}
+ onCloseAutoFocus={(event) => {
+ if (!restoreFocus()) event.preventDefault();
+ }}
+ >
+ <div className="remote-viewer-panel-heading">
+ <strong>{label}</strong>
+ <ViewerTool
+ label={t('remoteDesktop.closePermissionGuide')}
+ onClick={() => onOpenChange(false)}
+ >
+ <X size={16} />
+ </ViewerTool>
+ </div>
+ {children}
+ </PopoverContent>
+ </Popover>
+ );
+}
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.ts
index 27e67ba962..93e784d88f 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
@@ -55,6 +55,7 @@ async function fixture(
clipboard,
close: async () => {},
fullscreen: async () => {},
+ resize: async () => {},
rendererReady: async () => {},
presentationReady: async () => {},
inputFocus: async () => {},
@@ -106,7 +107,7 @@ async function fixture(
snapshot = state;
});
await vi.advanceTimersByTimeAsync(0);
- return { control, heartbeat, clipboard, resolution };
+ return { control, heartbeat, clipboard, resolution, api };
}
const present = () => runtime.post?.({ type: 'streaming', epoch: 'lease' });
const inputEnabled = () =>
@@ -294,3 +295,116 @@ it('does not re-enable input when a failed release is followed by another video
expect(f.control).toHaveBeenLastCalledWith(false);
expect(inputEnabled()).toBe(false);
});
+
+it('coalesces rapid quality changes and waits for the current negotiation to present', async () => {
+ await fixture();
+ present();
+ runtime.receive.mockClear();
+ controller.settings({ bitrate: 2000000 });
+ controller.settings({ bitrate: 20000000 });
+ await vi.advanceTimersByTimeAsync(0);
+ expect(
+ runtime.receive.mock.calls.filter(([message]) => message.type === 'videoSettings'),
+ ).toHaveLength(1);
+ controller.settings({ bitrate: 8000000 });
+ await vi.advanceTimersByTimeAsync(0);
+ expect(
+ runtime.receive.mock.calls.filter(([message]) => message.type === 'videoSettings'),
+ ).toHaveLength(1);
+ present();
+ expect(
+ runtime.receive.mock.calls.filter(([message]) => message.type === 'videoSettings'),
+ ).toHaveLength(2);
+ expect(snapshot.settings.bitrate).toBe(8000000);
+});
+
+it('expires stale bitrate and latency samples', async () => {
+ await fixture();
+ present();
+ runtime.post?.({
+ type: 'network',
+ epoch: 'lease',
+ transport: 'direct',
+ bytesPerSecond: 2048,
+ latencyMs: 12,
+ });
+ expect(snapshot.receiveRate).toBe(2048);
+ await vi.advanceTimersByTimeAsync(6000);
+ expect(snapshot.receiveRate).toBeNull();
+ expect(snapshot.latency).toBeNull();
+});
+
+it('does not let a fallback screenshot interrupt a pending video-settings negotiation', async () => {
+ await fixture();
+ runtime.post?.({ type: 'framePresented', epoch: 'lease' });
+ runtime.receive.mockClear();
+ controller.settings({ bitrate: 2000000 });
+ await vi.advanceTimersByTimeAsync(0);
+ controller.settings({ bitrate: 8000000 });
+ await vi.advanceTimersByTimeAsync(0);
+ runtime.post?.({ type: 'framePresented', epoch: 'lease' });
+ expect(
+ runtime.receive.mock.calls.filter(([message]) => message.type === 'videoSettings'),
+ ).toHaveLength(1);
+ runtime.post?.({ type: 'fallback', epoch: 'lease' });
+ expect(
+ runtime.receive.mock.calls.filter(([message]) => message.type === 'videoSettings'),
+ ).toHaveLength(2);
+});
+
+it.each(['enable', 'biometric'] as const)(
+ 'retries the failed %s action rather than silently switching to automatic unlock',
+ async (action) => {
+ const { api } = await fixture();
+ present();
+ const credential = vi
+ .fn()
+ .mockRejectedValueOnce(new Error('CREDENTIAL_UNAVAILABLE'))
+ .mockResolvedValue({
+ available: true,
+ autoUnlock: true,
+ biometricAvailable: true,
+ biometricVerification: true,
+ });
+ Object.assign(api, { credential });
+ await controller.credential(action, true);
+ controller.retryCredential();
+ await vi.advanceTimersByTimeAsync(0);
+ expect(credential).toHaveBeenNthCalledWith(2, 1, action, true);
+ expect(snapshot.credentialNotice).toBeNull();
+ },
+);
+
+it('requests window content sized for the actual desktop plus toolbar', async () => {
+ const { api } = await fixture();
+ present();
+ const resize = vi.spyOn(api, 'resize');
+ controller.actualSize();
+ expect(runtime.receive).toHaveBeenCalledWith({ type: 'actualSize' });
+ expect(resize).toHaveBeenCalledWith(1, 1280, 780);
+});
+
+it('passes actual logical geometry to rendering and reacquires control after fitting', async () => {
+ const f = await fixture();
+ const original = f.api.request;
+ vi.spyOn(f.api, 'request').mockImplementation(async (generation, request) =>
+ request.op === 'viewerDisplay'
+ ? {
+ lease: 'lease',
+ controlling: false,
+ display: { id: 'viewer', width: 960, height: 710 },
+ viewerDisplayRequest: { width: request.width, height: request.height },
+ }
+ : original(generation, request),
+ );
+ present();
+ await controller.fitDisplay(1920, 1420);
+ expect(runtime.receive).toHaveBeenCalledWith({
+ type: 'videoSettings',
+ width: 960,
+ height: 710,
+ audio: false,
+ });
+ expect(snapshot.controlling).toBe(true);
+ expect(f.control).toHaveBeenLastCalledWith(true);
+});
diff --git a/apps/desktop/src/renderer/features/remote-desktop/__tests__/viewerInput.test.ts b/apps/desktop/src/renderer/features/remote-desktop/__tests__/viewerInput.test.ts
index b3747354f7..0cda75ca89 100644
--- a/apps/desktop/src/renderer/features/remote-desktop/__tests__/viewerInput.test.ts
+++ b/apps/desktop/src/renderer/features/remote-desktop/__tests__/viewerInput.test.ts
@@ -6,6 +6,7 @@ import { DESKTOP_KEY_CODES, REMOTE_DESKTOP_NETWORK } from '@cindy/device-link';
let viewer: ReturnType<typeof mountRemoteDesktopViewer>;
let messages: Record<string, unknown>[];
let stage: HTMLElement;
+let drawImage: ReturnType<typeof vi.fn>;
beforeEach(() => {
vi.useFakeTimers();
vi.stubGlobal(
@@ -17,10 +18,19 @@ beforeEach(() => {
);
vi.stubGlobal('matchMedia', () => ({ matches: true }));
vi.spyOn(HTMLMediaElement.prototype, 'play').mockResolvedValue();
+ drawImage = vi.fn();
+ vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockReturnValue({
+ setTransform: vi.fn(),
+ clearRect: vi.fn(),
+ drawImage,
+ } as unknown as CanvasRenderingContext2D);
document.body.innerHTML =
- '<div id="stage"><img id="image"><video id="video"></video><div id="cursor"><img id="cursor-image"></div></div><textarea id="keyboard-input"></textarea><div id="mouse-buttons"><button id="mouse-left"></button><button id="mouse-right"></button><button id="mouse-wheel"><span id="mouse-wheel-grip"></span></button></div>';
+ '<div id="stage"><div id="bg"><canvas id="bg-canvas"></canvas></div><img id="image"><video id="video"></video><div id="cursor"><img id="cursor-image"></div></div><textarea id="keyboard-input"></textarea><div id="mouse-buttons"><button id="mouse-left"></button><button id="mouse-right"></button><button id="mouse-wheel"><span id="mouse-wheel-grip"></span></button></div>';
stage = document.getElementById('stage')!;
- Object.defineProperties(stage, { clientWidth: { value: 1000 }, clientHeight: { value: 600 } });
+ Object.defineProperties(stage, {
+ clientWidth: { value: 1000, configurable: true },
+ clientHeight: { value: 600, configurable: true },
+ });
stage.setPointerCapture = vi.fn();
vi.spyOn(stage, 'getBoundingClientRect').mockReturnValue({
x: 0,
@@ -69,7 +79,66 @@ function events() {
);
}
-it('moves the separate cursor before input is sent and ignores delayed host positions while moving', () => {
+it('zooms locally within bounds and fit restores scale and position', () => {
+ const image = document.getElementById('image')!;
+ viewer.receive({ type: 'zoom', factor: 1.25 });
+ expect(image.style.width).toBe('1250px');
+ expect(image.style.left).toBe('-125px');
+ viewer.receive({ type: 'zoom', factor: 0.8 });
+ expect(image.style.width).toBe('1000px');
+ viewer.receive({ type: 'zoom', factor: 100 });
+ expect(image.style.width).toBe('5000px');
+ viewer.receive({ type: 'zoom', factor: NaN });
+ expect(image.style.width).toBe('5000px');
+ viewer.receive({ type: 'fit' });
+ expect(image.style.width).toBe('1000px');
+ expect(image.style.left).toBe('0px');
+ viewer.receive({ type: 'zoom', factor: 0.1 });
+ expect(image.style.width).toBe('100px');
+ viewer.receive({ type: 'zoom', factor: 0.1 });
+ expect(image.style.width).toBe('100px');
+ expect(events()).toEqual([]);
+});
+
+it('maps clicks through the zoomed picture and allows local zoom in view-only mode', () => {
+ viewer.receive({ type: 'zoom', factor: 2 });
+ pointer('pointerdown', 750, 352);
+ pointer('pointerup', 750, 352);
+ vi.advanceTimersByTime(40);
+ expect(events()).toContainEqual({ kind: 'button', button: 0, down: true, x: 0.625, y: 0.5 });
+ viewer.receive({ type: 'control', enabled: false });
+ messages = [];
+ viewer.receive({ type: 'zoom', factor: 1.25 });
+ expect(document.getElementById('image')!.style.width).toBe('2500px');
+ expect(events()).toEqual([]);
+});
+
+it('pans a zoomed view with middle drag, preserves middle click and resets with fit', () => {
+ const image = document.getElementById('image')!;
+ viewer.receive({ type: 'zoom', factor: 2 });
+ pointer('pointerdown', 500, 352, 1);
+ pointer('pointermove', 600, 352, 1);
+ pointer('pointerup', 600, 352, 1);
+ vi.advanceTimersByTime(250);
+ expect(parseFloat(image.style.left)).toBeGreaterThan(-500);
+ expect(events()).toEqual([]);
+ viewer.receive({ type: 'fit' });
+ expect(image.style.left).toBe('0px');
+ viewer.receive({ type: 'zoom', factor: 2 });
+ pointer('pointerdown', 500, 352, 1);
+ pointer('pointerup', 500, 352, 1);
+ vi.advanceTimersByTime(40);
+ expect(
+ events()
+ .filter((e) => e.kind === 'button')
+ .map((e) => [e.button, e.down]),
+ ).toEqual([
+ [1, true],
+ [1, false],
+ ]);
+});
+
+it('uses the local pointer while retaining immediate remote input coordinates', () => {
const cursor = {
visible: true,
x: 0.1,
@@ -83,7 +152,8 @@ it('moves the separate cursor before input is sent and ignores delayed host posi
viewer.receive({ type: 'frame', jpeg: 'frame', cursor });
pointer('pointermove', 800, 452);
const overlay = document.getElementById('cursor')!;
- expect(overlay.style.display).toBe('block');
+ expect(overlay.style.display).toBe('none');
+ expect(stage.style.cursor).toBe('default');
expect(overlay.style.left).toBe('798px');
expect(overlay.style.top).toBe('397px');
expect(events()).toEqual([]);
@@ -95,25 +165,58 @@ it('moves the separate cursor before input is sent and ignores delayed host posi
viewer.receive({ type: 'frame', jpeg: 'frame', cursor });
expect(overlay.style.left).toBe('98px');
});
-it('scopes cursor hiding to the remote picture regardless of window focus or cursor metadata', () => {
- expect(stage.style.cursor).toBe('none');
+it('uses the local system cursor to the remote picture regardless of window focus or cursor metadata', () => {
+ expect(stage.style.cursor).toBe('default');
viewer.receive({ type: 'frame', jpeg: 'frame' });
- expect(stage.style.cursor).toBe('none');
+ expect(stage.style.cursor).toBe('default');
pointer('pointerleave');
expect(getComputedStyle(document.body).cursor).not.toBe('none');
expect(getComputedStyle(document.getElementById('mouse-left')!).cursor).not.toBe('none');
pointer('pointerenter');
- expect(stage.style.cursor).toBe('none');
+ expect(stage.style.cursor).toBe('default');
window.dispatchEvent(new Event('blur'));
- expect(stage.style.cursor).toBe('none');
+ expect(stage.style.cursor).toBe('default');
window.dispatchEvent(new Event('focus'));
- expect(stage.style.cursor).toBe('none');
+ expect(stage.style.cursor).toBe('default');
viewer.receive({ type: 'control', enabled: false });
expect(stage.style.cursor).toBe('default');
viewer.receive({ type: 'control', enabled: true });
viewer.receive({ type: 'stop' });
expect(stage.style.cursor).toBe('default');
});
+it.each([
+ 'text',
+ 'pointer',
+ 'ew-resize',
+ 'grab',
+ undefined,
+ 'future-shape',
+ 'url(https://invalid/cursor)',
+])('uses only native cursor keywords for %s, regardless of remote size and zoom', (shape) => {
+ viewer.receive({
+ type: 'frame',
+ jpeg: 'frame',
+ cursor: {
+ visible: true,
+ x: 0.5,
+ y: 0.5,
+ width: 256,
+ height: 256,
+ hotX: 32,
+ hotY: 32,
+ png: 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQIHWP4z8DwHwAFgAI/ScLbtAAAAABJRU5ErkJggg==',
+ shape,
+ },
+ });
+ viewer.receive({ type: 'zoom', factor: 5 });
+ expect(document.getElementById('cursor')!.style.display).toBe('none');
+ expect(stage.style.cursor).toBe(
+ ['text', 'pointer', 'ew-resize', 'grab'].includes(shape ?? '') ? shape : 'default',
+ );
+ viewer.receive({ type: 'control', enabled: false });
+ expect(stage.style.cursor).toBe('default');
+});
+
it.each(['control', 'meta'])(
'bridges %s clipboard shortcuts once without forwarding or inserting them',
(modifier) => {
@@ -285,3 +388,149 @@ it('releases held buttons on window blur and disposal removes all timers', () =>
vi.advanceTimersByTime(5000);
expect(messages).toEqual([]);
});
+
+it.each([500, 2000, 10000])('shows a %spx desktop at 1:1 independently of fit bounds', (width) => {
+ const image = document.getElementById('image')!;
+ viewer.receive({ type: 'init', epoch: 'lease', width, height: width * 0.6 });
+ viewer.receive({ type: 'actualSize' });
+ expect(image.style.width).toBe(`${width}px`);
+ expect(parseFloat(image.style.left)).toBe((1000 - width) / 2);
+ expect(messages).toContainEqual(expect.objectContaining({ type: 'scaleMode', mode: 'actual' }));
+ viewer.receive({ type: 'zoom', factor: 1.25 });
+ expect(parseFloat(image.style.width)).toBe(width * 1.25);
+ viewer.receive({ type: 'actualSize' });
+ Object.defineProperty(stage, 'clientWidth', { value: 800 });
+ viewer.receive({ type: 'control', enabled: true });
+ expect(image.style.width).toBe(`${width}px`);
+ expect(parseFloat(image.style.left)).toBe((800 - width) / 2);
+ viewer.receive({ type: 'fit' });
+ expect(image.style.width).toBe('800px');
+});
+
+it('maps input and pans an actual-size desktop larger than the window', () => {
+ viewer.receive({ type: 'init', epoch: 'lease', width: 2000, height: 1200 });
+ viewer.receive({ type: 'control', enabled: true });
+ viewer.receive({ type: 'actualSize' });
+ pointer('pointerdown', 750, 352);
+ pointer('pointerup', 750, 352);
+ vi.advanceTimersByTime(40);
+ expect(events()).toContainEqual({ kind: 'button', button: 0, down: true, x: 0.625, y: 0.5 });
+ pointer('pointerdown', 500, 352, 1);
+ pointer('pointermove', 600, 352, 1);
+ pointer('pointerup', 600, 352, 1);
+ vi.advanceTimersByTime(300);
+ expect(parseFloat(document.getElementById('image')!.style.left)).toBeGreaterThan(-500);
+});
+
+it('fills all margins of a shrunken desktop with the shared fitted backdrop', () => {
+ const image = document.getElementById('image')!;
+ const bg = document.getElementById('bg')!;
+ Object.defineProperties(image, { naturalWidth: { value: 1000 }, naturalHeight: { value: 600 } });
+ viewer.receive({ type: 'zoom', factor: 0.8 });
+ vi.advanceTimersByTime(32);
+ expect(image.style.width).toBe('800px');
+ expect(image.style.left).toBe('100px');
+ expect(image.style.top).toBe('60px');
+ expect(bg.style.display).toBe('block');
+ expect(drawImage).toHaveBeenCalledTimes(3);
+ expect(drawImage.mock.calls[0]).toEqual([image, 0, 0, 50, 600, 0, 0, 50, 600]);
+ expect(drawImage.mock.calls[1].slice(5)).toEqual([50, 0, 900, 600]);
+ expect(drawImage.mock.calls[2].slice(5)).toEqual([950, 0, 50, 600]);
+ pointer('pointerdown', 700, 352);
+ pointer('pointerup', 700, 352);
+ vi.advanceTimersByTime(40);
+ expect(events()).toContainEqual({ kind: 'button', button: 0, down: true, x: 0.75, y: 0.5 });
+ viewer.receive({ type: 'fit' });
+ expect(bg.style.display).toBe('none');
+});
+
+it('keeps the letterbox backdrop fitted while the foreground shrinks', () => {
+ const image = document.getElementById('image')!;
+ Object.defineProperties(image, { naturalWidth: { value: 600 }, naturalHeight: { value: 600 } });
+ viewer.receive({ type: 'init', epoch: 'lease', width: 600, height: 600 });
+ vi.advanceTimersByTime(32);
+ const fittedRects = drawImage.mock.calls.slice(-3).map((call) => call.slice(5));
+ expect(fittedRects).toEqual([
+ [0, 0, 230, 600],
+ [230, 0, 540, 600],
+ [770, 0, 230, 600],
+ ]);
+ drawImage.mockClear();
+ viewer.receive({ type: 'zoom', factor: 0.8 });
+ vi.advanceTimersByTime(32);
+ expect(image.style.width).toBe('480px');
+ expect(drawImage.mock.calls.map((call) => call.slice(5))).toEqual(fittedRects);
+});
+
+it.each([
+ [995, 352, 'left', -500, -1],
+ [5, 352, 'left', -500, 1],
+ [500, 647, 'top', -300, -1],
+ [500, 57, 'top', -300, 1],
+] as const)(
+ 'pans continuously toward pointer at %s,%s and stops on leaving',
+ (x, y, axis, initial, direction) => {
+ viewer.receive({ type: 'zoom', factor: 2 });
+ const image = document.getElementById('image')!;
+ pointer('pointermove', x, y);
+ vi.advanceTimersByTime(200);
+ expect((parseFloat(image.style[axis]) - initial) * direction).toBeGreaterThan(0);
+ const moved = image.style[axis];
+ pointer('pointerleave', x, y);
+ vi.advanceTimersByTime(200);
+ expect(image.style[axis]).toBe(moved);
+ },
+);
+
+it('clamps edge panning to desktop bounds and keeps the remote pointer mapped', () => {
+ viewer.receive({ type: 'zoom', factor: 2 });
+ pointer('pointermove', 995, 647);
+ vi.advanceTimersByTime(3000);
+ const image = document.getElementById('image')!;
+ expect(image.style.left).toBe('-1000px');
+ expect(image.style.top).toBe('-600px');
+ expect(
+ events()
+ .filter((e) => e.kind === 'move')
+ .at(-1),
+ ).toEqual({ kind: 'move', x: 0.9975, y: 1195 / 1200 });
+ pointer('pointermove', 500, 352);
+ vi.advanceTimersByTime(100);
+ expect(image.style.left).toBe('-1000px');
+});
+
+it('allows view-only edge panning but stops on focus loss and fit', () => {
+ viewer.receive({ type: 'control', enabled: false });
+ viewer.receive({ type: 'zoom', factor: 2 });
+ messages = [];
+ pointer('pointermove', 995, 352);
+ vi.advanceTimersByTime(200);
+ const image = document.getElementById('image')!;
+ expect(parseFloat(image.style.left)).toBeLessThan(-500);
+ expect(events()).toEqual([]);
+ window.dispatchEvent(new Event('blur'));
+ const left = image.style.left;
+ vi.advanceTimersByTime(400);
+ expect(image.style.left).toBe(left);
+ pointer('pointermove', 995, 352);
+ viewer.receive({ type: 'fit' });
+ vi.advanceTimersByTime(200);
+ expect(image.style.left).toBe('0px');
+ pointer('pointermove', 995, 352);
+ vi.advanceTimersByTime(200);
+ expect(image.style.left).toBe('0px');
+});
+
+it('continues edge panning when the first browser frame predates the pointer event', () => {
+ viewer.receive({ type: 'zoom', factor: 2 });
+ vi.advanceTimersByTime(32);
+ let firstFrame!: FrameRequestCallback;
+ vi.spyOn(window, 'requestAnimationFrame').mockImplementationOnce((callback) => {
+ firstFrame = callback;
+ return 123456;
+ });
+ pointer('pointermove', 995, 352);
+ firstFrame(performance.now() - 1);
+ vi.advanceTimersByTime(200);
+ expect(parseFloat(document.getElementById('image')!.style.left)).toBeLessThan(-500);
+});
diff --git a/apps/desktop/src/renderer/features/remote-desktop/__tests__/viewerLifecycle.test.ts b/apps/desktop/src/renderer/features/remote-desktop/__tests__/viewerLifecycle.test.ts
index 8213e6d5c2..094aaaae2b 100644
--- a/apps/desktop/src/renderer/features/remote-desktop/__tests__/viewerLifecycle.test.ts
+++ b/apps/desktop/src/renderer/features/remote-desktop/__tests__/viewerLifecycle.test.ts
@@ -104,6 +104,7 @@ async function fixture(
clipboard: async () => {},
close: async () => {},
fullscreen: async () => {},
+ resize: async () => {},
rendererReady: async () => {},
presentationReady: async () => {},
inputFocus: async () => {},
@@ -242,6 +243,7 @@ it('keeps a healthy viewer on the same host connected when another viewer times
clipboard: async () => {},
close: async () => {},
fullscreen: async () => {},
+ resize: async () => {},
rendererReady: async () => {},
presentationReady: async () => {},
inputFocus: async () => {},
diff --git a/apps/desktop/src/renderer/features/remote-desktop/__tests__/viewerTransport.test.ts b/apps/desktop/src/renderer/features/remote-desktop/__tests__/viewerTransport.test.ts
index 2cfd6762de..9e6e5ed394 100644
--- a/apps/desktop/src/renderer/features/remote-desktop/__tests__/viewerTransport.test.ts
+++ b/apps/desktop/src/renderer/features/remote-desktop/__tests__/viewerTransport.test.ts
@@ -29,6 +29,7 @@ beforeEach(async () => {
clipboard: async () => {},
close: async () => {},
fullscreen: async () => {},
+ resize: async () => {},
rendererReady: async () => {},
presentationReady: async () => {},
inputFocus: async () => {},
@@ -49,13 +50,9 @@ beforeEach(async () => {
return {};
},
} satisfies RemoteDesktopViewerApi;
- controller = new DesktopViewerController(
- api,
- {} as HTMLElement,
- (state) => {
- snapshot = state;
- },
- );
+ controller = new DesktopViewerController(api, {} as HTMLElement, (state) => {
+ snapshot = state;
+ });
await vi.advanceTimersByTimeAsync(0);
});
afterEach(() => {
diff --git a/apps/desktop/src/renderer/features/remote-desktop/__tests__/viewerWindow.test.tsx b/apps/desktop/src/renderer/features/remote-desktop/__tests__/viewerWindow.test.tsx
index 12478e1b86..0ce537a842 100644
--- a/apps/desktop/src/renderer/features/remote-desktop/__tests__/viewerWindow.test.tsx
+++ b/apps/desktop/src/renderer/features/remote-desktop/__tests__/viewerWindow.test.tsx
@@ -1,5 +1,5 @@
// @vitest-environment jsdom
-import { act, cleanup, fireEvent, render, screen, within } from '@testing-library/react';
+import { act, cleanup, fireEvent, render, screen, within, waitFor } from '@testing-library/react';
import { afterEach, expect, it, vi } from 'vitest';
import i18n from '@/i18n';
import { RemoteDesktopViewerWindow } from '../RemoteDesktopViewerWindow';
@@ -10,17 +10,28 @@ const lifecycle = vi.hoisted(() => ({
disposed: vi.fn(),
releaseInput: vi.fn(),
setControl: vi.fn(),
+ zoom: vi.fn(),
+ fit: vi.fn(),
+ actualSize: vi.fn(),
update: null as ((state: ViewerSnapshot) => void) | null,
}));
vi.mock('../viewerController', () => ({
DesktopViewerController: class {
- constructor(_api: unknown, _root: HTMLElement, update: typeof lifecycle.update) {
+ constructor(
+ private _api: { close(generation: number): Promise<void> },
+ _root: HTMLElement,
+ update: typeof lifecycle.update,
+ ) {
lifecycle.created();
lifecycle.update = update;
}
dispose = lifecycle.disposed;
releaseInput = lifecycle.releaseInput;
setControl = lifecycle.setControl;
+ zoom = lifecycle.zoom;
+ fit = lifecycle.fit;
+ actualSize = lifecycle.actualSize;
+ close = () => this._api.close(1);
},
}));
vi.mock('@/hooks/useMacFullscreen', () => ({
@@ -60,7 +71,60 @@ it('confirms toolbar and native exits, keeps cancellation connected, and discard
});
render(<RemoteDesktopViewerWindow />);
await act(async () => {});
- fireEvent.click(screen.getByRole('button', { name: i18n.t('remoteDesktop.disconnect') }));
+ // The window can be closed before the first controller snapshot arrives.
+ act(() => closeRequested(1));
+ expect(close).toHaveBeenCalledExactlyOnceWith(1);
+ expect(screen.queryByRole('alertdialog')).toBeNull();
+ close.mockClear();
+ const connected: ViewerSnapshot = {
+ target: null,
+ status: 'live',
+ error: null,
+ controlling: false,
+ controlPending: false,
+ caps: null,
+ displayId: '',
+ transport: 'direct',
+ latency: null,
+ settings: { fps: 30, bitrate: 0, audio: true },
+ ready: true,
+ preferences: {
+ audio: true,
+ privacyScreen: false,
+ hostMute: false,
+ clipboardSync: false,
+ lockOnExit: false,
+ },
+ safety: { privacyActive: false, notice: null, clipboardProgress: null },
+ receiveRate: null,
+ closing: false,
+ credential: null,
+ credentialBusy: false,
+ credentialNotice: null,
+ };
+ for (const status of ['connecting', 'reconnecting']) {
+ act(() => lifecycle.update?.({ ...connected, ready: false, status }));
+ expect(screen.queryByRole('button', { name: i18n.t('remoteDesktop.disconnect') })).toBeNull();
+ act(() => closeRequested(1));
+ expect(close).toHaveBeenCalledOnce();
+ expect(screen.queryByRole('alertdialog')).toBeNull();
+ close.mockClear();
+ }
+ act(() => lifecycle.update?.(connected));
+ expect(screen.queryByRole('button', { name: i18n.t('remoteDesktop.takeControl') })).toBeNull();
+ expect(screen.queryByRole('button', { name: i18n.t('remoteDesktop.releaseControl') })).toBeNull();
+ fireEvent.click(screen.getByRole('button', { name: '放大' }));
+ fireEvent.click(screen.getByRole('button', { name: '缩小' }));
+ fireEvent.click(screen.getByRole('button', { name: '适应窗口' }));
+ expect(lifecycle.zoom.mock.calls).toEqual([['in'], ['out']]);
+ expect(lifecycle.fit).toHaveBeenCalledOnce();
+ fireEvent.click(screen.getByRole('button', { name: '实际大小(1:1)' }));
+ expect(lifecycle.actualSize).toHaveBeenCalledOnce();
+ expect(
+ screen.queryByRole('button', { name: i18n.t('remoteDesktop.viewer.fullscreen') }),
+ ).toBeNull();
+ expect(screen.queryByRole('button', { name: i18n.t('remoteDesktop.disconnect') })).toBeNull();
+ act(() => closeRequested(1));
expect(close).not.toHaveBeenCalled();
expect(lifecycle.releaseInput).toHaveBeenCalled();
const dialog = within(screen.getByRole('alertdialog'));
@@ -105,6 +169,19 @@ it('explains view-only actions and enables the same actions when control is conf
});
render(<RemoteDesktopViewerWindow />);
const state: ViewerSnapshot = {
+ preferences: {
+ audio: true,
+ privacyScreen: false,
+ hostMute: false,
+ clipboardSync: false,
+ lockOnExit: false,
+ },
+ safety: { privacyActive: false, notice: null, clipboardProgress: null },
+ receiveRate: null,
+ closing: false,
+ credential: null,
+ credentialBusy: false,
+ credentialNotice: null,
target: { deviceId: 'host', name: 'Windows' },
ready: true,
controlling: false,
@@ -125,14 +202,21 @@ it('explains view-only actions and enables the same actions when control is conf
},
};
await act(async () => lifecycle.update?.(state));
- fireEvent.click(screen.getByRole('button', { name: '操作' }));
- const panel = within(screen.getByRole('complementary', { name: '操作' }));
- expect(panel.getByText(i18n.t('remoteDesktop.viewer.controlRequired'))).toBeDefined();
- expect(panel.queryByText('文字剪贴板')).toBeNull();
+ const openPanel = (label: string) => {
+ fireEvent.click(screen.getByRole('button', { name: label }));
+ return within(screen.getByRole('dialog', { name: label }));
+ };
+ let panel = openPanel('剪贴板');
expect(
- panel.getByText(i18n.t('remoteDesktop.viewer.clipboardShortcutHint', { modifier: '⌘' })),
- ).toBeDefined();
- const desktop = panel.getByRole('button', {
+ (
+ panel.getByRole('switch', {
+ name: i18n.t('remoteDesktop.clipboardSync'),
+ }) as HTMLButtonElement
+ ).disabled,
+ ).toBe(true);
+ expect(panel.getByText(i18n.t('remoteDesktop.settingUnsupported'))).toBeDefined();
+ expect(panel.getByText(i18n.t('remoteDesktop.viewer.controlRequired'))).toBeDefined();
+ const desktop = screen.getByRole('button', {
name: i18n.t('remoteDesktop.showDesktop'),
}) as HTMLButtonElement;
expect(desktop.disabled).toBe(true);
@@ -140,10 +224,61 @@ it('explains view-only actions and enables the same actions when control is conf
expect(lifecycle.setControl).toHaveBeenCalledWith(true);
await act(async () => lifecycle.update?.({ ...state, controlPending: true }));
expect(panel.getByText(i18n.t('remoteDesktop.viewer.controlPending'))).toBeDefined();
- expect(desktop.disabled).toBe(true);
- await act(async () => lifecycle.update?.({ ...state, controlling: true }));
+ const supported = {
+ ...state,
+ controlling: true,
+ caps: {
+ ...state.caps!,
+ privacyScreen: true,
+ hostMute: true,
+ clipboardSync: true,
+ lockOnExit: true,
+ },
+ };
+ await act(async () => lifecycle.update?.(supported));
expect(desktop.disabled).toBe(false);
- expect(panel.queryByText(i18n.t('remoteDesktop.viewer.controlRequired'))).toBeNull();
+ expect(
+ (
+ panel.getByRole('switch', {
+ name: i18n.t('remoteDesktop.clipboardSync'),
+ }) as HTMLButtonElement
+ ).disabled,
+ ).toBe(false);
+ panel = openPanel('安全');
+ expect(screen.queryByRole('dialog', { name: '剪贴板' })).toBeNull();
+ expect(panel.queryByRole('switch', { name: i18n.t('remoteDesktop.clipboardSync') })).toBeNull();
+ expect(
+ (
+ panel.getByRole('switch', {
+ name: i18n.t('remoteDesktop.privacyScreen'),
+ }) as HTMLButtonElement
+ ).disabled,
+ ).toBe(false);
+ expect(lifecycle.releaseInput).toHaveBeenCalled();
+ await act(async () => lifecycle.update?.({ ...supported, ready: false }));
+ panel = within(screen.getByRole('dialog', { name: '安全' }));
+ expect(
+ (
+ panel.getByRole('switch', {
+ name: i18n.t('remoteDesktop.privacyScreen'),
+ }) as HTMLButtonElement
+ ).disabled,
+ ).toBe(true);
+ expect(panel.getAllByText(i18n.t('remoteDesktop.loadingSettings'))).toHaveLength(2);
+ fireEvent.keyDown(screen.getByRole('dialog', { name: '安全' }), { key: 'Escape' });
+ expect(screen.queryByRole('dialog')).toBeNull();
+ await waitFor(() =>
+ expect(document.activeElement).toBe(screen.getByRole('button', { name: '安全' })),
+ );
+ openPanel('安全');
+ const remotePointer = vi.fn();
+ const stage = document.getElementById('stage')!;
+ stage.addEventListener('pointerdown', remotePointer);
+ fireEvent.pointerDown(stage);
+ expect(screen.queryByRole('dialog')).toBeNull();
+ expect(remotePointer).not.toHaveBeenCalled();
+ stage.removeEventListener('pointerdown', remotePointer);
+ expect(lifecycle.disposed).not.toHaveBeenCalled();
});
it.each([
@@ -167,6 +302,19 @@ it.each([
const view = render(<RemoteDesktopViewerWindow />);
await act(async () =>
lifecycle.update?.({
+ preferences: {
+ audio: true,
+ privacyScreen: false,
+ hostMute: false,
+ clipboardSync: false,
+ lockOnExit: false,
+ },
+ safety: { privacyActive: false, notice: null, clipboardProgress: null },
+ receiveRate: null,
+ closing: false,
+ credential: null,
+ credentialBusy: false,
+ credentialNotice: null,
target: null,
ready: true,
controlling: true,
@@ -209,7 +357,10 @@ it('updates translated controls without ending or recreating the viewer connecti
await act(async () => {
for (const listener of [...listeners]) listener('zh-CN');
});
- expect(screen.getByRole('button', { name: '操作' })).toBeDefined();
+ const display = screen.getByRole('button', { name: '影音' });
+ expect(display.textContent).toBe('');
+ expect(screen.getByRole('button', { name: '剪贴板' }).textContent).toBe('');
+ expect(screen.getByRole('button', { name: '安全' }).textContent).toBe('');
expect(lifecycle.disposed).not.toHaveBeenCalled();
expect(lifecycle.created).toHaveBeenCalledOnce();
expect(rendererReady).toHaveBeenCalledOnce();
diff --git a/apps/desktop/src/renderer/features/remote-desktop/useRemoteDesktopAvailability.ts b/apps/desktop/src/renderer/features/remote-desktop/useRemoteDesktopAvailability.ts
index 260972be14..b1761ba1c1 100644
--- a/apps/desktop/src/renderer/features/remote-desktop/useRemoteDesktopAvailability.ts
+++ b/apps/desktop/src/renderer/features/remote-desktop/useRemoteDesktopAvailability.ts
@@ -15,7 +15,8 @@ export function remoteDesktopUnavailableReason(value: unknown): string | null {
if (caps.version !== 1) return 'remoteDesktop.upgrade';
if (typeof caps.enabled !== 'boolean') return 'remoteDesktop.shortcut.checkFailed';
if (!caps.enabled) return 'remoteDesktop.disabled';
- if (caps.permissions?.screenRecording === 'missing') return 'remoteDesktop.permissionHint';
+ if (caps.permissions?.screenRecording === 'missing')
+ return 'remoteDesktop.shortcut.screenRecordingRequired';
if (
!Array.isArray(caps.displays) ||
!caps.displays.some(
@@ -49,7 +50,7 @@ export function remoteDesktopAvailabilityError(error: unknown): string {
return 'remoteDesktop.shortcut.checkFailed';
}
-export function useRemoteDesktopAvailability(deviceId: string, active: boolean) {
+export function useRemoteDesktopAvailability(deviceId: string) {
const devices = useDeviceLinkDeviceList();
const { status: directoryStatus } = useDeviceLinkDeviceListRequestState();
const revoked = useSyncExternalStore(
@@ -73,36 +74,41 @@ export function useRemoteDesktopAvailability(deviceId: string, active: boolean)
: !device.remoteControlEnabled
? 'remoteDesktop.remoteDisabled'
: null;
+ // Presence/permission changes invalidate a check; hover and directory object
+ // refreshes do not. Keep this scoped to the mounted device row, not disk.
+ const present = !!device;
+ const version = device?.appVersion;
+ const platform = device?.platform;
const scope = useMemo(
- () => ({ deviceId, device, active, denied, directoryStatus }),
- [deviceId, device, active, denied, directoryStatus],
+ () => ({ deviceId, present, blockedReason, version, platform }),
+ [deviceId, present, blockedReason, version, platform],
);
const [checked, setChecked] = useState<{ scope: typeof scope; reason: string | null } | null>(
null,
);
+ // Probe when an eligible device first appears, independently of pointer/focus.
useEffect(() => {
- if (!scope.active || !scope.device || blockedReason) return;
+ if (!scope.present || scope.blockedReason) return;
let current = true;
- const timer = setTimeout(() => {
- void Promise.resolve()
- .then(() =>
- window.electronAPI.deviceLink.invoke(scope.deviceId, REMOTE_DESKTOP_CHANNEL, [
- { op: 'capabilities' },
- ]),
- )
- .then((value) => {
- if (current) setChecked({ scope, reason: remoteDesktopUnavailableReason(value) });
- })
- .catch((error: unknown) => {
- if (current) setChecked({ scope, reason: remoteDesktopAvailabilityError(error) });
- });
- }, 150);
+ void Promise.resolve()
+ .then(() =>
+ current
+ ? window.electronAPI.deviceLink.invoke(scope.deviceId, REMOTE_DESKTOP_CHANNEL, [
+ { op: 'capabilities' },
+ ])
+ : undefined,
+ )
+ .then((value) => {
+ if (current) setChecked({ scope, reason: remoteDesktopUnavailableReason(value) });
+ })
+ .catch((error: unknown) => {
+ if (current) setChecked({ scope, reason: remoteDesktopAvailabilityError(error) });
+ });
return () => {
current = false;
- clearTimeout(timer);
};
- }, [scope, blockedReason]);
+ }, [scope]);
const reason = blockedReason ?? (checked?.scope === scope ? checked.reason : null);
return {
diff --git a/apps/desktop/src/renderer/features/remote-desktop/viewerController.ts b/apps/desktop/src/renderer/features/remote-desktop/viewerController.ts
index e4bf07a79f..fcf9886597 100644
--- a/apps/desktop/src/renderer/features/remote-desktop/viewerController.ts
+++ b/apps/desktop/src/renderer/features/remote-desktop/viewerController.ts
@@ -20,6 +20,12 @@ import type {
RemoteDesktopViewerApi,
RemoteViewerState,
} from '../../../shared/remoteDesktopViewer';
+import {
+ DEFAULT_VIEWER_PREFERENCES,
+ type RemoteViewerPreferences,
+ type RemoteViewerSafety,
+ type RemoteViewerCredentialState,
+} from '../../../shared/remoteDesktopViewer';
export interface ViewerSnapshot {
target: RemoteViewerState['target'];
@@ -34,6 +40,14 @@ export interface ViewerSnapshot {
latency: number | null;
settings: RemoteDesktopVideoSettings;
ready: boolean;
+ scaleMode?: 'fit' | 'actual' | 'custom';
+ preferences: RemoteViewerPreferences;
+ safety: RemoteViewerSafety;
+ receiveRate: number | null;
+ closing: boolean;
+ credential: RemoteViewerCredentialState | null;
+ credentialBusy: boolean;
+ credentialNotice: string | null;
}
function connectionBudget(caps: RemoteDesktopCapabilities | null): number {
@@ -60,6 +74,13 @@ export class DesktopViewerController {
latency: null,
settings: { fps: 30, bitrate: 0, audio: true },
ready: false,
+ preferences: { ...DEFAULT_VIEWER_PREFERENCES },
+ safety: { privacyActive: false, notice: null, clipboardProgress: null },
+ receiveRate: null,
+ closing: false,
+ credential: null,
+ credentialBusy: false,
+ credentialNotice: null,
};
private scope: RemoteViewerState = { target: null, active: false, generation: -1 };
private disposed = false;
@@ -76,6 +97,17 @@ export class DesktopViewerController {
private clipboardQueue: Promise<void> = Promise.resolve();
private clipboardQueued = 0;
private clipboardRevision = 0;
+ private pendingSettings = false;
+ private offers = new Set<import('@cindy/device-link').RemoteDesktopLease>();
+ private mediaChanging = false;
+ private settingsTimer: ReturnType<typeof setTimeout> | null = null;
+ private statsAt = 0;
+ private frameAt = 0;
+ private unlockAttempted = false;
+ private credentialRetry: {
+ action: 'settings' | 'enable' | 'disable' | 'unlock' | 'biometric';
+ enabled?: boolean;
+ } | null = null;
private session: RemoteDesktopViewerSession;
private media: RemoteDesktopViewerMedia;
private runtime: ReturnType<typeof mountRemoteDesktopViewer>;
@@ -83,7 +115,7 @@ export class DesktopViewerController {
private unsubscribers: (() => void)[];
constructor(
private readonly api: RemoteDesktopViewerApi,
- root: HTMLElement,
+ private readonly root: HTMLElement,
private readonly changed: (state: ViewerSnapshot) => void,
) {
this.session = new RemoteDesktopViewerSession(this.request);
@@ -102,6 +134,13 @@ export class DesktopViewerController {
},
}
: null,
+ onOfferStart: (lease) => {
+ this.offers.add(lease);
+ },
+ onOfferSettled: (lease) => {
+ this.offers.delete(lease);
+ this.applySettings();
+ },
});
this.runtime = mountRemoteDesktopViewer(root, (message) => this.message(message), {
desktop: true,
@@ -117,6 +156,11 @@ export class DesktopViewerController {
this.timers = [
setInterval(() => void this.heartbeat(), 3000),
setInterval(() => void this.frame(), 350),
+ setInterval(() => {
+ void this.refreshSafety();
+ if (this.statsAt && Date.now() - this.statsAt > 5000)
+ this.publish({ receiveRate: null, latency: null });
+ }, 1500),
];
}
private publish(patch: Partial<ViewerSnapshot>): void {
@@ -133,6 +177,7 @@ export class DesktopViewerController {
this.scope.active &&
!this.disposed &&
!this.state.error &&
+ !this.state.closing &&
(!this.state.ready || this.state.status === 'reconnecting');
if (!waiting) {
if (this.connectionTimer !== null) clearTimeout(this.connectionTimer);
@@ -177,11 +222,39 @@ export class DesktopViewerController {
status: 'connecting',
caps: null,
ready: false,
+ closing: false,
+ credential: null,
+ credentialBusy: false,
+ credentialNotice: null,
+ preferences: { ...DEFAULT_VIEWER_PREFERENCES },
});
- if (scope.active) void this.connect();
+ this.unlockAttempted = false;
+ this.credentialRetry = null;
+ if (scope.active)
+ void (async () => {
+ try {
+ const preferences = await this.api.preferences?.(scope.generation);
+ if (this.scope !== scope || this.disposed) return;
+ if (preferences)
+ this.publish({
+ preferences,
+ settings: { ...this.state.settings, audio: preferences.audio },
+ });
+ } catch {
+ /* Defaults preserve ordinary viewing if preference storage is unavailable. */
+ }
+ if (this.scope === scope && !this.disposed) void this.connect();
+ })();
}
private cancel(preserveFrame = false): void {
this.epoch++;
+ if (this.settingsTimer) clearTimeout(this.settingsTimer);
+ this.settingsTimer = null;
+ this.pendingSettings = false;
+ this.mediaChanging = false;
+ this.offers.clear();
+ this.statsAt = 0;
+ this.frameAt = 0;
this.clipboardQueue = Promise.resolve();
this.clipboardQueued = 0;
this.opening = false;
@@ -197,6 +270,8 @@ export class DesktopViewerController {
transport: '',
latency: null,
clipboardError: false,
+ receiveRate: null,
+ safety: { privacyActive: false, notice: null, clipboardProgress: null },
});
}
private async connect(takeover = false): Promise<void> {
@@ -230,7 +305,7 @@ export class DesktopViewerController {
fillHeight: false,
trickleIce: caps.trickleIce === true,
audio: caps.systemAudio && this.state.settings.audio,
- clipboardShortcuts: caps.clipboardText === true,
+ clipboardShortcuts: caps.clipboardText === true || caps.clipboardContent === true,
clipboardModifier:
typeof window !== 'undefined' && window.electronAPI?.platform === 'darwin'
? 'meta'
@@ -254,11 +329,12 @@ export class DesktopViewerController {
}
retry(): void {
this.cancel(true);
+ this.unlockAttempted = false;
this.resuming = false;
void this.connect(this.state.error === 'connectionBusy');
}
async setControl(enabled: boolean): Promise<void> {
- if (this.state.controlPending || !this.session.lease) return;
+ if (this.state.closing || this.state.controlPending || !this.session.lease) return;
this.wantsControl = enabled;
this.runtime.receive({ type: 'releaseInput' });
const lease = this.session.lease;
@@ -282,6 +358,7 @@ export class DesktopViewerController {
if (lease === this.session.lease) {
this.publish({ controlPending: false });
this.syncControl();
+ this.applySettings();
}
}
}
@@ -311,17 +388,139 @@ export class DesktopViewerController {
}
settings(patch: Partial<RemoteDesktopVideoSettings>): void {
this.publish({ settings: { ...this.state.settings, ...patch } });
+ if (patch.audio !== undefined) void this.preference({ audio: patch.audio });
+ this.pendingSettings = true;
+ if (this.settingsTimer) clearTimeout(this.settingsTimer);
+ this.settingsTimer = setTimeout(() => {
+ this.settingsTimer = null;
+ this.applySettings();
+ }, 0);
+ }
+ private applySettings(): void {
+ if (
+ !this.pendingSettings ||
+ !this.session.lease ||
+ !this.state.ready ||
+ this.state.closing ||
+ this.offers.size ||
+ this.state.controlPending ||
+ this.mediaChanging ||
+ !['live', 'compatibility'].includes(this.state.status)
+ )
+ return;
+ this.pendingSettings = false;
+ this.mediaChanging = true;
this.runtime.receive({
type: 'videoSettings',
audio: this.state.settings.audio && this.state.caps?.systemAudio === true,
});
}
+ async preference(patch: Partial<RemoteViewerPreferences>): Promise<void> {
+ const scope = this.scope;
+ try {
+ const preferences = await this.api.preferences?.(scope.generation, patch);
+ if (this.scope !== scope || this.disposed || !preferences) return;
+ this.publish({ preferences });
+ void this.refreshSafety();
+ } catch {
+ if (this.scope === scope)
+ this.publish({ safety: { ...this.state.safety, notice: 'viewer.settingsFailed' } });
+ }
+ }
+ async refreshSafety(retry = false): Promise<void> {
+ if (!this.state.ready || this.state.closing || !this.api.safety) return;
+ const epoch = this.epoch;
+ try {
+ const safety = await this.api.safety(this.scope.generation, retry);
+ if (epoch === this.epoch && !this.disposed) this.publish({ safety });
+ } catch {
+ /* Connection state owns disconnected/retired generations. */
+ }
+ }
+ async close(): Promise<void> {
+ if (this.state.closing) return;
+ this.releaseInput();
+ this.publish({ closing: true });
+ const epoch = this.epoch;
+ try {
+ await this.api.close(this.scope.generation);
+ } catch (error) {
+ if (epoch === this.epoch && !this.disposed) this.publish({ closing: false });
+ throw error;
+ }
+ }
+ async credential(
+ action: 'settings' | 'enable' | 'disable' | 'unlock' | 'biometric',
+ enabled?: boolean,
+ ): Promise<void> {
+ if (
+ !this.api.credential ||
+ this.state.credentialBusy ||
+ !this.state.ready ||
+ this.state.closing
+ )
+ return;
+ const scope = this.scope;
+ this.publish({ credentialBusy: true, credentialNotice: null });
+ this.credentialRetry = { action, enabled };
+ try {
+ const credential = await this.api.credential(scope.generation, action, enabled);
+ if (this.scope === scope && !this.disposed) {
+ this.credentialRetry = null;
+ this.publish({ credential });
+ }
+ } catch (error) {
+ if (this.scope !== scope || this.disposed) return;
+ const code = extractIpcError(error)?.message ?? String(error);
+ if (action === 'unlock' && code.includes('PASSWORD_REJECTED'))
+ this.credentialRetry = { action: 'enable' };
+ if (!code.includes('CREDENTIAL_CANCELLED'))
+ this.publish({
+ credentialNotice: code.includes('SIGNING_REQUIRED')
+ ? 'credentialSigningRequired'
+ : code.includes('PASSWORD_REJECTED')
+ ? 'credentialPasswordRejected'
+ : code.includes('INVALID_IDENTITY')
+ ? 'credentialIdentityChanged'
+ : code.includes('ACCESSIBILITY_REQUIRED')
+ ? 'credentialAccessibilityRequired'
+ : code.includes('UNLOCK_UNAVAILABLE')
+ ? 'credentialUnlockUnavailable'
+ : 'credentialRequired',
+ });
+ } finally {
+ if (this.scope === scope && !this.disposed) this.publish({ credentialBusy: false });
+ }
+ }
+ retryCredential(): void {
+ if (this.credentialRetry)
+ void this.credential(this.credentialRetry.action, this.credentialRetry.enabled);
+ }
releaseInput(): void {
this.runtime.receive({ type: 'releaseInput' });
}
+ actualSize(): void {
+ const lease = this.session.lease;
+ if (!lease || !this.state.ready) return;
+ this.runtime.receive({ type: 'actualSize' });
+ const toolbar = this.root.parentElement?.querySelector('.remote-viewer-toolbar');
+ const toolbarHeight = toolbar?.getBoundingClientRect().height ?? 60;
+ void this.api
+ .resize(
+ this.scope.generation,
+ Math.ceil(lease.display.width),
+ Math.ceil(lease.display.height + toolbarHeight),
+ )
+ .catch(() => {
+ /* A closed or replaced viewer must not resize its successor. */
+ });
+ }
fit(): void {
this.runtime.receive({ type: 'fit' });
}
+ zoom(direction: 'in' | 'out'): void {
+ this.runtime.receive({ type: 'zoom', factor: direction === 'in' ? 1.25 : 0.8 });
+ }
keys(codes: string[]): void {
if (!this.state.controlling) return;
const events: DesktopInput[] = [
@@ -363,13 +562,15 @@ export class DesktopViewerController {
async permissionGuide(): Promise<void> {
await this.request({ op: 'permissions', action: 'guide' });
}
- displayModes() {
+ async displayModes() {
const lease = this.session.lease;
- if (!lease) return Promise.resolve([]);
- return this.request<import('@cindy/device-link').RemoteDesktopDisplayMode[]>({
+ if (!lease) return [];
+ const modes = await this.request<import('@cindy/device-link').RemoteDesktopDisplayMode[]>({
op: 'displayModes',
lease: lease.lease,
});
+ if (this.session.lease !== lease) throw new Error('DESKTOP_STOPPED');
+ return modes;
}
async resolution(modeId: string): Promise<void> {
const lease = this.session.lease;
@@ -436,7 +637,8 @@ export class DesktopViewerController {
this.streaming = false;
this.runtime.receive({
type: 'videoSettings',
- ...size,
+ width: next.display.width,
+ height: next.display.height,
...(modeId ? { restore: true } : {}),
audio: this.state.settings.audio && this.state.caps?.systemAudio === true,
});
@@ -455,7 +657,7 @@ export class DesktopViewerController {
}
}
private async heartbeat(): Promise<void> {
- if (!this.scope.active || this.disposed) return;
+ if (!this.scope.active || this.disposed || this.state.closing) return;
if (!this.session.lease) {
if (!this.state.error && Date.now() >= this.retryAt) void this.connect();
return;
@@ -479,7 +681,14 @@ export class DesktopViewerController {
}
private async frame(): Promise<void> {
const lease = this.session.lease;
- if (!lease || this.streaming || this.frameBusy === lease.lease || !this.scope.active) return;
+ if (
+ !lease ||
+ this.state.closing ||
+ this.streaming ||
+ this.frameBusy === lease.lease ||
+ !this.scope.active
+ )
+ return;
this.frameBusy = lease.lease;
try {
const result = await this.request<{ jpeg: string | null; cursor?: unknown }>({
@@ -492,12 +701,22 @@ export class DesktopViewerController {
!this.streaming &&
typeof result.jpeg === 'string' &&
result.jpeg.length <= Math.ceil(REMOTE_DESKTOP_MAX_FRAME_BYTES / 3) * 4
- )
+ ) {
+ const now = Date.now();
+ this.publish({
+ receiveRate:
+ this.frameAt && now > this.frameAt
+ ? (result.jpeg.length * 0.75 * 1000) / (now - this.frameAt)
+ : null,
+ });
+ this.frameAt = now;
+ this.statsAt = now;
this.runtime.receive({
type: 'frame',
jpeg: result.jpeg,
...('cursor' in result ? { cursor: result.cursor } : {}),
});
+ }
} catch (error) {
if (this.session.lease === lease) this.fail(error);
} finally {
@@ -514,6 +733,10 @@ export class DesktopViewerController {
return;
}
switch (message.type) {
+ case 'scaleMode':
+ if (message.mode === 'fit' || message.mode === 'actual' || message.mode === 'custom')
+ this.publish({ scaleMode: message.mode });
+ break;
case 'clipboard': {
if (
!this.state.controlling ||
@@ -538,8 +761,10 @@ export class DesktopViewerController {
this.present('compatibility');
break;
case 'fallback':
+ this.mediaChanging = false;
this.streaming = false;
this.publish({ transport: 'screenshots', status: 'compatibility', latency: null });
+ this.applySettings();
break;
case 'reconnecting':
this.publish({ status: 'reconnecting' });
@@ -555,6 +780,12 @@ export class DesktopViewerController {
break;
this.publish({
transport,
+ receiveRate:
+ typeof message.bytesPerSecond === 'number' &&
+ Number.isFinite(message.bytesPerSecond) &&
+ message.bytesPerSecond >= 0
+ ? message.bytesPerSecond
+ : null,
latency:
typeof message.latencyMs === 'number' &&
Number.isFinite(message.latencyMs) &&
@@ -562,6 +793,7 @@ export class DesktopViewerController {
? message.latencyMs
: null,
});
+ this.statsAt = Date.now();
break;
}
case 'inputOverflow':
@@ -592,9 +824,16 @@ export class DesktopViewerController {
}
}
private present(status: string): void {
+ if (status === 'live') this.mediaChanging = false;
this.retryDelay = 1000;
this.publish({ ready: true, status });
this.syncControl();
+ this.applySettings();
+ void this.refreshSafety();
+ if (!this.unlockAttempted && this.state.caps?.platform === 'darwin') {
+ this.unlockAttempted = true;
+ void this.credential('unlock');
+ }
}
dispose(): void {
this.cancel();
diff --git a/apps/desktop/src/renderer/features/remote-desktop/viewerWindow.css b/apps/desktop/src/renderer/features/remote-desktop/viewerWindow.css
index 368ae34e17..7eb555ad63 100644
--- a/apps/desktop/src/renderer/features/remote-desktop/viewerWindow.css
+++ b/apps/desktop/src/renderer/features/remote-desktop/viewerWindow.css
@@ -9,7 +9,7 @@
position: relative;
display: flex;
flex: none;
- height: 52px;
+ height: 60px;
min-width: 0;
padding-right: var(--space-3);
gap: 6px;
@@ -33,7 +33,7 @@
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
- font-size: 13px;
+ font-size: var(--text-13);
}
.remote-viewer-status {
display: flex;
@@ -72,6 +72,25 @@
pointer-events: none;
transform-origin: 0 0;
}
+.remote-viewer-content #bg {
+ position: absolute;
+ inset: 0;
+ overflow: hidden;
+ z-index: 0;
+ display: none;
+ pointer-events: none;
+}
+/* Match the Mobile ambient backdrop's blur, overscan and translucency. */
+.remote-viewer-content #bg-canvas {
+ position: absolute;
+ inset: 0;
+ width: 100%;
+ height: 100%;
+ filter: blur(16px);
+ transform: scale(1.12);
+ transform-origin: center;
+ opacity: 0.72;
+}
.remote-viewer-content video {
display: none;
z-index: 0;
@@ -122,7 +141,7 @@
border-radius: 12px;
padding: 12px 20px;
background: var(--surface);
- font-size: 13px;
+ font-size: var(--text-13);
z-index: 4;
}
.remote-viewer-network {
@@ -165,32 +184,119 @@
display: none;
}
}
-.remote-viewer-settings {
- position: absolute;
- right: 12px;
- top: calc(100% + var(--space-2));
- max-height: calc(100vh - 100% - var(--space-5));
- overflow: auto;
- width: 320px;
+/* Each anchored panel has one job. Radix owns focus, dismissal and collision handling. */
+.remote-viewer-popover {
+ width: 360px;
max-width: calc(100vw - 24px);
- padding: 16px;
+ max-height: min(640px, var(--radix-popover-content-available-height));
+ overflow-y: auto;
display: flex;
flex-direction: column;
- gap: 12px;
- border: 1px solid var(--border-default);
- border-radius: 12px;
- background: var(--surface);
- z-index: 6;
+ gap: var(--space-4);
+ padding: var(--space-4);
+ background: var(--surface-elevated);
+ border-color: var(--border-default);
+ color: var(--text-primary);
+ -webkit-app-region: no-drag;
+}
+.remote-viewer-popover p {
+ font-size: var(--text-12);
+ line-height: 1.5;
+ color: var(--text-secondary);
+}
+.remote-viewer-panel-heading,
+.remote-viewer-preference label,
+.remote-viewer-toggle-row {
+ display: flex;
+ align-items: center;
+ justify-content: space-between;
+ gap: var(--space-3);
+ font-size: var(--text-13);
+}
+.remote-viewer-panel-heading strong {
+ font-weight: 600;
+}
+.remote-viewer-panel-heading {
+ padding-bottom: var(--space-2);
+ border-bottom: 1px solid var(--border-default);
+}
+.remote-viewer-preference {
+ display: flex;
+ flex-direction: column;
+ gap: var(--space-2);
+}
+.remote-viewer-panel-section {
+ display: flex;
+ flex-direction: column;
+ gap: var(--space-4);
+ padding-top: var(--space-4);
+ border-top: 1px solid var(--border-default);
+}
+.remote-viewer-panel-section:empty {
+ display: none;
+}
+.remote-viewer-toolgroup {
+ display: flex;
+ flex: none;
+ align-items: center;
+ gap: var(--space-1);
+ padding-inline: var(--space-2);
+ border-right: 1px solid var(--border-default);
-webkit-app-region: no-drag;
}
+.remote-viewer-icon {
+ width: var(--space-9);
+ height: var(--space-9);
+ padding: 0;
+}
+.remote-viewer-panel-trigger {
+ position: relative;
+ width: var(--space-9);
+ padding: 0;
+}
+.remote-viewer-active-dot {
+ position: absolute;
+ right: var(--space-1);
+ bottom: var(--space-1);
+ width: var(--space-1);
+ height: var(--space-1);
+ border-radius: var(--radius-full);
+ background: var(--text-primary);
+}
+.remote-viewer-feedback {
+ position: absolute;
+ bottom: var(--space-4);
+ left: 50%;
+ transform: translateX(-50%);
+ display: flex;
+ align-items: center;
+ gap: var(--space-2);
+ max-width: min(600px, calc(100% - 32px));
+ padding: var(--space-2) var(--space-3);
+ border: 1px solid var(--border-default);
+ border-radius: var(--radius-xl);
+ background: var(--surface-elevated);
+ font-size: var(--text-12);
+ z-index: 4;
+}
+@media (max-width: 1100px) {
+ .remote-viewer-toolbar-network {
+ display: none;
+ }
+}
+@media (max-width: 900px) {
+ .remote-viewer-toolgroup {
+ padding-inline: var(--space-1);
+ }
+}
.remote-viewer-field {
display: grid;
grid-template-columns: minmax(0, 1fr) minmax(0, var(--space-40));
align-items: center;
}
.remote-viewer-display-select {
- flex: 0 1 var(--space-40);
- width: var(--space-40);
+ flex: none;
+ width: 100%;
}
@media (max-width: 620px) {
.remote-viewer-toolbar {
@@ -200,13 +306,9 @@
padding-block: var(--space-1);
}
.remote-viewer-heading {
- flex-basis: var(--space-28);
+ flex-basis: calc(100% - 24px);
}
}
-.remote-viewer-settings p {
- font-size: 12px;
- color: var(--text-secondary);
-}
.remote-viewer-fullscreen .remote-viewer-toolbar {
position: absolute;
inset: 0 0 auto;
@@ -224,3 +326,37 @@
transition: none;
}
}
+
+.remote-viewer-zoom-group,
+.remote-viewer-panels {
+ box-sizing: border-box;
+ height: var(--space-9);
+ display: flex;
+ flex: none;
+ align-items: center;
+ gap: var(--space-1);
+ padding: 0 var(--space-1);
+ border: 1px solid var(--border-default);
+ border-radius: var(--radius-full);
+ background: var(--surface-elevated);
+ -webkit-app-region: no-drag;
+}
+.remote-viewer-zoom-group .remote-viewer-icon,
+.remote-viewer-panels .remote-viewer-panel-trigger {
+ height: var(--space-7);
+ border: 0;
+ border-radius: var(--radius-full);
+ background: transparent;
+ box-shadow: none;
+}
+.remote-viewer-zoom-group .remote-viewer-icon:enabled:hover,
+.remote-viewer-zoom-group .remote-viewer-icon[aria-pressed='true'],
+.remote-viewer-panels .remote-viewer-panel-trigger:enabled:hover,
+.remote-viewer-panels .remote-viewer-panel-trigger[aria-pressed='true'] {
+ background: var(--button-secondary-hover);
+}
+.remote-viewer-zoom-divider {
+ width: 1px;
+ height: var(--space-5);
+ background: var(--border-default);
+}
diff --git a/apps/desktop/src/renderer/i18n/locales/en/common.json b/apps/desktop/src/renderer/i18n/locales/en/common.json
index f509110ee1..051e8200b9 100644
--- a/apps/desktop/src/renderer/i18n/locales/en/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/en/common.json
@@ -64,7 +64,10 @@
"pan": "Move view",
"keyboard": "Keyboard",
"rightClick": "Right click",
- "fit": "Fit",
+ "actualSize": "Actual size (1:1)",
+ "zoomIn": "Zoom in",
+ "zoomOut": "Zoom out",
+ "fit": "Fit to window",
"display": "Display",
"gestureHint": "Pinch to zoom. Two fingers scroll; Move view pans. Hold, then move to drag.",
"textPlaceholder": "Type on the computer",
@@ -77,7 +80,7 @@
"connectionBusy": "Another device is connected to this remote desktop. Exit remote desktop on that device, then try connecting again.",
"takeoverConnection": "Take over connection",
"connectionBusyTakeover": "Another device is connected to this desktop. Taking over will disconnect that device.",
- "remoteDisabled": "Remote control is disabled on the computer.",
+ "remoteDisabled": "On the remote computer, open Cindy Settings → Remote control and enable remote control.",
"accessRevoked": "Access to this computer was revoked.",
"directConnection": "Direct to computer",
"videoRelay": "Server video relay",
@@ -88,12 +91,18 @@
"opening": "Opening remote desktop…",
"unavailable": "Remote desktop unavailable: {{reason}}",
"offline": "This computer is offline.",
- "controlDisabled": "Allow control of this computer in Remote control settings.",
+ "controlDisabled": "On the remote computer, open Cindy Settings → Remote control and allow control of this computer.",
"unsupported": "This device does not support remote desktop.",
- "checkFailed": "Could not check this computer. Hover again to retry.",
+ "checkFailed": "Could not check this computer. Check its network connection.",
+ "screenRecordingRequired": "On the remote Mac, open System Settings → Privacy & Security and allow screen recording for Cindy. Reopen Cindy if prompted.",
"noDisplay": "No available display on this computer."
},
"viewer": {
+ "displayPanel": "Display & Sound",
+ "clipboardPanel": "Clipboard",
+ "securityPanel": "Security",
+ "controlGroup": "Remote control",
+ "controlMode": "Control",
"settings": "Controls",
"sound": "Sound",
"fullscreen": "Full screen",
@@ -105,19 +114,57 @@
"balanced": "Balanced",
"clear": "Clear",
"resolution": "Computer resolution",
- "clipboardFailed": "Could not transfer text. Check control access and keep text under 16,384 characters.",
- "settingsFailed": "Could not change the display setting.",
+ "clipboardFailed": "Could not transfer clipboard content. Check control access and try again.",
+ "settingsFailed": "Could not save this setting. Try again.",
"inputHint": "Click the picture to type. Ctrl+Alt+Esc releases keyboard focus; Cmd/Ctrl+W closes this window.",
"controlPending": "Requesting control…",
- "controlRequired": "View only. Take control to use the clipboard and desktop actions.",
+ "controlRequired": "View only. Take control to interact with the remote computer.",
"confirmDisconnect": "Disconnect from this computer?",
"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.",
+ "clipboardShortcutHint": "Use {{modifier}}+C / {{modifier}}+V in the picture to copy and paste. Supported content depends on the remote computer; files are not transferred.",
"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."
- }
+ },
+ "settingUnsupported": "This computer does not support this setting yet",
+ "privacyScreen": "Computer privacy screen",
+ "privacyScreenHint": "Cover the computer’s screens. Click or press a key on the computer, then confirm to disconnect. Lock on exit applies if enabled.",
+ "privacyActive": "Privacy screen is on. Click or press a key on the computer, then confirm to disconnect. Lock on exit applies if enabled.",
+ "privacyFailed": "Privacy screen could not be enabled. The computer’s screens may still be visible.",
+ "hostMute": "Mute controlled computer",
+ "hostMuteHint": "Mute the controlled computer while connected",
+ "hostMuteFailed": "Unable to change controlled computer mute state",
+ "clipboardSync": "Clipboard sync",
+ "clipboardSyncConflict": "Both clipboards changed. Both copies were kept. Copy again on either device to sync.",
+ "clipboardSyncFailed": "Sync temporarily failed. Retrying automatically; your setting stays on.",
+ "clipboardSyncSkipped": "This content could not be synced and was skipped. Check paste access, or copy text or an image to try again.",
+ "clipboardSyncPermission": "Sync is paused. Allow paste access, then retry. Your setting stays on.",
+ "lockingOnExit": "Locking computer…",
+ "lockOnExit": "Lock Computer on Exit",
+ "lockOnExitHint": "Locks when you leave remote desktop. Reconnecting or switching apps does not lock the computer.",
+ "lockOnExitFailed": "Could not confirm that the computer locked. Check the computer before leaving it unattended.",
+ "nativeResolution": "Native",
+ "autoUnlock": "Automatically unlock computer",
+ "autoUnlockHint": "Save your Mac login password on this device to unlock the computer when connecting.",
+ "autoUnlockStorageHint": "The password is stored encrypted on this device only. Settings and passwords do not sync with your account.",
+ "autoUnlockUnavailable": "Automatic unlock is unavailable. You can still connect and unlock the Mac manually.",
+ "biometricVerification": "Biometric verification",
+ "biometricVerificationHint": "Verify your identity before using the saved password.",
+ "loadingSettings": "Loading…",
+ "credentialSigningRequired": "The Mac development build is missing its signing configuration. Restart Cindy on the Mac with signing configured, then try again.",
+ "credentialPasswordRejected": "The Mac login password was not accepted. Enter it again.",
+ "credentialIdentityChanged": "The computer identity could not be verified. No password was sent to an unverified device.",
+ "credentialAccessibilityRequired": "Allow Cindy in System Settings → Privacy & Security → Accessibility on the Mac, then reconnect.",
+ "credentialUnlockUnavailable": "Could not safely unlock this Mac. Select your account on its lock screen, then reconnect; if it still fails, unlock the Mac locally.",
+ "disconnecting": "Disconnecting…",
+ "loadingDisplayModes": "Loading resolutions…",
+ "displayModesFailed": "Could not load resolutions. Try again.",
+ "retry": "Retry",
+ "copy": "Copy to this computer",
+ "paste": "Paste to remote computer",
+ "clipboardSyncHint": "Sync text, images and rich text both ways while this window is in the foreground.",
+ "credentialRequired": "Complete Mac password verification. If unavailable, update Cindy on both computers."
},
"ipcError": {
"INVALID_PARAMS": "Invalid input. Please check and try again.",
diff --git a/apps/desktop/src/renderer/i18n/locales/ja/common.json b/apps/desktop/src/renderer/i18n/locales/ja/common.json
index d58b11e2b6..f5e664b54b 100644
--- a/apps/desktop/src/renderer/i18n/locales/ja/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/ja/common.json
@@ -64,7 +64,10 @@
"pan": "画面を移動",
"keyboard": "キーボード",
"rightClick": "右クリック",
- "fit": "全体表示",
+ "actualSize": "実際のサイズ(1:1)",
+ "zoomIn": "拡大",
+ "zoomOut": "縮小",
+ "fit": "ウィンドウに合わせる",
"display": "ディスプレイ",
"gestureHint": "ピンチで拡大縮小。2本指でスクロール、画面移動モードではパン。長押しして移動するとドラッグします。",
"textPlaceholder": "コンピューターに文字を入力",
@@ -77,7 +80,7 @@
"connectionBusy": "別のデバイスがこのリモートデスクトップに接続しています。そのデバイスでリモートデスクトップを終了してから、もう一度接続してください。",
"takeoverConnection": "接続を引き継ぐ",
"connectionBusyTakeover": "別のデバイスが接続中です。接続を引き継ぐと、そのデバイスは切断されます。",
- "remoteDisabled": "コンピュータのリモート操作が無効になっています。",
+ "remoteDisabled": "接続先コンピュータの Cindy の設定 → リモートコントロールで、リモート操作を有効にしてください。",
"accessRevoked": "このコンピュータへのアクセスが取り消されました。",
"directConnection": "パソコンに直接接続",
"videoRelay": "サーバー経由の映像",
@@ -88,12 +91,18 @@
"opening": "リモートデスクトップを開いています…",
"unavailable": "リモートデスクトップに接続できません:{{reason}}",
"offline": "このコンピュータはオフラインです。",
- "controlDisabled": "リモートコントロール設定で、このコンピュータの操作を許可してください。",
+ "controlDisabled": "接続先コンピュータの Cindy の設定 → リモートコントロールで、このコンピュータの操作を許可してください。",
"unsupported": "このデバイスはリモートデスクトップに対応していません。",
- "checkFailed": "コンピュータの状態を確認できません。もう一度カーソルを合わせて再試行してください。",
+ "checkFailed": "コンピュータの状態を確認できません。接続先のネットワーク接続を確認してください。",
+ "screenRecordingRequired": "接続先 Mac のシステム設定 → プライバシーとセキュリティで、Cindy の画面収録を許可してください。求められた場合は Cindy を開き直してください。",
"noDisplay": "このコンピュータに利用可能なディスプレイがありません。"
},
"viewer": {
+ "displayPanel": "画面とサウンド",
+ "clipboardPanel": "クリップボード",
+ "securityPanel": "セキュリティ",
+ "controlGroup": "リモート操作",
+ "controlMode": "操作",
"settings": "操作",
"sound": "音声",
"fullscreen": "全画面表示",
@@ -105,19 +114,57 @@
"balanced": "バランス",
"clear": "高画質",
"resolution": "コンピュータの解像度",
- "clipboardFailed": "テキストを転送できません。操作権限を確認し、16,384 文字以内にしてください。",
- "settingsFailed": "表示設定を変更できませんでした。",
+ "clipboardFailed": "クリップボードの内容を転送できませんでした。操作権限を確認して再試行してください。",
+ "settingsFailed": "設定を保存できませんでした。再試行してください。",
"inputHint": "画面をクリックして入力します。Ctrl+Alt+Esc でキーボードのフォーカスを解除し、Cmd/Ctrl+W でこのウィンドウを閉じます。",
"controlPending": "操作権限を取得中…",
- "controlRequired": "表示のみです。クリップボードとデスクトップ操作を使うには操作権限を取得してください。",
+ "controlRequired": "表示のみです。リモートのパソコンを操作するには操作権限を取得してください。",
"confirmDisconnect": "このコンピュータとの接続を切断しますか?",
"confirmDisconnectHint": "リモートデスクトップ接続を終了します。コンピュータ上のアプリは動作を続けます。",
"disconnectFailed": "切断できませんでした。もう一度お試しください。",
- "clipboardShortcutHint": "画面内で {{modifier}}+C / {{modifier}}+V を使うと、コンピュータ間でテキストをコピーして貼り付けられます。画像とファイルにはまだ対応していません。",
+ "clipboardShortcutHint": "画面内で {{modifier}}+C / {{modifier}}+V を使ってコピー・貼り付けできます。対応する内容は接続先のパソコンによって異なります。ファイルは転送されません。",
"fitViewerDisplay": "現在の画面比率に合わせる",
"fitViewerDisplayHint": "デスクトップの内容を保ったまま、パソコンの画面比率を一時的に変更します。切断すると元の解像度に戻ります。",
"resolutionHint": "解像度の変更はリモートの実際のモニターに反映されます。"
- }
+ },
+ "settingUnsupported": "このパソコンはまだこの設定に対応していません",
+ "privacyScreen": "パソコンのプライバシースクリーン",
+ "privacyScreenHint": "パソコンの画面を隠します。パソコンでクリックまたはキーを押し、確認すると切断します。終了時のロック設定も適用されます。",
+ "privacyActive": "プライバシースクリーンが有効です。パソコンでクリックまたはキーを押し、確認すると切断します。終了時のロック設定も適用されます。",
+ "privacyFailed": "プライバシースクリーンを有効にできませんでした。パソコンの画面が見えている可能性があります。",
+ "hostMute": "操作先をミュート",
+ "hostMuteHint": "接続中は操作先の音声をミュートします",
+ "hostMuteFailed": "操作先のミュート状態を変更できません",
+ "clipboardSync": "クリップボード同期",
+ "clipboardSyncConflict": "両方のクリップボードが変更されました。それぞれの内容は保持されています。どちらかのデバイスでもう一度コピーすると同期されます。",
+ "clipboardSyncFailed": "同期に一時的に失敗しました。自動で再試行します。設定はオンのままです。",
+ "clipboardSyncSkipped": "この内容は同期できないためスキップしました。ペーストの許可を確認するか、テキストや画像をコピーして再試行してください。",
+ "clipboardSyncPermission": "同期を一時停止しました。ペーストを許可してから再試行してください。設定はオンのままです。",
+ "lockingOnExit": "コンピューターをロック中…",
+ "lockOnExit": "終了時にパソコンをロック",
+ "lockOnExitHint": "リモートデスクトップを終了するとロックします。再接続やアプリ切り替えではロックしません。",
+ "lockOnExitFailed": "パソコンのロックを確認できませんでした。パソコンの状態を確認してください。",
+ "nativeResolution": "ネイティブ",
+ "autoUnlock": "コンピューターを自動ロック解除",
+ "autoUnlockHint": "Mac のログインパスワードをこのデバイスに保存し、接続時にパソコンのロックを解除します。",
+ "autoUnlockStorageHint": "パスワードはこの端末にのみ暗号化して保存されます。設定とパスワードはアカウントに同期されません。",
+ "autoUnlockUnavailable": "自動ロック解除を利用できません。接続して Mac を手動でロック解除できます。",
+ "biometricVerification": "生体認証",
+ "biometricVerificationHint": "保存したパスワードを使う前に本人確認を行います。",
+ "loadingSettings": "読み込み中…",
+ "credentialSigningRequired": "Mac の開発版に署名設定がありません。Mac で署名を設定して Cindy を再起動し、もう一度お試しください。",
+ "credentialPasswordRejected": "Mac のログインパスワードを確認できませんでした。もう一度入力してください。",
+ "credentialIdentityChanged": "コンピュータの識別情報を確認できません。未確認のデバイスにはパスワードを送信しません。",
+ "credentialAccessibilityRequired": "Mac のシステム設定 → プライバシーとセキュリティ → アクセシビリティで Cindy を許可してから再接続してください。",
+ "credentialUnlockUnavailable": "この Mac を安全にロック解除できませんでした。ロック画面で自分のアカウントを選んで再接続してください。解決しない場合は Mac で手動解除してください。",
+ "disconnecting": "切断中…",
+ "loadingDisplayModes": "解像度を読み込み中…",
+ "displayModesFailed": "解像度を読み込めませんでした。再試行してください。",
+ "retry": "再試行",
+ "copy": "このパソコンにコピー",
+ "paste": "リモートパソコンに貼り付け",
+ "clipboardSyncHint": "このウィンドウが最前面にある間、テキスト、画像、リッチテキストを双方向に同期します。",
+ "credentialRequired": "Mac のパスワードを確認してください。確認できない場合は両方のパソコンで Cindy を更新してください。"
},
"ipcError": {
"INVALID_PARAMS": "パラメータが無効です。確認して再試行してください。",
diff --git a/apps/desktop/src/renderer/i18n/locales/ko/common.json b/apps/desktop/src/renderer/i18n/locales/ko/common.json
index 603c249381..466d825713 100644
--- a/apps/desktop/src/renderer/i18n/locales/ko/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/ko/common.json
@@ -64,7 +64,10 @@
"pan": "화면 이동",
"keyboard": "키보드",
"rightClick": "오른쪽 클릭",
- "fit": "화면 맞춤",
+ "actualSize": "실제 크기 (1:1)",
+ "zoomIn": "확대",
+ "zoomOut": "축소",
+ "fit": "창에 맞춤",
"display": "디스플레이",
"gestureHint": "두 손가락으로 확대/축소 및 스크롤합니다. 화면 이동 모드에서는 이동합니다. 길게 누른 후 움직이면 드래그합니다.",
"textPlaceholder": "컴퓨터에 텍스트 입력",
@@ -77,7 +80,7 @@
"connectionBusy": "다른 기기가 이 원격 데스크톱에 연결되어 있습니다. 해당 기기에서 원격 데스크톱을 종료한 후 다시 연결하세요.",
"takeoverConnection": "연결 넘겨받기",
"connectionBusyTakeover": "다른 기기가 연결되어 있습니다. 연결을 넘겨받으면 해당 기기의 연결이 끊어집니다.",
- "remoteDisabled": "컴퓨터에서 원격 제어가 꺼져 있습니다.",
+ "remoteDisabled": "원격 컴퓨터의 Cindy 설정 → 원격 제어에서 원격 제어를 켜세요.",
"accessRevoked": "이 컴퓨터에 대한 접근 권한이 취소되었습니다.",
"directConnection": "컴퓨터에 직접 연결",
"videoRelay": "서버 영상 중계",
@@ -88,12 +91,18 @@
"opening": "원격 데스크톱 여는 중…",
"unavailable": "원격 데스크톱에 연결할 수 없습니다: {{reason}}",
"offline": "이 컴퓨터는 오프라인입니다.",
- "controlDisabled": "원격 제어 설정에서 이 컴퓨터의 제어를 허용하세요.",
+ "controlDisabled": "원격 컴퓨터의 Cindy 설정 → 원격 제어에서 이 컴퓨터의 제어를 허용하세요.",
"unsupported": "이 기기는 원격 데스크톱을 지원하지 않습니다.",
- "checkFailed": "컴퓨터 상태를 확인하지 못했습니다. 마우스를 다시 올려 재시도하세요.",
+ "checkFailed": "컴퓨터 상태를 확인하지 못했습니다. 해당 컴퓨터의 네트워크 연결을 확인하세요.",
+ "screenRecordingRequired": "원격 Mac의 시스템 설정 → 개인정보 보호 및 보안에서 Cindy의 화면 기록을 허용하세요. 요청이 표시되면 Cindy를 다시 여세요.",
"noDisplay": "이 컴퓨터에 사용 가능한 디스플레이가 없습니다."
},
"viewer": {
+ "displayPanel": "화면 및 소리",
+ "clipboardPanel": "클립보드",
+ "securityPanel": "보안",
+ "controlGroup": "원격 제어",
+ "controlMode": "제어",
"settings": "조작",
"sound": "소리",
"fullscreen": "전체 화면",
@@ -105,19 +114,57 @@
"balanced": "균형",
"clear": "선명",
"resolution": "컴퓨터 해상도",
- "clipboardFailed": "텍스트를 전송할 수 없습니다. 제어 권한을 확인하고 내용을 16,384자 이내로 줄이세요.",
- "settingsFailed": "디스플레이 설정을 변경할 수 없습니다.",
+ "clipboardFailed": "클립보드 내용을 전송하지 못했습니다. 제어 권한을 확인하고 다시 시도하세요.",
+ "settingsFailed": "설정을 저장하지 못했습니다. 다시 시도하세요.",
"inputHint": "화면을 클릭하여 입력하세요. Ctrl+Alt+Esc로 키보드 포커스를 해제하고 Cmd/Ctrl+W로 이 창을 닫습니다.",
"controlPending": "제어 권한을 가져오는 중…",
- "controlRequired": "보기 전용입니다. 클립보드와 데스크톱 작업을 사용하려면 제어 권한을 가져오세요.",
+ "controlRequired": "보기 전용입니다. 원격 컴퓨터를 조작하려면 제어 권한을 가져오세요.",
"confirmDisconnect": "이 컴퓨터와의 연결을 끊을까요?",
"confirmDisconnectHint": "원격 데스크톱 연결이 종료됩니다. 컴퓨터의 앱은 계속 실행됩니다.",
"disconnectFailed": "연결을 끊지 못했습니다. 다시 시도하세요.",
- "clipboardShortcutHint": "화면 안에서 {{modifier}}+C / {{modifier}}+V로 컴퓨터 간에 텍스트를 복사하고 붙여넣으세요. 이미지와 파일은 아직 지원하지 않습니다.",
+ "clipboardShortcutHint": "화면 안에서 {{modifier}}+C / {{modifier}}+V로 복사하고 붙여넣으세요. 지원되는 내용은 원격 컴퓨터에 따라 다르며 파일은 전송되지 않습니다.",
"fitViewerDisplay": "현재 화면 비율에 맞추기",
"fitViewerDisplayHint": "데스크톱 내용을 유지하면서 컴퓨터 화면 비율을 일시적으로 변경합니다. 연결을 종료하면 원래 해상도로 돌아갑니다.",
"resolutionHint": "해상도를 변경하면 원격 컴퓨터의 실제 모니터에 적용됩니다."
- }
+ },
+ "settingUnsupported": "이 컴퓨터는 아직 이 설정을 지원하지 않습니다",
+ "privacyScreen": "컴퓨터 프라이버시 화면",
+ "privacyScreenHint": "컴퓨터 화면을 가립니다. 컴퓨터에서 클릭하거나 키를 누른 뒤 확인하면 연결이 끊어집니다. 종료 시 잠금 설정도 적용됩니다.",
+ "privacyActive": "프라이버시 화면이 켜져 있습니다. 컴퓨터에서 클릭하거나 키를 누른 뒤 확인하면 연결이 끊어집니다. 종료 시 잠금 설정도 적용됩니다.",
+ "privacyFailed": "프라이버시 화면을 켜지 못했습니다. 컴퓨터 화면이 여전히 보일 수 있습니다.",
+ "hostMute": "원격 컴퓨터 음소거",
+ "hostMuteHint": "연결 중 원격 컴퓨터의 소리를 음소거합니다",
+ "hostMuteFailed": "원격 컴퓨터의 음소거 상태를 변경할 수 없습니다",
+ "clipboardSync": "클립보드 동기화",
+ "clipboardSyncConflict": "양쪽 클립보드가 변경되어 각각의 내용을 유지했습니다. 어느 기기에서든 다시 복사하면 동기화됩니다.",
+ "clipboardSyncFailed": "동기화가 일시적으로 실패했습니다. 자동으로 재시도하며 설정은 켜진 상태로 유지됩니다.",
+ "clipboardSyncSkipped": "현재 콘텐츠를 동기화할 수 없어 건너뛰었습니다. 붙여넣기 권한을 확인하거나 텍스트 또는 이미지를 복사한 후 다시 시도해 주세요.",
+ "clipboardSyncPermission": "동기화가 일시 중지되었습니다. 붙여넣기를 허용한 후 다시 시도해 주세요. 설정은 켜진 상태로 유지됩니다.",
+ "lockingOnExit": "컴퓨터를 잠그는 중…",
+ "lockOnExit": "종료 시 컴퓨터 잠금",
+ "lockOnExitHint": "원격 데스크톱을 종료하면 잠급니다. 재연결이나 앱 전환 시에는 잠그지 않습니다.",
+ "lockOnExitFailed": "컴퓨터가 잠겼는지 확인하지 못했습니다. 컴퓨터 상태를 확인해 주세요.",
+ "nativeResolution": "기본 해상도",
+ "autoUnlock": "컴퓨터 자동 잠금 해제",
+ "autoUnlockHint": "Mac 로그인 암호를 이 기기에 저장하고 연결할 때 컴퓨터 잠금을 해제합니다.",
+ "autoUnlockStorageHint": "암호는 현재 기기에만 암호화되어 저장됩니다. 설정과 암호는 계정에 동기화되지 않습니다.",
+ "autoUnlockUnavailable": "자동 잠금 해제를 사용할 수 없습니다. 연결한 뒤 Mac을 수동으로 잠금 해제할 수 있습니다.",
+ "biometricVerification": "생체 인증",
+ "biometricVerificationHint": "저장된 암호를 사용하기 전에 본인 인증을 합니다.",
+ "loadingSettings": "불러오는 중…",
+ "credentialSigningRequired": "Mac 개발 버전에 서명 설정이 없습니다. Mac에서 서명을 설정하고 Cindy를 다시 시작한 후 다시 시도해 주세요.",
+ "credentialPasswordRejected": "Mac 로그인 암호를 확인하지 못했습니다. 다시 입력해 주세요.",
+ "credentialIdentityChanged": "컴퓨터의 신원을 확인할 수 없습니다. 확인되지 않은 기기로 암호를 보내지 않습니다.",
+ "credentialAccessibilityRequired": "Mac의 시스템 설정 → 개인정보 보호 및 보안 → 손쉬운 사용에서 Cindy를 허용한 후 다시 연결하세요.",
+ "credentialUnlockUnavailable": "이 Mac의 잠금을 안전하게 해제하지 못했습니다. 잠금 화면에서 본인 계정을 선택한 후 다시 연결하세요. 계속 실패하면 Mac에서 직접 잠금을 해제하세요.",
+ "disconnecting": "연결 끊는 중…",
+ "loadingDisplayModes": "해상도 불러오는 중…",
+ "displayModesFailed": "해상도를 불러오지 못했습니다. 다시 시도하세요.",
+ "retry": "다시 시도",
+ "copy": "이 컴퓨터로 복사",
+ "paste": "원격 컴퓨터에 붙여넣기",
+ "clipboardSyncHint": "이 창이 전면에 있을 때 텍스트, 이미지, 서식 있는 텍스트를 양방향으로 동기화합니다.",
+ "credentialRequired": "Mac 암호를 확인하세요. 확인할 수 없으면 두 컴퓨터에서 Cindy를 업데이트하세요."
},
"ipcError": {
"INVALID_PARAMS": "매개변수가 잘못되었습니다. 확인 후 다시 시도하세요.",
diff --git a/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json b/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json
index be46a21c33..c11364d062 100644
--- a/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json
@@ -64,7 +64,10 @@
"pan": "移动画面",
"keyboard": "键盘",
"rightClick": "右键",
- "fit": "适应屏幕",
+ "actualSize": "实际大小(1:1)",
+ "zoomIn": "放大",
+ "zoomOut": "缩小",
+ "fit": "适应窗口",
"display": "显示器",
"gestureHint": "双指缩放;双指移动可滚动,移动画面模式下可平移。长按后移动可拖拽。",
"textPlaceholder": "在电脑上输入文字",
@@ -77,7 +80,7 @@
"connectionBusy": "已有其他设备连接此远程桌面。请先在那台设备上退出远程桌面,再尝试连接。",
"takeoverConnection": "接管连接",
"connectionBusyTakeover": "已有其他设备连接此远程桌面。接管连接后,上一台设备会断开。",
- "remoteDisabled": "电脑端已关闭远程控制。",
+ "remoteDisabled": "请在远程电脑的 Cindy 设置 → 远程控制中开启远程控制。",
"accessRevoked": "对这台电脑的访问已撤销。",
"directConnection": "电脑直连",
"videoRelay": "服务器视频中转",
@@ -88,12 +91,18 @@
"opening": "正在打开远程桌面…",
"unavailable": "无法连接远程桌面:{{reason}}",
"offline": "这台电脑已离线。",
- "controlDisabled": "请先在远程控制设置中允许控制这台电脑。",
+ "controlDisabled": "请在远程电脑的 Cindy 设置 → 远程控制中,允许控制这台电脑。",
"unsupported": "此设备不支持远程桌面。",
- "checkFailed": "未能确认电脑状态,重新悬停可重试。",
+ "checkFailed": "未能确认电脑状态,请检查这台电脑的网络连接。",
+ "screenRecordingRequired": "请在远程 Mac 的系统设置 → 隐私与安全性中,为 Cindy 开启屏幕录制权限。如果系统要求,请重新打开 Cindy。",
"noDisplay": "这台电脑没有可用的显示器。"
},
"viewer": {
+ "displayPanel": "影音",
+ "clipboardPanel": "剪贴板",
+ "securityPanel": "安全",
+ "controlGroup": "远程操作",
+ "controlMode": "控制",
"settings": "操作",
"sound": "声音",
"fullscreen": "全屏",
@@ -105,19 +114,57 @@
"balanced": "均衡",
"clear": "清晰",
"resolution": "电脑分辨率",
- "clipboardFailed": "文字传输失败,请确认已取得控制权,且内容不超过 16,384 字符。",
- "settingsFailed": "未能修改显示设置。",
+ "clipboardFailed": "剪贴板内容传输失败,请检查控制权限后重试。",
+ "settingsFailed": "设置未能保存,请重试。",
"inputHint": "点击画面即可输入。Ctrl+Alt+Esc 释放键盘焦点;Cmd/Ctrl+W 关闭这个窗口。",
"controlPending": "正在取得控制权…",
- "controlRequired": "当前仅查看,取得控制权后可使用剪贴板和桌面操作。",
+ "controlRequired": "当前仅查看。接管后可操作远端电脑。",
"confirmDisconnect": "断开与这台电脑的连接?",
"confirmDisconnectHint": "将结束远程桌面连接,电脑上的应用仍会继续运行。",
"disconnectFailed": "未能断开连接,请重试。",
- "clipboardShortcutHint": "在画面内使用 {{modifier}}+C / {{modifier}}+V,即可在两台电脑之间复制粘贴文字。暂不支持图片和文件。",
+ "clipboardShortcutHint": "在画面内使用 {{modifier}}+C / {{modifier}}+V 复制粘贴。支持的内容取决于远程电脑,暂不传输文件。",
"fitViewerDisplay": "按当前画面比例调整",
"fitViewerDisplayHint": "临时调整电脑桌面比例,保留当前桌面内容。退出连接后恢复原分辨率。",
"resolutionHint": "分辨率修改会影响远端的实际显示器。"
- }
+ },
+ "settingUnsupported": "当前电脑端暂不支持此设置",
+ "privacyScreen": "电脑防窥",
+ "privacyScreenHint": "遮住电脑屏幕。在电脑上点击鼠标或按键,确认后断开连接,并按设置锁屏。",
+ "privacyActive": "防窥已开启。在电脑上点击鼠标或按键,确认后断开连接,并按设置锁屏。",
+ "privacyFailed": "未能开启防窥,电脑屏幕可能仍可见。",
+ "hostMute": "被控端静音",
+ "hostMuteHint": "连接期间将被控电脑静音",
+ "hostMuteFailed": "无法修改被控端静音状态",
+ "clipboardSync": "剪贴板同步",
+ "clipboardSyncConflict": "两端剪贴板都发生了变化,已分别保留。请在任一设备上重新复制以同步。",
+ "clipboardSyncFailed": "同步暂时失败,正在自动重试。开关保持开启。",
+ "clipboardSyncSkipped": "当前内容无法同步,已跳过。请检查粘贴权限,或复制文字、图片后重试。",
+ "clipboardSyncPermission": "同步已暂停,请允许粘贴权限后重试。开关保持开启。",
+ "lockingOnExit": "正在锁定电脑…",
+ "lockOnExit": "退出后锁定电脑",
+ "lockOnExitHint": "退出远程桌面时锁屏。重连或切换 App 不会触发。",
+ "lockOnExitFailed": "未能确认电脑已锁屏,请检查电脑的锁屏状态。",
+ "nativeResolution": "原生",
+ "autoUnlock": "自动解锁电脑",
+ "autoUnlockHint": "在此设备保存 Mac 登录密码,连接时自动解锁电脑。",
+ "autoUnlockStorageHint": "密码仅加密保存在当前设备。设置和密码不随账号同步。",
+ "autoUnlockUnavailable": "暂时无法自动解锁。你仍可连接,并手动解锁 Mac。",
+ "biometricVerification": "生物识别验证",
+ "biometricVerificationHint": "使用保存的密码前,验证你的身份。",
+ "loadingSettings": "读取中…",
+ "credentialSigningRequired": "Mac 上的开发版缺少签名配置。请在 Mac 上配置签名并重新启动 Cindy,然后重试。",
+ "credentialPasswordRejected": "Mac 登录密码验证失败,请重新输入。",
+ "credentialIdentityChanged": "无法确认电脑身份,不会向未经验证的设备发送密码。",
+ "credentialAccessibilityRequired": "请在 Mac 的系统设置 → 隐私与安全性 → 辅助功能中允许 Cindy,然后重新连接。",
+ "credentialUnlockUnavailable": "暂时无法安全解锁这台 Mac。请在锁屏界面选中你的账户后重连;若仍失败,请在电脑上手动解锁。",
+ "disconnecting": "正在断开…",
+ "loadingDisplayModes": "正在读取分辨率…",
+ "displayModesFailed": "未能读取分辨率,请重试。",
+ "retry": "重试",
+ "copy": "复制到本机",
+ "paste": "粘贴到远程电脑",
+ "clipboardSyncHint": "此窗口在前台时,双向同步文字、图片和富文本。",
+ "credentialRequired": "请完成 Mac 密码验证。如果无法验证,请更新两台电脑上的 Cindy。"
},
"ipcError": {
"INVALID_PARAMS": "参数无效,请检查后重试。",
diff --git a/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json b/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json
index 2177809340..b95ee3d75f 100644
--- a/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json
@@ -64,7 +64,10 @@
"pan": "移動畫面",
"keyboard": "鍵盤",
"rightClick": "右鍵",
- "fit": "符合螢幕",
+ "actualSize": "實際大小(1:1)",
+ "zoomIn": "放大",
+ "zoomOut": "縮小",
+ "fit": "適應視窗",
"display": "顯示器",
"gestureHint": "雙指縮放;雙指移動可捲動,移動畫面模式下可平移。長按後移動可拖曳。",
"textPlaceholder": "在電腦上輸入文字",
@@ -77,7 +80,7 @@
"connectionBusy": "已有其他裝置連線至此遠端桌面。請先在該裝置上退出遠端桌面,再嘗試連線。",
"takeoverConnection": "接管連線",
"connectionBusyTakeover": "已有其他裝置連線至此遠端桌面。接管連線後,上一台裝置會中斷。",
- "remoteDisabled": "電腦端已關閉遠端控制。",
+ "remoteDisabled": "請在遠端電腦的 Cindy 設定 → 遠端控制中啟用遠端控制。",
"accessRevoked": "對這台電腦的存取權已撤銷。",
"directConnection": "電腦直連",
"videoRelay": "伺服器影片中繼",
@@ -88,12 +91,18 @@
"opening": "正在開啟遠端桌面…",
"unavailable": "無法連線至遠端桌面:{{reason}}",
"offline": "這台電腦已離線。",
- "controlDisabled": "請先在遠端控制設定中允許控制這台電腦。",
+ "controlDisabled": "請在遠端電腦的 Cindy 設定 → 遠端控制中,允許控制這台電腦。",
"unsupported": "此裝置不支援遠端桌面。",
- "checkFailed": "無法確認電腦狀態,重新移入游標可重試。",
+ "checkFailed": "無法確認電腦狀態,請檢查這台電腦的網路連線。",
+ "screenRecordingRequired": "請在遠端 Mac 的系統設定 → 隱私權與安全性中,為 Cindy 啟用螢幕錄製權限。如果系統要求,請重新開啟 Cindy。",
"noDisplay": "這台電腦沒有可用的顯示器。"
},
"viewer": {
+ "displayPanel": "影音",
+ "clipboardPanel": "剪貼簿",
+ "securityPanel": "安全",
+ "controlGroup": "遠端操作",
+ "controlMode": "控制",
"settings": "操作",
"sound": "聲音",
"fullscreen": "全螢幕",
@@ -105,19 +114,57 @@
"balanced": "均衡",
"clear": "清晰",
"resolution": "電腦解析度",
- "clipboardFailed": "文字傳輸失敗,請確認已取得控制權,且內容不超過 16,384 字元。",
- "settingsFailed": "無法修改顯示設定。",
+ "clipboardFailed": "剪貼簿內容傳輸失敗,請檢查控制權限後重試。",
+ "settingsFailed": "設定未能儲存,請重試。",
"inputHint": "點擊畫面即可輸入。Ctrl+Alt+Esc 釋放鍵盤焦點;Cmd/Ctrl+W 關閉這個視窗。",
"controlPending": "正在取得控制權…",
- "controlRequired": "目前僅供檢視,取得控制權後可使用剪貼簿和桌面操作。",
+ "controlRequired": "目前僅供檢視。取得控制權後可操作遠端電腦。",
"confirmDisconnect": "中斷與這台電腦的連線?",
"confirmDisconnectHint": "將結束遠端桌面連線,電腦上的應用程式仍會繼續執行。",
"disconnectFailed": "無法中斷連線,請重試。",
- "clipboardShortcutHint": "在畫面內使用 {{modifier}}+C / {{modifier}}+V,即可在兩台電腦之間複製貼上文字。暫不支援圖片和檔案。",
+ "clipboardShortcutHint": "在畫面內使用 {{modifier}}+C / {{modifier}}+V 複製貼上。支援的內容取決於遠端電腦,暫不傳輸檔案。",
"fitViewerDisplay": "依目前畫面比例調整",
"fitViewerDisplayHint": "暫時調整電腦桌面比例,保留目前桌面內容。離開連線後恢復原解析度。",
"resolutionHint": "解析度修改會影響遠端的實際顯示器。"
- }
+ },
+ "settingUnsupported": "目前電腦端尚不支援此設定",
+ "privacyScreen": "電腦防窺",
+ "privacyScreenHint": "遮住電腦螢幕。在電腦上點擊滑鼠或按鍵,確認後中斷連線,並依設定鎖定螢幕。",
+ "privacyActive": "防窺已開啟。在電腦上點擊滑鼠或按鍵,確認後中斷連線,並依設定鎖定螢幕。",
+ "privacyFailed": "無法開啟防窺,電腦螢幕可能仍可見。",
+ "hostMute": "被控端靜音",
+ "hostMuteHint": "連線期間將被控電腦靜音",
+ "hostMuteFailed": "無法修改被控端靜音狀態",
+ "clipboardSync": "剪貼簿同步",
+ "clipboardSyncConflict": "兩端剪貼簿都發生了變更,已分別保留。請在任一裝置上重新複製以同步。",
+ "clipboardSyncFailed": "同步暫時失敗,正在自動重試。開關保持開啟。",
+ "clipboardSyncSkipped": "目前內容無法同步,已略過。請檢查貼上權限,或複製文字、圖片後重試。",
+ "clipboardSyncPermission": "同步已暫停,請允許貼上權限後重試。開關保持開啟。",
+ "lockingOnExit": "正在鎖定電腦…",
+ "lockOnExit": "退出後鎖定電腦",
+ "lockOnExitHint": "離開遠端桌面時鎖定螢幕。重新連線或切換 App 不會觸發。",
+ "lockOnExitFailed": "無法確認電腦已鎖定螢幕,請檢查電腦狀態。",
+ "nativeResolution": "原生",
+ "autoUnlock": "自動解鎖電腦",
+ "autoUnlockHint": "在此裝置儲存 Mac 登入密碼,連線時自動解鎖電腦。",
+ "autoUnlockStorageHint": "密碼僅加密儲存在目前裝置。設定和密碼不會隨帳號同步。",
+ "autoUnlockUnavailable": "暫時無法自動解鎖。你仍可連線,並手動解鎖 Mac。",
+ "biometricVerification": "生物辨識驗證",
+ "biometricVerificationHint": "使用儲存的密碼前,驗證你的身分。",
+ "loadingSettings": "讀取中…",
+ "credentialSigningRequired": "Mac 上的開發版缺少簽名設定。請在 Mac 上設定簽名並重新啟動 Cindy,然後重試。",
+ "credentialPasswordRejected": "Mac 登入密碼驗證失敗,請重新輸入。",
+ "credentialIdentityChanged": "無法確認電腦身分,不會向未經驗證的裝置傳送密碼。",
+ "credentialAccessibilityRequired": "請在 Mac 的系統設定 → 隱私權與安全性 → 輔助使用中允許 Cindy,再重新連線。",
+ "credentialUnlockUnavailable": "暫時無法安全解鎖這台 Mac。請在鎖定畫面選取你的帳號後重新連線;若仍失敗,請在電腦上手動解鎖。",
+ "disconnecting": "正在斷開…",
+ "loadingDisplayModes": "正在讀取解析度…",
+ "displayModesFailed": "無法讀取解析度,請重試。",
+ "retry": "重試",
+ "copy": "複製到本機",
+ "paste": "貼上到遠端電腦",
+ "clipboardSyncHint": "此視窗在前景時,雙向同步文字、圖片和富文字。",
+ "credentialRequired": "請完成 Mac 密碼驗證。如果無法驗證,請更新兩台電腦上的 Cindy。"
},
"ipcError": {
"INVALID_PARAMS": "引數無效,請檢查後重試。",
diff --git a/apps/desktop/src/shared/remoteDesktopViewer.ts b/apps/desktop/src/shared/remoteDesktopViewer.ts
index 8f838c784d..4da454abcd 100644
--- a/apps/desktop/src/shared/remoteDesktopViewer.ts
+++ b/apps/desktop/src/shared/remoteDesktopViewer.ts
@@ -13,7 +13,11 @@ export const REMOTE_VIEWER = {
ACTIVE: 'remote-desktop-viewer:active',
LOCALE: 'remote-desktop-viewer:locale',
FULLSCREEN: 'remote-desktop-viewer:fullscreen',
+ RESIZE: 'remote-desktop-viewer:resize',
INPUT_FOCUS: 'remote-desktop-viewer:input-focus',
+ PREFERENCES: 'remote-desktop-viewer:preferences',
+ SAFETY: 'remote-desktop-viewer:safety',
+ CREDENTIAL: 'remote-desktop-viewer:credential',
} as const;
export interface RemoteViewerTarget {
@@ -27,6 +31,31 @@ export interface RemoteViewerState {
resume?: boolean;
}
export type RemoteViewerReply = { ok: true; result: unknown } | { ok: false; code: string };
+export interface RemoteViewerPreferences {
+ audio: boolean;
+ privacyScreen: boolean;
+ hostMute: boolean;
+ clipboardSync: boolean;
+ lockOnExit: boolean;
+}
+export const DEFAULT_VIEWER_PREFERENCES: RemoteViewerPreferences = {
+ audio: true,
+ privacyScreen: false,
+ hostMute: false,
+ clipboardSync: false,
+ lockOnExit: false,
+};
+export interface RemoteViewerSafety {
+ privacyActive: boolean;
+ notice: string | null;
+ clipboardProgress: number | null;
+}
+export interface RemoteViewerCredentialState {
+ available: boolean;
+ autoUnlock: boolean;
+ biometricAvailable: boolean;
+ biometricVerification: boolean;
+}
/** Dedicated window bridge. The target and account are bound by Main, never by payload. */
export interface RemoteDesktopViewerApi {
state(): Promise<RemoteViewerState>;
@@ -39,10 +68,21 @@ export interface RemoteDesktopViewerApi {
clipboard(generation: number, action: 'copy' | 'paste'): Promise<void>;
close(generation: number): Promise<void>;
fullscreen(): Promise<void>;
+ resize(generation: number, width: number, height: number): Promise<void>;
rendererReady(): Promise<void>;
presentationReady(): Promise<void>;
onActive(listener: (state: RemoteViewerState) => void): () => void;
onLocale(listener: (locale: string) => void): () => void;
onCloseRequested(listener: (generation: number) => void): () => void;
inputFocus(generation: number, focused: boolean): Promise<void>;
+ preferences?(
+ generation: number,
+ patch?: Partial<RemoteViewerPreferences>,
+ ): Promise<RemoteViewerPreferences>;
+ safety?(generation: number, retry?: boolean): Promise<RemoteViewerSafety>;
+ credential?(
+ generation: number,
+ action: 'settings' | 'enable' | 'disable' | 'unlock' | 'biometric',
+ enabled?: boolean,
+ ): Promise<RemoteViewerCredentialState>;
}
diff --git a/apps/mobile/src/remote-desktop/clipboardSync.ts b/apps/mobile/src/remote-desktop/clipboardSync.ts
index a2fe2b7b3f..448faecdc6 100644
--- a/apps/mobile/src/remote-desktop/clipboardSync.ts
+++ b/apps/mobile/src/remote-desktop/clipboardSync.ts
@@ -1,44 +1,16 @@
import {
- CLIPBOARD_CHUNK_CHARS,
- CLIPBOARD_MAX_CHARS,
+ ClipboardSync as SharedClipboardSync,
parseClipboardContent,
- type RemoteDesktopRequest,
+ type ClipboardSyncDeps as SharedClipboardSyncDeps,
+ type ClipboardSyncBaseline,
} from "@cindy/device-link";
import { sha256 } from "@noble/hashes/sha256";
import { bytesToHex } from "@noble/hashes/utils";
-import { clipboardSyncErrorCode } from "./clipboardSyncFailure";
-
-function skippableContent(error: unknown): boolean {
- return [
- "CLIPBOARD_EMPTY",
- "CLIPBOARD_UNSUPPORTED",
- "CLIPBOARD_TOO_LONG",
- ].includes(clipboardSyncErrorCode(error));
-}
-
-export interface LocalClipboardReadCache {
- version?: string;
- digest?: string;
-}
-export interface ClipboardSyncDeps {
- lease: string;
- request<T>(message: RemoteDesktopRequest): Promise<T>;
- localVersion(): Promise<string>;
- readLocal(): Promise<string>;
- writeLocal(json: string, version: string): Promise<string>;
- current(): boolean;
- inline?: boolean;
- localReadCache?: LocalClipboardReadCache;
- trace?(stage: string): void;
-}
-export interface ClipboardSyncBaseline {
- local: string;
- remote: string;
- localDigest?: string;
- remoteDigest?: string;
-}
-
-/** Hash every portable representation, in stable key order. No content is retained. */
+export type {
+ ClipboardSyncBaseline,
+ LocalClipboardReadCache,
+} from "@cindy/device-link";
+export type ClipboardSyncDeps = Omit<SharedClipboardSyncDeps, "digest">;
export function clipboardDigest(json: string): string {
const content = parseClipboardContent(json);
return bytesToHex(
@@ -53,343 +25,8 @@ export function clipboardDigest(json: string): string {
),
);
}
-
-/** Serial sync: counter changes trigger reads; content changes trigger writes. */
-export class ClipboardSync {
- private pending = false;
- constructor(
- private readonly deps: ClipboardSyncDeps,
- private readonly baseline: ClipboardSyncBaseline = {
- local: "",
- remote: "",
- },
- ) {}
- private check() {
- if (!this.deps.current()) throw new Error("DESKTOP_LEASE_EXPIRED");
- }
- private async remoteVersion() {
- const { version } = await this.deps.request<{ version: string }>({
- op: "clipboardVersion",
- lease: this.deps.lease,
- });
- this.check();
- if (typeof version !== "string" || !/^\d{1,128}$/.test(version))
- throw new Error("CLIPBOARD_UNSUPPORTED");
- return version;
- }
- private async readLocal(version: string) {
- this.deps.trace?.("phone-read-start");
- const json = await this.deps.readLocal();
- this.check();
- const digest = clipboardDigest(json);
- if ((await this.deps.localVersion()) !== version)
- throw new Error("CLIPBOARD_CHANGED");
- this.check();
- this.rememberLocal(version, digest);
- this.deps.trace?.("phone-read-complete");
- return { json, digest };
- }
- private rememberLocal(version: string, digest: string | undefined) {
- if (this.deps.localReadCache)
- Object.assign(this.deps.localReadCache, { version, digest });
- }
- private async cancel(id: string) {
- await this.deps
- .request({
- op: "clipboardContent",
- lease: this.deps.lease,
- sync: true,
- action: "cancel",
- id,
- })
- .catch(() => {});
- }
- private async readRemote(version: string) {
- const { lease, request } = this.deps;
- let id: string | undefined;
- try {
- this.deps.trace?.("computer-read-start");
- const result = await request<{
- id?: string;
- length: number;
- data?: string;
- }>({
- op: "clipboardContent",
- lease,
- sync: true,
- version,
- action: "copy",
- ...(this.deps.inline ? { inline: true } : {}),
- });
- this.check();
- if (
- !Number.isSafeInteger(result.length) ||
- result.length <= 0 ||
- result.length > CLIPBOARD_MAX_CHARS
- )
- throw new Error("CLIPBOARD_TOO_LONG");
- let json = "";
- if (this.deps.inline && typeof result.data === "string") {
- if (
- result.data.length > CLIPBOARD_CHUNK_CHARS ||
- result.data.length !== result.length
- )
- throw new Error("CLIPBOARD_UNSUPPORTED");
- json = result.data;
- } else {
- if (typeof result.id !== "string" || !result.id)
- throw new Error("CLIPBOARD_UNSUPPORTED");
- id = result.id;
- while (json.length < result.length) {
- this.check();
- const part = await request<{ data: string }>({
- op: "clipboardContent",
- lease,
- sync: true,
- action: "read",
- id,
- offset: json.length,
- });
- this.check();
- if (
- typeof part.data !== "string" ||
- !part.data.length ||
- part.data.length > CLIPBOARD_CHUNK_CHARS ||
- json.length + part.data.length > result.length
- )
- throw new Error("CLIPBOARD_UNSUPPORTED");
- json += part.data;
- }
- }
- const digest = clipboardDigest(json);
- if ((await this.remoteVersion()) !== version)
- throw new Error("CLIPBOARD_CHANGED");
- return { json, digest };
- } finally {
- if (id) void this.cancel(id);
- }
- }
- private async writeRemote(json: string, local: string, remote: string) {
- const { lease, request } = this.deps;
- let id: string | undefined;
- try {
- this.check();
- let committed: { version: string };
- if (this.deps.inline && json.length <= CLIPBOARD_CHUNK_CHARS) {
- if ((await this.deps.localVersion()) !== local)
- throw new Error("CLIPBOARD_CHANGED");
- this.check();
- committed = await request({
- op: "clipboardContent",
- lease,
- sync: true,
- version: remote,
- action: "paste",
- data: json,
- });
- } else {
- const result = await request<{ id: string }>({
- op: "clipboardContent",
- lease,
- sync: true,
- version: remote,
- action: "begin",
- length: json.length,
- });
- id = result.id;
- for (
- let offset = 0;
- offset < json.length;
- offset += CLIPBOARD_CHUNK_CHARS
- ) {
- this.check();
- await request({
- op: "clipboardContent",
- lease,
- sync: true,
- action: "write",
- id,
- offset,
- data: json.slice(offset, offset + CLIPBOARD_CHUNK_CHARS),
- });
- }
- if ((await this.deps.localVersion()) !== local)
- throw new Error("CLIPBOARD_CHANGED");
- this.check();
- committed = await request({
- op: "clipboardContent",
- lease,
- sync: true,
- action: "commit",
- id,
- });
- }
- this.check();
- if (
- typeof committed.version !== "string" ||
- !/^\d{1,128}$/.test(committed.version)
- )
- throw new Error("CLIPBOARD_UNSUPPORTED");
- return committed.version;
- } finally {
- if (id) void this.cancel(id);
- }
- }
- async tick(): Promise<void> {
- if (this.pending) return;
- this.pending = true;
- let local: string | undefined;
- let remote: string | undefined;
- let readLocalDigest: string | undefined;
- let readRemoteDigest: string | undefined;
- try {
- this.check();
- local = await this.deps.localVersion();
- this.check();
- remote = await this.remoteVersion();
- if (!this.baseline.local) {
- let digest: string | undefined;
- const cache = this.deps.localReadCache;
- if (cache?.version === local) {
- digest = cache.digest;
- } else {
- // Keep the proven initial native permission read. Reuse it only while
- // the phone pasteboard version remains exactly the same.
- try {
- digest = (await this.readLocal(local)).digest;
- } catch (error) {
- if (!skippableContent(error)) throw error;
- this.deps.trace?.("content-skipped");
- if ((await this.deps.localVersion()) !== local)
- throw new Error("CLIPBOARD_CHANGED");
- }
- }
- // Establish both content baselines without writing either clipboard.
- // Counter-only rewrites must not import contents predating opt-in.
- let remoteDigest: string | undefined;
- try {
- remoteDigest = (await this.readRemote(remote)).digest;
- } catch (error) {
- if (!skippableContent(error)) throw error;
- this.deps.trace?.("content-skipped");
- if ((await this.remoteVersion()) !== remote)
- throw new Error("CLIPBOARD_CHANGED");
- }
- if ((await this.deps.localVersion()) !== local)
- throw new Error("CLIPBOARD_CHANGED");
- this.check();
- this.rememberLocal(local, digest);
- Object.assign(this.baseline, {
- local,
- remote,
- localDigest: digest,
- remoteDigest,
- });
- return;
- }
- let localChanged = local !== this.baseline.local;
- let remoteChanged = remote !== this.baseline.remote;
- if (!localChanged && !remoteChanged) return;
- const localContent = localChanged
- ? await this.readLocal(local).then((content) => {
- readLocalDigest = content.digest;
- return content;
- })
- : undefined;
- const remoteContent = remoteChanged
- ? await this.readRemote(remote).then((content) => {
- readRemoteDigest = content.digest;
- return content;
- })
- : undefined;
- this.check();
- if ((await this.deps.localVersion()) !== local)
- throw new Error("CLIPBOARD_CHANGED");
- this.check();
- if (localContent && localContent.digest === this.baseline.localDigest)
- localChanged = false;
- if (remoteContent && remoteContent.digest === this.baseline.remoteDigest)
- remoteChanged = false;
- const localDigest = localContent?.digest ?? this.baseline.localDigest;
- const remoteDigest = remoteContent?.digest ?? this.baseline.remoteDigest;
- if (localDigest && localDigest === remoteDigest) {
- localChanged = false;
- remoteChanged = false;
- }
- if (localChanged && remoteChanged) {
- Object.assign(this.baseline, {
- local,
- remote,
- localDigest,
- remoteDigest,
- });
- throw new Error("CLIPBOARD_CONFLICT");
- }
- if (localChanged && localContent) {
- const version = await this.writeRemote(
- localContent.json,
- local,
- remote,
- );
- // Native clipboards may normalize or drop portable representations.
- // Record the destination's actual, version-checked content, not its input.
- const actual = await this.readRemote(version);
- Object.assign(this.baseline, {
- local,
- remote: version,
- localDigest: localContent.digest,
- remoteDigest: actual.digest,
- });
- this.deps.trace?.("phone-to-computer-complete");
- } else if (remoteChanged && remoteContent) {
- const written = await this.deps.writeLocal(remoteContent.json, local);
- this.check();
- const actual = await this.readLocal(written);
- Object.assign(this.baseline, {
- local: written,
- remote,
- localDigest: actual.digest,
- remoteDigest: remoteContent.digest,
- });
- this.deps.trace?.("computer-to-phone-complete");
- } else {
- Object.assign(this.baseline, {
- local,
- remote,
- localDigest,
- remoteDigest,
- });
- this.deps.trace?.("unchanged-content");
- }
- } catch (error) {
- // Skip this snapshot, not the user's setting. Read again only after a
- // new copy; do not hammer iOS with repeated reads of an unreadable item.
- if (
- local !== undefined &&
- remote !== undefined &&
- skippableContent(error)
- ) {
- this.check();
- Object.assign(this.baseline, {
- local,
- remote,
- // Keep evidence for an unchanged or successfully read peer. Only
- // an unreadable new version loses its digest, never both peers.
- localDigest:
- local === this.baseline.local
- ? this.baseline.localDigest
- : readLocalDigest,
- remoteDigest:
- remote === this.baseline.remote
- ? this.baseline.remoteDigest
- : readRemoteDigest,
- });
- this.deps.trace?.("content-skipped");
- return;
- }
- throw error;
- } finally {
- this.pending = false;
- }
+export class ClipboardSync extends SharedClipboardSync {
+ constructor(deps: ClipboardSyncDeps, baseline?: ClipboardSyncBaseline) {
+ super({ ...deps, digest: clipboardDigest }, baseline);
}
}
diff --git a/apps/mobile/src/remote-desktop/clipboardSyncFailure.ts b/apps/mobile/src/remote-desktop/clipboardSyncFailure.ts
index fcac12e9c6..ae3c2fa960 100644
--- a/apps/mobile/src/remote-desktop/clipboardSyncFailure.ts
+++ b/apps/mobile/src/remote-desktop/clipboardSyncFailure.ts
@@ -1,31 +1,4 @@
-/** Error metadata only: never include clipboard contents in diagnostics. */
-export function clipboardSyncErrorCode(error: unknown): string {
- const value = error instanceof Error ? error.message : String(error);
- const code =
- error && typeof error === "object" && "code" in error
- ? String(error.code)
- : "";
- return (
- (value + " " + code).match(/\b(?:CLIPBOARD|PASTE)_[A-Z_]+\b/)?.[0] ??
- (code + " " + value).match(
- /\b(?:CLIPBOARD|DESKTOP|INVOKE|PASTE|ERR|E)_[A-Z_]+\b/,
- )?.[0] ??
- "UNKNOWN"
- );
-}
-export function clipboardSyncFailure(error: unknown, failures: number) {
- const code = clipboardSyncErrorCode(error);
- // Android uses this exact code for temporary window-focus loss.
- if (
- code !== "CLIPBOARD_NOT_ALLOWED" &&
- /PERMISSION|DENIED|NOT_ALLOWED/.test(code)
- )
- return { code, notice: "clipboardSyncPermission", delay: null };
- if (code === "CLIPBOARD_CONFLICT" || code.endsWith("_CHANGED"))
- return { code, notice: "clipboardSyncConflict", delay: 1500 };
- return {
- code,
- notice: "clipboardSyncFailed",
- delay: Math.min(30000, 1500 * 2 ** Math.min(failures, 5)),
- };
-}
+export {
+ clipboardSyncErrorCode,
+ clipboardSyncFailure,
+} from "@cindy/device-link";
diff --git a/apps/mobile/src/remote-desktop/clipboardTransfer.ts b/apps/mobile/src/remote-desktop/clipboardTransfer.ts
index 5be6e8a2fa..5262efd5b3 100644
--- a/apps/mobile/src/remote-desktop/clipboardTransfer.ts
+++ b/apps/mobile/src/remote-desktop/clipboardTransfer.ts
@@ -1,54 +1,19 @@
import {
- CLIPBOARD_CHUNK_CHARS, CLIPBOARD_MAX_CHARS, parseClipboardContent,
+ transferClipboardContent as transfer,
type RemoteDesktopRequest,
} from "@cindy/device-link";
import { remotePresentation } from "../../modules/cindy-remote-presentation/src";
-
-/** Explicit transfer only. Sequential bounded invokes keep screen traffic responsive. */
export async function transferClipboardContent(
action: "copy" | "paste",
lease: string,
request: <T>(message: RemoteDesktopRequest) => Promise<T>,
check: () => void,
): Promise<void> {
- if (!remotePresentation?.readClipboard || !remotePresentation.writeClipboard)
+ const native = remotePresentation;
+ if (!native?.readClipboard || !native.writeClipboard)
throw new Error("CLIPBOARD_UPGRADE");
- let id: string | undefined;
- try {
- check();
- if (action === "paste") {
- const json = await remotePresentation.readClipboard();
- check();
- parseClipboardContent(json);
- const result = await request<{ id: string }>({ op: "clipboardContent", lease, action: "begin", length: json.length });
- id = result.id;
- for (let offset = 0; offset < json.length; offset += CLIPBOARD_CHUNK_CHARS) {
- check();
- await request({ op: "clipboardContent", lease, action: "write", id, offset, data: json.slice(offset, offset + CLIPBOARD_CHUNK_CHARS) });
- }
- check();
- await request({ op: "clipboardContent", lease, action: "commit", id });
- check();
- } else {
- const result = await request<{ id: string; length: number }>({ op: "clipboardContent", lease, action: "copy" });
- id = result.id;
- if (!Number.isSafeInteger(result.length) || result.length <= 0 || result.length > CLIPBOARD_MAX_CHARS)
- throw new Error("CLIPBOARD_TOO_LONG");
- let json = "";
- while (json.length < result.length) {
- check();
- const { data } = await request<{ data: string }>({ op: "clipboardContent", lease, action: "read", id, offset: json.length });
- if (typeof data !== "string" || !data.length || data.length > CLIPBOARD_CHUNK_CHARS || json.length + data.length > result.length)
- throw new Error("CLIPBOARD_UNSUPPORTED");
- json += data;
- }
- check();
- parseClipboardContent(json);
- await remotePresentation.writeClipboard(json);
- check();
- }
- } finally {
- // Best-effort disposal only; never retry a paste after an uncertain response.
- if (id) void request({ op: "clipboardContent", lease, action: "cancel", id }).catch(() => {});
- }
+ return transfer(action, lease, request, check, {
+ read: native.readClipboard.bind(native),
+ write: native.writeClipboard.bind(native),
+ });
}
diff --git a/docs/design-rules/design-inventory.md b/docs/design-rules/design-inventory.md
index c5f4814a3e..a3b6f1db4f 100644
--- a/docs/design-rules/design-inventory.md
+++ b/docs/design-rules/design-inventory.md
@@ -44,7 +44,7 @@
| `desktop.skillhub.market` | desktop | SkillHub 市场 | hash `/skillhub/market`(SkillhubMarketListView) | InstallTargetPicker, MarketCard, MarketInfoEditDialog, SkillhubMarketListView, SkillhubMarketPreviewPanel, VisibilityEditorDialog | apps/desktop/src/renderer/features/skillhub/SkillhubMarketListView.tsx, apps/desktop/src/renderer/features/skillhub/SkillhubMarketPreviewPanel.tsx, apps/desktop/src/renderer/features/skillhub/components/HomeMarketCard.tsx, apps/desktop/src/renderer/features/skillhub/components/InstallTargetPicker.tsx, apps/desktop/src/renderer/features/skillhub/components/LocalSkillControls.tsx, apps/desktop/src/renderer/features/skillhub/components/MarketCard.tsx, apps/desktop/src/renderer/features/skillhub/components/MarketInfoEditDialog.tsx, apps/desktop/src/renderer/features/skillhub/components/MarketLocalSkills.tsx, apps/desktop/src/renderer/features/skillhub/components/MarketPreviewTree.tsx, apps/desktop/src/renderer/features/skillhub/components/OfficialSkillBadge.tsx, apps/desktop/src/renderer/features/skillhub/components/PlatformTagSelector.tsx, apps/desktop/src/renderer/features/skillhub/components/SkillCategoryFilterBar.tsx, apps/desktop/src/renderer/features/skillhub/components/SkillIcon.tsx, apps/desktop/src/renderer/features/skillhub/components/SkillTagList.tsx, apps/desktop/src/renderer/features/skillhub/components/TeamScopePicker.tsx, apps/desktop/src/renderer/features/skillhub/components/VisibilityEditorDialog.tsx | 53 | 0 | 62 |
| `desktop.window.computer-permission-guide` | desktop | 电脑使用权限引导 | `?view=computer-permission-guide` → ComputerPermissionGuideWindow;backdrop 同文件 | ComputerPermissionBackdrop, ComputerPermissionGuideWindow | apps/desktop/src/main/computer-permission-guide/MacComputerPermissionGuideNativeHost.ts, apps/desktop/src/main/computer-permission-guide/placement.ts, apps/desktop/src/main/computer-permission-guide/request.ts, apps/desktop/src/main/computer-permission-guide/switch-target.ts, apps/desktop/src/main/computer-permission-guide/window.ts, apps/desktop/src/renderer/components/settings/ComputerPermissionGuideWindow.tsx | 7 | 2 | 9 |
| `desktop.window.ghost-panel` | desktop | 插件面板独立窗口 | `?ghostPanelWindow=<id>` → renderer/ghost-panel-window-entry.tsx;hash `/ghost-panel-window` | GhostPanelWindowLayout | apps/desktop/src/main/ghost-panel-window/controller.ts, apps/desktop/src/main/ghost-panel-window/ipc.ts, apps/desktop/src/main/ghost-panel-window/registry.ts, apps/desktop/src/main/ghost-panel-window/settings-store.ts, apps/desktop/src/main/ghost-panel-window/window.ts, apps/desktop/src/renderer/components/layout/GhostPanelWindowLayout.tsx, apps/desktop/src/renderer/ghost-panel-window-entry.tsx, apps/desktop/src/renderer/styles/generated/tokens.css, apps/desktop/src/renderer/styles/globals.css | 63 | 38 | 14 |
-| `desktop.window.remote-desktop` | desktop | 远程桌面独立窗口 | `?remoteDesktopViewer=1` → renderer/remote-desktop-viewer-entry.tsx | Button, ConfirmDialog, FormField, RemoteDesktopViewerWindow, Select | apps/desktop/src/main/remote-desktop-viewer/connection.ts, apps/desktop/src/main/remote-desktop-viewer/registry.ts, apps/desktop/src/main/remote-desktop-viewer/windows.ts, apps/desktop/src/renderer/components/ui/button.tsx, apps/desktop/src/renderer/components/ui/confirm-dialog.tsx, apps/desktop/src/renderer/components/ui/form-field.tsx, apps/desktop/src/renderer/components/ui/select.tsx, apps/desktop/src/renderer/features/remote-desktop/RemoteDesktopViewerWindow.tsx, apps/desktop/src/renderer/features/remote-desktop/viewerWindow.css, apps/desktop/src/renderer/remote-desktop-viewer-entry.tsx, apps/desktop/src/renderer/styles/generated/tokens.css, apps/desktop/src/renderer/styles/globals.css | 103 | 36 | 26 |
+| `desktop.window.remote-desktop` | desktop | 远程桌面独立窗口 | `?remoteDesktopViewer=1` → renderer/remote-desktop-viewer-entry.tsx | Button, ConfirmDialog, FormField, Popover, RemoteDesktopViewerWindow, Select, Switch, Tip | apps/desktop/src/main/remote-desktop-viewer/connection.ts, apps/desktop/src/main/remote-desktop-viewer/credentials.ts, apps/desktop/src/main/remote-desktop-viewer/preferences.ts, apps/desktop/src/main/remote-desktop-viewer/registry.ts, apps/desktop/src/main/remote-desktop-viewer/safety.ts, apps/desktop/src/main/remote-desktop-viewer/windows.ts, apps/desktop/src/renderer/components/ui/button.tsx, apps/desktop/src/renderer/components/ui/confirm-dialog.tsx, apps/desktop/src/renderer/components/ui/form-field.tsx, apps/desktop/src/renderer/components/ui/popover.tsx, apps/desktop/src/renderer/components/ui/select.tsx, apps/desktop/src/renderer/components/ui/switch.tsx, apps/desktop/src/renderer/components/ui/tooltip.tsx, apps/desktop/src/renderer/features/remote-desktop/RemoteDesktopViewerWindow.tsx, apps/desktop/src/renderer/features/remote-desktop/viewerWindow.css, apps/desktop/src/renderer/remote-desktop-viewer-entry.tsx, apps/desktop/src/renderer/styles/generated/tokens.css, apps/desktop/src/renderer/styles/globals.css | 114 | 36 | 34 |
| `desktop.window.resource-usage` | desktop | 资源用量窗口 | `?resourceUsageWindow=1` → renderer/resource-usage-entry.tsx(不走 router.tsx) | ResourceUsageWindowLayout, ResourceUsageWindowRoot | apps/desktop/src/main/resource-usage-window/controller.ts, apps/desktop/src/main/resource-usage-window/ipc.ts, apps/desktop/src/main/resource-usage-window/open-sender.ts, apps/desktop/src/main/resource-usage-window/registry.ts, apps/desktop/src/main/resource-usage-window/window.ts, apps/desktop/src/renderer/components/resource-usage/ResourceUsageWindowLayout.tsx, apps/desktop/src/renderer/resource-usage-entry.tsx, apps/desktop/src/renderer/styles/generated/tokens.css, apps/desktop/src/renderer/styles/globals.css | 63 | 38 | 14 |
| `desktop.window.review-artifact-confirm` | desktop | Review 成果确认窗 | main/reviewer/reviewArtifactConfirmWindow.ts 内联 HTML/CSS(不走 renderer 路由) | reviewArtifactConfirmWindow | apps/desktop/src/main/reviewer/reviewArtifactConfirmWindow.ts | 8 | 18 | 2 |
| `desktop.window.session-drag-preview` | desktop | 会话拖拽预览 | main/session-drag-preview.ts 透明预览窗 | session-drag-preview | apps/desktop/src/main/session-drag-preview.ts | 0 | 1 | 0 |
diff --git a/docs/dev-rules/protocol-compatibility.md b/docs/dev-rules/protocol-compatibility.md
index 78797880e9..aed4cc9481 100644
--- a/docs/dev-rules/protocol-compatibility.md
+++ b/docs/dev-rules/protocol-compatibility.md
@@ -273,3 +273,12 @@ X 快照有请求正文时,按原始 triggerMessageId 排除引用列表中的
Seed 2.1 Pro 按火山方舟官方示例选择 Chat Completions 为 Cindy 的标准接入协议,
依据与全路由覆盖验收见 model-catalog-maintenance.md。
价格、窗口、推理档位不随此次协议补全修改;协议默认开启策略仍保留用户显式覆盖。
+
+### 远程桌面虚拟显示尺寸回执
+
+`viewerDisplay` 成功响应可附加 `viewerDisplayRequest: { width, height }`,回显本次请求。
+`display.width/height` 始终是系统实际逻辑尺寸,用于画面与输入坐标;macOS 可能选择同一比例的较小逻辑模式。
+客户端仅在回执匹配请求、实际尺寸为有效整数且比例一致时接受这种差异,仍校验 lease 与控制状态。
+缺少回执的旧服务端保持原来的精确尺寸判断;显式 `resolution` 模式不放宽。
+旧客户端仍可处理原来成功的精确尺寸响应;系统调整后的尺寸需要控制端和被控端同时更新。
+不修改请求格式、relay、IPC allowlist 或协议版本。
diff --git a/docs/remote-desktop-credentials.md b/docs/remote-desktop-credentials.md
index 1f04630304..1ceb1dfc6f 100644
--- a/docs/remote-desktop-credentials.md
+++ b/docs/remote-desktop-credentials.md
@@ -8,8 +8,9 @@ Mobile places **Security** at the bottom of the remote desktop control panel,
after display settings. Automatic unlock defaults off. Face ID only protects a
saved password; turning automatic unlock off deletes the local saved password.
-Automatic unlock targets iOS controlling macOS or supported Linux lock screens
-(see the Linux integration and acceptance limits below). Android hides the unsupported
+Automatic unlock supports iOS controlling macOS or supported Linux lock screens
+(see the Linux integration and acceptance limits below), and macOS Desktop controlling
+macOS. Android and Windows viewers hide the unsupported
automatic-unlock and biometric settings; its independent lock-on-exit setting
remains available when the host advertises support. This does not add a new server,
identity registry, database migration, or server deployment requirement. The existing
@@ -52,6 +53,20 @@ secrets device-only, with optional current-biometric-enrollment protection. Sett
and pins are scoped to account, region, controller installation and target device;
secrets additionally bind the Mac key fingerprint and OS user record.
+The Desktop controller reuses the native credential transaction in its own signed
+helper instance. AppKit owns password entry; only encrypted packets cross the
+dedicated viewer bridge. Its login Keychain items restrict access to the helper.
+Optional biometric protection wraps the saved password with a Secure Enclave
+key requiring the current biometric enrollment for key agreement. The Keychain
+then contains ciphertext and the hardware-wrapped key representation, never a
+biometric-protected password that can be retrieved using only a UI Boolean.
+This avoids requiring Data Protection Keychain entitlements on the standalone
+helper. Unsupported biometric hardware leaves the setting unavailable.
+
+An explicit exit cancels in-flight native authentication before requesting
+lock-on-exit. Ordinary media retries, display changes and window hiding never
+request a lock. A lock failure remains visible instead of silently closing.
+
Biometric protection changes preserve the pin and replace the secret atomically.
Offline deletion needs no network. Unavailable biometrics do not silently weaken
existing protected items. When an invalidated secret no longer exists, setup can
@@ -72,11 +87,12 @@ identity without an account token or identity service. JS tests cover native
pairing transport, ordinary desktop bypass, offline deletion, UI placement and
focus cancellation.
-First setup on physical devices, Face ID success/cancellation, real locked-Mac unlock,
-Light/Dark visual inspection, and signed production-helper operation while locked
-remain acceptance work. Source tests and signed builds do not establish those facts.
-The earlier repository-wide gate still has recorded Desktop failures outside the
-remote-desktop targeted suites; no release-ready full gate is claimed.
+First setup on physical devices, Face ID/Touch ID success and cancellation, real
+locked-Mac unlock, and signed production-helper operation while locked remain
+acceptance work. Desktop Light/Dark renderer previews use synthetic connections;
+they do not validate Electron, native password UI or a real remote connection.
+Source tests, isolated native storage tests and signed builds do not establish
+those end-to-end facts.
The abandoned companion-server experiment is not required and has not been deployed.
diff --git a/docs/remote-desktop.md b/docs/remote-desktop.md
index 7ca5b7e0d5..078258df20 100644
--- a/docs/remote-desktop.md
+++ b/docs/remote-desktop.md
@@ -2,36 +2,61 @@
## Desktop viewer
+The desktop interaction hierarchy follows a canvas-first approach: device and
+connection status at the left, grouped zoom/fit/actual-size actions, then grouped
+icon-only Display & Sound, Clipboard and Security popovers. These are
+separate, short task panels rather than one scrolling checklist. Local fit never
+changes the host resolution; received audio and host mute have separate controls.
+Unavailable safety options remain visible with an explanation. Confirmed privacy
+and actionable failures remain visible outside the popovers.
+
+Popovers release held remote input, support Escape and outside dismissal, and
+return keyboard focus to their trigger. Switching panels does not restore focus
+to the previous trigger. Clicking the remote canvas while a panel is open only
+dismisses it; it does not send that click to the remote computer. Fullscreen keeps
+the toolbar visible while a panel is open, including its portalled controls.
+
Desktop can open a same-account computer's remote desktop from its device card
in Remote control settings, or the task-list machine menu's Remote desktop submenu.
When the sidebar is grouped by machine, hovering or keyboard-focusing a remote
-machine reveals a desktop shortcut. A debounced, read-only capability check
-distinguishes available desktops from offline, disabled, revoked or unsupported
-targets; unavailable shortcuts show a crossed-out monitor with an explanation.
+machine reveals a desktop shortcut. A read-only capability check runs when an
+eligible device first appears, and is reused until presence, permission, platform
+or version changes. Unavailable shortcuts remain clickable and explain how to
+enable the required permission. Online devices remain listed without tasks.
Hovering never starts desktop capture or takes over another viewer, and clicking
the shortcut does not expand or collapse the machine group.
It opens a clean, independent window with native mouse/keyboard input and a small
toolbar. Reopening the same target focuses its existing window. Full screen,
-view-only/control, display selection, sound, video settings and text
+display selection, sound, video settings and portable
clipboard shortcuts are available. Resolution changes appear only for a capable
host and affect its actual monitor. Ctrl+Alt+Esc releases keyboard focus;
Cmd/Ctrl+W requests closing this viewer, including while it owns keyboard focus.
-The toolbar exit, native window close and close shortcut share a confirmation
-dialog; cancelling keeps the connection and control lease. Confirmation belongs
+Native window close and the close shortcut share a confirmation
+dialog only after a connection is established; cancelling keeps the connection and control lease. Confirmation belongs
to the current window generation and cannot close a later connection.
-While controlling, the local cursor is hidden inside the remote picture even
-when Windows embeds its cursor in the video rather than sending cursor metadata.
-Cursor hiding is scoped to the remote picture, not the system or window focus:
-moving outside it immediately restores the local cursor even if the viewer keeps
-focus. Local toolbar controls and dialogs retain their cursor. View-only mode
-also restores the local cursor inside the picture.
+Desktop uses the local system cursor inside the remote picture, with standard
+shape hints from the host. Pointer size is independent of remote resolution and zoom.
With the picture focused, Cmd+C/V on macOS or Ctrl+C/V on Windows copies selected
-remote text to the local clipboard or pastes local text remotely. Transfers use
+remote content to the local clipboard or pastes local content remotely. Transfers use
the existing authorized Main bridge, are ordered and user-triggered, and report
-failure without reconnecting. These Desktop keyboard shortcuts remain text-only;
-images, files and cut are not bridged by them. The opt-in Mobile clipboard sync
-described below is a separate, foreground-only operation.
+failure without reconnecting. Capable hosts support text, HTML, RTF, URLs and PNG;
+older hosts retain text-only behavior. Arbitrary files and cut are not bridged.
+Manual actions and progress are also available in the settings panel. Optional
+clipboard synchronization reuses Mobile's version, conflict and bounded-transfer
+logic, only while this viewer is focused and holds confirmed control. Contents
+stay in Main/native code and do not cross the viewer Renderer bridge.
+
+The settings panel also supports host privacy screen, host mute, lock-on-exit and
+macOS-to-macOS automatic unlock. Non-secret preferences are scoped to the local
+account and target computer; only explicit overrides are stored. Host effects
+are capability-gated and applied after control is confirmed. Privacy failures
+remain visible ahead of mute or clipboard notices, with explicit retry. Explicit
+exit waits for the requested lock; reconnect and display changes do not lock.
+See [native credential behavior](remote-desktop-credentials.md) for password storage.
+Video quality changes are coalesced while negotiation is pending. The toolbar
+shows receive rate and clears stale network samples, while resolution discovery
+shows loading/failure and offers retry and native-resolution labels.
The shared viewer session marks recovery only when a start is attempted, so an
initial capability-query timeout does not turn a retry against a legacy host into
@@ -978,6 +1003,14 @@ old RTC generations are discarded. Hidden cursors are not drawn. Unsupported
cursor images fall back to the existing position indicator without killing video.
Both Light and Dark use the source raster rather than tinting it.
+Desktop viewers use the local system pointer instead of drawing this raster.
+An optional `shape` hint selects a bounded standard cursor keyword (text, hand,
+resize, etc.); size, DPI and accessibility appearance remain owned by the local
+OS and do not follow the remote screen zoom. Missing, custom or unknown shapes
+fall back to the local arrow. Old viewers ignore the hint and Mobile retains the
+raster path. Desktop view-only mode shows the local arrow rather than a second
+remote pointer overlay.
+
Checked: desktop/mobile types, native compilation, a bounded read-only native
capture returning cursor geometry/raster metadata. Real phone gestures,
application-by-application cursor transitions and sustained frame rate remain
@@ -1043,3 +1076,22 @@ connection. Actual backgrounding releases control unless system PiP is active.
Reconnection retains the last picture but disables input until a fresh lease is
ready. Touch/trackpad, mouse-button visibility and audio preferences are saved
locally. Landscape hides the network status overlay to preserve picture space.
+
+Desktop zoom controls share one group: zoom out, fit to window, actual size (1:1),
+and zoom in. Fit follows the window size; actual size maps one remote logical
+screen unit to one local CSS pixel, independently of video encoding resolution
+and local display density. Actual size stays fixed when the window is resized.
+Clicking actual size also sizes the window to the remote desktop plus toolbar
+and native frame, within the window minimum and the local display work area.
+Fullscreen/maximized windows restore first; oversized desktops remain at 1:1
+and can be panned inside the screen-limited window.
+An overflowing picture can be panned with the middle mouse button or by hovering
+within 28 local pixels of a viewport edge. Edge panning accelerates toward the
+edge, supports diagonal movement, and stops at the desktop bounds, on pointer
+leave or focus loss. Remote hover/drag coordinates follow the moving picture;
+view-only sessions pan locally without sending input. Window maximization/fullscreen
+uses the native window controls.
+Zoom out can go below fit, down to 10% of the smaller of fit and actual size.
+Exposed margins use the same fixed-fit, three-segment ambient canvas as Mobile,
+with 16px blur, 1.12 overscan and 0.72 opacity. Same-aspect desktops also retain
+the backdrop when shrunk; covering the window stops its live redraw loop.
diff --git a/i18n/GLOSSARY.md b/i18n/GLOSSARY.md
index 4d80f911ae..877c9e9c24 100644
--- a/i18n/GLOSSARY.md
+++ b/i18n/GLOSSARY.md
@@ -431,6 +431,10 @@ dev 版登录页区域徽标上的标签值(DESIGN.md §16.3),四语同值
远程桌面传输状态,桌面端沿用手机端现有用语。表示画面不经过中转服务器;直连也可能穿过公网 NAT,不能据此标成内网直连。
+### Display & Sound
+
+Remote desktop panel containing picture and audio settings; icon-only toolbar trigger.
+
### Lock screen control
Windows 远程桌面设置中的系统服务授权能力;不表示支持开机前解密或无人登录。
diff --git a/i18n/glossary.json b/i18n/glossary.json
index 76ef928e13..f766a31f7e 100644
--- a/i18n/glossary.json
+++ b/i18n/glossary.json
@@ -65,6 +65,19 @@
"translations": { "zh-CN": "个人版", "zh-TW": "個人版", "ja": "個人版", "ko": "개인 버전" },
"note": "Cindy Make 生成的本机定制应用成品。可与原版切换并沿用同一份日常数据,与使用独立数据的隔离测试版区分。"
},
+ {
+ "id": "remote-desktop-display-sound",
+ "status": "proposed",
+ "en": "Display & Sound",
+ "translations": {
+ "zh-CN": "影音",
+ "zh-TW": "影音",
+ "ja": "画面とサウンド",
+ "ko": "화면 및 소리"
+ },
+ "checkCase": false,
+ "note": "Remote desktop panel containing picture and audio settings; icon-only toolbar trigger."
+ },
{
"id": "local-session-scope",
"status": "proposed",
diff --git a/packages/device-link/src/__tests__/viewerDisplay.test.ts b/packages/device-link/src/__tests__/viewerDisplay.test.ts
index 5fa65ed8b9..be8d69d39b 100644
--- a/packages/device-link/src/__tests__/viewerDisplay.test.ts
+++ b/packages/device-link/src/__tests__/viewerDisplay.test.ts
@@ -154,3 +154,61 @@ describe("viewer display dimensions", () => {
expect(session.lease?.display.width).toBe(3840);
});
});
+
+const adjustedLease = {
+ lease: "lease",
+ controlling: false,
+ display: { id: "virtual", width: 960, height: 710 },
+ viewerDisplayRequest: { width: 1920, height: 1420 },
+};
+async function adjustedSession(result: unknown) {
+ const session = new RemoteDesktopViewerSession((async (request) => {
+ if (request.op === "capabilities")
+ return { version: 1, enabled: true, displays: [{ id: "1" }] };
+ if (request.op === "start")
+ return {
+ lease: "lease",
+ controlling: false,
+ display: { id: "1", width: 2560, height: 1440 },
+ };
+ if (request.op === "control") return { controlling: request.enabled };
+ return result;
+ }) as DesktopViewerRequest);
+ await session.connect({ isCurrent: () => true });
+ await session.control(true);
+ return session;
+}
+it("accepts acknowledged OS logical geometry and releases control until reacquired", async () => {
+ const session = await adjustedSession(adjustedLease);
+ await expect(session.fitDisplay(1920, 1420)).resolves.toMatchObject({
+ display: adjustedLease.display,
+ controlling: false,
+ });
+ await session.control(true);
+ expect(session.lease?.controlling).toBe(true);
+});
+it.each([
+ { viewerDisplayRequest: undefined },
+ { viewerDisplayRequest: { width: 1920, height: 1080 } },
+ { display: { id: "virtual", width: 960, height: 540 } },
+ { display: { id: "virtual", width: 960.5, height: 710 } },
+ { display: { id: "", width: 960, height: 710 } },
+ { display: { id: "virtual", width: 0, height: 0 } },
+ { lease: "stale" },
+ { controlling: true },
+])(
+ "rejects unacknowledged, invalid or stale adjusted geometry: %j",
+ async (patch) => {
+ const session = await adjustedSession({ ...adjustedLease, ...patch });
+ await expect(session.fitDisplay(1920, 1420)).rejects.toThrow(
+ "INVALID_RESPONSE",
+ );
+ expect(session.lease?.display.id).toBe("1");
+ },
+);
+it("keeps explicit system modes exact even with a virtual-display acknowledgement", async () => {
+ const session = await adjustedSession(adjustedLease);
+ await expect(session.fitDisplay(1920, 1420, false, "mode")).rejects.toThrow(
+ "INVALID_RESPONSE",
+ );
+});
diff --git a/packages/device-link/src/clipboardSync.ts b/packages/device-link/src/clipboardSync.ts
new file mode 100644
index 0000000000..d751974d86
--- /dev/null
+++ b/packages/device-link/src/clipboardSync.ts
@@ -0,0 +1,378 @@
+import {
+ CLIPBOARD_CHUNK_CHARS,
+ CLIPBOARD_MAX_CHARS,
+ parseClipboardContent,
+} from "./remoteClipboard.js";
+import type { RemoteDesktopRequest } from "./remoteDesktop.js";
+import { clipboardSyncErrorCode } from "./clipboardSyncFailure.js";
+
+function skippableContent(error: unknown): boolean {
+ return [
+ "CLIPBOARD_EMPTY",
+ "CLIPBOARD_UNSUPPORTED",
+ "CLIPBOARD_TOO_LONG",
+ ].includes(clipboardSyncErrorCode(error));
+}
+
+export interface LocalClipboardReadCache {
+ version?: string;
+ digest?: string;
+}
+export interface ClipboardSyncDeps {
+ digest(json: string): string;
+ lease: string;
+ request<T>(message: RemoteDesktopRequest): Promise<T>;
+ localVersion(): Promise<string>;
+ readLocal(): Promise<string>;
+ writeLocal(json: string, version: string): Promise<string>;
+ current(): boolean;
+ inline?: boolean;
+ localReadCache?: LocalClipboardReadCache;
+ trace?(stage: string): void;
+}
+export interface ClipboardSyncBaseline {
+ local: string;
+ remote: string;
+ localDigest?: string;
+ remoteDigest?: string;
+}
+
+/** Serial sync: counter changes trigger reads; content changes trigger writes. */
+export class ClipboardSync {
+ private pending = false;
+ constructor(
+ private readonly deps: ClipboardSyncDeps,
+ private readonly baseline: ClipboardSyncBaseline = {
+ local: "",
+ remote: "",
+ },
+ ) {}
+ private check() {
+ if (!this.deps.current()) throw new Error("DESKTOP_LEASE_EXPIRED");
+ }
+ private async remoteVersion() {
+ const { version } = await this.deps.request<{ version: string }>({
+ op: "clipboardVersion",
+ lease: this.deps.lease,
+ });
+ this.check();
+ if (typeof version !== "string" || !/^\d{1,128}$/.test(version))
+ throw new Error("CLIPBOARD_UNSUPPORTED");
+ return version;
+ }
+ private async readLocal(version: string) {
+ this.deps.trace?.("phone-read-start");
+ const json = await this.deps.readLocal();
+ this.check();
+ const digest = this.deps.digest(json);
+ if ((await this.deps.localVersion()) !== version)
+ throw new Error("CLIPBOARD_CHANGED");
+ this.check();
+ this.rememberLocal(version, digest);
+ this.deps.trace?.("phone-read-complete");
+ return { json, digest };
+ }
+ private rememberLocal(version: string, digest: string | undefined) {
+ if (this.deps.localReadCache)
+ Object.assign(this.deps.localReadCache, { version, digest });
+ }
+ private async cancel(id: string) {
+ await this.deps
+ .request({
+ op: "clipboardContent",
+ lease: this.deps.lease,
+ sync: true,
+ action: "cancel",
+ id,
+ })
+ .catch(() => {});
+ }
+ private async readRemote(version: string) {
+ const { lease, request } = this.deps;
+ let id: string | undefined;
+ try {
+ this.deps.trace?.("computer-read-start");
+ const result = await request<{
+ id?: string;
+ length: number;
+ data?: string;
+ }>({
+ op: "clipboardContent",
+ lease,
+ sync: true,
+ version,
+ action: "copy",
+ ...(this.deps.inline ? { inline: true } : {}),
+ });
+ this.check();
+ if (
+ !Number.isSafeInteger(result.length) ||
+ result.length <= 0 ||
+ result.length > CLIPBOARD_MAX_CHARS
+ )
+ throw new Error("CLIPBOARD_TOO_LONG");
+ let json = "";
+ if (this.deps.inline && typeof result.data === "string") {
+ if (
+ result.data.length > CLIPBOARD_CHUNK_CHARS ||
+ result.data.length !== result.length
+ )
+ throw new Error("CLIPBOARD_UNSUPPORTED");
+ json = result.data;
+ } else {
+ if (typeof result.id !== "string" || !result.id)
+ throw new Error("CLIPBOARD_UNSUPPORTED");
+ id = result.id;
+ while (json.length < result.length) {
+ this.check();
+ const part = await request<{ data: string }>({
+ op: "clipboardContent",
+ lease,
+ sync: true,
+ action: "read",
+ id,
+ offset: json.length,
+ });
+ this.check();
+ if (
+ typeof part.data !== "string" ||
+ !part.data.length ||
+ part.data.length > CLIPBOARD_CHUNK_CHARS ||
+ json.length + part.data.length > result.length
+ )
+ throw new Error("CLIPBOARD_UNSUPPORTED");
+ json += part.data;
+ }
+ }
+ const digest = this.deps.digest(json);
+ if ((await this.remoteVersion()) !== version)
+ throw new Error("CLIPBOARD_CHANGED");
+ return { json, digest };
+ } finally {
+ if (id) void this.cancel(id);
+ }
+ }
+ private async writeRemote(json: string, local: string, remote: string) {
+ const { lease, request } = this.deps;
+ let id: string | undefined;
+ try {
+ this.check();
+ let committed: { version: string };
+ if (this.deps.inline && json.length <= CLIPBOARD_CHUNK_CHARS) {
+ if ((await this.deps.localVersion()) !== local)
+ throw new Error("CLIPBOARD_CHANGED");
+ this.check();
+ committed = await request({
+ op: "clipboardContent",
+ lease,
+ sync: true,
+ version: remote,
+ action: "paste",
+ data: json,
+ });
+ } else {
+ const result = await request<{ id: string }>({
+ op: "clipboardContent",
+ lease,
+ sync: true,
+ version: remote,
+ action: "begin",
+ length: json.length,
+ });
+ id = result.id;
+ for (
+ let offset = 0;
+ offset < json.length;
+ offset += CLIPBOARD_CHUNK_CHARS
+ ) {
+ this.check();
+ await request({
+ op: "clipboardContent",
+ lease,
+ sync: true,
+ action: "write",
+ id,
+ offset,
+ data: json.slice(offset, offset + CLIPBOARD_CHUNK_CHARS),
+ });
+ }
+ if ((await this.deps.localVersion()) !== local)
+ throw new Error("CLIPBOARD_CHANGED");
+ this.check();
+ committed = await request({
+ op: "clipboardContent",
+ lease,
+ sync: true,
+ action: "commit",
+ id,
+ });
+ }
+ this.check();
+ if (
+ typeof committed.version !== "string" ||
+ !/^\d{1,128}$/.test(committed.version)
+ )
+ throw new Error("CLIPBOARD_UNSUPPORTED");
+ return committed.version;
+ } finally {
+ if (id) void this.cancel(id);
+ }
+ }
+ async tick(): Promise<void> {
+ if (this.pending) return;
+ this.pending = true;
+ let local: string | undefined;
+ let remote: string | undefined;
+ let readLocalDigest: string | undefined;
+ let readRemoteDigest: string | undefined;
+ try {
+ this.check();
+ local = await this.deps.localVersion();
+ this.check();
+ remote = await this.remoteVersion();
+ if (!this.baseline.local) {
+ let digest: string | undefined;
+ const cache = this.deps.localReadCache;
+ if (cache?.version === local) {
+ digest = cache.digest;
+ } else {
+ // Keep the proven initial native permission read. Reuse it only while
+ // the phone pasteboard version remains exactly the same.
+ try {
+ digest = (await this.readLocal(local)).digest;
+ } catch (error) {
+ if (!skippableContent(error)) throw error;
+ this.deps.trace?.("content-skipped");
+ if ((await this.deps.localVersion()) !== local)
+ throw new Error("CLIPBOARD_CHANGED");
+ }
+ }
+ // Establish both content baselines without writing either clipboard.
+ // Counter-only rewrites must not import contents predating opt-in.
+ let remoteDigest: string | undefined;
+ try {
+ remoteDigest = (await this.readRemote(remote)).digest;
+ } catch (error) {
+ if (!skippableContent(error)) throw error;
+ this.deps.trace?.("content-skipped");
+ if ((await this.remoteVersion()) !== remote)
+ throw new Error("CLIPBOARD_CHANGED");
+ }
+ if ((await this.deps.localVersion()) !== local)
+ throw new Error("CLIPBOARD_CHANGED");
+ this.check();
+ this.rememberLocal(local, digest);
+ Object.assign(this.baseline, {
+ local,
+ remote,
+ localDigest: digest,
+ remoteDigest,
+ });
+ return;
+ }
+ let localChanged = local !== this.baseline.local;
+ let remoteChanged = remote !== this.baseline.remote;
+ if (!localChanged && !remoteChanged) return;
+ const localContent = localChanged
+ ? await this.readLocal(local).then((content) => {
+ readLocalDigest = content.digest;
+ return content;
+ })
+ : undefined;
+ const remoteContent = remoteChanged
+ ? await this.readRemote(remote).then((content) => {
+ readRemoteDigest = content.digest;
+ return content;
+ })
+ : undefined;
+ this.check();
+ if ((await this.deps.localVersion()) !== local)
+ throw new Error("CLIPBOARD_CHANGED");
+ this.check();
+ if (localContent && localContent.digest === this.baseline.localDigest)
+ localChanged = false;
+ if (remoteContent && remoteContent.digest === this.baseline.remoteDigest)
+ remoteChanged = false;
+ const localDigest = localContent?.digest ?? this.baseline.localDigest;
+ const remoteDigest = remoteContent?.digest ?? this.baseline.remoteDigest;
+ if (localDigest && localDigest === remoteDigest) {
+ localChanged = false;
+ remoteChanged = false;
+ }
+ if (localChanged && remoteChanged) {
+ Object.assign(this.baseline, {
+ local,
+ remote,
+ localDigest,
+ remoteDigest,
+ });
+ throw new Error("CLIPBOARD_CONFLICT");
+ }
+ if (localChanged && localContent) {
+ const version = await this.writeRemote(
+ localContent.json,
+ local,
+ remote,
+ );
+ // Native clipboards may normalize or drop portable representations.
+ // Record the destination's actual, version-checked content, not its input.
+ const actual = await this.readRemote(version);
+ Object.assign(this.baseline, {
+ local,
+ remote: version,
+ localDigest: localContent.digest,
+ remoteDigest: actual.digest,
+ });
+ this.deps.trace?.("phone-to-computer-complete");
+ } else if (remoteChanged && remoteContent) {
+ const written = await this.deps.writeLocal(remoteContent.json, local);
+ this.check();
+ const actual = await this.readLocal(written);
+ Object.assign(this.baseline, {
+ local: written,
+ remote,
+ localDigest: actual.digest,
+ remoteDigest: remoteContent.digest,
+ });
+ this.deps.trace?.("computer-to-phone-complete");
+ } else {
+ Object.assign(this.baseline, {
+ local,
+ remote,
+ localDigest,
+ remoteDigest,
+ });
+ this.deps.trace?.("unchanged-content");
+ }
+ } catch (error) {
+ // Skip this snapshot, not the user's setting. Read again only after a
+ // new copy; do not hammer iOS with repeated reads of an unreadable item.
+ if (
+ local !== undefined &&
+ remote !== undefined &&
+ skippableContent(error)
+ ) {
+ this.check();
+ Object.assign(this.baseline, {
+ local,
+ remote,
+ // Keep evidence for an unchanged or successfully read peer. Only
+ // an unreadable new version loses its digest, never both peers.
+ localDigest:
+ local === this.baseline.local
+ ? this.baseline.localDigest
+ : readLocalDigest,
+ remoteDigest:
+ remote === this.baseline.remote
+ ? this.baseline.remoteDigest
+ : readRemoteDigest,
+ });
+ this.deps.trace?.("content-skipped");
+ return;
+ }
+ throw error;
+ } finally {
+ this.pending = false;
+ }
+ }
+}
diff --git a/packages/device-link/src/clipboardSyncFailure.ts b/packages/device-link/src/clipboardSyncFailure.ts
new file mode 100644
index 0000000000..fcac12e9c6
--- /dev/null
+++ b/packages/device-link/src/clipboardSyncFailure.ts
@@ -0,0 +1,31 @@
+/** Error metadata only: never include clipboard contents in diagnostics. */
+export function clipboardSyncErrorCode(error: unknown): string {
+ const value = error instanceof Error ? error.message : String(error);
+ const code =
+ error && typeof error === "object" && "code" in error
+ ? String(error.code)
+ : "";
+ return (
+ (value + " " + code).match(/\b(?:CLIPBOARD|PASTE)_[A-Z_]+\b/)?.[0] ??
+ (code + " " + value).match(
+ /\b(?:CLIPBOARD|DESKTOP|INVOKE|PASTE|ERR|E)_[A-Z_]+\b/,
+ )?.[0] ??
+ "UNKNOWN"
+ );
+}
+export function clipboardSyncFailure(error: unknown, failures: number) {
+ const code = clipboardSyncErrorCode(error);
+ // Android uses this exact code for temporary window-focus loss.
+ if (
+ code !== "CLIPBOARD_NOT_ALLOWED" &&
+ /PERMISSION|DENIED|NOT_ALLOWED/.test(code)
+ )
+ return { code, notice: "clipboardSyncPermission", delay: null };
+ if (code === "CLIPBOARD_CONFLICT" || code.endsWith("_CHANGED"))
+ return { code, notice: "clipboardSyncConflict", delay: 1500 };
+ return {
+ code,
+ notice: "clipboardSyncFailed",
+ delay: Math.min(30000, 1500 * 2 ** Math.min(failures, 5)),
+ };
+}
diff --git a/packages/device-link/src/clipboardTransfer.ts b/packages/device-link/src/clipboardTransfer.ts
new file mode 100644
index 0000000000..1a945f9123
--- /dev/null
+++ b/packages/device-link/src/clipboardTransfer.ts
@@ -0,0 +1,108 @@
+import {
+ CLIPBOARD_CHUNK_CHARS,
+ CLIPBOARD_MAX_CHARS,
+ parseClipboardContent,
+} from "./remoteClipboard.js";
+import type { RemoteDesktopRequest } from "./remoteDesktop.js";
+
+/** Explicit transfer only. Sequential bounded invokes keep screen traffic responsive. */
+export async function transferClipboardContent(
+ action: "copy" | "paste",
+ lease: string,
+ request: <T>(message: RemoteDesktopRequest) => Promise<T>,
+ check: () => void,
+ clipboard: {
+ read(): Promise<string>;
+ write(json: string): Promise<void>;
+ progress?(done: number, total: number): void;
+ },
+): Promise<void> {
+ let id: string | undefined;
+ try {
+ check();
+ if (action === "paste") {
+ const json = await clipboard.read();
+ check();
+ parseClipboardContent(json);
+ const result = await request<{ id: string }>({
+ op: "clipboardContent",
+ lease,
+ action: "begin",
+ length: json.length,
+ });
+ id = result.id;
+ if (typeof id !== "string" || !id)
+ throw new Error("CLIPBOARD_UNSUPPORTED");
+ for (
+ let offset = 0;
+ offset < json.length;
+ offset += CLIPBOARD_CHUNK_CHARS
+ ) {
+ check();
+ await request({
+ op: "clipboardContent",
+ lease,
+ action: "write",
+ id,
+ offset,
+ data: json.slice(offset, offset + CLIPBOARD_CHUNK_CHARS),
+ });
+ clipboard.progress?.(
+ Math.min(json.length, offset + CLIPBOARD_CHUNK_CHARS),
+ json.length,
+ );
+ }
+ check();
+ await request({ op: "clipboardContent", lease, action: "commit", id });
+ check();
+ } else {
+ const result = await request<{ id: string; length: number }>({
+ op: "clipboardContent",
+ lease,
+ action: "copy",
+ });
+ id = result.id;
+ if (typeof id !== "string" || !id)
+ throw new Error("CLIPBOARD_UNSUPPORTED");
+ if (
+ !Number.isSafeInteger(result.length) ||
+ result.length <= 0 ||
+ result.length > CLIPBOARD_MAX_CHARS
+ )
+ throw new Error("CLIPBOARD_TOO_LONG");
+ let json = "";
+ while (json.length < result.length) {
+ check();
+ const { data } = await request<{ data: string }>({
+ op: "clipboardContent",
+ lease,
+ action: "read",
+ id,
+ offset: json.length,
+ });
+ if (
+ typeof data !== "string" ||
+ !data.length ||
+ data.length > CLIPBOARD_CHUNK_CHARS ||
+ json.length + data.length > result.length
+ )
+ throw new Error("CLIPBOARD_UNSUPPORTED");
+ json += data;
+ clipboard.progress?.(json.length, result.length);
+ }
+ check();
+ parseClipboardContent(json);
+ await clipboard.write(json);
+ check();
+ }
+ } finally {
+ // Best-effort disposal only; never retry a paste after an uncertain response.
+ if (id)
+ void request({
+ op: "clipboardContent",
+ lease,
+ action: "cancel",
+ id,
+ }).catch(() => {});
+ }
+}
diff --git a/packages/device-link/src/index.ts b/packages/device-link/src/index.ts
index 69973273dd..f8c791d0d4 100644
--- a/packages/device-link/src/index.ts
+++ b/packages/device-link/src/index.ts
@@ -33,3 +33,7 @@ export * from "./filePeerRuntime.js";
export { FILE_PEER_RUNTIME_SOURCE } from "./filePeerRuntimeSource.js";
export * from "./modelFavorites.js";
+
+export * from "./clipboardSync.js";
+export * from "./clipboardSyncFailure.js";
+export * from "./clipboardTransfer.js";
diff --git a/packages/device-link/src/remoteCursor.ts b/packages/device-link/src/remoteCursor.ts
index 1a23410388..646c29c58a 100644
--- a/packages/device-link/src/remoteCursor.ts
+++ b/packages/device-link/src/remoteCursor.ts
@@ -8,16 +8,35 @@ export interface RemoteDesktopCursor {
hotX: number;
hotY: number;
png: string;
+ /** Optional native cursor hint. Unknown hints fall back to the local arrow. */
+ shape?: string;
}
-export interface RemoteDesktopCursorFrame { jpeg: string; cursor: RemoteDesktopCursor | null }
-export function isRemoteDesktopCursor(value: unknown): value is RemoteDesktopCursor {
+export interface RemoteDesktopCursorFrame {
+ jpeg: string;
+ cursor: RemoteDesktopCursor | null;
+}
+export function isRemoteDesktopCursor(
+ value: unknown,
+): value is RemoteDesktopCursor {
if (!value || typeof value !== "object") return false;
const v = value as RemoteDesktopCursor;
- return typeof v.visible === "boolean" &&
+ return (
+ typeof v.visible === "boolean" &&
[v.x, v.y, v.width, v.height, v.hotX, v.hotY].every(Number.isFinite) &&
- v.x >= 0 && v.x <= 1 && v.y >= 0 && v.y <= 1 &&
- v.width > 0 && v.width <= 256 && v.height > 0 && v.height <= 256 &&
- v.hotX >= 0 && v.hotX <= v.width && v.hotY >= 0 && v.hotY <= v.height &&
- typeof v.png === "string" && v.png.length <= 65536 &&
- /^iVBORw0KGgo[A-Za-z0-9+/]*={0,2}$/.test(v.png);
+ v.x >= 0 &&
+ v.x <= 1 &&
+ v.y >= 0 &&
+ v.y <= 1 &&
+ v.width > 0 &&
+ v.width <= 256 &&
+ v.height > 0 &&
+ v.height <= 256 &&
+ v.hotX >= 0 &&
+ v.hotX <= v.width &&
+ v.hotY >= 0 &&
+ v.hotY <= v.height &&
+ typeof v.png === "string" &&
+ v.png.length <= 65536 &&
+ /^iVBORw0KGgo[A-Za-z0-9+/]*={0,2}$/.test(v.png)
+ );
}
diff --git a/packages/device-link/src/remoteDesktop.ts b/packages/device-link/src/remoteDesktop.ts
index fa7be668d7..2a81e965de 100644
--- a/packages/device-link/src/remoteDesktop.ts
+++ b/packages/device-link/src/remoteDesktop.ts
@@ -176,6 +176,8 @@ export interface RemoteDesktopLease {
lease: string;
display: RemoteDesktopDisplay;
controlling: boolean;
+ /** Acknowledges the requested virtual mode when OS logical geometry differs. */
+ viewerDisplayRequest?: { width: number; height: number };
}
export type RemoteDesktopRequest =
| { op: "windowAction"; lease: string; action: "list" | "desktop" }
diff --git a/packages/device-link/src/remoteDesktopViewerSession.ts b/packages/device-link/src/remoteDesktopViewerSession.ts
index 221e2939f6..0af41f0c38 100644
--- a/packages/device-link/src/remoteDesktopViewerSession.ts
+++ b/packages/device-link/src/remoteDesktopViewerSession.ts
@@ -188,10 +188,26 @@ export class RemoteDesktopViewerSession {
try {
const result = await operation;
check();
+ const adjustedViewerDisplay =
+ !modeId &&
+ !restore &&
+ result.viewerDisplayRequest?.width === width &&
+ result.viewerDisplayRequest?.height === height &&
+ Number.isSafeInteger(result.display?.width) &&
+ result.display.width > 0 &&
+ result.display.width <= 4096 &&
+ Number.isSafeInteger(result.display?.height) &&
+ result.display.height > 0 &&
+ result.display.height <= 4096 &&
+ Math.abs(
+ result.display.width * height - result.display.height * width,
+ ) <= Math.max(width, height);
if (
result.lease !== lease.lease ||
typeof result.display?.id !== "string" ||
+ result.display.id.length === 0 ||
(!restore &&
+ !adjustedViewerDisplay &&
(result.display.width !== width ||
result.display.height !== height)) ||
!Number.isFinite(result.display.width) ||
diff --git a/packages/maker-shared/src/remote-desktop/viewerRuntime.js b/packages/maker-shared/src/remote-desktop/viewerRuntime.js
index 003da4690d..e7d05a957f 100644
--- a/packages/maker-shared/src/remote-desktop/viewerRuntime.js
+++ b/packages/maker-shared/src/remote-desktop/viewerRuntime.js
@@ -106,9 +106,7 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {
image = find("image"),
video = find("video"),
cursor = find("cursor");
- // Ambient three-segment backdrop (mobile markup only; desktop keeps its
- // solid surface because its stage has no #bg subtree, so every entry may
- // be missing and all backdrop work below must tolerate that).
+ // Shared ambient backdrop. Minimal viewer embeds may omit its subtree.
const bg = find("bg") || null,
bgCanvas = find("bg-canvas") || null,
bgContext = bgCanvas ? bgCanvas.getContext("2d") : null;
@@ -116,6 +114,12 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {
dh = 1080,
viewerSized = false,
zoom = 1,
+ desktopScale = null,
+ reportedScaleMode = null,
+ desktopPan = null,
+ edgePointer = null,
+ edgeFrame = null,
+ edgeTime = 0,
fx = 0.5,
fy = 0.5,
mode = "pointer",
@@ -274,16 +278,23 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {
Math.max(0, (viewportHeight() - height) / 2),
));
const layout = () => {
- const r = transform(
- viewportWidth(),
- viewportHeight(),
- dw,
- dh,
- zoom,
- fx,
- fy,
- fillHeight && !viewerSized,
- );
+ const r =
+ config.desktop && desktopScale !== null
+ ? {
+ scale: desktopScale,
+ width: dw * desktopScale,
+ height: dh * desktopScale,
+ }
+ : transform(
+ viewportWidth(),
+ viewportHeight(),
+ dw,
+ dh,
+ zoom,
+ fx,
+ fy,
+ fillHeight && !viewerSized,
+ );
return {
...r,
x: viewportLeft() + viewportWidth() / 2 - fx * r.width,
@@ -295,17 +306,18 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {
// to fill the remaining space, horizontally or vertically as needed.
// Zoom and pan never move the backdrop: where the picture covers it, the
// opaque picture simply occludes it, so input mapping stays uniform. Returns
- // null when the fitted picture already covers the stage (nothing to fill).
+ // null when the fitted picture already covers the stage, unless a Desktop
+ // viewer has shrunk its foreground below fit and still needs the backdrop.
const bgEdgeFraction = 0.05;
const bgCenterFraction = 1 - 2 * bgEdgeFraction;
- function backgroundSegments(vw, vh, dw, dh, fill) {
+ function backgroundSegments(vw, vh, dw, dh, fill, includeFitted = false) {
if (!(vw > 0 && vh > 0 && dw > 0 && dh > 0)) return null;
const s = fill
? vh / Math.max(1, dh)
: Math.min(vw / dw, vh / Math.max(1, dh));
const w = dw * s,
h = dh * s;
- if (w < vw) {
+ if (w < vw || (includeFitted && w === vw && h === vh)) {
const centerW = w * bgCenterFraction,
centerX = vw / 2 - centerW / 2,
sideW = centerX,
@@ -329,7 +341,16 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {
if (!bg || !bgCanvas || !bgContext) return;
const vw = stage.clientWidth,
vh = stage.clientHeight;
- const seg = backgroundSegments(vw, vh, dw, dh, fillHeight);
+ const picture = config.desktop ? layout() : null;
+ const coversStage =
+ picture &&
+ picture.x <= 0 &&
+ picture.y <= 0 &&
+ picture.x + picture.width >= vw &&
+ picture.y + picture.height >= vh;
+ const seg = coversStage
+ ? null
+ : backgroundSegments(vw, vh, dw, dh, fillHeight, config.desktop);
if (!seg) {
bg.style.display = "none";
bgRects = null;
@@ -627,6 +648,18 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {
width: r.width,
height: r.height,
});
+ if (config.desktop && epoch) {
+ const scaleMode =
+ desktopScale === null
+ ? "fit"
+ : desktopScale === 1
+ ? "actual"
+ : "custom";
+ if (reportedScaleMode !== scaleMode) {
+ reportedScaleMode = scaleMode;
+ post({ type: "scaleMode", mode: scaleMode });
+ }
+ }
const touchFeedback = control && mode === "touch" && touchCursorVisible;
const cursorX = mode === "touch" && touchCursor ? touchCursor.x : cx;
const cursorY = mode === "touch" && touchCursor ? touchCursor.y : cy;
@@ -674,7 +707,36 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {
? "block"
: "none";
}
- if (config.desktop) stage.style.cursor = control ? "none" : "default";
+ if (config.desktop) {
+ // Let the local OS own pointer size, DPI and accessibility preferences.
+ // Never use a remote URL or bitmap as the desktop's native cursor.
+ cursor.style.display = "none";
+ const shape = remoteCursor?.shape;
+ stage.style.cursor =
+ control &&
+ [
+ "default",
+ "text",
+ "vertical-text",
+ "pointer",
+ "crosshair",
+ "grab",
+ "grabbing",
+ "ew-resize",
+ "ns-resize",
+ "nwse-resize",
+ "nesw-resize",
+ "move",
+ "not-allowed",
+ "copy",
+ "alias",
+ "wait",
+ "progress",
+ "help",
+ ].includes(shape)
+ ? shape
+ : "default";
+ }
}
// Literal source is required: Hermes function.toString() yields bytecode.
function validCursor(v) {
@@ -790,6 +852,8 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {
}, 33);
cleanups.push(() => clearInterval(inputTimer));
function release() {
+ stopEdgePan();
+ desktopPan = null;
deferredClipboardModifier = null;
clearTimeout(hold);
if (gestureFrame !== null) cancelAnimationFrame(gestureFrame);
@@ -917,6 +981,82 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {
const bounds = stage.getBoundingClientRect();
return { x: e.clientX - bounds.left, y: e.clientY - bounds.top };
};
+ function stopEdgePan() {
+ if (edgeFrame !== null) cancelAnimationFrame(edgeFrame);
+ edgeFrame = null;
+ edgePointer = null;
+ }
+ function edgePanTick(now) {
+ edgeFrame = null;
+ if (!edgePointer || desktopPan || !epoch) return;
+ const r = layout(),
+ vw = viewportWidth(),
+ vh = viewportHeight();
+ const p = edgePointer;
+ const dt = Math.min(32, Math.max(0, now - edgeTime)) / 1000;
+ edgeTime = now;
+ // rAF timestamps describe the frame start and can precede the pointer
+ // handler's performance.now(). A zero-time first frame is not a boundary.
+ if (dt === 0) {
+ edgeFrame = requestAnimationFrame(edgePanTick);
+ return;
+ }
+ // A narrow edge band accelerates smoothly to 720 local pixels per second.
+ const velocity = (position, extent) => {
+ const band = Math.min(28, extent / 4);
+ return position < band
+ ? -(1 - position / band)
+ : position > extent - band
+ ? (position - extent + band) / band
+ : 0;
+ };
+ const x =
+ r.width > vw
+ ? bounded(
+ r.x - velocity(p.x - viewportLeft(), vw) * 720 * dt,
+ viewportLeft() + vw - r.width,
+ viewportLeft(),
+ )
+ : r.x;
+ const y =
+ r.height > vh
+ ? bounded(r.y - velocity(p.y, vh) * 720 * dt, vh - r.height, 0)
+ : r.y;
+ if (Math.abs(x - r.x) < 0.001 && Math.abs(y - r.y) < 0.001) return;
+ stopPanAnimation();
+ manualViewMoved = true;
+ place(x, y, r);
+ // Keep remote hover/drag attached to the local pointer as the picture moves.
+ if (control) {
+ const next = point(p);
+ cx = next.x;
+ cy = next.y;
+ queue({ kind: "move", x: cx, y: cy });
+ }
+ render();
+ edgeFrame = requestAnimationFrame(edgePanTick);
+ }
+ function trackEdgePointer(e) {
+ const p = desktopPoint(e);
+ if (
+ !epoch ||
+ desktopPan ||
+ p.x < viewportLeft() ||
+ p.x > viewportLeft() + viewportWidth() ||
+ p.y < 0 ||
+ p.y > viewportHeight() ||
+ !insideDesktop(p)
+ ) {
+ stopEdgePan();
+ return;
+ }
+ edgePointer = p;
+ if (edgeFrame === null) {
+ edgeTime = performance.now();
+ edgeFrame = requestAnimationFrame(edgePanTick);
+ }
+ }
+ listen(stage, "pointerleave", stopEdgePan);
function mouse(e, down) {
if (!control || ![0, 1, 2].includes(e.button)) return;
const local = desktopPoint(e);
@@ -1044,6 +1184,22 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {
listen(stage, "pointerdown", (e) => {
if (config.desktop) {
e.preventDefault();
+ if (
+ e.button === 1 &&
+ (layout().width > viewportWidth() || layout().height > viewportHeight())
+ ) {
+ stopEdgePan();
+ stopPanAnimation();
+ desktopPan = {
+ id: e.pointerId,
+ x: e.clientX,
+ y: e.clientY,
+ start: e,
+ moved: false,
+ };
+ stage.setPointerCapture(e.pointerId);
+ return;
+ }
mouse(e, true);
return;
}
@@ -1092,6 +1248,17 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {
});
listen(stage, "pointermove", (e) => {
if (config.desktop) {
+ if (desktopPan?.id === e.pointerId) {
+ const dx = e.clientX - desktopPan.x,
+ dy = e.clientY - desktopPan.y;
+ if (!desktopPan.moved && Math.hypot(dx, dy) < 3) return;
+ desktopPan.moved = true;
+ desktopPan.x = e.clientX;
+ desktopPan.y = e.clientY;
+ pan(dx, dy);
+ return;
+ }
+ trackEdgePointer(e);
if (!control) return;
const local = desktopPoint(e);
if (!heldMouse.size && !insideDesktop(local)) return;
@@ -1138,6 +1305,16 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {
});
function up(e) {
if (config.desktop) {
+ if (desktopPan?.id === e.pointerId) {
+ const gesture = desktopPan;
+ desktopPan = null;
+ if (gesture.moved) settlePan();
+ else {
+ mouse(gesture.start, true);
+ mouse(e, false);
+ }
+ return;
+ }
mouse(e, false);
return;
}
@@ -1182,6 +1359,7 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {
settlePan();
});
listen(stage, "lostpointercapture", () => {
+ desktopPan = null;
if (config.desktop && heldMouse.size) release();
});
if (config.desktop)
@@ -1814,6 +1992,7 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {
stopPanAnimation();
viewerSized = message.restore !== true;
zoom = 1;
+ desktopScale = null;
fx = fy = 0.5;
followRest = null;
dw = message.width;
@@ -2005,6 +2184,13 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {
case "init":
nativeVideoActive = false;
image.style.visibility = "visible";
+ desktopScale = null;
+ reportedScaleMode = null;
+ if (config.desktop) {
+ zoom = 1;
+ fx = 0.5;
+ fy = 0.5;
+ }
viewerSized = false;
followRest = null;
cursorNeedsEntry = true;
@@ -2090,7 +2276,48 @@ export function mountRemoteDesktopViewer(root, postMessage, config) {
mode = message.mode;
render();
break;
+ case "zoom":
+ stopEdgePan();
+ if (
+ !config.desktop ||
+ !Number.isFinite(message.factor) ||
+ message.factor <= 0
+ )
+ break;
+ followRest = null;
+ cursorNeedsEntry = true;
+ manualViewMoved = true;
+ stopPanAnimation();
+ const fitScale = Math.min(viewportWidth() / dw, viewportHeight() / dh);
+ // Include actual size even when it lies outside the fit-relative range.
+ desktopScale = Math.max(
+ Math.min(fitScale, 1) * 0.1,
+ Math.min(Math.max(5 * fitScale, 5), layout().scale * message.factor),
+ );
+ if (Math.abs(desktopScale - fitScale) < 1e-9) desktopScale = null;
+ if (desktopScale === null) {
+ fx = 0.5;
+ fy = 0.5;
+ manualViewMoved = false;
+ }
+ render();
+ settlePan();
+ break;
+ case "actualSize":
+ stopEdgePan();
+ if (!config.desktop) break;
+ followRest = null;
+ cursorNeedsEntry = true;
+ manualViewMoved = true;
+ stopPanAnimation();
+ desktopScale = 1;
+ fx = 0.5;
+ fy = 0.5;
+ render();
+ break;
case "fit":
+ stopEdgePan();
+ desktopScale = null;
followRest = null;
cursorNeedsEntry = true;
manualViewMoved = false;
diff --git a/packages/maker-shared/src/remote-desktop/viewerSource.ts b/packages/maker-shared/src/remote-desktop/viewerSource.ts
index dfa98f522c..f44e64d716 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 let nativeVideoActive = false;\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 // Ambient three-segment backdrop (mobile markup only; desktop keeps its\n // solid surface because its stage has no #bg subtree, so every entry may\n // be missing and all backdrop work below must tolerate that).\n const bg = find(\"bg\") || null,\n bgCanvas = find(\"bg-canvas\") || null,\n bgContext = bgCanvas ? bgCanvas.getContext(\"2d\") : null;\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 let nativeMouseControls = false;\n const mouseControlHandlers = new Map();\n function listenMouse(el, type, handler) {\n listen(el, type, handler);\n mouseControlHandlers.set(el.id + \":\" + type, handler);\n }\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 viewportTopInset = 0,\n keyboardViewportInset = 0,\n portraitKeyboardTopInset = 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 usableHeight = () =>\n Math.max(1, stage.clientHeight - viewportTopInset - viewportBottomInset);\n const viewportHeight = () =>\n viewerSized ? usableHeight() : Math.max(1, stage.clientHeight);\n const viewportWidth = () =>\n Math.max(\n 1,\n stage.clientWidth -\n viewportLeft() -\n (fillHeight ? viewportRightInset : 0),\n );\n // Center between the measured top controls and the keyboard edge (stage bottom).\n // The usable center is (topInset + stageHeight) / 2, so shift by topInset / 2.\n // Keep the fitted size and cap the shift when the desktop is too tall to fit.\n const verticalOffset = (height) =>\n (viewerSized ? viewportTopInset : 0) +\n (fillHeight\n ? 0\n : Math.min(\n Math.max(\n 0,\n portraitKeyboardTopInset - (viewerSized ? viewportTopInset : 0),\n ) / 2,\n Math.max(0, (viewportHeight() - height) / 2),\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 + verticalOffset(r.height) - fy * r.height,\n };\n };\n // The backdrop uses 5% / 90% / 5% source segments. The middle 90%\n // keeps the fitted picture's scale; each outer 5% stretches uniformly\n // to fill the remaining space, horizontally or vertically as needed.\n // Zoom and pan never move the backdrop: where the picture covers it, the\n // opaque picture simply occludes it, so input mapping stays uniform. Returns\n // null when the fitted picture already covers the stage (nothing to fill).\n const bgEdgeFraction = 0.05;\n const bgCenterFraction = 1 - 2 * bgEdgeFraction;\n function backgroundSegments(vw, vh, dw, dh, fill) {\n if (!(vw > 0 && vh > 0 && dw > 0 && dh > 0)) return null;\n const s = fill\n ? vh / Math.max(1, dh)\n : Math.min(vw / dw, vh / Math.max(1, dh));\n const w = dw * s,\n h = dh * s;\n if (w < vw) {\n const centerW = w * bgCenterFraction,\n centerX = vw / 2 - centerW / 2,\n sideW = centerX,\n top = (vh - h) / 2;\n return { axis: \"x\", s, w, h, top, centerW, centerX, sideW };\n }\n if (h < vh) {\n const centerH = h * bgCenterFraction,\n centerY = vh / 2 - centerH / 2,\n sideH = centerY,\n left = (vw - w) / 2;\n return { axis: \"y\", s, w, h, left, centerH, centerY, sideH };\n }\n return null;\n }\n function paintBackground() {\n if (nativeVideoActive) {\n if (bg) bg.style.display = \"none\";\n return;\n }\n if (!bg || !bgCanvas || !bgContext) return;\n const vw = stage.clientWidth,\n vh = stage.clientHeight;\n const seg = backgroundSegments(vw, vh, dw, dh, fillHeight);\n if (!seg) {\n bg.style.display = \"none\";\n bgRects = null;\n stopBackgroundLoop();\n return;\n }\n bg.style.display = \"block\";\n bgAxis = seg.axis;\n // The ambient layer also fills the notch and the area beneath native\n // controls. Only the interactive picture respects viewport insets.\n const originX = 0;\n // Source segments are resolved at draw time from the live source dimensions;\n // these dest rects only encode where each segment lands on the stage.\n bgRects =\n seg.axis === \"x\"\n ? [\n {\n x: originX,\n y: seg.top,\n w: seg.sideW,\n h: seg.h,\n },\n {\n x: originX + seg.centerX,\n y: seg.top,\n w: seg.centerW,\n h: seg.h,\n },\n {\n x: originX + seg.centerX + seg.centerW,\n y: seg.top,\n w: seg.sideW,\n h: seg.h,\n },\n ]\n : [\n {\n x: seg.left,\n y: 0,\n w: seg.w,\n h: seg.sideH,\n },\n {\n x: seg.left,\n y: seg.centerY,\n w: seg.w,\n h: seg.centerH,\n },\n {\n x: seg.left,\n y: seg.centerY + seg.centerH,\n w: seg.w,\n h: seg.sideH,\n },\n ];\n // Keep the backing store at stage resolution (capped) so the stretched\n // edges stay crisp without oversized allocations.\n bgDpr = Math.min(2, window.devicePixelRatio || 1);\n const bw = Math.round(stage.clientWidth * bgDpr),\n bh = Math.round(vh * bgDpr);\n if (bgCanvas.width !== bw || bgCanvas.height !== bh) {\n bgCanvas.width = bw;\n bgCanvas.height = bh;\n }\n scheduleBackgroundDraw();\n startBackgroundLoop();\n }\n // One canvas, three draws: each source segment stretches into whatever bar\n // the picture leaves open. The live video feeds it once presented; until\n // then the decoded JPEG frame does, so the backdrop always matches the\n // picture's own pixels and scale in the center segment.\n let bgRects = null,\n bgAxis = \"x\",\n bgDpr = 1,\n bgDrawFrame = null,\n bgLoopFrame = null;\n function drawBackground() {\n if (!bgContext || !bgRects) return;\n const useVideo =\n videoPresented && video.videoWidth > 0 && video.videoHeight > 0;\n const src = useVideo ? video : image;\n // drawImage source rects live in the source's own pixel space, which can\n // differ from dw/dh; fractions of the real bitmap keep every segment lined\n // up with what the picture element actually shows.\n const sw = useVideo ? video.videoWidth : image.naturalWidth;\n const sh = useVideo ? video.videoHeight : image.naturalHeight;\n // Cursor updates can schedule a repaint while the next JPEG is loading.\n // Keep the last backdrop until the replacement has drawable pixels.\n if ((!useVideo && !image.complete) || !(sw > 0 && sh > 0)) return;\n bgContext.setTransform(bgDpr, 0, 0, bgDpr, 0, 0);\n bgContext.clearRect(0, 0, stage.clientWidth, stage.clientHeight);\n const horizontal = bgAxis === \"x\";\n const sourceLength = horizontal ? sw : sh;\n const cuts = [0, bgEdgeFraction, 1 - bgEdgeFraction, 1];\n for (let i = 0; i < bgRects.length; i++) {\n const rect = bgRects[i];\n const origin = cuts[i] * sourceLength;\n const length = (cuts[i + 1] - cuts[i]) * sourceLength;\n bgContext.drawImage(\n src,\n horizontal ? origin : 0,\n horizontal ? 0 : origin,\n horizontal ? length : sw,\n horizontal ? sh : length,\n rect.x,\n rect.y,\n rect.w,\n rect.h,\n );\n }\n }\n function scheduleBackgroundDraw() {\n if (bgDrawFrame !== null || !bgRects || !bgContext) return;\n bgDrawFrame = requestAnimationFrame(() => {\n bgDrawFrame = null;\n drawBackground();\n });\n }\n function startBackgroundLoop() {\n if (\n bgLoopFrame !== null ||\n !bgContext ||\n !bgRects ||\n !videoPresented ||\n typeof video.requestVideoFrameCallback !== \"function\"\n )\n return;\n const frame = video.requestVideoFrameCallback(() => {\n if (bgLoopFrame !== frame) return;\n bgLoopFrame = null;\n drawBackground();\n startBackgroundLoop();\n });\n bgLoopFrame = frame;\n }\n // Only the streaming loop stops here; a pending one-shot draw is harmless\n // (it re-paints the preserved frame) and must survive reconnects.\n function stopBackgroundLoop() {\n if (bgLoopFrame !== null) video.cancelVideoFrameCallback?.(bgLoopFrame);\n bgLoopFrame = null;\n }\n function clearBackground() {\n stopBackgroundLoop();\n bgRects = null;\n // Resizing a canvas resets its bitmap; same-value assignment still clears.\n if (bgCanvas) bgCanvas.width = bgCanvas.width;\n }\n listen(image, \"load\", () => scheduleBackgroundDraw());\n // Older WebViews update the ambient layer on media progress, never with an\n // idle display-refresh loop. Geometry changes still get a one-shot repaint.\n listen(video, \"timeupdate\", () => {\n if (videoPresented && typeof video.requestVideoFrameCallback !== \"function\")\n scheduleBackgroundDraw();\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 + verticalOffset(r.height),\n maxY: y.max + verticalOffset(r.height),\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 + verticalOffset(r.height) - 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 + verticalOffset(r.height) - 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(\n bottom - 1,\n (viewerSized ? viewportTopInset : 0) + 8 + hotY,\n );\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 if (config.nativeMedia && epoch)\n post({\n type: \"nativeViewport\",\n fillHeight,\n x: r.x,\n y: r.y,\n width: r.width,\n height: r.height,\n });\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 paintBackground();\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 =\n showMouseButtons && control && !nativeMouseControls ? \"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 listenMouse(el, \"pointerdown\", (e) => {\n e.preventDefault();\n if (!control || !showMouseButtons || heldMouse.has(button)) return;\n clearTimeout(hold);\n if (!e.nativeInput) 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 listenMouse(el, \"pointerup\", up);\n listenMouse(el, \"pointercancel\", up);\n listenMouse(el, \"lostpointercapture\", up);\n listenMouse(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 listenMouse(wheel, \"pointerdown\", (e) => {\n e.preventDefault();\n if (!control || !showMouseButtons || wheelY) return;\n if (!e.nativeInput) 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 listenMouse(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 listenMouse(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 listenMouse(wheel, \"pointercancel\", cancelWheel);\n listenMouse(wheel, \"lostpointercapture\", cancelWheel);\n listenMouse(wheel, \"click\", (e) => {\n if (e.detail === 0 && control && showMouseButtons) {\n click(1);\n flush();\n }\n });\n listenMouse(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 =\n multi.anchor.y +\n (viewportHeight() / 2 + verticalOffset(r.height) - 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: usableHeight(),\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 () => stopBackgroundLoop(),\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 if (typeof startBackgroundLoop === \"function\") startBackgroundLoop();\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 (typeof stopBackgroundLoop === \"function\") stopBackgroundLoop();\n if (!preserveFrame) {\n image.removeAttribute(\"src\");\n if (typeof clearBackground === \"function\") clearBackground();\n }\n }\n function failRtc(reason, canRetry = true) {\n const failedAttempt = attemptId;\n release();\n closeRtc();\n post({ type: \"fallback\", attemptId: failedAttempt, reason });\n // Local consent is bounded by the host's picker lifetime. Reuse this retry\n // timer without consuming the finite network-recovery attempts.\n const capturePending = reason === \"capture-pending\";\n if (canRetry && epoch && (capturePending || retries < net.retryMs.length)) {\n const delay = capturePending\n ? net.retryMs[net.retryMs.length - 1]\n : 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 () => {\n if (g === generation && configPending) {\n configPending = false;\n void startRtc(g, iceServers);\n }\n },\n (net.iceConfigMs ?? 8000) + (net.iceConfigBridgeMs ?? 500),\n );\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 scheduleBackgroundDraw === \"function\")\n scheduleBackgroundDraw();\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: usableHeight(),\n });\n break;\n case \"presentation\":\n if (config.nativeMedia) {\n release();\n showKeyboard(false);\n break;\n }\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 if (!config.nativeMedia) 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 = config.nativeMedia\n ? \"transparent\"\n : message.surface;\n document.documentElement.style.background = config.nativeMedia\n ? \"transparent\"\n : 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 \"networkStatus\": {\n const status = document.getElementById(\"network-status\");\n if (!status) break;\n status.textContent =\n typeof message.text === \"string\" ? message.text : \"\";\n status.style.display = status.textContent ? \"block\" : \"none\";\n for (const key of [\"top\", \"right\", \"fontSize\"]) {\n if (Number.isFinite(message[key]) && message[key] >= 0)\n status.style[key] = message[key] + \"px\";\n }\n if (/^#[0-9a-f]{3,8}$/i.test(message.color))\n status.style.color = message.color;\n break;\n }\n case \"nativeMouse\": {\n if (\n !nativeMouseControls ||\n ![\"left\", \"right\", \"wheel\"].includes(message.control)\n )\n break;\n if (\n ![\n \"pointerdown\",\n \"pointermove\",\n \"pointerup\",\n \"pointercancel\",\n \"click\",\n ].includes(message.event)\n )\n break;\n if (!Number.isFinite(message.id) || !Number.isFinite(message.y)) break;\n mouseControlHandlers.get(\n \"mouse-\" + message.control + \":\" + message.event,\n )?.({\n pointerId: message.id,\n clientY: message.y,\n detail: 0,\n nativeInput: true,\n preventDefault() {},\n });\n break;\n }\n case \"mouseButtons\": {\n nativeMouseControls = message.native === true;\n const before = layout(),\n previousTopInset = viewportTopInset,\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 const previousOffset = portraitKeyboardTopInset;\n if (Number.isFinite(message.portraitKeyboardTopInset))\n portraitKeyboardTopInset = Math.max(\n 0,\n message.portraitKeyboardTopInset,\n );\n const offsetChanged = previousOffset !== portraitKeyboardTopInset;\n let sideInsetsChanged = false;\n if (Number.isFinite(message.topInset))\n viewportTopInset = Math.max(\n 0,\n Math.min(stage.clientHeight - 1, message.topInset),\n );\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 offsetChanged ||\n (viewerSized && previousTopInset !== viewportTopInset) ||\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 nativeVideoActive = false;\n image.style.visibility = \"visible\";\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 if (!config.nativeMedia) connect();\n break;\n case \"nativeVideo\":\n if (!config.nativeMedia || message.epoch !== epoch) break;\n nativeVideoActive = message.active === true;\n image.style.visibility = nativeVideoActive ? \"hidden\" : \"visible\";\n paintBackground();\n break;\n case \"nativeCursor\":\n if (config.nativeMedia && message.epoch === epoch)\n receiveCursor(message.cursor);\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(\n message.capturePending === true ? \"capture-pending\" : \"host\",\n message.retry !== false,\n );\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) {\n scheduleBackgroundDraw();\n post({ type: \"framePresented\" });\n }\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 nativeVideoActive = false;\n image.style.visibility = \"visible\";\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 // iOS can report equal left/right safe areas in both landscape directions.\n // Its interface angle distinguishes the notch side even on a 180-degree turn.\n const reportOrientation = () => {\n const angle =\n typeof window.orientation === \"number\"\n ? window.orientation\n : window.screen?.orientation?.angle;\n if (Number.isFinite(angle)) post({ type: \"orientation\", angle });\n };\n listen(window, \"orientationchange\", reportOrientation);\n if (window.screen?.orientation?.addEventListener)\n listen(window.screen.orientation, \"change\", reportOrientation);\n render();\n reportOrientation();\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 if (bgDrawFrame !== null) cancelAnimationFrame(bgDrawFrame);\n bgDrawFrame = null;\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 let nativeVideoActive = false;\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 // Shared ambient backdrop. Minimal viewer embeds may omit its subtree.\n const bg = find(\"bg\") || null,\n bgCanvas = find(\"bg-canvas\") || null,\n bgContext = bgCanvas ? bgCanvas.getContext(\"2d\") : null;\n let dw = 1920,\n dh = 1080,\n viewerSized = false,\n zoom = 1,\n desktopScale = null,\n reportedScaleMode = null,\n desktopPan = null,\n edgePointer = null,\n edgeFrame = null,\n edgeTime = 0,\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 let nativeMouseControls = false;\n const mouseControlHandlers = new Map();\n function listenMouse(el, type, handler) {\n listen(el, type, handler);\n mouseControlHandlers.set(el.id + \":\" + type, handler);\n }\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 viewportTopInset = 0,\n keyboardViewportInset = 0,\n portraitKeyboardTopInset = 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 usableHeight = () =>\n Math.max(1, stage.clientHeight - viewportTopInset - viewportBottomInset);\n const viewportHeight = () =>\n viewerSized ? usableHeight() : Math.max(1, stage.clientHeight);\n const viewportWidth = () =>\n Math.max(\n 1,\n stage.clientWidth -\n viewportLeft() -\n (fillHeight ? viewportRightInset : 0),\n );\n // Center between the measured top controls and the keyboard edge (stage bottom).\n // The usable center is (topInset + stageHeight) / 2, so shift by topInset / 2.\n // Keep the fitted size and cap the shift when the desktop is too tall to fit.\n const verticalOffset = (height) =>\n (viewerSized ? viewportTopInset : 0) +\n (fillHeight\n ? 0\n : Math.min(\n Math.max(\n 0,\n portraitKeyboardTopInset - (viewerSized ? viewportTopInset : 0),\n ) / 2,\n Math.max(0, (viewportHeight() - height) / 2),\n ));\n const layout = () => {\n const r =\n config.desktop && desktopScale !== null\n ? {\n scale: desktopScale,\n width: dw * desktopScale,\n height: dh * desktopScale,\n }\n : 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 + verticalOffset(r.height) - fy * r.height,\n };\n };\n // The backdrop uses 5% / 90% / 5% source segments. The middle 90%\n // keeps the fitted picture's scale; each outer 5% stretches uniformly\n // to fill the remaining space, horizontally or vertically as needed.\n // Zoom and pan never move the backdrop: where the picture covers it, the\n // opaque picture simply occludes it, so input mapping stays uniform. Returns\n // null when the fitted picture already covers the stage, unless a Desktop\n // viewer has shrunk its foreground below fit and still needs the backdrop.\n const bgEdgeFraction = 0.05;\n const bgCenterFraction = 1 - 2 * bgEdgeFraction;\n function backgroundSegments(vw, vh, dw, dh, fill, includeFitted = false) {\n if (!(vw > 0 && vh > 0 && dw > 0 && dh > 0)) return null;\n const s = fill\n ? vh / Math.max(1, dh)\n : Math.min(vw / dw, vh / Math.max(1, dh));\n const w = dw * s,\n h = dh * s;\n if (w < vw || (includeFitted && w === vw && h === vh)) {\n const centerW = w * bgCenterFraction,\n centerX = vw / 2 - centerW / 2,\n sideW = centerX,\n top = (vh - h) / 2;\n return { axis: \"x\", s, w, h, top, centerW, centerX, sideW };\n }\n if (h < vh) {\n const centerH = h * bgCenterFraction,\n centerY = vh / 2 - centerH / 2,\n sideH = centerY,\n left = (vw - w) / 2;\n return { axis: \"y\", s, w, h, left, centerH, centerY, sideH };\n }\n return null;\n }\n function paintBackground() {\n if (nativeVideoActive) {\n if (bg) bg.style.display = \"none\";\n return;\n }\n if (!bg || !bgCanvas || !bgContext) return;\n const vw = stage.clientWidth,\n vh = stage.clientHeight;\n const picture = config.desktop ? layout() : null;\n const coversStage =\n picture &&\n picture.x <= 0 &&\n picture.y <= 0 &&\n picture.x + picture.width >= vw &&\n picture.y + picture.height >= vh;\n const seg = coversStage\n ? null\n : backgroundSegments(vw, vh, dw, dh, fillHeight, config.desktop);\n if (!seg) {\n bg.style.display = \"none\";\n bgRects = null;\n stopBackgroundLoop();\n return;\n }\n bg.style.display = \"block\";\n bgAxis = seg.axis;\n // The ambient layer also fills the notch and the area beneath native\n // controls. Only the interactive picture respects viewport insets.\n const originX = 0;\n // Source segments are resolved at draw time from the live source dimensions;\n // these dest rects only encode where each segment lands on the stage.\n bgRects =\n seg.axis === \"x\"\n ? [\n {\n x: originX,\n y: seg.top,\n w: seg.sideW,\n h: seg.h,\n },\n {\n x: originX + seg.centerX,\n y: seg.top,\n w: seg.centerW,\n h: seg.h,\n },\n {\n x: originX + seg.centerX + seg.centerW,\n y: seg.top,\n w: seg.sideW,\n h: seg.h,\n },\n ]\n : [\n {\n x: seg.left,\n y: 0,\n w: seg.w,\n h: seg.sideH,\n },\n {\n x: seg.left,\n y: seg.centerY,\n w: seg.w,\n h: seg.centerH,\n },\n {\n x: seg.left,\n y: seg.centerY + seg.centerH,\n w: seg.w,\n h: seg.sideH,\n },\n ];\n // Keep the backing store at stage resolution (capped) so the stretched\n // edges stay crisp without oversized allocations.\n bgDpr = Math.min(2, window.devicePixelRatio || 1);\n const bw = Math.round(stage.clientWidth * bgDpr),\n bh = Math.round(vh * bgDpr);\n if (bgCanvas.width !== bw || bgCanvas.height !== bh) {\n bgCanvas.width = bw;\n bgCanvas.height = bh;\n }\n scheduleBackgroundDraw();\n startBackgroundLoop();\n }\n // One canvas, three draws: each source segment stretches into whatever bar\n // the picture leaves open. The live video feeds it once presented; until\n // then the decoded JPEG frame does, so the backdrop always matches the\n // picture's own pixels and scale in the center segment.\n let bgRects = null,\n bgAxis = \"x\",\n bgDpr = 1,\n bgDrawFrame = null,\n bgLoopFrame = null;\n function drawBackground() {\n if (!bgContext || !bgRects) return;\n const useVideo =\n videoPresented && video.videoWidth > 0 && video.videoHeight > 0;\n const src = useVideo ? video : image;\n // drawImage source rects live in the source's own pixel space, which can\n // differ from dw/dh; fractions of the real bitmap keep every segment lined\n // up with what the picture element actually shows.\n const sw = useVideo ? video.videoWidth : image.naturalWidth;\n const sh = useVideo ? video.videoHeight : image.naturalHeight;\n // Cursor updates can schedule a repaint while the next JPEG is loading.\n // Keep the last backdrop until the replacement has drawable pixels.\n if ((!useVideo && !image.complete) || !(sw > 0 && sh > 0)) return;\n bgContext.setTransform(bgDpr, 0, 0, bgDpr, 0, 0);\n bgContext.clearRect(0, 0, stage.clientWidth, stage.clientHeight);\n const horizontal = bgAxis === \"x\";\n const sourceLength = horizontal ? sw : sh;\n const cuts = [0, bgEdgeFraction, 1 - bgEdgeFraction, 1];\n for (let i = 0; i < bgRects.length; i++) {\n const rect = bgRects[i];\n const origin = cuts[i] * sourceLength;\n const length = (cuts[i + 1] - cuts[i]) * sourceLength;\n bgContext.drawImage(\n src,\n horizontal ? origin : 0,\n horizontal ? 0 : origin,\n horizontal ? length : sw,\n horizontal ? sh : length,\n rect.x,\n rect.y,\n rect.w,\n rect.h,\n );\n }\n }\n function scheduleBackgroundDraw() {\n if (bgDrawFrame !== null || !bgRects || !bgContext) return;\n bgDrawFrame = requestAnimationFrame(() => {\n bgDrawFrame = null;\n drawBackground();\n });\n }\n function startBackgroundLoop() {\n if (\n bgLoopFrame !== null ||\n !bgContext ||\n !bgRects ||\n !videoPresented ||\n typeof video.requestVideoFrameCallback !== \"function\"\n )\n return;\n const frame = video.requestVideoFrameCallback(() => {\n if (bgLoopFrame !== frame) return;\n bgLoopFrame = null;\n drawBackground();\n startBackgroundLoop();\n });\n bgLoopFrame = frame;\n }\n // Only the streaming loop stops here; a pending one-shot draw is harmless\n // (it re-paints the preserved frame) and must survive reconnects.\n function stopBackgroundLoop() {\n if (bgLoopFrame !== null) video.cancelVideoFrameCallback?.(bgLoopFrame);\n bgLoopFrame = null;\n }\n function clearBackground() {\n stopBackgroundLoop();\n bgRects = null;\n // Resizing a canvas resets its bitmap; same-value assignment still clears.\n if (bgCanvas) bgCanvas.width = bgCanvas.width;\n }\n listen(image, \"load\", () => scheduleBackgroundDraw());\n // Older WebViews update the ambient layer on media progress, never with an\n // idle display-refresh loop. Geometry changes still get a one-shot repaint.\n listen(video, \"timeupdate\", () => {\n if (videoPresented && typeof video.requestVideoFrameCallback !== \"function\")\n scheduleBackgroundDraw();\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 + verticalOffset(r.height),\n maxY: y.max + verticalOffset(r.height),\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 + verticalOffset(r.height) - 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 + verticalOffset(r.height) - 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(\n bottom - 1,\n (viewerSized ? viewportTopInset : 0) + 8 + hotY,\n );\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 if (config.nativeMedia && epoch)\n post({\n type: \"nativeViewport\",\n fillHeight,\n x: r.x,\n y: r.y,\n width: r.width,\n height: r.height,\n });\n if (config.desktop && epoch) {\n const scaleMode =\n desktopScale === null\n ? \"fit\"\n : desktopScale === 1\n ? \"actual\"\n : \"custom\";\n if (reportedScaleMode !== scaleMode) {\n reportedScaleMode = scaleMode;\n post({ type: \"scaleMode\", mode: scaleMode });\n }\n }\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 paintBackground();\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) {\n // Let the local OS own pointer size, DPI and accessibility preferences.\n // Never use a remote URL or bitmap as the desktop's native cursor.\n cursor.style.display = \"none\";\n const shape = remoteCursor?.shape;\n stage.style.cursor =\n control &&\n [\n \"default\",\n \"text\",\n \"vertical-text\",\n \"pointer\",\n \"crosshair\",\n \"grab\",\n \"grabbing\",\n \"ew-resize\",\n \"ns-resize\",\n \"nwse-resize\",\n \"nesw-resize\",\n \"move\",\n \"not-allowed\",\n \"copy\",\n \"alias\",\n \"wait\",\n \"progress\",\n \"help\",\n ].includes(shape)\n ? shape\n : \"default\";\n }\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 stopEdgePan();\n desktopPan = null;\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 =\n showMouseButtons && control && !nativeMouseControls ? \"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 listenMouse(el, \"pointerdown\", (e) => {\n e.preventDefault();\n if (!control || !showMouseButtons || heldMouse.has(button)) return;\n clearTimeout(hold);\n if (!e.nativeInput) 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 listenMouse(el, \"pointerup\", up);\n listenMouse(el, \"pointercancel\", up);\n listenMouse(el, \"lostpointercapture\", up);\n listenMouse(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 listenMouse(wheel, \"pointerdown\", (e) => {\n e.preventDefault();\n if (!control || !showMouseButtons || wheelY) return;\n if (!e.nativeInput) 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 listenMouse(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 listenMouse(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 listenMouse(wheel, \"pointercancel\", cancelWheel);\n listenMouse(wheel, \"lostpointercapture\", cancelWheel);\n listenMouse(wheel, \"click\", (e) => {\n if (e.detail === 0 && control && showMouseButtons) {\n click(1);\n flush();\n }\n });\n listenMouse(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 stopEdgePan() {\n if (edgeFrame !== null) cancelAnimationFrame(edgeFrame);\n edgeFrame = null;\n edgePointer = null;\n }\n function edgePanTick(now) {\n edgeFrame = null;\n if (!edgePointer || desktopPan || !epoch) return;\n const r = layout(),\n vw = viewportWidth(),\n vh = viewportHeight();\n const p = edgePointer;\n const dt = Math.min(32, Math.max(0, now - edgeTime)) / 1000;\n edgeTime = now;\n // rAF timestamps describe the frame start and can precede the pointer\n // handler's performance.now(). A zero-time first frame is not a boundary.\n if (dt === 0) {\n edgeFrame = requestAnimationFrame(edgePanTick);\n return;\n }\n // A narrow edge band accelerates smoothly to 720 local pixels per second.\n const velocity = (position, extent) => {\n const band = Math.min(28, extent / 4);\n return position < band\n ? -(1 - position / band)\n : position > extent - band\n ? (position - extent + band) / band\n : 0;\n };\n const x =\n r.width > vw\n ? bounded(\n r.x - velocity(p.x - viewportLeft(), vw) * 720 * dt,\n viewportLeft() + vw - r.width,\n viewportLeft(),\n )\n : r.x;\n const y =\n r.height > vh\n ? bounded(r.y - velocity(p.y, vh) * 720 * dt, vh - r.height, 0)\n : r.y;\n if (Math.abs(x - r.x) < 0.001 && Math.abs(y - r.y) < 0.001) return;\n stopPanAnimation();\n manualViewMoved = true;\n place(x, y, r);\n // Keep remote hover/drag attached to the local pointer as the picture moves.\n if (control) {\n const next = point(p);\n cx = next.x;\n cy = next.y;\n queue({ kind: \"move\", x: cx, y: cy });\n }\n render();\n edgeFrame = requestAnimationFrame(edgePanTick);\n }\n function trackEdgePointer(e) {\n const p = desktopPoint(e);\n if (\n !epoch ||\n desktopPan ||\n p.x < viewportLeft() ||\n p.x > viewportLeft() + viewportWidth() ||\n p.y < 0 ||\n p.y > viewportHeight() ||\n !insideDesktop(p)\n ) {\n stopEdgePan();\n return;\n }\n edgePointer = p;\n if (edgeFrame === null) {\n edgeTime = performance.now();\n edgeFrame = requestAnimationFrame(edgePanTick);\n }\n }\n listen(stage, \"pointerleave\", stopEdgePan);\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 =\n multi.anchor.y +\n (viewportHeight() / 2 + verticalOffset(r.height) - 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 if (\n e.button === 1 &&\n (layout().width > viewportWidth() || layout().height > viewportHeight())\n ) {\n stopEdgePan();\n stopPanAnimation();\n desktopPan = {\n id: e.pointerId,\n x: e.clientX,\n y: e.clientY,\n start: e,\n moved: false,\n };\n stage.setPointerCapture(e.pointerId);\n return;\n }\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 (desktopPan?.id === e.pointerId) {\n const dx = e.clientX - desktopPan.x,\n dy = e.clientY - desktopPan.y;\n if (!desktopPan.moved && Math.hypot(dx, dy) < 3) return;\n desktopPan.moved = true;\n desktopPan.x = e.clientX;\n desktopPan.y = e.clientY;\n pan(dx, dy);\n return;\n }\n trackEdgePointer(e);\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 if (desktopPan?.id === e.pointerId) {\n const gesture = desktopPan;\n desktopPan = null;\n if (gesture.moved) settlePan();\n else {\n mouse(gesture.start, true);\n mouse(e, false);\n }\n return;\n }\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 desktopPan = null;\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: usableHeight(),\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 () => stopBackgroundLoop(),\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 if (typeof startBackgroundLoop === \"function\") startBackgroundLoop();\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 (typeof stopBackgroundLoop === \"function\") stopBackgroundLoop();\n if (!preserveFrame) {\n image.removeAttribute(\"src\");\n if (typeof clearBackground === \"function\") clearBackground();\n }\n }\n function failRtc(reason, canRetry = true) {\n const failedAttempt = attemptId;\n release();\n closeRtc();\n post({ type: \"fallback\", attemptId: failedAttempt, reason });\n // Local consent is bounded by the host's picker lifetime. Reuse this retry\n // timer without consuming the finite network-recovery attempts.\n const capturePending = reason === \"capture-pending\";\n if (canRetry && epoch && (capturePending || retries < net.retryMs.length)) {\n const delay = capturePending\n ? net.retryMs[net.retryMs.length - 1]\n : 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 () => {\n if (g === generation && configPending) {\n configPending = false;\n void startRtc(g, iceServers);\n }\n },\n (net.iceConfigMs ?? 8000) + (net.iceConfigBridgeMs ?? 500),\n );\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 scheduleBackgroundDraw === \"function\")\n scheduleBackgroundDraw();\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: usableHeight(),\n });\n break;\n case \"presentation\":\n if (config.nativeMedia) {\n release();\n showKeyboard(false);\n break;\n }\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 desktopScale = null;\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 if (!config.nativeMedia) 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 = config.nativeMedia\n ? \"transparent\"\n : message.surface;\n document.documentElement.style.background = config.nativeMedia\n ? \"transparent\"\n : 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 \"networkStatus\": {\n const status = document.getElementById(\"network-status\");\n if (!status) break;\n status.textContent =\n typeof message.text === \"string\" ? message.text : \"\";\n status.style.display = status.textContent ? \"block\" : \"none\";\n for (const key of [\"top\", \"right\", \"fontSize\"]) {\n if (Number.isFinite(message[key]) && message[key] >= 0)\n status.style[key] = message[key] + \"px\";\n }\n if (/^#[0-9a-f]{3,8}$/i.test(message.color))\n status.style.color = message.color;\n break;\n }\n case \"nativeMouse\": {\n if (\n !nativeMouseControls ||\n ![\"left\", \"right\", \"wheel\"].includes(message.control)\n )\n break;\n if (\n ![\n \"pointerdown\",\n \"pointermove\",\n \"pointerup\",\n \"pointercancel\",\n \"click\",\n ].includes(message.event)\n )\n break;\n if (!Number.isFinite(message.id) || !Number.isFinite(message.y)) break;\n mouseControlHandlers.get(\n \"mouse-\" + message.control + \":\" + message.event,\n )?.({\n pointerId: message.id,\n clientY: message.y,\n detail: 0,\n nativeInput: true,\n preventDefault() {},\n });\n break;\n }\n case \"mouseButtons\": {\n nativeMouseControls = message.native === true;\n const before = layout(),\n previousTopInset = viewportTopInset,\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 const previousOffset = portraitKeyboardTopInset;\n if (Number.isFinite(message.portraitKeyboardTopInset))\n portraitKeyboardTopInset = Math.max(\n 0,\n message.portraitKeyboardTopInset,\n );\n const offsetChanged = previousOffset !== portraitKeyboardTopInset;\n let sideInsetsChanged = false;\n if (Number.isFinite(message.topInset))\n viewportTopInset = Math.max(\n 0,\n Math.min(stage.clientHeight - 1, message.topInset),\n );\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 offsetChanged ||\n (viewerSized && previousTopInset !== viewportTopInset) ||\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 nativeVideoActive = false;\n image.style.visibility = \"visible\";\n desktopScale = null;\n reportedScaleMode = null;\n if (config.desktop) {\n zoom = 1;\n fx = 0.5;\n fy = 0.5;\n }\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 if (!config.nativeMedia) connect();\n break;\n case \"nativeVideo\":\n if (!config.nativeMedia || message.epoch !== epoch) break;\n nativeVideoActive = message.active === true;\n image.style.visibility = nativeVideoActive ? \"hidden\" : \"visible\";\n paintBackground();\n break;\n case \"nativeCursor\":\n if (config.nativeMedia && message.epoch === epoch)\n receiveCursor(message.cursor);\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(\n message.capturePending === true ? \"capture-pending\" : \"host\",\n message.retry !== false,\n );\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) {\n scheduleBackgroundDraw();\n post({ type: \"framePresented\" });\n }\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 \"zoom\":\n stopEdgePan();\n if (\n !config.desktop ||\n !Number.isFinite(message.factor) ||\n message.factor <= 0\n )\n break;\n followRest = null;\n cursorNeedsEntry = true;\n manualViewMoved = true;\n stopPanAnimation();\n const fitScale = Math.min(viewportWidth() / dw, viewportHeight() / dh);\n // Include actual size even when it lies outside the fit-relative range.\n desktopScale = Math.max(\n Math.min(fitScale, 1) * 0.1,\n Math.min(Math.max(5 * fitScale, 5), layout().scale * message.factor),\n );\n if (Math.abs(desktopScale - fitScale) < 1e-9) desktopScale = null;\n if (desktopScale === null) {\n fx = 0.5;\n fy = 0.5;\n manualViewMoved = false;\n }\n render();\n settlePan();\n break;\n case \"actualSize\":\n stopEdgePan();\n if (!config.desktop) break;\n followRest = null;\n cursorNeedsEntry = true;\n manualViewMoved = true;\n stopPanAnimation();\n desktopScale = 1;\n fx = 0.5;\n fy = 0.5;\n render();\n break;\n case \"fit\":\n stopEdgePan();\n desktopScale = null;\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 nativeVideoActive = false;\n image.style.visibility = \"visible\";\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 // iOS can report equal left/right safe areas in both landscape directions.\n // Its interface angle distinguishes the notch side even on a 180-degree turn.\n const reportOrientation = () => {\n const angle =\n typeof window.orientation === \"number\"\n ? window.orientation\n : window.screen?.orientation?.angle;\n if (Number.isFinite(angle)) post({ type: \"orientation\", angle });\n };\n listen(window, \"orientationchange\", reportOrientation);\n if (window.screen?.orientation?.addEventListener)\n listen(window.screen.orientation, \"change\", reportOrientation);\n render();\n reportOrientation();\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 if (bgDrawFrame !== null) cancelAnimationFrame(bgDrawFrame);\n bgDrawFrame = null;\n for (const cleanup of cleanups) cleanup();\n },\n };\n}\n";
diff --git a/packages/remote-credentials-native/Sources/CindyRemoteCredentials/CredentialVault.swift b/packages/remote-credentials-native/Sources/CindyRemoteCredentials/CredentialVault.swift
index 9cbf724be6..74213fa1bf 100644
--- a/packages/remote-credentials-native/Sources/CindyRemoteCredentials/CredentialVault.swift
+++ b/packages/remote-credentials-native/Sources/CindyRemoteCredentials/CredentialVault.swift
@@ -1,4 +1,4 @@
-#if os(iOS)
+#if os(iOS) || os(macOS)
import CryptoKit
import Foundation
import LocalAuthentication
@@ -59,7 +59,11 @@ final class CredentialVault: Sendable {
let data = try JSONEncoder().encode(value)
var attributes = query
attributes[kSecValueData] = data
+ #if os(macOS)
+ attributes[kSecAttrAccess] = try loginAccess()
+ #else
attributes[kSecAttrAccessible] = kSecAttrAccessibleWhenUnlockedThisDeviceOnly
+ #endif
let status = SecItemAdd(attributes as CFDictionary, nil)
if status == errSecDuplicateItem {
guard SecItemUpdate(query as CFDictionary, [kSecValueData: data] as CFDictionary) == errSecSuccess else { throw CredentialError.unavailable }
@@ -67,14 +71,26 @@ final class CredentialVault: Sendable {
}
private func settingsItem(_ key: String) -> [CFString: Any] {
- [kSecClass: kSecClassGenericPassword, kSecAttrService: service + ".settings",
- kSecAttrAccount: key, kSecAttrSynchronizable: false, kSecUseDataProtectionKeychain: true]
+ query(service: service + ".settings", account: key)
}
func store(_ secret: Data, binding: CredentialBinding, requireBiometric: Bool = true) throws {
+ #if os(macOS)
+ guard !secret.isEmpty, secret.count <= 4096 else { throw CredentialError.unavailable }
+ let data = try MacCredentialSecret.seal(secret, binding: binding, biometric: requireBiometric)
+ let query = try item(binding)
+ var attributes = query
+ attributes[kSecAttrAccess] = try loginAccess()
+ attributes[kSecValueData] = data
+ let result = SecItemAdd(attributes as CFDictionary, nil)
+ if result == errSecDuplicateItem {
+ guard SecItemUpdate(query as CFDictionary, [kSecValueData: data] as CFDictionary) == errSecSuccess else { throw CredentialError.unavailable }
+ } else if result != errSecSuccess { throw CredentialError.unavailable }
+ #else
+ let accessibility = kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly
guard !secret.isEmpty, secret.count <= 4096,
let access = SecAccessControlCreateWithFlags(nil,
- kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly, requireBiometric ? .biometryCurrentSet : [], nil) else {
+ accessibility, requireBiometric ? .biometryCurrentSet : [], nil) else {
throw CredentialError.unavailable
}
let query = try item(binding)
@@ -90,18 +106,21 @@ final class CredentialVault: Sendable {
throw CredentialError.unavailable
}
} else if result != errSecSuccess { throw CredentialError.unavailable }
+ #endif
}
func read(binding: CredentialBinding, reason: String, context: LAContext) throws -> Data {
var query = try item(binding)
query[kSecReturnData] = true
query[kSecMatchLimit] = kSecMatchLimitOne
+ #if !os(macOS)
query[kSecUseAuthenticationContext] = context
+ #endif
context.localizedReason = reason
// No separate evaluatePolicy bool: authorization protects the actual read.
var result: CFTypeRef?
let status = SecItemCopyMatching(query as CFDictionary, &result)
- guard status == errSecSuccess, let data = result as? Data, !data.isEmpty, data.count <= 4096 else {
+ guard status == errSecSuccess, let data = result as? Data, !data.isEmpty, data.count <= 16384 else {
switch status {
case errSecUserCanceled: throw CredentialError.cancelled
case errSecItemNotFound: throw CredentialError.savedPasswordMissing
@@ -110,7 +129,12 @@ final class CredentialVault: Sendable {
default: throw CredentialError.savedReadFailed
}
}
+ #if os(macOS)
+ return try MacCredentialSecret.open(data, binding: binding, context: context)
+ #else
+ guard data.count <= 4096 else { throw CredentialError.savedReadFailed }
return data
+ #endif
}
func forget(binding: CredentialBinding) throws {
@@ -123,7 +147,9 @@ final class CredentialVault: Sendable {
query[kSecMatchLimit] = kSecMatchLimitOne
let context = LAContext(); context.interactionNotAllowed = true
defer { context.invalidate() }
+ #if !os(macOS)
query[kSecUseAuthenticationContext] = context
+ #endif
let result = SecItemCopyMatching(query as CFDictionary, nil)
if result == errSecItemNotFound { return false }
// This is only a native presentation hint. Actual retrieval still uses
@@ -133,9 +159,25 @@ final class CredentialVault: Sendable {
}
private func item(_ binding: CredentialBinding) throws -> [CFString: Any] {
- [kSecClass: kSecClassGenericPassword, kSecAttrService: service,
- kSecAttrAccount: try binding.storageKey, kSecAttrSynchronizable: false,
- kSecUseDataProtectionKeychain: true]
+ query(service: service, account: try binding.storageKey)
+ }
+ private func query(service: String, account: String) -> [CFString: Any] {
+ var value: [CFString: Any] = [kSecClass: kSecClassGenericPassword,
+ kSecAttrService: service, kSecAttrAccount: account, kSecAttrSynchronizable: false]
+ #if !os(macOS)
+ value[kSecUseDataProtectionKeychain] = true
+ #endif
+ return value
+ }
+ #if os(macOS)
+ private func loginAccess() throws -> SecAccess {
+ var trusted: SecTrustedApplication?
+ var access: SecAccess?
+ guard SecTrustedApplicationCreateFromPath(nil, &trusted) == errSecSuccess, let trusted,
+ SecAccessCreate("Cindy remote desktop credentials" as CFString, [trusted] as CFArray, &access) == errSecSuccess,
+ let access else { throw CredentialError.unavailable }
+ return access
}
+ #endif
}
#endif
diff --git a/packages/remote-credentials-native/Sources/CindyRemoteCredentials/MacCredentialPasswordForm.swift b/packages/remote-credentials-native/Sources/CindyRemoteCredentials/MacCredentialPasswordForm.swift
new file mode 100644
index 0000000000..5b9a5a561b
--- /dev/null
+++ b/packages/remote-credentials-native/Sources/CindyRemoteCredentials/MacCredentialPasswordForm.swift
@@ -0,0 +1,100 @@
+#if os(macOS)
+import AppKit
+
+struct CredentialLabels {
+ private let values: [String: String]
+ init(locale: String) throws {
+ guard let url = Bundle.module.url(forResource: "credentials", withExtension: "json"),
+ let catalog = try? JSONDecoder().decode([String: [String: String]].self, from: Data(contentsOf: url)),
+ let values = catalog[locale] ?? catalog["en"] else { throw CredentialError.unavailable }
+ self.values = values
+ }
+ subscript(_ key: String) -> String { values[key] ?? key }
+}
+
+/// AppKit owns the secure field and its lifetime. No password crosses stdio.
+@MainActor
+final class CredentialPasswordForm: NSObject, NSWindowDelegate {
+ private let panel = NSPanel(contentRect: NSRect(x: 0, y: 0, width: 360, height: 220),
+ styleMask: [.titled, .closable], backing: .buffered, defer: false)
+ private let password = NSSecureTextField()
+ private let completion: (Result<(Data, Bool), CredentialError>) -> Void
+ private var finished = false
+ init(labels: CredentialLabels, account: String, saveRequired: Bool,
+ completion: @escaping (Result<(Data, Bool), CredentialError>) -> Void) {
+ self.completion = completion
+ super.init()
+ panel.title = labels["title"]; panel.delegate = self; panel.isReleasedWhenClosed = false
+ panel.titleVisibility = .hidden
+ panel.titlebarAppearsTransparent = true
+ let stack = NSStackView(); stack.orientation = .vertical; stack.alignment = .leading; stack.spacing = 16
+ stack.translatesAutoresizingMaskIntoConstraints = false
+ let content = panel.contentView!; content.addSubview(stack)
+ NSLayoutConstraint.activate([
+ stack.leadingAnchor.constraint(equalTo: content.leadingAnchor, constant: 24),
+ stack.trailingAnchor.constraint(equalTo: content.trailingAnchor, constant: -24),
+ stack.topAnchor.constraint(equalTo: content.topAnchor, constant: 12),
+ stack.bottomAnchor.constraint(equalTo: content.bottomAnchor, constant: -24),
+ stack.widthAnchor.constraint(equalToConstant: 312),
+ ])
+ let heading = NSStackView(); heading.spacing = 12; heading.alignment = .centerY
+ let icon = NSImageView(image: NSImage(systemSymbolName: "lock.fill", accessibilityDescription: nil)!)
+ icon.contentTintColor = .secondaryLabelColor
+ icon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 24, weight: .regular)
+ icon.widthAnchor.constraint(equalToConstant: 32).isActive = true
+ icon.setAccessibilityElement(false)
+ let identity = NSStackView(); identity.orientation = .vertical; identity.alignment = .leading; identity.spacing = 4
+ let title = NSTextField(wrappingLabelWithString: labels["title"])
+ title.font = .systemFont(ofSize: 15, weight: .semibold)
+ let accountLabel = NSTextField(wrappingLabelWithString: String(format: labels["accountFormat"], account))
+ accountLabel.font = .systemFont(ofSize: 13); accountLabel.textColor = .secondaryLabelColor
+ identity.addArrangedSubview(title); identity.addArrangedSubview(accountLabel)
+ heading.addArrangedSubview(icon); heading.addArrangedSubview(identity)
+ stack.addArrangedSubview(heading)
+ heading.widthAnchor.constraint(equalTo: stack.widthAnchor).isActive = true
+ password.placeholderString = labels["password"]; password.setAccessibilityLabel(labels["password"])
+ password.controlSize = .regular; password.font = .systemFont(ofSize: 13)
+ stack.addArrangedSubview(password)
+ password.widthAnchor.constraint(equalTo: stack.widthAnchor).isActive = true
+ // Keep the native cell and field editor at their intrinsic height. Stretching
+ // the bezel leaves the secure glyphs and insertion caret aligned to its top.
+ password.setContentHuggingPriority(.required, for: .vertical)
+ stack.setCustomSpacing(8, after: password)
+ let explanation = NSTextField(wrappingLabelWithString: labels["explanation"])
+ explanation.font = .systemFont(ofSize: 12)
+ explanation.textColor = .secondaryLabelColor; stack.addArrangedSubview(explanation)
+ explanation.widthAnchor.constraint(equalTo: stack.widthAnchor).isActive = true
+ let buttons = NSStackView(); buttons.spacing = 8
+ let spacer = NSView(); buttons.addArrangedSubview(spacer)
+ let cancel = NSButton(title: labels["cancel"], target: self, action: #selector(cancelAction)); cancel.keyEquivalent = "\u{1b}"
+ let verify = NSButton(title: labels["verify"], target: self, action: #selector(verifyAction)); verify.keyEquivalent = "\r"
+ buttons.addArrangedSubview(cancel); buttons.addArrangedSubview(verify); stack.addArrangedSubview(buttons)
+ buttons.widthAnchor.constraint(equalTo: stack.widthAnchor).isActive = true
+ for button in [cancel, verify] {
+ button.controlSize = .regular
+ button.setContentHuggingPriority(.required, for: .horizontal)
+ button.setContentCompressionResistancePriority(.required, for: .horizontal)
+ }
+ // Size to localized content instead of reserving an empty lower half.
+ content.layoutSubtreeIfNeeded()
+ panel.setContentSize(content.fittingSize)
+ }
+ func present(theme: String) {
+ panel.appearance = NSAppearance(named: theme == "dark" ? .darkAqua : .aqua)
+ panel.center(); NSApp.activate(ignoringOtherApps: true); panel.makeKeyAndOrderFront(nil)
+ panel.makeFirstResponder(password)
+ }
+ @objc private func verifyAction() {
+ let data = Data(password.stringValue.utf8)
+ guard !data.isEmpty, data.count <= 4096, !data.contains(0) else { NSSound.beep(); return }
+ finish(.success((data, true)))
+ }
+ @objc private func cancelAction() { cancel() }
+ func cancel() { finish(.failure(.cancelled)) }
+ func windowShouldClose(_ sender: NSWindow) -> Bool { cancel(); return false }
+ private func finish(_ result: Result<(Data, Bool), CredentialError>) {
+ guard !finished else { return }; finished = true
+ password.stringValue = ""; panel.orderOut(nil); panel.close(); completion(result)
+ }
+}
+#endif
diff --git a/packages/remote-credentials-native/Sources/CindyRemoteCredentials/MacCredentialSecret.swift b/packages/remote-credentials-native/Sources/CindyRemoteCredentials/MacCredentialSecret.swift
new file mode 100644
index 0000000000..760ae3ed37
--- /dev/null
+++ b/packages/remote-credentials-native/Sources/CindyRemoteCredentials/MacCredentialSecret.swift
@@ -0,0 +1,60 @@
+#if os(macOS)
+import CryptoKit
+import Foundation
+import LocalAuthentication
+import Security
+
+/// The standalone helper uses the login Keychain, which cannot enforce
+/// SecAccessControl biometric constraints. For biometric entries the Keychain
+/// stores only ciphertext and a hardware-wrapped Secure Enclave key. The actual
+/// key agreement (not a separate evaluatePolicy Boolean) authorizes decryption.
+enum MacCredentialSecret {
+ private struct Envelope: Codable {
+ let version: Int
+ let plain: Data?
+ let key: Data?
+ let peer: Data?
+ let sealed: Data?
+ }
+ static func seal(_ secret: Data, binding: CredentialBinding, biometric: Bool) throws -> Data {
+ guard biometric else {
+ return try JSONEncoder().encode(Envelope(version: 1, plain: secret, key: nil, peer: nil, sealed: nil))
+ }
+ guard SecureEnclave.isAvailable,
+ let access = SecAccessControlCreateWithFlags(nil, kSecAttrAccessibleWhenUnlockedThisDeviceOnly,
+ [.privateKeyUsage, .biometryCurrentSet], nil) else { throw CredentialError.unavailable }
+ let recipient = try SecureEnclave.P256.KeyAgreement.PrivateKey(accessControl: access)
+ let sender = P256.KeyAgreement.PrivateKey()
+ let shared = try sender.sharedSecretFromKeyAgreement(with: recipient.publicKey)
+ let bindingData = Data(try binding.storageKey.utf8)
+ let key = shared.hkdfDerivedSymmetricKey(using: SHA256.self, salt: bindingData,
+ sharedInfo: Data("cindy.desktop.saved-password.v1".utf8), outputByteCount: 32)
+ let ciphertext = try AES.GCM.seal(secret, using: key, authenticating: bindingData)
+ return try JSONEncoder().encode(Envelope(version: 1, plain: nil,
+ key: recipient.dataRepresentation, peer: sender.publicKey.x963Representation, sealed: ciphertext.combined))
+ }
+ static func open(_ data: Data, binding: CredentialBinding, context: LAContext) throws -> Data {
+ do {
+ let envelope = try JSONDecoder().decode(Envelope.self, from: data)
+ guard envelope.version == 1 else { throw CredentialError.savedReadFailed }
+ let secret: Data
+ if let plain = envelope.plain {
+ guard envelope.key == nil, envelope.peer == nil, envelope.sealed == nil else { throw CredentialError.savedReadFailed }
+ secret = plain
+ } else {
+ guard let representation = envelope.key, let peer = envelope.peer, let sealed = envelope.sealed else { throw CredentialError.savedReadFailed }
+ let recipient = try SecureEnclave.P256.KeyAgreement.PrivateKey(dataRepresentation: representation, authenticationContext: context)
+ let shared = try recipient.sharedSecretFromKeyAgreement(with: P256.KeyAgreement.PublicKey(x963Representation: peer))
+ let bindingData = Data(try binding.storageKey.utf8)
+ let key = shared.hkdfDerivedSymmetricKey(using: SHA256.self, salt: bindingData,
+ sharedInfo: Data("cindy.desktop.saved-password.v1".utf8), outputByteCount: 32)
+ secret = try AES.GCM.open(AES.GCM.SealedBox(combined: sealed), using: key, authenticating: bindingData)
+ }
+ guard !secret.isEmpty, secret.count <= 4096 else { throw CredentialError.savedReadFailed }
+ return secret
+ } catch let error as CredentialError { throw error }
+ catch let error as LAError where error.code == .userCancel || error.code == .appCancel || error.code == .systemCancel { throw CredentialError.cancelled }
+ catch { throw CredentialError.savedReadDenied }
+ }
+}
+#endif
diff --git a/packages/remote-credentials-native/Sources/CindyRemoteCredentials/MobileCredentialClient.swift b/packages/remote-credentials-native/Sources/CindyRemoteCredentials/MobileCredentialClient.swift
index 60288731f4..7ddbfbdd8e 100644
--- a/packages/remote-credentials-native/Sources/CindyRemoteCredentials/MobileCredentialClient.swift
+++ b/packages/remote-credentials-native/Sources/CindyRemoteCredentials/MobileCredentialClient.swift
@@ -1,7 +1,11 @@
-#if os(iOS)
+#if os(iOS) || os(macOS)
import Foundation
import CryptoKit
+#if os(iOS)
import UIKit
+#else
+import AppKit
+#endif
// The system context is read on the worker and only invalidated on Main for
// cancellation; LocalAuthentication predates Swift Sendable annotations.
@preconcurrency import LocalAuthentication
@@ -46,11 +50,23 @@ public final class MobileCredentialClient {
private var authentication: LAContext?
public var onInvalidated: ((String) -> Void)?
- public init() {}
+ private let storageDirectory: URL?
+ public init(storageDirectory: URL? = nil) { self.storageDirectory = storageDirectory }
+ private var foreground: Bool {
+ #if os(iOS)
+ return UIApplication.shared.applicationState == .active
+ #else
+ return NSApp.isActive || NSWorkspace.shared.frontmostApplication?.processIdentifier == getppid()
+ #endif
+ }
+ private func storageRoot() throws -> URL {
+ if let storageDirectory { return storageDirectory }
+ return try FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask,
+ appropriateFor: nil, create: true).appendingPathComponent("CindyRemoteCredentials", isDirectory: true)
+ }
private func localVault() throws -> CredentialVault {
- let root = try FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask,
- appropriateFor: nil, create: true).appendingPathComponent("CindyRemoteCredentials", isDirectory: true)
+ let root = try storageRoot()
return CredentialVault(installation: try InstallationMarker.loadOrCreate(directory: root))
}
private func settingsKey(realm: String, membership: String, authDevice: String, target: String) throws -> String {
@@ -61,7 +77,10 @@ public final class MobileCredentialClient {
public func savedSettings(realm: String, membership: String, authDevice: String, target: String) throws -> [String: Bool] {
let vault = try localVault()
let value = try vault.settings(settingsKey(realm: realm, membership: membership, authDevice: authDevice, target: target))
- let biometricAvailable = LAContext().canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil)
+ var biometricAvailable = LAContext().canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil)
+ #if os(macOS)
+ biometricAvailable = biometricAvailable && SecureEnclave.isAvailable
+ #endif
let autoUnlock = try value.map { settings in
guard settings.targetPublicKey != nil else { return false }
return try vault.contains(binding: settings.binding)
@@ -78,7 +97,7 @@ public final class MobileCredentialClient {
}
public func changeBiometric(realm: String, membership: String, authDevice: String, target: String,
enabled: Bool, locale: String) async throws {
- guard UIApplication.shared.applicationState == .active, authentication == nil, !operationPending else { throw CredentialError.cancelled }
+ guard foreground, authentication == nil, !operationPending else { throw CredentialError.cancelled }
let vault = try localVault(), key = try settingsKey(realm: realm, membership: membership, authDevice: authDevice, target: target)
guard let settings = try vault.settings(key) else { throw CredentialError.unavailable }
if settings.biometric == enabled { return }
@@ -104,8 +123,7 @@ public final class MobileCredentialClient {
}
let next = Owner(realm: realm, membership: membership, authDevice: authDevice)
if owner != next { reset(); owner = next }
- let root = try FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask,
- appropriateFor: nil, create: true).appendingPathComponent("CindyRemoteCredentials", isDirectory: true)
+ let root = try storageRoot()
let installation = try InstallationMarker.loadOrCreate(directory: root)
let signingKey = try InstallationIdentity.loadOrCreate(installation: installation, realm: realm)
let descriptor = LocalCredentialIdentity(device: authDevice, membership: membership, realm: realm, publicKey: signingKey.publicKey)
@@ -120,7 +138,7 @@ public final class MobileCredentialClient {
public func pushHeaders(method: String, body: [String: String]) async throws -> [String: String] { throw CredentialError.unavailable }
public func begin(target: String, descriptor: String = "", setup: Bool = false, savedOnly: Bool = false, biometric: Bool = false) async throws -> [String: String] {
- guard UIApplication.shared.applicationState == .active,
+ guard foreground,
let owner, let key, let vault else { throw CredentialError.unavailable }
close()
let local = LocalCredentialIdentity(device: owner.authDevice, membership: owner.membership, realm: owner.realm, publicKey: key.publicKey).peer
@@ -182,7 +200,7 @@ public final class MobileCredentialClient {
public func password(handle: String, useSaved: Bool, locale: String, theme: String) async throws -> String {
let state = try current(handle)
try state.channel.requireConfirmed()
- guard UIApplication.shared.applicationState == .active else { throw CredentialError.applicationInactive }
+ guard foreground else { throw CredentialError.applicationInactive }
guard let account = state.account, let vault else { throw CredentialError.unavailable }
guard form == nil, authentication == nil, !operationPending else { throw CredentialError.authenticationBusy }
operationPending = true
@@ -214,22 +232,28 @@ public final class MobileCredentialClient {
// Cancellation is never turned into another prompt.
}
}
+ #if os(iOS)
guard let scene = UIApplication.shared.connectedScenes.compactMap({ $0 as? UIWindowScene })
.first(where: { $0.activationState == .foregroundActive }),
var presenter = scene.windows.first(where: { $0.isKeyWindow })?.rootViewController else {
throw CredentialError.unavailable
}
while let presented = presenter.presentedViewController { presenter = presented }
+ #endif
defer { if session === state { form = nil } }
let entered: (Data, Bool) = try await withCheckedThrowingContinuation { continuation in
let form = CredentialPasswordForm(labels: labels, account: account.name, saveRequired: state.setup) {
continuation.resume(with: $0.mapError { $0 as Error })
}
- form.overrideUserInterfaceStyle = theme == "dark" ? .dark : .light
self.form = form
+ #if os(iOS)
+ form.overrideUserInterfaceStyle = theme == "dark" ? .dark : .light
presenter.present(form, animated: true)
+ #else
+ form.present(theme: theme)
+ #endif
}
- guard session === state, UIApplication.shared.applicationState == .active else { throw CredentialError.cancelled }
+ guard session === state, foreground else { throw CredentialError.cancelled }
_ = try current(handle)
state.usedSavedPassword = false
return try state.controller.authenticate(password: entered.0, remember: state.setup || entered.1)
@@ -292,11 +316,15 @@ public final class MobileCredentialClient {
private func requireForeground(epoch: UInt64) throws {
let state: CredentialForegroundGate.State
+ #if os(iOS)
switch UIApplication.shared.applicationState {
case .active: state = .active
case .inactive: state = .inactive
default: state = .background
}
+ #else
+ state = foreground ? .active : .background
+ #endif
try CredentialForegroundGate.require(isCurrent: generation == epoch, state: state)
}
diff --git a/packages/remote-credentials-native/Sources/CindyRemoteCredentials/Resources/credentials.json b/packages/remote-credentials-native/Sources/CindyRemoteCredentials/Resources/credentials.json
index 19bd3f9659..b1093d22d3 100644
--- a/packages/remote-credentials-native/Sources/CindyRemoteCredentials/Resources/credentials.json
+++ b/packages/remote-credentials-native/Sources/CindyRemoteCredentials/Resources/credentials.json
@@ -1,6 +1,7 @@
{
"en": {
"title": "Mac Login Password",
+ "accountFormat": "Account: %@",
"password": "Password",
"remember": "Remember on this phone",
"explanation": "Your password is encrypted and stored only on this device.",
@@ -12,6 +13,7 @@
},
"zh-CN": {
"title": "Mac 登录密码",
+ "accountFormat": "账号:%@",
"password": "密码",
"remember": "在此手机记住密码",
"explanation": "密码仅加密后保存在本地。",
@@ -23,6 +25,7 @@
},
"zh-TW": {
"title": "Mac 登入密碼",
+ "accountFormat": "帳號:%@",
"password": "密碼",
"remember": "在此手機記住密碼",
"explanation": "密碼僅加密後儲存在本機。",
@@ -34,6 +37,7 @@
},
"ja": {
"title": "Mac のログインパスワード",
+ "accountFormat": "アカウント:%@",
"password": "パスワード",
"remember": "このスマートフォンに保存",
"explanation": "パスワードは暗号化して、このデバイスにのみ保存されます。",
@@ -45,6 +49,7 @@
},
"ko": {
"title": "Mac 로그인 암호",
+ "accountFormat": "계정: %@",
"password": "암호",
"remember": "이 휴대폰에 암호 저장",
"explanation": "암호는 암호화되어 이 기기에만 저장됩니다.",
diff --git a/packages/remote-credentials-native/Sources/CredentialHost/main.swift b/packages/remote-credentials-native/Sources/CredentialHost/main.swift
index 33434710c6..60a84c1237 100644
--- a/packages/remote-credentials-native/Sources/CredentialHost/main.swift
+++ b/packages/remote-credentials-native/Sources/CredentialHost/main.swift
@@ -3,16 +3,24 @@ import CindyRemoteCredentials
import DesktopNativeCaller
#if os(macOS)
+import AppKit
/// The parent process is authenticated before arguments, storage or network are
/// touched. Only fixed errors are emitted; no diagnostic may echo a payload.
@main
@MainActor
struct CredentialHostMain {
- static func main() async {
+ static func main() {
+ let application = NSApplication.shared
+ application.setActivationPolicy(.accessory)
+ Task { await serve(); application.terminate(nil) }
+ application.run()
+ }
+ static func serve() async {
guard let caller = DesktopInputCaller.authenticate(resourceName: "cindy-macos-remote-credentials",
developmentExecutable: "CREDENTIAL_HOST_DEVELOPMENT_EXECUTABLE"),
CommandLine.arguments.count == 2, CommandLine.arguments[1].hasPrefix("/") else { exit(77) }
let host = HostCredentialServer(directory: URL(fileURLWithPath: CommandLine.arguments[1], isDirectory: true))
+ let viewer = MobileCredentialClient(storageDirectory: URL(fileURLWithPath: CommandLine.arguments[1], isDirectory: true).appendingPathComponent("viewer"))
let input = AsyncStream<Data>(bufferingPolicy: .bufferingOldest(64)) { continuation in
DispatchQueue(label: "cindy.credential.stdin").async {
var buffer = Data()
@@ -42,6 +50,17 @@ struct CredentialHostMain {
}
let result: Any
switch method {
+ case "viewerSettings": result = try viewer.savedSettings(realm: string("realm"), membership: string("membership"), authDevice: string("authDevice"), target: string("target"))
+ case "viewerConfigure": result = try await viewer.configure(realm: string("realm"), membership: string("membership"), authDevice: string("authDevice"), token: string("token"))
+ case "viewerBegin": result = try await viewer.begin(target: string("target"), descriptor: string("descriptor"), setup: value["setup"] as? Bool == true, savedOnly: value["setup"] as? Bool != true, biometric: value["biometric"] as? Bool == true)
+ case "viewerAccept": result = try viewer.accept(handle: string("handle"), offer: string("offer"))
+ case "viewerReceive": result = try viewer.receive(handle: string("handle"), ciphertext: string("ciphertext"))
+ case "viewerPassword": result = try await viewer.password(handle: string("handle"), useSaved: value["saved"] as? Bool == true, locale: string("locale"), theme: string("theme"))
+ case "viewerAuthenticationStatus": result = try viewer.authenticationStatus(handle: string("handle"))
+ case "viewerForget": try viewer.forgetSaved(realm: string("realm"), membership: string("membership"), authDevice: string("authDevice"), target: string("target")); result = true
+ case "viewerBiometric": try await viewer.changeBiometric(realm: string("realm"), membership: string("membership"), authDevice: string("authDevice"), target: string("target"), enabled: value["enabled"] as? Bool == true, locale: string("locale")); result = true
+ case "viewerEnd": result = viewer.end(handle: try string("handle")) ?? ""
+ case "viewerReset": viewer.reset(); result = true
case "configure": result = try await host.configure(realm: string("realm"), membership: string("membership"),
authDevice: string("authDevice"), token: string("token"))
case "updateToken":
@@ -69,7 +88,7 @@ struct CredentialHostMain {
for await line in input {
let value = try? JSONSerialization.jsonObject(with: line) as? [String: Any]
let method = value?["method"] as? String
- if let method, ["configure", "relayHeaders", "begin", "receive"].contains(method) {
+ if let method, ["configure", "relayHeaders", "begin", "receive", "viewerConfigure", "viewerBegin", "viewerPassword", "viewerBiometric"].contains(method) {
// Setup and OS unlock await independently. Status and close must remain
// serviceable while an authentication attempt is awaiting loginwindow.
guard networkJobs.count < 8 else {
@@ -82,6 +101,7 @@ struct CredentialHostMain {
}
for job in networkJobs.values { job.cancel() }
host.reset()
+ viewer.reset()
}
private static func emit(_ value: [String: Any]) throws {
var data = try JSONSerialization.data(withJSONObject: value); data.append(10)
diff --git a/packages/remote-credentials-native/Tests/CindyRemoteCredentialsTests/MacCredentialSecretTests.swift b/packages/remote-credentials-native/Tests/CindyRemoteCredentialsTests/MacCredentialSecretTests.swift
new file mode 100644
index 0000000000..e75f86aaca
--- /dev/null
+++ b/packages/remote-credentials-native/Tests/CindyRemoteCredentialsTests/MacCredentialSecretTests.swift
@@ -0,0 +1,71 @@
+#if os(macOS)
+import CryptoKit
+import Foundation
+import LocalAuthentication
+import Security
+import XCTest
+@testable import CindyRemoteCredentials
+
+final class MacCredentialSecretTests: XCTestCase {
+ private let binding = CredentialBinding(realm: .global, membership: "test-owner",
+ controller: "test-controller", target: "test-target", targetThumbprint: "test-pin", systemRecord: "test-account")
+ func testOptionalBiometricEnvelopeRoundTrip() throws {
+ let secret = Data("synthetic-test-only".utf8)
+ let encoded = try MacCredentialSecret.seal(secret, binding: binding, biometric: false)
+ XCTAssertEqual(try MacCredentialSecret.open(encoded, binding: binding, context: LAContext()), secret)
+ XCTAssertThrowsError(try MacCredentialSecret.open(Data("{}".utf8), binding: binding, context: LAContext()))
+ }
+ func testProtectedEnvelopeCannotDecryptWithoutBiometricAuthorization() throws {
+ guard SecureEnclave.isAvailable else { throw XCTSkip("Secure Enclave unavailable") }
+ let secret = Data("synthetic-test-only".utf8)
+ let encoded = try MacCredentialSecret.seal(secret, binding: binding, biometric: true)
+ let object = try XCTUnwrap(JSONSerialization.jsonObject(with: encoded) as? [String: Any])
+ XCTAssertNil(object["plain"])
+ let context = LAContext(); context.interactionNotAllowed = true
+ defer { context.invalidate() }
+ XCTAssertThrowsError(try MacCredentialSecret.open(encoded, binding: binding, context: context))
+ }
+ func testHardwareEnvelopeRoundTripAndBindingAuthentication() throws {
+ guard SecureEnclave.isAvailable else { throw XCTSkip("Secure Enclave unavailable") }
+ // A transient test-only key without biometric gating lets CI exercise the
+ // same envelope decoding and ECDH path without displaying an OS prompt.
+ let access = try XCTUnwrap(SecAccessControlCreateWithFlags(nil,
+ kSecAttrAccessibleWhenUnlockedThisDeviceOnly, .privateKeyUsage, nil))
+ let recipient = try SecureEnclave.P256.KeyAgreement.PrivateKey(accessControl: access)
+ let sender = P256.KeyAgreement.PrivateKey()
+ let shared = try sender.sharedSecretFromKeyAgreement(with: recipient.publicKey)
+ let bindingData = Data(try binding.storageKey.utf8)
+ let key = shared.hkdfDerivedSymmetricKey(using: SHA256.self, salt: bindingData,
+ sharedInfo: Data("cindy.desktop.saved-password.v1".utf8), outputByteCount: 32)
+ let secret = Data("synthetic-test-only".utf8)
+ let sealed = try AES.GCM.seal(secret, using: key, authenticating: bindingData)
+ let encoded = try JSONSerialization.data(withJSONObject: ["version": 1,
+ "key": recipient.dataRepresentation.base64EncodedString(),
+ "peer": sender.publicKey.x963Representation.base64EncodedString(),
+ "sealed": try XCTUnwrap(sealed.combined).base64EncodedString()])
+ XCTAssertEqual(try MacCredentialSecret.open(encoded, binding: binding, context: LAContext()), secret)
+ var other = binding; other.protectionVersion = "another-saved-version"
+ XCTAssertThrowsError(try MacCredentialSecret.open(encoded, binding: other, context: LAContext()))
+ }
+ func testLoginKeychainVaultRoundTripAndForgetPreservesPreference() throws {
+ guard ProcessInfo.processInfo.environment["CINDY_TEST_LOGIN_KEYCHAIN"] == "1" else {
+ throw XCTSkip("Opt-in test creates and removes only its own synthetic Keychain items")
+ }
+ let installation = UUID(), settingsKey = "synthetic-test-settings"
+ let vault = CredentialVault(installation: installation)
+ defer {
+ try? vault.forget(binding: binding)
+ try? vault.setSettings(settingsKey, nil)
+ }
+ let secret = Data("synthetic-test-only".utf8)
+ try vault.store(secret, binding: binding, requireBiometric: false)
+ try vault.setSettings(settingsKey, SavedUnlockSettings(binding: binding, biometric: false))
+ XCTAssertTrue(try vault.contains(binding: binding))
+ XCTAssertEqual(try vault.read(binding: binding, reason: "Synthetic test", context: LAContext()), secret)
+ try vault.forget(binding: binding)
+ XCTAssertFalse(try vault.contains(binding: binding))
+ XCTAssertEqual(try vault.settings(settingsKey)?.binding, binding)
+ XCTAssertEqual(try vault.settings(settingsKey)?.biometric, false)
+ }
+}
+#endif
diff --git a/scripts/shared/design-inventory.mjs b/scripts/shared/design-inventory.mjs
index 1df6ada8cd..b7cf98ede0 100644
--- a/scripts/shared/design-inventory.mjs
+++ b/scripts/shared/design-inventory.mjs
@@ -784,10 +784,13 @@ export function catalogSurfaces() {
platform: 'desktop',
title: '远程桌面独立窗口',
productionEntry: '`?remoteDesktopViewer=1` → renderer/remote-desktop-viewer-entry.tsx',
- reachableComponents: ['RemoteDesktopViewerWindow', 'Select', 'Button', 'FormField', 'ConfirmDialog'],
+ reachableComponents: ['RemoteDesktopViewerWindow', 'Select', 'Button', 'FormField', 'ConfirmDialog', 'Switch', 'Popover', 'Tip'],
styleRoots: [
'apps/desktop/src/renderer/remote-desktop-viewer-entry.tsx',
'apps/desktop/src/renderer/features/remote-desktop/RemoteDesktopViewerWindow.tsx',
+ 'apps/desktop/src/renderer/components/ui/popover.tsx',
+ 'apps/desktop/src/renderer/components/ui/tooltip.tsx',
+ 'apps/desktop/src/renderer/components/ui/switch.tsx',
'apps/desktop/src/renderer/components/ui/confirm-dialog.tsx',
'apps/desktop/src/renderer/components/ui/select.tsx',
'apps/desktop/src/renderer/components/ui/button.tsx',