-
Notifications
You must be signed in to change notification settings - Fork 0
/
main-window.js
34 lines (30 loc) · 968 Bytes
/
main-window.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const { contextBridge, ipcRenderer } = require('electron/renderer')
// We need to wait until the main world is ready to receive the message before
// sending the port. We create this promise in the preload so it's guaranteed
// to register the onload listener before the load event is fired.
const windowLoaded = new Promise((resolve) => {
window.onload = resolve
})
/**
* @type {import('./runtime.js').RuntimeApi}
*/
const runtimeApi = {
// Setup
init() {
ipcRenderer.send('request-comapeo-port')
ipcRenderer.once('provide-comapeo-port', async (event) => {
await windowLoaded
window.postMessage('comapeo-port', '*', event.ports)
})
},
// Locale
async getLocale() {
const locale = await ipcRenderer.invoke('locale:get')
if (typeof locale !== 'string') throw Error('Locale must be a string')
return locale
},
updateLocale(locale) {
ipcRenderer.send('locale:update', locale)
},
}
contextBridge.exposeInMainWorld('runtime', runtimeApi)