electron/lib/browser/api/system-preferences.js

48 lines
1.6 KiB
JavaScript
Raw Normal View History

'use strict'
const { app } = require('electron')
2018-09-13 16:10:51 +00:00
const { EventEmitter } = require('events')
const { systemPreferences, SystemPreferences } = process.atomBinding('system_preferences')
2016-04-24 12:13:46 +00:00
// SystemPreferences is an EventEmitter.
2016-08-02 11:38:35 +00:00
Object.setPrototypeOf(SystemPreferences.prototype, EventEmitter.prototype)
EventEmitter.call(systemPreferences)
2016-04-24 12:13:46 +00:00
if (process.platform === 'darwin') {
let appearanceTrackingSubscriptionID = null
systemPreferences.startAppLevelAppearanceTrackingOS = () => {
if (appearanceTrackingSubscriptionID !== null) return
const updateAppearanceBasedOnOS = () => {
const newAppearance = systemPreferences.isDarkMode()
? 'dark'
: 'light'
if (systemPreferences.getAppLevelAppearance() !== newAppearance) {
systemPreferences.setAppLevelAppearance(newAppearance)
// TODO(MarshallOfSound): Once we remove this logic and build against 10.14
// SDK we should re-implement this event as a monitor of `effectiveAppearance`
systemPreferences.emit('appearance-changed', newAppearance)
}
}
appearanceTrackingSubscriptionID = systemPreferences.subscribeNotification(
'AppleInterfaceThemeChangedNotification',
updateAppearanceBasedOnOS
)
updateAppearanceBasedOnOS()
}
systemPreferences.stopAppLevelAppearanceTrackingOS = () => {
if (appearanceTrackingSubscriptionID === null) return
systemPreferences.unsubscribeNotification(appearanceTrackingSubscriptionID)
}
app.on('quit', systemPreferences.stopAppLevelAppearanceTrackingOS)
}
2016-04-24 12:13:46 +00:00
module.exports = systemPreferences