2021-11-08 19:17:39 +08:00
|
|
|
import { join } from 'path'
|
2021-11-15 20:00:13 +08:00
|
|
|
import { app, BrowserWindow, ipcMain } from 'electron'
|
|
|
|
import Store from 'electron-store'
|
2021-11-08 19:17:39 +08:00
|
|
|
|
2021-11-09 11:12:32 +08:00
|
|
|
app.disableHardwareAcceleration()
|
|
|
|
|
|
|
|
if (!app.requestSingleInstanceLock()) {
|
|
|
|
app.quit()
|
|
|
|
process.exit(0)
|
|
|
|
}
|
|
|
|
|
2021-11-08 19:17:39 +08:00
|
|
|
let win: BrowserWindow | null = null
|
|
|
|
|
|
|
|
async function mainWin() {
|
|
|
|
win = new BrowserWindow({
|
|
|
|
title: 'Main window',
|
|
|
|
webPreferences: {
|
|
|
|
preload: join(__dirname, '../preload/index.cjs')
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if (app.isPackaged) {
|
2021-11-11 12:46:00 +08:00
|
|
|
win.loadFile(join(__dirname, '../renderer/index.html'))
|
2021-11-08 19:17:39 +08:00
|
|
|
} else {
|
|
|
|
const pkg = await import('../../package.json')
|
|
|
|
const url = `http://${pkg.env.HOST || '127.0.0.1'}:${pkg.env.PORT}`
|
|
|
|
|
|
|
|
win.loadURL(url)
|
|
|
|
win.maximize()
|
2021-11-15 20:00:13 +08:00
|
|
|
win.webContents.openDevTools()
|
2021-11-08 19:17:39 +08:00
|
|
|
}
|
2021-11-25 19:39:30 +08:00
|
|
|
|
|
|
|
// Test active push message to Renderer-process.
|
2021-11-26 08:49:02 +08:00
|
|
|
win.webContents.on('did-finish-load', () => {
|
2021-11-25 19:39:30 +08:00
|
|
|
win?.webContents.send('main-process-message', (new Date).toLocaleString())
|
2021-11-26 08:49:02 +08:00
|
|
|
})
|
2021-11-08 19:17:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
app.whenReady().then(mainWin)
|
2021-11-09 11:12:32 +08:00
|
|
|
|
2021-11-08 19:17:39 +08:00
|
|
|
app.on('window-all-closed', () => {
|
|
|
|
win = null
|
2021-11-09 11:12:32 +08:00
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
app.quit()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
app.on('second-instance', () => {
|
|
|
|
if (win) {
|
2021-11-15 20:00:13 +08:00
|
|
|
// Someone tried to run a second instance, we should focus our window.
|
2021-11-09 11:12:32 +08:00
|
|
|
if (win.isMinimized()) win.restore()
|
|
|
|
win.focus()
|
|
|
|
}
|
2021-11-08 19:17:39 +08:00
|
|
|
})
|
2021-11-15 20:00:13 +08:00
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expose 'electron-store' to renderer through 'ipcMain.handle'
|
|
|
|
*/
|
|
|
|
const store = new Store
|
|
|
|
ipcMain.handle('electron-store', async (_evnet, methodSign: string, ...args: any[]) => {
|
|
|
|
if (typeof (store as any)[methodSign] === 'function') {
|
|
|
|
return (store as any)[methodSign](...args)
|
|
|
|
}
|
|
|
|
return (store as any)[methodSign]
|
|
|
|
})
|