2023-01-16 15:31:46 +01:00
|
|
|
const { ipcRenderer } = require('electron')
|
|
|
|
|
|
|
|
const getComputedTheme = () => window.matchMedia('(prefers-color-scheme: dark)').matches ? "dark" : "light"
|
|
|
|
const mediaMatch = window.matchMedia('(prefers-color-scheme: dark)')
|
2023-12-25 14:18:44 +01:00
|
|
|
let themeModeChangeListener = null
|
2023-01-16 15:31:46 +01:00
|
|
|
|
|
|
|
export default {
|
|
|
|
set: (mode) => ipcRenderer.invoke('dark-mode:set', mode),
|
|
|
|
get: async () => {
|
|
|
|
const mode = await ipcRenderer.invoke('dark-mode:get')
|
|
|
|
return {
|
|
|
|
theme: mode,
|
|
|
|
computed: getComputedTheme(),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onChange: (callback) => {
|
2023-12-25 14:18:44 +01:00
|
|
|
themeModeChangeListener = (event) => {
|
2023-01-16 15:31:46 +01:00
|
|
|
callback(event.matches ? "dark" : "light")
|
|
|
|
}
|
2023-12-25 14:18:44 +01:00
|
|
|
mediaMatch.addEventListener('change', themeModeChangeListener)
|
2023-01-16 15:31:46 +01:00
|
|
|
return mediaMatch
|
|
|
|
},
|
|
|
|
removeListener() {
|
2023-12-25 14:18:44 +01:00
|
|
|
mediaMatch.removeEventListener('change', themeModeChangeListener)
|
2023-01-16 15:31:46 +01:00
|
|
|
},
|
|
|
|
initial: getComputedTheme(),
|
|
|
|
}
|