diff --git a/apps/desktop/src/main.mjs b/apps/desktop/src/main.mjs index 38f72cf94..799c044cd 100644 --- a/apps/desktop/src/main.mjs +++ b/apps/desktop/src/main.mjs @@ -108,6 +108,7 @@ function coreVersion() { } function currentChromeBackground() { + if (process.platform === 'darwin') return ui.theme === 'dark' ? '#202023' : '#f5f5f7'; if (!WINDOWS) return ui.theme === 'dark' ? '#161618' : '#ffffff'; return ui.chromeBg || PALETTE_BACKGROUND[ui.palette] || PALETTE_BACKGROUND.snow; } @@ -172,10 +173,20 @@ function resolveChromeTheme(preference) { return palette === 'dark' || palette === 'glass' ? 'dark' : 'light'; } +function resolveMacTheme(preference) { + const source = preference === 'system' ? 'system' : resolveChromeTheme(preference); + if (nativeTheme.themeSource !== source) nativeTheme.themeSource = source; + return nativeTheme.shouldUseDarkColors ? 'dark' : 'light'; +} + async function readInterface() { let saved; try { saved = await readJSON(interfaceFile) ?? {}; } catch { return ui; } // A half-written or hand-edited file keeps the current choice. + if (process.platform === 'darwin') { + const themePreference = normalizeThemePreference(saved.theme); + return { ...ui, language: resolveLanguage(saved.language), themePreference, theme: resolveMacTheme(themePreference) }; + } if (!WINDOWS) { const dark = saved.theme === 'dark' || (saved.theme === 'system' && nativeTheme.shouldUseDarkColors); return { ...ui, language: resolveLanguage(saved.language), theme: dark ? 'dark' : 'light' }; @@ -205,7 +216,10 @@ function applyThemeChrome() { } async function refreshInterface() { - const next = await readInterface(); + updateInterface(await readInterface()); +} + +function updateInterface(next) { const paletteChanged = next.palette !== ui.palette; if ( next.language === ui.language @@ -360,8 +374,8 @@ async function applyLanguagePreference(nextLanguage) { async function applyThemePreference(nextPreference) { const themePreference = normalizeThemePreference(nextPreference); + const theme = process.platform === 'darwin' ? resolveMacTheme(themePreference) : resolveChromeTheme(themePreference); const palette = resolvePalette(themePreference); - const theme = resolveChromeTheme(themePreference); ui = { ...ui, themePreference, palette, theme, chromeBg: null }; buildMenu(); applyThemeChrome(); @@ -858,7 +872,14 @@ else { // at module scope or the packaged application deadlocks before its first window. app.whenReady().then(async () => { ui = await readInterface(); - nativeTheme.on('updated', () => void refreshInterface()); + nativeTheme.on('updated', () => { + if (process.platform !== 'darwin') { + void refreshInterface(); + } else if (ui.themePreference === 'system' && nativeTheme.themeSource === 'system') { + // An appearance event can also come from setting themeSource before the preference is saved. + updateInterface({ ...ui, theme: nativeTheme.shouldUseDarkColors ? 'dark' : 'light' }); + } + }); const coreSession = session.fromPartition('persist:deeptutor'); // The web UI reads this marker to name the client, e.g. "Open DeepTutor for MacOS with Apple Silicon". coreSession.setUserAgent(`${coreSession.getUserAgent()} ${agentMarker(app.getVersion())}`); diff --git a/apps/desktop/src/renderer.js b/apps/desktop/src/renderer.js index 0fe01291d..3b028cfb8 100644 --- a/apps/desktop/src/renderer.js +++ b/apps/desktop/src/renderer.js @@ -139,10 +139,10 @@ function apply(state) { root.dataset.theme = state.theme || 'light'; root.dataset.phase = phase; root.dataset.content = state.contentVisible ? 'visible' : 'hidden'; + if (state.chromeBg) root.style.setProperty('--bg', state.chromeBg); + else root.style.removeProperty('--bg'); if (window.desktop.platform === 'win32') { root.dataset.palette = state.palette; - if (state.chromeBg) root.style.setProperty('--bg', state.chromeBg); - else root.style.removeProperty('--bg'); renderMenus(state.menus); }