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): pause detached sidebar wallpaper while hidden

DavidShen240e3ce14e99
diff --git a/apps/desktop/src/main/right-sidebar-window/__tests__/windowState.test.ts b/apps/desktop/src/main/right-sidebar-window/__tests__/windowState.test.ts
index 064fbf864f..0d53d84748 100644
--- a/apps/desktop/src/main/right-sidebar-window/__tests__/windowState.test.ts
+++ b/apps/desktop/src/main/right-sidebar-window/__tests__/windowState.test.ts
@@ -3,6 +3,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
const mocks = vi.hoisted(() => ({
manage: vi.fn(),
windowStateKeeper: vi.fn(),
+ installWindowHiddenBroadcast: vi.fn(),
}));
vi.mock('electron-window-state', () => ({ default: mocks.windowStateKeeper }));
@@ -17,6 +18,9 @@ vi.mock('../../secondary-windows.js', () => ({ installExternalLinkGuards: vi.fn(
vi.mock('../../selection-context-menu.js', () => ({ installSelectionContextMenu: vi.fn() }));
vi.mock('../../appearance-settings-ipc.js', () => ({ applyAppearanceToWindow: vi.fn() }));
vi.mock('../registry.js', () => ({ markRsbWindowWebContentsId: vi.fn() }));
+vi.mock('../../windowHiddenBroadcast.js', () => ({
+ installWindowHiddenBroadcast: mocks.installWindowHiddenBroadcast,
+}));
import { createRightSidebarWindow } from '../window';
@@ -49,5 +53,6 @@ describe('createRightSidebarWindow window state', () => {
fullScreen: false,
});
expect(mocks.manage).toHaveBeenCalledWith(win);
+ expect(mocks.installWindowHiddenBroadcast).toHaveBeenCalledWith(win);
});
});
diff --git a/apps/desktop/src/main/right-sidebar-window/window.ts b/apps/desktop/src/main/right-sidebar-window/window.ts
index 55edfeb372..cb8eb27a34 100644
--- a/apps/desktop/src/main/right-sidebar-window/window.ts
+++ b/apps/desktop/src/main/right-sidebar-window/window.ts
@@ -23,6 +23,7 @@ import { installExternalLinkGuards } from '../secondary-windows.js';
import { installSelectionContextMenu } from '../selection-context-menu.js';
import { applyAppearanceToWindow } from '../appearance-settings-ipc.js';
import { markRsbWindowWebContentsId } from './registry.js';
+import { installWindowHiddenBroadcast } from '../windowHiddenBroadcast.js';
const log = createLogger('right-sidebar-window');
@@ -89,6 +90,7 @@ export function createRightSidebarWindow(): BrowserWindow {
});
markRsbWindowWebContentsId(win.webContents.id);
markAppContentWindow(win);
+ installWindowHiddenBroadcast(win);
applyAppearanceToWindow(win);
win.webContents.on('did-finish-load', () => {
if (win.isDestroyed()) return;
diff --git a/apps/desktop/src/preload/__tests__/resourceUsageVisibility.test.ts b/apps/desktop/src/preload/__tests__/resourceUsageVisibility.test.ts
index 66daca8116..fd8668fb4d 100644
--- a/apps/desktop/src/preload/__tests__/resourceUsageVisibility.test.ts
+++ b/apps/desktop/src/preload/__tests__/resourceUsageVisibility.test.ts
@@ -20,13 +20,14 @@ vi.mock('electron', () => ({
function emit(hidden: boolean) {
for (const listener of mocks.listeners.get('window-hidden-change') ?? []) listener({}, hidden);
}
-let subscribe: (callback: (hidden: boolean) => void) => () => void;
-beforeAll(async () => {
- await import('../resourceUsagePreload');
- subscribe = mocks.expose.mock.calls[0][1].onWindowHiddenChange;
-});
-
-describe('prewarmed resource window visibility', () => {
+describe.each(['resource', 'sidebar'])('prewarmed %s window visibility', (kind) => {
+ let subscribe: (callback: (hidden: boolean) => void) => () => void;
+ beforeAll(async () => {
+ mocks.listeners.clear();
+ if (kind === 'resource') await import('../resourceUsagePreload');
+ else await import('../sidebarWindowPreload');
+ subscribe = mocks.expose.mock.calls.at(-1)![1].onWindowHiddenChange;
+ });
it('starts hidden before the lazy renderer or first broadcast', () => {
const listener = vi.fn();
const off = subscribe(listener);
diff --git a/apps/desktop/src/preload/sidebarWindowPreload.ts b/apps/desktop/src/preload/sidebarWindowPreload.ts
index ca66b63593..c765a7f0a1 100644
--- a/apps/desktop/src/preload/sidebarWindowPreload.ts
+++ b/apps/desktop/src/preload/sidebarWindowPreload.ts
@@ -39,6 +39,13 @@ function onPayload<T>(channel: string, cb: (payload: T) => void): () => void {
return () => ipcRenderer.removeListener(channel, listener);
}
+// Match the resource window: hidden prewarm must not start decorative playback,
+// and late renderer/HMR subscribers must receive the latest native state.
+let windowHidden = true;
+onPayload<boolean>('window-hidden-change', (hidden) => {
+ windowHidden = hidden;
+});
+
function onPayloadWithMetadata<T, M>(
channel: string,
cb: (payload: T, metadata?: M) => void,
@@ -69,6 +76,11 @@ const fanOutFullscreenChange = (cb: (isFullscreen: boolean) => void): (() => voi
contextBridge.exposeInMainWorld('electronAPI', {
platform: process.platform,
+ onWindowHiddenChange: (cb: (hidden: boolean) => void): (() => void) => {
+ const off = onPayload('window-hidden-change', cb);
+ cb(windowHidden);
+ return off;
+ },
preferredSystemLocale: readPreferredSystemLocale(),
windowMinimize: (): void => ipcRenderer.send('window-minimize'),
windowMaximize: (): void => ipcRenderer.send('window-maximize'),
diff --git a/apps/desktop/src/renderer/__tests__/detachedWallpaperEntry.test.tsx b/apps/desktop/src/renderer/__tests__/detachedWallpaperEntry.test.tsx
index ab149c86ab..3fa27cc825 100644
--- a/apps/desktop/src/renderer/__tests__/detachedWallpaperEntry.test.tsx
+++ b/apps/desktop/src/renderer/__tests__/detachedWallpaperEntry.test.tsx
@@ -53,6 +53,12 @@ afterEach(async () => {
for (const root of h.roots.splice(0)) root.unmount();
});
document.body.innerHTML = '';
+ const gateWindow = window as typeof window & {
+ __xdtHiddenAnimationGateDisposer?: () => void;
+ __xdtHiddenAnimationGateLastHidden?: boolean;
+ };
+ gateWindow.__xdtHiddenAnimationGateDisposer?.();
+ delete gateWindow.__xdtHiddenAnimationGateLastHidden;
document.documentElement.classList.remove('dark');
vi.unstubAllGlobals();
vi.restoreAllMocks();
@@ -62,13 +68,22 @@ it.each(['sidebar', 'plugin'] as const)(
'boots %s with a wallpaper using its read-only bridge and follows live changes',
async (kind) => {
vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true);
- vi.spyOn(HTMLMediaElement.prototype, 'play').mockResolvedValue();
- vi.spyOn(HTMLMediaElement.prototype, 'pause').mockImplementation(() => {});
+ const play = vi.spyOn(HTMLMediaElement.prototype, 'play').mockResolvedValue();
+ const pause = vi.spyOn(HTMLMediaElement.prototype, 'pause').mockImplementation(() => {});
vi.spyOn(HTMLMediaElement.prototype, 'load').mockImplementation(() => {});
document.body.innerHTML = '<div id="root"></div>';
let changed!: (settings: AppearanceSettings) => void;
+ let hiddenChanged!: (hidden: boolean) => void;
+ // Electron can report visible while a prewarmed native window is hidden.
+ vi.spyOn(document, 'hidden', 'get').mockReturnValue(false);
+ vi.spyOn(document, 'visibilityState', 'get').mockReturnValue('visible');
vi.stubGlobal('electronAPI', {
platform: 'win32',
+ onWindowHiddenChange: (fn: typeof hiddenChanged) => {
+ hiddenChanged = fn;
+ fn(true);
+ return () => {};
+ },
appearanceSettings: {
getSync: () => ({
...DEFAULT_APPEARANCE_SETTINGS,
@@ -90,6 +105,22 @@ it.each(['sidebar', 'plugin'] as const)(
);
expect(document.documentElement.dataset.wallpaperActive).toBe('true');
expect(document.querySelectorAll('video')).toHaveLength(1);
+ if (kind === 'sidebar') {
+ expect(play).not.toHaveBeenCalled();
+ expect(pause).toHaveBeenCalled();
+ const video = document.querySelector('video')!;
+ await act(async () => hiddenChanged(false));
+ expect(play).toHaveBeenCalledOnce();
+ video.currentTime = 3;
+ pause.mockClear();
+ await act(async () => hiddenChanged(true));
+ expect(pause).toHaveBeenCalledOnce();
+ expect(play).toHaveBeenCalledOnce();
+ await act(async () => hiddenChanged(false));
+ expect(play).toHaveBeenCalledTimes(2);
+ expect(document.querySelector('video')).toBe(video);
+ expect(video.currentTime).toBe(3);
+ }
const lightVeil = document.documentElement.style.getPropertyValue('--app-wallpaper-veil');
await act(async () => {
document.documentElement.classList.add('dark');
diff --git a/apps/desktop/src/renderer/sidebar-window-entry.tsx b/apps/desktop/src/renderer/sidebar-window-entry.tsx
index 30e8c74248..ddacc8db58 100644
--- a/apps/desktop/src/renderer/sidebar-window-entry.tsx
+++ b/apps/desktop/src/renderer/sidebar-window-entry.tsx
@@ -28,8 +28,10 @@ import { getInitialThemeVariant } from './hooks/useTheme';
import { WallpaperSettingsProvider } from './hooks/useWallpaperSettings';
import { bootstrapLocalThemesSync } from './themes/local-themes';
import { themeService } from './themes/theme-service';
+import { installHiddenAnimationGate } from './lib/hiddenAnimationGate';
document.documentElement.dataset.platform = window.electronAPI.platform;
+installHiddenAnimationGate();
bootstrapLocalThemesSync();
themeService.applyTheme(getInitialThemeVariant().theme);
applyFontSettings(getInitialFontSettings());