electron-vite-react/src/main/index.ts

48 lines
987 B
TypeScript
Raw Normal View History

2021-11-08 19:17:39 +08:00
import { join } from 'path'
import { app, BrowserWindow } from 'electron'
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()
}
}
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) {
// someone tried to run a second instance, we should focus our window.
if (win.isMinimized()) win.restore()
win.focus()
}
2021-11-08 19:17:39 +08:00
})