From 440359cf757a3df15d6d29dbf04e21a042ba9ff5 Mon Sep 17 00:00:00 2001 From: beyondkmp Date: Mon, 12 May 2025 21:57:50 +0800 Subject: [PATCH] fix(ipc): enhance theme handling with title bar overlay updates and broadcast notifications (#5915) feat(ipc): enhance theme handling with title bar overlay updates and broadcast notifications --- src/main/ipc.ts | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/main/ipc.ts b/src/main/ipc.ts index d628dd45a5..ecb74a57b4 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -121,11 +121,21 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) { // theme ipcMain.handle(IpcChannel.App_SetTheme, (_, theme: ThemeMode) => { + const updateTitleBarOverlay = () => { + if (!mainWindow?.setTitleBarOverlay) return + const isDark = nativeTheme.shouldUseDarkColors + mainWindow.setTitleBarOverlay(isDark ? titleBarOverlayDark : titleBarOverlayLight) + } + + const broadcastThemeChange = () => { + const isDark = nativeTheme.shouldUseDarkColors + const effectiveTheme = isDark ? ThemeMode.dark : ThemeMode.light + BrowserWindow.getAllWindows().forEach((win) => win.webContents.send(IpcChannel.ThemeChange, effectiveTheme)) + } + const notifyThemeChange = () => { - const windows = BrowserWindow.getAllWindows() - windows.forEach((win) => - win.webContents.send(IpcChannel.ThemeChange, nativeTheme.shouldUseDarkColors ? ThemeMode.dark : ThemeMode.light) - ) + updateTitleBarOverlay() + broadcastThemeChange() } if (theme === ThemeMode.auto) { @@ -133,11 +143,10 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) { nativeTheme.on('updated', notifyThemeChange) } else { nativeTheme.themeSource = theme - nativeTheme.removeAllListeners('updated') + nativeTheme.off('updated', notifyThemeChange) } - mainWindow?.setTitleBarOverlay && - mainWindow.setTitleBarOverlay(nativeTheme.shouldUseDarkColors ? titleBarOverlayDark : titleBarOverlayLight) + updateTitleBarOverlay() configManager.setTheme(theme) notifyThemeChange() })