diff --git a/ivette/src/dome/main/dome.ts b/ivette/src/dome/main/dome.ts index 37078a13cfab0a2c41fdde96c4f9aeb09e215ab5..f58edbeca4e0c4b3b79bb9d19d58d44a95d6c6cb 100644 --- a/ivette/src/dome/main/dome.ts +++ b/ivette/src/dome/main/dome.ts @@ -85,6 +85,30 @@ export const { DEVEL } = System; /** System platform */ export const { platform } = System; +// -------------------------------------------------------------------------- +// --- Native Theme +// -------------------------------------------------------------------------- + +ipcMain.handle('dome.ipc.theme', () => { + return nativeTheme.shouldUseDarkColors ? 'dark' : 'light'; +}); + +nativeTheme.on('updated', () => { + broadcast('dome.theme.updated'); +}); + +function setNativeTheme(theme: string | undefined) { + switch (theme) { + case 'dark': + case 'light': + case 'system': + nativeTheme.themeSource = theme; + return; + default: + console.warn('[dome] unknown theme', theme); + } +} + // -------------------------------------------------------------------------- // --- Settings // -------------------------------------------------------------------------- @@ -208,7 +232,10 @@ function applyStorageSettings(event: IpcMainEvent, args: Patch[]) { } function applyGlobalSettings(event: IpcMainEvent, args: Patch[]) { - applyPatches(obtainGlobalSettings(), args); + const settings: Store = obtainGlobalSettings(); + applyPatches(settings, args); + const theme = settings['dome-color-theme']; + if (typeof (theme) === 'string') setNativeTheme(theme); BrowserWindow.getAllWindows().forEach((w: BrowserWindow) => { const contents = w.webContents; if (contents.id !== event.sender.id) { @@ -513,6 +540,7 @@ function showSettingsWindow() { function restoreDefaultSettings() { GlobalSettings = {}; + nativeTheme.themeSource = 'system'; if (DEVEL) saveGlobalSettings(); WindowHandles.forEach((handle) => { @@ -620,28 +648,3 @@ ipcMain.handle( ); // -------------------------------------------------------------------------- -// --- Native Theme -// -------------------------------------------------------------------------- - -// eslint-disable-next-line @typescript-eslint/no-explicit-any -ipcMain.handle('dome.ipc.theme.setSource', (_evt, theme) => { - switch (theme) { - case 'dark': - case 'light': - case 'system': - nativeTheme.themeSource = theme; - return; - default: - console.warn('[dome] unknown theme', theme); - } -}); - -ipcMain.handle('dome.ipc.theme.getDefault', () => { - return nativeTheme.shouldUseDarkColors ? 'dark' : 'light'; -}); - -nativeTheme.on('updated', () => { - broadcast('dome.theme.updated'); -}); - -// -------------------------------------------------------------------------- diff --git a/ivette/src/dome/renderer/themes.tsx b/ivette/src/dome/renderer/themes.tsx index 8bb3e2a3bef3881761776a1f81221ebc0219f139..fc24cb2bd4ec9f1fea98e3c17a7bde85eb131c3d 100644 --- a/ivette/src/dome/renderer/themes.tsx +++ b/ivette/src/dome/renderer/themes.tsx @@ -61,7 +61,7 @@ const NativeThemeUpdated = new Dome.Event('dome.theme.updated'); ipcRenderer.on('dome.theme.updated', () => NativeThemeUpdated.emit()); async function getNativeTheme(): Promise<ColorTheme> { - const th = await ipcRenderer.invoke('dome.ipc.theme.getDefault'); + const th = await ipcRenderer.invoke('dome.ipc.theme'); return jColorTheme(th); } @@ -72,20 +72,12 @@ async function getNativeTheme(): Promise<ColorTheme> { export function useColorTheme(): [ColorTheme, (upd: ColorSettings) => void] { Dome.useUpdate(NativeThemeUpdated); const { result: current } = Dome.usePromise(getNativeTheme()); - const [pref, setPref] = Settings.useGlobalSettings(ColorThemeSettings); - const setTheme = (upd: ColorSettings): void => { - setPref(upd); - ipcRenderer.invoke('dome.ipc.theme.setSource', upd); - }; + const [pref, setTheme] = Settings.useGlobalSettings(ColorThemeSettings); return [current ?? jColorTheme(pref), setTheme]; } export function useColorThemeSettings(): State<ColorSettings> { - const [pref, setPref] = Settings.useGlobalSettings(ColorThemeSettings); - const setTheme = (upd: ColorSettings): void => { - setPref(upd); - ipcRenderer.invoke('dome.ipc.theme.setSource', upd); - }; + const [pref, setTheme] = Settings.useGlobalSettings(ColorThemeSettings); return [jColorSettings(pref), setTheme]; }