Files
RUWIO/electron/main.cts
T
2026-06-05 14:11:44 +02:00

138 lines
3.6 KiB
TypeScript

import { app, BrowserWindow, Menu, shell } from 'electron'
import type { BrowserWindow as BrowserWindowInstance, Event, MenuItemConstructorOptions } from 'electron'
import path from 'node:path'
const isDev = !app.isPackaged
const devServerUrl = process.env.VITE_DEV_SERVER_URL ?? 'http://localhost:5200'
let mainWindow: BrowserWindowInstance | null = null
function createMainWindow(): void {
mainWindow = new BrowserWindow({
width: 1280,
height: 860,
minWidth: 1024,
minHeight: 700,
title: 'RUWIO',
show: false,
backgroundColor: '#14171f',
webPreferences: {
preload: path.join(__dirname, 'preload.cjs'),
contextIsolation: true,
nodeIntegration: false,
sandbox: true,
},
})
mainWindow.once('ready-to-show', () => {
mainWindow?.show()
})
mainWindow.webContents.setWindowOpenHandler(({ url }: { url: string }) => {
if (url.startsWith('https://') || url.startsWith('http://')) {
void shell.openExternal(url)
}
return { action: 'deny' }
})
mainWindow.webContents.on('will-navigate', (event: Event, url: string) => {
const allowedUrl = isDev ? devServerUrl : `file://${path.join(app.getAppPath(), 'dist', 'index.html')}`
if (!url.startsWith(allowedUrl)) {
event.preventDefault()
}
})
if (isDev) {
void mainWindow.loadURL(devServerUrl)
mainWindow.webContents.openDevTools({ mode: 'detach' })
} else {
void mainWindow.loadFile(path.join(app.getAppPath(), 'dist', 'index.html'))
}
}
function createMenu(): void {
const template: MenuItemConstructorOptions[] = [
{
label: 'File',
submenu: [
{ label: 'New Project', accelerator: 'CmdOrCtrl+N', enabled: false },
{ label: 'Open...', accelerator: 'CmdOrCtrl+O', enabled: false },
{ type: 'separator' },
{ label: 'Save', accelerator: 'CmdOrCtrl+S', enabled: false },
{ label: 'Save As...', accelerator: 'CmdOrCtrl+Shift+S', enabled: false },
{ type: 'separator' },
{ label: 'Export...', accelerator: 'CmdOrCtrl+E', enabled: false },
{ type: 'separator' },
process.platform === 'darwin' ? { role: 'close' } : { role: 'quit' },
],
},
{
label: 'Edit',
submenu: [
{ role: 'undo' },
{ role: 'redo' },
{ type: 'separator' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
{ role: 'selectAll' },
],
},
{
label: 'View',
submenu: [
{ role: 'reload' },
{ role: 'forceReload' },
{ role: 'toggleDevTools' },
{ type: 'separator' },
{ role: 'resetZoom' },
{ role: 'zoomIn' },
{ role: 'zoomOut' },
{ type: 'separator' },
{ role: 'togglefullscreen' },
],
},
{
label: 'Window',
submenu: [
{ role: 'minimize' },
{ role: 'zoom' },
],
},
]
if (process.platform === 'darwin') {
template.unshift({
label: app.name,
submenu: [
{ role: 'about' },
{ type: 'separator' },
{ role: 'services' },
{ type: 'separator' },
{ role: 'hide' },
{ role: 'hideOthers' },
{ role: 'unhide' },
{ type: 'separator' },
{ role: 'quit' },
],
})
}
Menu.setApplicationMenu(Menu.buildFromTemplate(template))
}
app.setName('RUWIO')
app.whenReady().then(() => {
createMenu()
createMainWindow()
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createMainWindow()
})
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
})