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。

fix(mobile): 保持远程桌面分辨率与适配比例一致 (#4681)

Dash726896735f64
diff --git a/apps/mobile/src/i18n/locales/en/remoteDesktop.json b/apps/mobile/src/i18n/locales/en/remoteDesktop.json
index 4f6749c3d7..f7d44a27c7 100644
--- a/apps/mobile/src/i18n/locales/en/remoteDesktop.json
+++ b/apps/mobile/src/i18n/locales/en/remoteDesktop.json
@@ -128,7 +128,7 @@
"loadingSettings": "Loading…",
"retrySettings": "Could not load. Tap to retry.",
"followComputer": "Follow computer",
- "restoreViewerDisplay": "Restore Original Resolution",
+ "restoreViewerDisplay": "Restore Original Aspect Ratio",
"fitViewerDisplay": "Fit to This Screen",
"fitViewerDisplayHint": "Adjusts the computer resolution to reduce empty space.",
"resolutionHint": "Resolution changes affect the computer’s window layout. The original resolution is restored on disconnect.",
diff --git a/apps/mobile/src/i18n/locales/ja/remoteDesktop.json b/apps/mobile/src/i18n/locales/ja/remoteDesktop.json
index 70e6c60d2e..c52b51ba0b 100644
--- a/apps/mobile/src/i18n/locales/ja/remoteDesktop.json
+++ b/apps/mobile/src/i18n/locales/ja/remoteDesktop.json
@@ -128,7 +128,7 @@
"loadingSettings": "読み込み中…",
"retrySettings": "読み込み失敗。タップして再試行。",
"followComputer": "パソコンに合わせる",
- "restoreViewerDisplay": "元の解像度に戻す",
+ "restoreViewerDisplay": "元の縦横比に戻す",
"fitViewerDisplay": "この画面に合わせる",
"fitViewerDisplayHint": "パソコンの解像度を調整して余白を減らします。",
"resolutionHint": "解像度の変更はパソコンのウィンドウ配置に影響します。切断すると元の解像度に戻ります。",
diff --git a/apps/mobile/src/i18n/locales/ko/remoteDesktop.json b/apps/mobile/src/i18n/locales/ko/remoteDesktop.json
index 0adf248237..f5e85c3d8c 100644
--- a/apps/mobile/src/i18n/locales/ko/remoteDesktop.json
+++ b/apps/mobile/src/i18n/locales/ko/remoteDesktop.json
@@ -128,7 +128,7 @@
"loadingSettings": "불러오는 중…",
"retrySettings": "불러오지 못했습니다. 눌러서 다시 시도하세요.",
"followComputer": "컴퓨터에 맞춤",
- "restoreViewerDisplay": "원래 해상도로 복원",
+ "restoreViewerDisplay": "원래 화면 비율로 복원",
"fitViewerDisplay": "이 화면에 맞추기",
"fitViewerDisplayHint": "컴퓨터 해상도를 조정해 여백을 줄입니다.",
"resolutionHint": "해상도를 변경하면 컴퓨터의 창 배치가 바뀝니다. 연결 종료 시 원래 해상도로 복원됩니다.",
diff --git a/apps/mobile/src/i18n/locales/zh-CN/remoteDesktop.json b/apps/mobile/src/i18n/locales/zh-CN/remoteDesktop.json
index 4972ea0cf2..90db70d247 100644
--- a/apps/mobile/src/i18n/locales/zh-CN/remoteDesktop.json
+++ b/apps/mobile/src/i18n/locales/zh-CN/remoteDesktop.json
@@ -129,7 +129,7 @@
"retrySettings": "读取失败,轻点重试",
"followComputer": "跟随电脑",
"resolutionHint": "调整分辨率会改变电脑上的窗口布局,断开连接后恢复原分辨率。",
- "restoreViewerDisplay": "恢复原分辨率",
+ "restoreViewerDisplay": "恢复原始比例",
"fitViewerDisplay": "适配此屏幕",
"fitViewerDisplayHint": "调整电脑分辨率,减少留白。",
"resolutionControlHint": "接管操作后可以更改分辨率。",
diff --git a/apps/mobile/src/i18n/locales/zh-TW/remoteDesktop.json b/apps/mobile/src/i18n/locales/zh-TW/remoteDesktop.json
index d481dc3ad2..c4d3a9c22a 100644
--- a/apps/mobile/src/i18n/locales/zh-TW/remoteDesktop.json
+++ b/apps/mobile/src/i18n/locales/zh-TW/remoteDesktop.json
@@ -128,7 +128,7 @@
"loadingSettings": "讀取中…",
"retrySettings": "讀取失敗,輕點重試",
"followComputer": "跟隨電腦",
- "restoreViewerDisplay": "恢復原解析度",
+ "restoreViewerDisplay": "恢復原始比例",
"fitViewerDisplay": "配合此螢幕",
"fitViewerDisplayHint": "調整電腦解析度,減少留白。",
"resolutionHint": "調整解析度會改變電腦上的視窗配置,中斷連線後恢復原解析度。",
diff --git a/apps/mobile/src/remote-desktop/RemoteDesktopScreen.tsx b/apps/mobile/src/remote-desktop/RemoteDesktopScreen.tsx
index 73018f754c..ae02ea4876 100644
--- a/apps/mobile/src/remote-desktop/RemoteDesktopScreen.tsx
+++ b/apps/mobile/src/remote-desktop/RemoteDesktopScreen.tsx
@@ -35,6 +35,7 @@ import {
import { useSafeAreaInsets } from "react-native-safe-area-context";
import { WebView, type WebViewMessageEvent } from "react-native-webview";
import { connectionDiagnostics } from "./connectionDiagnostics";
+import { fittedDisplayModes } from "./fittedDisplayModes";
import { controlFailureAction, remoteDesktopErrorCode } from "./controlFailure";
import { transferClipboardContent } from "./clipboardTransfer";
import * as Clipboard from "expo-clipboard";
@@ -307,7 +308,10 @@ export function RemoteDesktopSession({
const unlockFrame = useRef<((presented: boolean) => void) | null>(null);
const inputBusy = useRef<string | null>(null);
const [lease, setLease] = useState<RemoteDesktopLease | null>(null);
- const [viewerDisplayApplied, setViewerDisplayApplied] = useState(false);
+ const [fittedDisplay, setFittedDisplay] = useState<{
+ width: number;
+ height: number;
+ } | null>(null);
const [viewerViewport, setViewerViewport] = useState({ width: 0, height: 0 });
const viewportGeneration = useRef(0);
const matchesViewer = (
@@ -319,10 +323,10 @@ export function RemoteDesktopSession({
height > 0 &&
Math.abs(display.width / display.height - width / height) < 0.003;
const viewerDisplayMatched = Boolean(
- viewerDisplayApplied &&
+ fittedDisplay &&
caps?.viewerDisplayRestore &&
lease &&
- matchesViewer(lease.display, viewerViewport.width, viewerViewport.height),
+ matchesViewer(fittedDisplay, viewerViewport.width, viewerViewport.height),
);
exitLock.current =
lockOnExitLoaded && lockOnExit && caps?.lockOnExit === true;
@@ -673,7 +677,7 @@ export function RemoteDesktopSession({
send({ type: "stop", preserveFrame, epoch: previous?.lease });
if (alive.current) {
setLease(null);
- setViewerDisplayApplied(false);
+ setFittedDisplay(null);
setFrameReady(false);
setControlReady(false);
setCanPip(false);
@@ -2230,6 +2234,23 @@ export function RemoteDesktopSession({
void remotePresentation?.playback(false).catch(() => {});
}
};
+ const readResolutionModes = async (): Promise<RemoteDesktopDisplayMode[]> => {
+ const current = active.current;
+ if (!current) return [];
+ if (fittedDisplay)
+ return fittedDisplayModes(fittedDisplay, current.display);
+ const { width, height } = current.display;
+ const modes = await request<RemoteDesktopDisplayMode[]>({
+ op: "displayModes",
+ lease: current.lease,
+ });
+ // CoreGraphics modes keep their own orientation when Electron's display
+ // geometry is rotated. Compare modes in the enumeration's coordinate space.
+ const reference = modes.find((mode) => mode.current) ?? { width, height };
+ return modes.filter((mode) =>
+ matchesViewer(mode, reference.width, reference.height),
+ );
+ };
const changeResolution = async (modeId: string) => {
const current = active.current;
if (!current?.controlling || settingInFlight.current) return;
@@ -2238,9 +2259,7 @@ export function RemoteDesktopSession({
(caps?.viewerDisplay && caps.viewerDisplayRestore)
) {
try {
- const modes = await request<
- import("@cindy/device-link").RemoteDesktopDisplayMode[]
- >({ op: "displayModes", lease: current.lease });
+ const modes = await readResolutionModes();
if (active.current !== current) return;
const mode = modes.find((item) => item.id === modeId);
if (!mode) throw new Error("DESKTOP_DISPLAY_MODE_MISSING");
@@ -2252,7 +2271,7 @@ export function RemoteDesktopSession({
mode.width,
mode.height,
true,
- caps?.resolutionRestore ? mode.id : undefined,
+ !fittedDisplay && caps?.resolutionRestore ? mode.id : undefined,
);
return;
}
@@ -2302,9 +2321,9 @@ export function RemoteDesktopSession({
viewerMedia.reset();
const restore = Boolean(
!exactResolution &&
- viewerDisplayApplied &&
+ fittedDisplay &&
caps?.viewerDisplayRestore &&
- matchesViewer(current.display, width, height),
+ matchesViewer(fittedDisplay, width, height),
);
const requestGeneration = viewportGeneration.current;
const next = await viewerSession.current.fitDisplay(
@@ -2318,7 +2337,13 @@ export function RemoteDesktopSession({
recovery.current.displayId = sourceDisplayId;
if (!exactResolution && viewportGeneration.current === requestGeneration)
setViewerViewport({ width, height });
- setViewerDisplayApplied(!modeId && !restore);
+ setFittedDisplay((previous) =>
+ modeId || restore
+ ? null
+ : exactResolution && previous
+ ? previous
+ : { width: next.display.width, height: next.display.height },
+ );
setLease({ ...next });
streaming.current = false;
setCanPip(false);
@@ -2770,7 +2795,7 @@ export function RemoteDesktopSession({
busy: settingBusy,
modesSupported: Boolean(caps?.displayModes),
displayGeometry: lease
- ? `${lease.display.id}:${lease.display.width}:${lease.display.height}`
+ ? `${lease.display.id}:${lease.display.width}:${lease.display.height}:${Boolean(fittedDisplay)}`
: undefined,
viewerDisplaySupported: caps?.viewerDisplay === true,
viewerDisplayMatched,
@@ -2781,13 +2806,7 @@ export function RemoteDesktopSession({
onChange: (settings) => {
void changeVideoSettings(settings);
},
- readModes: () =>
- active.current
- ? request<RemoteDesktopDisplayMode[]>({
- op: "displayModes",
- lease: active.current.lease,
- })
- : Promise.resolve([]),
+ readModes: readResolutionModes,
onResolution: changeResolution,
}}
inputMode={inputMode}
diff --git a/apps/mobile/src/remote-desktop/__tests__/fittedDisplayModes.test.ts b/apps/mobile/src/remote-desktop/__tests__/fittedDisplayModes.test.ts
new file mode 100644
index 0000000000..8eadd2d201
--- /dev/null
+++ b/apps/mobile/src/remote-desktop/__tests__/fittedDisplayModes.test.ts
@@ -0,0 +1,34 @@
+import { describe, expect, it } from "vitest";
+import { fittedDisplayModes } from "../fittedDisplayModes";
+
+describe("fittedDisplayModes", () => {
+ it.each([
+ { width: 986, height: 1920 },
+ { width: 1920, height: 986 },
+ { width: 320, height: 1920 },
+ ])("offers bounded same-ratio sizes for $width × $height", (fitted) => {
+ const modes = fittedDisplayModes(fitted, fitted);
+ expect(modes.length).toBeGreaterThan(1);
+ expect(modes.filter((mode) => mode.current)).toEqual([
+ expect.objectContaining(fitted),
+ ]);
+ expect(new Set(modes.map((mode) => mode.id)).size).toBe(modes.length);
+ for (const mode of modes) {
+ expect(mode.native).toBeUndefined();
+ for (const size of [mode.width, mode.height]) {
+ expect(size).toBeGreaterThanOrEqual(320);
+ expect(size).toBeLessThanOrEqual(2560);
+ }
+ expect(
+ Math.abs(mode.width / mode.height / (fitted.width / fitted.height) - 1),
+ ).toBeLessThan(0.003);
+ }
+ const next = modes.find((mode) => !mode.current)!;
+ expect(fittedDisplayModes(fitted, next).map((mode) => mode.id)).toEqual(
+ modes.map((mode) => mode.id),
+ );
+ expect(
+ fittedDisplayModes(fitted, next).filter((mode) => mode.current),
+ ).toEqual([{ ...next, current: true }]);
+ });
+});
diff --git a/apps/mobile/src/remote-desktop/__tests__/screen.test.tsx b/apps/mobile/src/remote-desktop/__tests__/screen.test.tsx
index b759c240b7..6b96086ac4 100644
--- a/apps/mobile/src/remote-desktop/__tests__/screen.test.tsx
+++ b/apps/mobile/src/remote-desktop/__tests__/screen.test.tsx
@@ -1609,135 +1609,191 @@ describe("remote desktop controls", () => {
act(() => button()?.click());
expect(onFitDisplay).toHaveBeenCalledOnce();
});
- it("changes 4K system resolution on a restorable lease without reconnecting", async () => {
- const original = fixture.invoke.getMockImplementation()!;
- fixture.invoke.mockImplementation(async (...args) => {
- const request = args[2][0];
- if (request.op === "capabilities")
- return {
- ...(await original(...args)),
- viewerDisplay: true,
- viewerDisplayRestore: true,
- resolutionRestore: true,
- videoSettings: true,
- displayModes: true,
- };
- if (request.op === "displayModes")
- return [{ id: "4k", width: 3840, height: 2160, current: false }];
- if (request.op === "resolution")
- return {
- lease: "lease",
- display: { id: "display", width: 3840, height: 2160 },
- controlling: false,
- };
- return original(...args);
- });
- await connect();
- act(() => button("operations").click());
- await act(async () => button("displaySettings").click());
- act(() => button("resolution").click());
- const mode = Array.from(
- host.querySelectorAll<HTMLButtonElement>("button"),
- ).find((item) => item.textContent === "3840 × 2160")!;
- expect(mode).toBeDefined();
- await act(async () => mode.click());
- expect(requests()).toContainEqual({
- op: "resolution",
- lease: "lease",
- modeId: "4k",
- temporary: true,
- });
- expect(requests().some((request) => request.op === "viewerDisplay")).toBe(
- false,
- );
- expect(requests().some((request) => request.op === "stop")).toBe(false);
- expect(sent()).toContainEqual(
- expect.objectContaining({
- type: "videoSettings",
- width: 3840,
- restore: true,
- }),
- );
- });
+ it.each([false, true])(
+ "changes 4K system resolution without reconnecting (rotated monitor: %s)",
+ async (rotated) => {
+ const original = fixture.invoke.getMockImplementation()!;
+ fixture.invoke.mockImplementation(async (...args) => {
+ const request = args[2][0];
+ if (request.op === "start" && rotated)
+ return {
+ ...(await original(...args)),
+ display: {
+ ...display,
+ width: display.height,
+ height: display.width,
+ },
+ };
+ if (request.op === "capabilities")
+ return {
+ ...(await original(...args)),
+ viewerDisplay: true,
+ viewerDisplayRestore: true,
+ resolutionRestore: true,
+ videoSettings: true,
+ displayModes: true,
+ };
+ if (request.op === "displayModes")
+ return [
+ { id: "current", width: 1920, height: 1080, current: true },
+ { id: "4k", width: 3840, height: 2160, current: false },
+ { id: "4:3", width: 1024, height: 768, current: false },
+ ];
+ if (request.op === "resolution")
+ return {
+ lease: "lease",
+ display: { id: "display", width: 3840, height: 2160 },
+ controlling: false,
+ };
+ return original(...args);
+ });
+ await connect();
+ act(() => button("operations").click());
+ await act(async () => button("displaySettings").click());
+ act(() => button("resolution").click());
+ const mode = Array.from(
+ host.querySelectorAll<HTMLButtonElement>("button"),
+ ).find((item) => item.textContent === "3840 × 2160")!;
+ expect(mode).toBeDefined();
+ expect(host.textContent).toContain("1920 × 1080");
+ expect(host.textContent).not.toContain("1024 × 768");
+ await act(async () => mode.click());
+ expect(requests()).toContainEqual({
+ op: "resolution",
+ lease: "lease",
+ modeId: "4k",
+ temporary: true,
+ });
+ expect(requests().some((request) => request.op === "viewerDisplay")).toBe(
+ false,
+ );
+ expect(requests().some((request) => request.op === "stop")).toBe(false);
+ expect(sent()).toContainEqual(
+ expect.objectContaining({
+ type: "videoSettings",
+ width: 3840,
+ restore: true,
+ }),
+ );
+ },
+ );
- it("changes portrait resolution without stopping the temporary display and stops it on exit", async () => {
- const original = fixture.invoke.getMockImplementation()!;
- fixture.invoke.mockImplementation(async (...args) => {
- const request = args[2][0];
- if (request.op === "capabilities")
- return {
- ...(await original(...args)),
- viewerDisplay: true,
- viewerDisplayRestore: true,
- videoSettings: true,
- displayModes: true,
- };
- if (request.op === "displayModes")
- return [{ id: "640", width: 640, height: 1242, current: false }];
- if (request.op === "viewerDisplay")
- return {
- lease: "lease",
- controlling: false,
- display: {
- ...display,
- id: "virtual",
- width: request.width,
- height: request.height,
+ it.each([
+ [false, 390, 760, 658, 1280],
+ [true, 760, 390, 1280, 658],
+ ])(
+ "keeps fitted resolution choices until explicit restore (native menu: %s)",
+ async (nativeMenus, width, height, modeWidth, modeHeight) => {
+ fixture.nativeMenus = nativeMenus;
+ const original = fixture.invoke.getMockImplementation()!;
+ fixture.invoke.mockImplementation(async (...args) => {
+ const request = args[2][0];
+ if (request.op === "capabilities")
+ return {
+ ...(await original(...args)),
+ viewerDisplay: true,
+ viewerDisplayRestore: true,
+ resolutionRestore: true,
+ videoSettings: true,
+ displayModes: true,
+ };
+ if (request.op === "displayModes")
+ return [
+ { id: "4k", width: 3840, height: 2160, current: false },
+ { id: "4:3", width: 1024, height: 768, current: false },
+ ];
+ if (request.op === "restoreViewerDisplay")
+ return { lease: "lease", controlling: false, display };
+ if (request.op === "viewerDisplay")
+ return {
+ lease: "lease",
+ controlling: false,
+ display: {
+ ...display,
+ id: "virtual",
+ width: request.width,
+ height: request.height,
+ },
+ };
+ return original(...args);
+ });
+ await connect();
+ act(() => button("operations").click());
+ act(() => button("displaySettings").click());
+ await act(async () =>
+ fixture.message!({
+ nativeEvent: {
+ data: JSON.stringify({
+ type: "viewportSize",
+ epoch: "lease",
+ width,
+ height,
+ }),
},
- };
- return original(...args);
- });
- await connect();
- act(() => button("operations").click());
- act(() => button("displaySettings").click());
- await act(async () =>
- fixture.message!({
- nativeEvent: {
- data: JSON.stringify({
- type: "viewportSize",
- epoch: "lease",
- width: 390,
- height: 760,
- }),
- },
- }),
- );
- act(() => button("resolution").click());
- const mode = Array.from(
- host.querySelectorAll<HTMLButtonElement>("button"),
- ).find((item) => item.textContent === "640 × 1242")!;
- expect(mode).toBeDefined();
- await act(async () => mode.click());
- expect(
- requests()
- .filter((request) => request.op === "viewerDisplay")
- .at(-1),
- ).toEqual({
- op: "viewerDisplay",
- lease: "lease",
- width: 640,
- height: 1242,
- });
- expect(
- requests().filter(
- (request) =>
- request.op === "resolution" ||
- request.op === "restoreViewerDisplay" ||
- request.op === "stop",
- ),
- ).toHaveLength(0);
- expect(requests().filter((request) => request.op === "start")).toHaveLength(
- 1,
- );
- expect(host.textContent).not.toContain(
- "remoteDesktop.resolutionControlHint",
- );
- await act(async () => root.unmount());
- mounted = false;
- expect(requests().filter((request) => request.op === "stop")).toHaveLength(
- 1,
- );
- });
+ }),
+ );
+ act(() => button("resolution").click());
+ const mode = Array.from(
+ host.querySelectorAll<HTMLButtonElement>("button"),
+ ).find((item) => item.textContent === `${modeWidth} × ${modeHeight}`)!;
+ expect(mode).toBeDefined();
+ expect(host.textContent).not.toContain("3840 × 2160");
+ await act(async () => mode.click());
+ expect(
+ requests()
+ .filter((request) => request.op === "viewerDisplay")
+ .at(-1),
+ ).toEqual({
+ op: "viewerDisplay",
+ lease: "lease",
+ width: modeWidth,
+ height: modeHeight,
+ });
+ expect(
+ requests().filter(
+ (request) =>
+ request.op === "resolution" ||
+ request.op === "restoreViewerDisplay" ||
+ request.op === "stop",
+ ),
+ ).toHaveLength(0);
+ expect(
+ requests().filter((request) => request.op === "start"),
+ ).toHaveLength(1);
+ expect(host.textContent).not.toContain(
+ "remoteDesktop.resolutionControlHint",
+ );
+ expect(button("restoreViewerDisplay").disabled).toBe(false);
+ act(() => button("resolution").click());
+ expect(host.textContent).not.toContain("3840 × 2160");
+ expect(host.textContent).toContain(`${modeWidth} × ${modeHeight}`);
+ act(() => button("restoreViewerDisplay").click());
+ await act(async () =>
+ fixture.message!({
+ nativeEvent: {
+ data: JSON.stringify({
+ type: "viewportSize",
+ epoch: "lease",
+ width,
+ height,
+ }),
+ },
+ }),
+ );
+ expect(
+ requests().filter((request) => request.op === "restoreViewerDisplay"),
+ ).toHaveLength(1);
+ expect(button("fitViewerDisplay").disabled).toBe(false);
+ expect(host.textContent).toContain("3840 × 2160");
+ expect(host.textContent).not.toContain("1024 × 768");
+ expect(host.textContent).not.toContain(`${modeWidth} × ${modeHeight}`);
+ await act(async () => root.unmount());
+ mounted = false;
+ expect(
+ requests().filter((request) => request.op === "stop"),
+ ).toHaveLength(1);
+ },
+ );
it.each([true, false])(
"keeps matching after rotation and supports restore when advertised: %s",
async (canRestore) => {
diff --git a/apps/mobile/src/remote-desktop/fittedDisplayModes.ts b/apps/mobile/src/remote-desktop/fittedDisplayModes.ts
new file mode 100644
index 0000000000..2db4a4b5f7
--- /dev/null
+++ b/apps/mobile/src/remote-desktop/fittedDisplayModes.ts
@@ -0,0 +1,35 @@
+import type { RemoteDesktopDisplayMode } from "@cindy/device-link";
+
+type Size = { width: number; height: number };
+
+/** Resize the fitted desktop without offering modes from the physical monitor. */
+export function fittedDisplayModes(
+ fitted: Size,
+ current: Size,
+): RemoteDesktopDisplayMode[] {
+ const modes = new Map<string, RemoteDesktopDisplayMode>();
+ const add = ({ width, height }: Size) => {
+ if (
+ ![width, height].every(
+ (size) => Number.isInteger(size) && size >= 320 && size <= 2560,
+ )
+ )
+ return;
+ const id = `fitted:${width}x${height}`;
+ modes.set(id, {
+ id,
+ width,
+ height,
+ current: width === current.width && height === current.height,
+ });
+ };
+ for (const edge of [960, 1280, 1600, 1920, 2560]) {
+ const scale = edge / Math.max(fitted.width, fitted.height);
+ add({
+ width: Math.round((fitted.width * scale) / 2) * 2,
+ height: Math.round((fitted.height * scale) / 2) * 2,
+ });
+ }
+ add(current);
+ return [...modes.values()].sort((a, b) => a.width - b.width);
+}