fix(desktop): 远程桌面窗口隐藏、最小化、切桌面或全屏时不再断开连接

macOS 会把切换 Space、原生全屏切换和窗口被完全遮挡都报告成原生 hide/show,
远程桌面查看器原先在 hide/minimize 时结束会话、show/restore 时重连。
给辅助窗口控制器加 pauseWhenHidden 选项(默认不变),查看器关闭它,
会话只在用户关闭窗口时结束。

Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com>
This commit is contained in:
Dash
2026-10-05 15:57:22 +13:00
parent 7551664992
commit 3cf5e2ebce
5 changed files with 69 additions and 1 deletions

View File

@@ -251,3 +251,27 @@ it('waits for fullscreen exit and rejects delayed resize after a session replace
win.emit('leave-full-screen');
expect(win.setBounds).not.toHaveBeenCalled();
});
it('keeps the session through native hide/minimize and ends it only on close', async () => {
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];
call(REMOTE_VIEWER.READY, win);
call(REMOTE_VIEWER.PRESENTED, win);
const state = call(REMOTE_VIEWER.STATE, win);
await call(REMOTE_VIEWER.REQUEST, win, state.generation, { op: 'start', displayId: 'screen' });
// macOS reports Space switches and native fullscreen transitions as hide/show.
for (const name of ['hide', 'show', 'minimize', 'restore']) win.emit(name);
await Promise.resolve();
expect(call(REMOTE_VIEWER.STATE, win)).toMatchObject({
active: true,
generation: state.generation,
});
expect(fixture.calls.filter((c) => c[2][0].op === 'stop')).toHaveLength(0);
await call(REMOTE_VIEWER.CLOSE, win, state.generation);
await Promise.resolve();
expect(call(REMOTE_VIEWER.STATE, win).active).toBe(false);
expect(fixture.calls.filter((c) => c[2][0].op === 'stop')).toHaveLength(1);
});

View File

@@ -166,6 +166,9 @@ export class RemoteDesktopViewerWindows {
isOpenSender: this.isOpenSender,
// Independent top-level windows do not follow main-window minimize/hide.
prewarmWork: false,
// A live session survives minimize, Space switches and fullscreen transitions;
// only closing the viewer disconnects.
pauseWhenHidden: false,
activityChannel: REMOTE_VIEWER.ACTIVE,
activityPayload: () => connection.snapshot(),
localeChannel: REMOTE_VIEWER.LOCALE,

View File

@@ -741,6 +741,39 @@ describe('ResourceUsageWindowController', () => {
);
});
it('keeps work active across native hide events when pauseWhenHidden is false', () => {
const windows: FakeWindow[] = [];
const mainSender = { id: 100 } as WebContents;
const onActivityChanged = vi.fn();
const controller = new ResourceUsageWindowController({
createWindow: () => {
const win = fakeWindow(windows.length + 1);
windows.push(win);
return win as unknown as BrowserWindow;
},
isOpenSender: (sender) => sender === mainSender,
pauseWhenHidden: false,
onActivityChanged,
platform: 'darwin',
});
controller.prewarm();
markPrewarmed(controller, windows[0]!);
controller.open(mainSender);
onActivityChanged.mockClear();
// macOS reports Space switches and fullscreen transitions as hide/show.
for (const event of ['hide', 'show', 'minimize', 'restore'] as const) {
windows[0]?.emitWindow(event);
}
expect(onActivityChanged).not.toHaveBeenCalled();
controller.close(windows[0]!.webContents);
expect(onActivityChanged).toHaveBeenLastCalledWith(windows[0], false);
onActivityChanged.mockClear();
windows[0]?.emitWindow('show');
expect(onActivityChanged).not.toHaveBeenCalled();
});
it('shows a loading fallback only after the renderer shell has mounted', () => {
const { controller, windows, mainSender } = makeHarness(2500);
controller.open(mainSender);

View File

@@ -39,6 +39,11 @@ export interface ResourceUsageWindowControllerDeps {
activityPayload?: (active: boolean) => unknown;
localeChannel?: string;
prewarmWork?: boolean;
/**
* 原生 hide / minimize 是否暂停窗口工作;默认 true。只有用户关闭(hideWindow)才应结束
* 工作的窗口传 false——macOS 会把切换 Space、原生全屏切换和完全遮挡都报告成 hide / show。
*/
pauseWhenHidden?: boolean;
onActivityChanged?: (window: BrowserWindow, active: boolean) => void;
onCloseRequested?: (window: BrowserWindow) => void;
isOpenSender: (sender: WebContents) => boolean;
@@ -447,6 +452,7 @@ export class ResourceUsageWindowController {
private onNativeVisibilityChanged(win: BrowserWindow, visible: boolean): void {
if (win !== this.winRef || win.isDestroyed()) return;
this.visible = visible;
if (this.deps.pauseWhenHidden === false) return;
this.setSamplingActive(win, visible);
if (!visible && this.pendingOpen) {
this.pendingOpen = false;

View File

@@ -95,7 +95,9 @@ Renderer 可以负责组件渲染、交互状态、表单状态、展示数据
才显示冷窗口。超时只能展示已经挂载的 Loading 壳,不得展示空白窗口,也不得永久隐藏。
3. **隐藏复用而非反复冷启动**:普通关闭转换为 `hide()`,再次打开复用同一 Renderer;
主窗口真正销毁或应用退出时才 `destroy()`。隐藏/最小化必须暂停轮询、采样等后台重活,
再次显示时先展示保留快照并异步刷新。
再次显示时先展示保留快照并异步刷新。例外:承载实时会话的窗口(远程桌面查看器)
以 `pauseWhenHidden: false` 让会话跨越最小化、切换 Space 与原生全屏切换,只在用户
关闭窗口时结束——macOS 会把后两者及完全遮挡都报告成原生 hide。
4. **隐藏时重置交互态**:必须清除焦点,并重置自绘窗口按钮、hover/pressed、确认框、
选中项与待执行操作等瞬时状态,确保二次打开不会继承上次关闭时的视觉或交互状态。
5. **使用真正的轻量入口**:独立窗口不得先进入完整 `App` 再在组件树内分流;使用独立的