85 lines
2.9 KiB
JavaScript
85 lines
2.9 KiB
JavaScript
import { app, BrowserWindow, ipcMain, Menu } from 'electron'
|
|
import { fileURLToPath } from 'node:url'
|
|
import path from 'node:path'
|
|
import { disableElectronHistoryGestures } from '@native-vue-router/electron'
|
|
|
|
const directory = path.dirname(fileURLToPath(import.meta.url))
|
|
const smokeMode = process.env.ELECTRON_SMOKE === '1'
|
|
|| process.argv.includes('--smoke')
|
|
|| app.commandLine.hasSwitch('smoke')
|
|
disableElectronHistoryGestures(app.commandLine)
|
|
app.commandLine.appendSwitch('disable-pinch')
|
|
|
|
function createWindow() {
|
|
const window = new BrowserWindow({
|
|
width: 430,
|
|
height: 860,
|
|
minWidth: 360,
|
|
minHeight: 620,
|
|
backgroundColor: '#0b0d12',
|
|
titleBarStyle: process.platform === 'darwin' ? 'hiddenInset' : 'default',
|
|
show: false,
|
|
webPreferences: {
|
|
preload: path.join(directory, 'preload.mjs'),
|
|
contextIsolation: true,
|
|
nodeIntegration: false,
|
|
sandbox: true,
|
|
},
|
|
})
|
|
|
|
const developmentUrl = process.env.VITE_DEV_SERVER_URL
|
|
if (developmentUrl) void window.loadURL(developmentUrl)
|
|
else void window.loadFile(path.resolve(directory, '../demo/dist/index.html'))
|
|
|
|
window.once('ready-to-show', () => { if (!smokeMode) window.show() })
|
|
if (smokeMode) {
|
|
console.log('Electron smoke mode started')
|
|
const timeout = setTimeout(() => {
|
|
console.error('Electron renderer smoke check timed out')
|
|
app.exit(1)
|
|
}, 15_000)
|
|
window.webContents.once('dom-ready', async () => {
|
|
const rendered = await window.webContents.executeJavaScript("Boolean(document.querySelector('h1'))")
|
|
console.log(rendered ? 'Electron renderer smoke check passed' : 'Electron renderer did not render')
|
|
clearTimeout(timeout)
|
|
app.exit(rendered ? 0 : 1)
|
|
})
|
|
window.webContents.once('did-fail-load', (_event, code, description) => {
|
|
console.error(`Electron load failed (${code}): ${description}`)
|
|
})
|
|
}
|
|
window.on('app-command', (_event, command) => {
|
|
if (command === 'browser-backward') window.webContents.send('native-vue:back')
|
|
if (command === 'browser-forward') window.webContents.send('native-vue:forward')
|
|
})
|
|
|
|
const template = [
|
|
...(process.platform === 'darwin' ? [{ role: 'appMenu' }] : []),
|
|
{
|
|
label: 'Navigate',
|
|
submenu: [
|
|
{ label: 'Back', accelerator: 'Alt+Left', click: () => window.webContents.send('native-vue:back') },
|
|
{ label: 'Forward', accelerator: 'Alt+Right', click: () => window.webContents.send('native-vue:forward') },
|
|
{ type: 'separator' },
|
|
{ role: 'reload' },
|
|
],
|
|
},
|
|
{ role: 'editMenu' },
|
|
{ role: 'viewMenu' },
|
|
{ role: 'windowMenu' },
|
|
]
|
|
Menu.setApplicationMenu(Menu.buildFromTemplate(template))
|
|
return window
|
|
}
|
|
|
|
await app.whenReady()
|
|
createWindow()
|
|
app.on('activate', () => {
|
|
if (BrowserWindow.getAllWindows().length === 0) createWindow()
|
|
})
|
|
app.on('window-all-closed', () => {
|
|
if (process.platform !== 'darwin') app.quit()
|
|
})
|
|
|
|
ipcMain.handle('native-vue:platform', () => process.platform)
|