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(desktop): restore persisted zoom when app windows regain focus

zcinta05149bd142666cad
diff --git a/apps/desktop/src/main/__tests__/appearanceSettingsIpcSecurity.test.ts b/apps/desktop/src/main/__tests__/appearanceSettingsIpcSecurity.test.ts
index 30a5f0b5cd..9146977ff9 100644
--- a/apps/desktop/src/main/__tests__/appearanceSettingsIpcSecurity.test.ts
+++ b/apps/desktop/src/main/__tests__/appearanceSettingsIpcSecurity.test.ts
@@ -1,6 +1,7 @@
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
const mocks = vi.hoisted(() => ({
+ appOn: vi.fn(),
ipcOn: vi.fn(),
ipcHandle: vi.fn(),
allWindows: [] as Array<unknown>,
@@ -34,6 +35,7 @@ vi.mock('../wallpaper-video.js', () => ({
}));
vi.mock('electron', () => ({
+ app: { on: mocks.appOn },
BrowserWindow: { getAllWindows: () => mocks.allWindows, fromWebContents: mocks.fromWebContents },
ipcMain: { on: mocks.ipcOn, handle: mocks.ipcHandle },
}));
@@ -214,6 +216,47 @@ describe('appearance settings IPC authorization', () => {
mocks.readAppearanceSettings.mockReset().mockReturnValue(persisted);
mocks.fromWebContents.mockReset().mockReturnValue({ isDestroyed: () => false });
mocks.writeAppearanceSettingsPatch.mockReset().mockResolvedValue(persisted);
+ mocks.updateAppearanceSettingsAtomic.mockReset();
+ });
+
+ // #7486: the file can retain 90% while an activated window displays 100%.
+ // Existing IPC tests only covered preference writes, not native focus recovery.
+ // Exercise the registered Electron event with a stateful window; unit tier.
+ it('restores a drifted app window on focus without rewriting preferences or stable zoom', () => {
+ const settings = { ...persisted, windowZoom: 0.9 };
+ mocks.readAppearanceSettings.mockReturnValue(settings);
+ let zoom = 1;
+ const setZoomFactor = vi.fn((value: number) => { zoom = value; });
+ const win = {
+ isDestroyed: () => false,
+ webContents: { isDestroyed: () => false, getZoomFactor: () => zoom, setZoomFactor },
+ };
+ markAppContentWindow(win as never);
+ const focus = mocks.appOn.mock.calls.find(([name]) => name === 'browser-window-focus')?.[1];
+ expect(focus).toBeTypeOf('function');
+ focus({}, win);
+ expect(zoom).toBe(0.9);
+ focus({}, win);
+ expect(setZoomFactor).toHaveBeenCalledOnce();
+ expect(mocks.writeAppearanceSettingsPatch).not.toHaveBeenCalled();
+ expect(mocks.updateAppearanceSettingsAtomic).not.toHaveBeenCalled();
+ });
+
+ it('does not apply the app zoom to unregistered or destroyed focus targets', () => {
+ const focus = mocks.appOn.mock.calls.find(([name]) => name === 'browser-window-focus')?.[1];
+ expect(focus).toBeTypeOf('function');
+ const setZoomFactor = vi.fn();
+ const win = {
+ isDestroyed: vi.fn(() => false),
+ webContents: { isDestroyed: vi.fn(() => false), getZoomFactor: () => 1, setZoomFactor },
+ };
+ focus({}, win);
+ markAppContentWindow(win as never);
+ win.webContents.isDestroyed.mockReturnValue(true);
+ focus({}, win);
+ win.isDestroyed.mockReturnValue(true);
+ focus({}, win);
+ expect(setZoomFactor).not.toHaveBeenCalled();
});
it('同步启动读取只向已授权的外观 reader 返回持久快照', () => {
diff --git a/apps/desktop/src/main/appearance-settings-ipc.ts b/apps/desktop/src/main/appearance-settings-ipc.ts
index 7761428123..d91b8ec961 100644
--- a/apps/desktop/src/main/appearance-settings-ipc.ts
+++ b/apps/desktop/src/main/appearance-settings-ipc.ts
@@ -1,4 +1,4 @@
-import { BrowserWindow, ipcMain } from 'electron';
+import { app, BrowserWindow, ipcMain } from 'electron';
import { extractIpcError } from '../shared/ipcError.js';
import {
@@ -40,6 +40,17 @@ let registered = false;
export function registerAppearanceSettingsIpc(): void {
if (registered) return;
registered = true;
+ // The saved scale can survive while the native window displays a different
+ // zoom (#7486). Restore it on activation without rewriting the preference or
+ // repeatedly applying an already matching scale. Only app-content windows
+ // participate; embedded webpages and utility windows keep their own zoom.
+ app.on('browser-window-focus', function restoreFocusedWindowZoom(_event, win) {
+ if (!isAppContentWindow(win) || win.webContents.isDestroyed()) return;
+ const settings = readAppearanceSettings();
+ if (Math.abs(win.webContents.getZoomFactor() - settings.windowZoom) > 0.000001) {
+ applyAppearanceToWindow(win, settings);
+ }
+ });
// Must be registered before BrowserWindow creation: preload reads this on
// the first synchronous bootstrap frame.