2022-06-14 20:21:14 +08:00
|
|
|
import { app, BrowserWindow, shell, ipcMain } from 'electron'
|
|
|
|
import { release } from 'os'
|
|
|
|
import { join } from 'path'
|
|
|
|
|
2022-07-27 00:05:20 +08:00
|
|
|
const { isPackaged } = app
|
|
|
|
|
2022-06-14 20:21:14 +08:00
|
|
|
// Disable GPU Acceleration for Windows 7
|
|
|
|
if (release().startsWith('6.1')) app.disableHardwareAcceleration()
|
|
|
|
|
|
|
|
// Set application name for Windows 10+ notifications
|
|
|
|
if (process.platform === 'win32') app.setAppUserModelId(app.getName())
|
|
|
|
|
|
|
|
if (!app.requestSingleInstanceLock()) {
|
|
|
|
app.quit()
|
|
|
|
process.exit(0)
|
|
|
|
}
|
2022-06-29 08:36:04 +08:00
|
|
|
|
2022-07-25 19:37:14 +08:00
|
|
|
// Remove electron security warnings
|
|
|
|
// This warning only shows in development mode
|
|
|
|
// Read more on https://www.electronjs.org/docs/latest/tutorial/security
|
|
|
|
// process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true'
|
2022-06-14 20:21:14 +08:00
|
|
|
|
2022-06-29 08:36:04 +08:00
|
|
|
export const ROOT_PATH = {
|
|
|
|
// /dist
|
|
|
|
dist: join(__dirname, '../..'),
|
|
|
|
// /dist or /public
|
|
|
|
public: join(__dirname, app.isPackaged ? '../..' : '../../../public'),
|
|
|
|
}
|
|
|
|
|
2022-06-14 20:21:14 +08:00
|
|
|
let win: BrowserWindow | null = null
|
|
|
|
// Here, you can also use other preload
|
2022-06-29 07:33:37 +08:00
|
|
|
const preload = join(__dirname, '../preload/index.js')
|
2022-07-02 09:12:13 +08:00
|
|
|
// 🚧 Use ['ENV_NAME'] avoid vite:define plugin
|
|
|
|
const url = `http://${process.env['VITE_DEV_SERVER_HOST']}:${process.env['VITE_DEV_SERVER_PORT']}`
|
2022-07-01 17:09:30 +08:00
|
|
|
const indexHtml = join(ROOT_PATH.dist, 'index.html')
|
2022-06-14 20:21:14 +08:00
|
|
|
|
|
|
|
async function createWindow() {
|
|
|
|
win = new BrowserWindow({
|
|
|
|
title: 'Main window',
|
2022-06-29 08:36:04 +08:00
|
|
|
icon: join(ROOT_PATH.public, 'favicon.ico'),
|
2022-06-14 20:21:14 +08:00
|
|
|
webPreferences: {
|
2022-06-29 07:33:37 +08:00
|
|
|
preload,
|
2022-07-25 19:37:14 +08:00
|
|
|
// Warning: Enable nodeIntegration and disable contextIsolation is not secure in production
|
|
|
|
// Consider using contextBridge.exposeInMainWorld
|
|
|
|
// Read more on https://www.electronjs.org/docs/latest/tutorial/context-isolation
|
2022-06-14 20:21:14 +08:00
|
|
|
nodeIntegration: true,
|
|
|
|
contextIsolation: false,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2022-07-27 00:05:20 +08:00
|
|
|
if (isPackaged) {
|
2022-06-29 08:36:04 +08:00
|
|
|
win.loadFile(indexHtml)
|
2022-06-14 20:21:14 +08:00
|
|
|
} else {
|
|
|
|
win.loadURL(url)
|
|
|
|
// win.webContents.openDevTools()
|
|
|
|
}
|
|
|
|
|
2022-07-27 00:05:20 +08:00
|
|
|
// Open devTool if the app is not packaged
|
|
|
|
!isPackaged && win.webContents.openDevTools()
|
|
|
|
|
2022-06-29 08:36:04 +08:00
|
|
|
// Test actively push message to the Electron-Renderer
|
2022-06-14 20:21:14 +08:00
|
|
|
win.webContents.on('did-finish-load', () => {
|
|
|
|
win?.webContents.send('main-process-message', new Date().toLocaleString())
|
|
|
|
})
|
|
|
|
|
|
|
|
// Make all links open with the browser, not with the application
|
|
|
|
win.webContents.setWindowOpenHandler(({ url }) => {
|
|
|
|
if (url.startsWith('https:')) shell.openExternal(url)
|
|
|
|
return { action: 'deny' }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
app.whenReady().then(createWindow)
|
|
|
|
|
|
|
|
app.on('window-all-closed', () => {
|
|
|
|
win = null
|
|
|
|
if (process.platform !== 'darwin') app.quit()
|
|
|
|
})
|
|
|
|
|
|
|
|
app.on('second-instance', () => {
|
|
|
|
if (win) {
|
|
|
|
// Focus on the main window if the user tried to open another
|
|
|
|
if (win.isMinimized()) win.restore()
|
|
|
|
win.focus()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
app.on('activate', () => {
|
|
|
|
const allWindows = BrowserWindow.getAllWindows()
|
|
|
|
if (allWindows.length) {
|
|
|
|
allWindows[0].focus()
|
|
|
|
} else {
|
|
|
|
createWindow()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// new window example arg: new windows url
|
2022-06-29 08:36:04 +08:00
|
|
|
ipcMain.handle('open-win', (event, arg) => {
|
2022-06-14 20:21:14 +08:00
|
|
|
const childWindow = new BrowserWindow({
|
|
|
|
webPreferences: {
|
2022-06-29 07:33:37 +08:00
|
|
|
preload,
|
2022-06-14 20:21:14 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if (app.isPackaged) {
|
2022-06-29 08:36:04 +08:00
|
|
|
childWindow.loadFile(indexHtml, { hash: arg })
|
2022-06-14 20:21:14 +08:00
|
|
|
} else {
|
|
|
|
childWindow.loadURL(`${url}/#${arg}`)
|
|
|
|
// childWindow.webContents.openDevTools({ mode: "undocked", activate: true })
|
|
|
|
}
|
|
|
|
})
|