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() })