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
This commit is contained in:
beyondkmp 2025-05-12 21:57:50 +08:00 committed by kangfenmao
parent d456b3d4ed
commit 341f744e07

View File

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