Set the application's dark mode to follow the app settings on macOS. (#10186)

This commit is contained in:
fireblue 2025-01-11 05:07:17 +08:00 committed by GitHub
parent 0a475daa9c
commit f9dadf0816
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 2 deletions

View File

@ -1,7 +1,7 @@
import * as glasstron from 'glasstron' import * as glasstron from 'glasstron'
import { autoUpdater } from 'electron-updater' import { autoUpdater } from 'electron-updater'
import { Subject, Observable, debounceTime } from 'rxjs' import { Subject, Observable, debounceTime } from 'rxjs'
import { BrowserWindow, app, ipcMain, Rectangle, Menu, screen, BrowserWindowConstructorOptions, TouchBar, nativeImage, WebContents } from 'electron' import { BrowserWindow, app, ipcMain, Rectangle, Menu, screen, BrowserWindowConstructorOptions, TouchBar, nativeImage, WebContents, nativeTheme } from 'electron'
import ElectronConfig = require('electron-config') import ElectronConfig = require('electron-config')
import { enable as enableRemote } from '@electron/remote/main' import { enable as enableRemote } from '@electron/remote/main'
import * as os from 'os' import * as os from 'os'
@ -115,6 +115,8 @@ export class Window {
this.setVibrancy(true) this.setVibrancy(true)
} }
this.setDarkMode(this.configStore.appearance?.colorSchemeMode ?? 'dark')
if (!options.hidden) { if (!options.hidden) {
if (maximized) { if (maximized) {
this.window.maximize() this.window.maximize()
@ -201,6 +203,18 @@ export class Window {
} }
} }
setDarkMode (mode: string): void {
if (process.platform === 'darwin') {
if ('light' === mode ) {
nativeTheme.themeSource = 'light'
} else if ('auto' === mode) {
nativeTheme.themeSource = 'system'
} else {
nativeTheme.themeSource = 'dark'
}
}
}
focus (): void { focus (): void {
this.window.focus() this.window.focus()
} }
@ -373,6 +387,10 @@ export class Window {
this.setVibrancy(enabled, type) this.setVibrancy(enabled, type)
}) })
this.on('window-set-dark-mode', (_, mode) => {
this.setDarkMode(mode)
})
this.on('window-set-window-controls-color', (_, theme) => { this.on('window-set-window-controls-color', (_, theme) => {
if (process.platform === 'win32') { if (process.platform === 'win32') {
const symbolColor: string = theme.foreground const symbolColor: string = theme.foreground

View File

@ -130,7 +130,10 @@ export default class ElectronModule {
}) })
}) })
config.changed$.subscribe(() => this.updateVibrancy()) config.changed$.subscribe(() => {
this.updateVibrancy()
this.updateDarkMode()
})
config.changed$.subscribe(() => this.updateWindowControlsColor()) config.changed$.subscribe(() => this.updateWindowControlsColor())
@ -173,6 +176,11 @@ export default class ElectronModule {
this.hostWindow.setOpacity(this.config.store.appearance.opacity) this.hostWindow.setOpacity(this.config.store.appearance.opacity)
} }
private updateDarkMode () {
const colorSchemeMode = this.config.store.appearance.colorSchemeMode
this.electron.ipcRenderer.send('window-set-dark-mode', colorSchemeMode)
}
private updateWindowControlsColor () { private updateWindowControlsColor () {
// if windows and not using native frame, WCO does not exist, return. // if windows and not using native frame, WCO does not exist, return.
if (this.hostApp.platform === Platform.Windows && this.config.store.appearance.frame === 'native') { if (this.hostApp.platform === Platform.Windows && this.config.store.appearance.frame === 'native') {