mirror of
https://github.com/heyman/heynote.git
synced 2024-11-21 23:43:22 +01:00
Move theme/mode related preload code into it's own module
This commit is contained in:
parent
818d7f0260
commit
d4078d50d4
@ -1,30 +1,8 @@
|
||||
const { contextBridge, ipcRenderer } = require('electron')
|
||||
const { contextBridge } = require('electron')
|
||||
import darkMode from "./theme-mode"
|
||||
|
||||
const getComputedTheme = () => window.matchMedia('(prefers-color-scheme: dark)').matches ? "dark" : "light"
|
||||
const mediaMatch = window.matchMedia('(prefers-color-scheme: dark)')
|
||||
let darkModeChangeListener = null
|
||||
|
||||
contextBridge.exposeInMainWorld('darkMode', {
|
||||
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) => {
|
||||
darkModeChangeListener = (event) => {
|
||||
callback(event.matches ? "dark" : "light")
|
||||
}
|
||||
mediaMatch.addEventListener('change', darkModeChangeListener)
|
||||
return mediaMatch
|
||||
},
|
||||
removeListener() {
|
||||
mediaMatch.removeEventListener('change', darkModeChangeListener)
|
||||
},
|
||||
initial: getComputedTheme(),
|
||||
})
|
||||
contextBridge.exposeInMainWorld('darkMode', darkMode)
|
||||
|
||||
|
||||
function domReady(condition: DocumentReadyState[] = ['complete', 'interactive']) {
|
||||
|
27
electron/preload/theme-mode.ts
Normal file
27
electron/preload/theme-mode.ts
Normal file
@ -0,0 +1,27 @@
|
||||
const { ipcRenderer } = require('electron')
|
||||
|
||||
const getComputedTheme = () => window.matchMedia('(prefers-color-scheme: dark)').matches ? "dark" : "light"
|
||||
const mediaMatch = window.matchMedia('(prefers-color-scheme: dark)')
|
||||
let darkModeChangeListener = null
|
||||
|
||||
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) => {
|
||||
darkModeChangeListener = (event) => {
|
||||
callback(event.matches ? "dark" : "light")
|
||||
}
|
||||
mediaMatch.addEventListener('change', darkModeChangeListener)
|
||||
return mediaMatch
|
||||
},
|
||||
removeListener() {
|
||||
mediaMatch.removeEventListener('change', darkModeChangeListener)
|
||||
},
|
||||
initial: getComputedTheme(),
|
||||
}
|
Loading…
Reference in New Issue
Block a user