electron-vite-vue/packages/main/index.ts

71 lines
1.6 KiB
TypeScript
Raw Normal View History

import os from 'os'
2021-09-09 09:37:02 +08:00
import path from 'path'
2021-11-09 09:00:12 +08:00
import { app, BrowserWindow } from 'electron'
2021-02-18 17:29:45 +08:00
// https://stackoverflow.com/questions/42524606/how-to-get-windows-version-using-node-js
const isWin7 = os.release().startsWith('6.1')
if (isWin7) app.disableHardwareAcceleration()
2021-11-08 10:21:15 +08:00
if (!app.requestSingleInstanceLock()) {
app.quit()
process.exit(0)
}
2021-09-09 09:37:02 +08:00
let win: BrowserWindow | null = null
2020-08-16 20:42:52 +08:00
2021-12-30 10:30:50 +08:00
async function createWindow() {
2020-08-16 20:42:52 +08:00
win = new BrowserWindow({
2020-08-31 09:50:57 +08:00
webPreferences: {
2021-11-11 17:52:50 +08:00
preload: path.join(__dirname, '../preload/index.cjs'),
2020-08-31 09:50:57 +08:00
},
2020-08-16 20:42:52 +08:00
})
2021-09-09 09:37:02 +08:00
if (app.isPackaged) {
2021-11-11 17:52:50 +08:00
win.loadFile(path.join(__dirname, '../renderer/index.html'))
2021-09-09 09:37:02 +08:00
} else {
2021-11-11 17:52:50 +08:00
const pkg = await import('../../package.json')
const url = `http://${pkg.env.HOST || '127.0.0.1'}:${pkg.env.PORT}`
win.loadURL(url)
2021-09-09 09:37:02 +08:00
win.webContents.openDevTools()
}
2020-08-16 20:42:52 +08:00
}
2021-12-30 10:30:50 +08:00
app.whenReady().then(createWindow)
2021-09-09 09:37:02 +08:00
2021-09-09 09:50:18 +08:00
app.on('window-all-closed', () => {
win = null
2021-11-08 10:21:15 +08:00
if (process.platform !== 'darwin') {
app.quit()
}
2021-09-09 09:50:18 +08:00
})
2021-11-08 10:21:15 +08:00
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-12-30 10:30:50 +08:00
app.on('activate', () => {
const allWindows = BrowserWindow.getAllWindows()
if (allWindows.length) {
allWindows[0].focus()
} else {
createWindow()
}
})
2021-11-08 20:42:31 +08:00
// @TODO
2021-11-08 10:21:15 +08:00
// auto update
2021-11-08 20:42:31 +08:00
/* if (app.isPackaged) {
2021-11-08 10:21:15 +08:00
app.whenReady()
.then(() => import('electron-updater'))
.then(({ autoUpdater }) => autoUpdater.checkForUpdatesAndNotify())
.catch((e) =>
// maybe you need to record some log files.
console.error('Failed check update:', e)
)
2021-11-08 20:42:31 +08:00
} */