diff --git a/electron/main/index.ts b/electron/main/index.ts index ea98581..bd4d379 100644 --- a/electron/main/index.ts +++ b/electron/main/index.ts @@ -116,9 +116,6 @@ class MainProcess { }, }) - // 设置默认日间模式 - nativeTheme.themeSource = 'light' - this.mainWindow.once('ready-to-show', () => { this.mainWindow?.show() }) diff --git a/electron/main/ipc/window.ts b/electron/main/ipc/window.ts index 99a3f20..cd4a7c5 100644 --- a/electron/main/ipc/window.ts +++ b/electron/main/ipc/window.ts @@ -53,6 +53,12 @@ export function registerWindowHandlers(ctx: IpcContext): void { win.webContents.openDevTools() }) + // 设置主题模式 + ipcMain.on('window:setThemeSource', (_, mode: 'system' | 'light' | 'dark') => { + const { nativeTheme } = require('electron') + nativeTheme.themeSource = mode + }) + // ==================== 应用信息 ==================== ipcMain.handle('app:getVersion', () => { return app.getVersion() diff --git a/electron/preload/index.d.ts b/electron/preload/index.d.ts index 85ca39f..1ddacc8 100644 --- a/electron/preload/index.d.ts +++ b/electron/preload/index.d.ts @@ -110,6 +110,7 @@ interface Api { send: (channel: string, data?: unknown) => void receive: (channel: string, func: (...args: unknown[]) => void) => void removeListener: (channel: string, func: (...args: unknown[]) => void) => void + setThemeSource: (mode: 'system' | 'light' | 'dark') => void dialog: { showOpenDialog: (options: Electron.OpenDialogOptions) => Promise } diff --git a/electron/preload/index.ts b/electron/preload/index.ts index 099136c..24adf20 100644 --- a/electron/preload/index.ts +++ b/electron/preload/index.ts @@ -56,6 +56,9 @@ const api = { removeListener: (channel: string, func: (...args: unknown[]) => void) => { ipcRenderer.removeListener(channel, func) }, + setThemeSource: (mode: 'system' | 'light' | 'dark') => { + ipcRenderer.send('window:setThemeSource', mode) + }, } // Chat Analysis API diff --git a/src/components/common/settings/BasicSettingsTab.vue b/src/components/common/settings/BasicSettingsTab.vue index 84bbaeb..9e7a205 100644 --- a/src/components/common/settings/BasicSettingsTab.vue +++ b/src/components/common/settings/BasicSettingsTab.vue @@ -42,6 +42,17 @@ const currentLocale = computed({ get: () => locale.value, set: (val: LocaleType) => settingsStore.setLocale(val), }) + +// Sync theme with main process +import { watch } from 'vue' +watch( + colorMode, + (val) => { + const mode = val === 'auto' ? 'system' : (val as 'light' | 'dark') + window.api.setThemeSource(mode) + }, + { immediate: true } +)