const { app, BrowserWindow, ipcMain } = require('electron'); const path = require('path'); let mainWindow; const isDev = !app.isPackaged || process.env.ELECTRON_DEV === 'true'; app.on('ready', () => { startBackend(); createWindow(); }); app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit(); } }); app.on('activate', () => { if (mainWindow === null) { createWindow(); } }); // --- IPC Handlers for window controls --- ipcMain.on('window-minimize', () => mainWindow?.minimize()); ipcMain.on('window-maximize', () => { if (mainWindow?.isMaximized()) { mainWindow.unmaximize(); } else { mainWindow?.maximize(); } }); ipcMain.on('window-close', () => mainWindow?.close()); // --- Backend (in-process) --- function startBackend() { // Set env before requiring backend so configService / server.js can read it process.env.PARENT_PROCESS = JSON.stringify({ electron_path: app.getPath('userData'), }); try { require(path.join(__dirname, '..', 'backend', 'server.js')); } catch (err) { console.error('Failed to start backend:', err); } } // --- Window --- function createWindow() { mainWindow = new BrowserWindow({ width: 1280, height: 800, minWidth: 900, minHeight: 600, frame: false, titleBarStyle: 'hidden', backgroundColor: '#09090b', webPreferences: { preload: path.join(__dirname, 'preload.js'), nodeIntegration: false, contextIsolation: true, sandbox: false, // required for preload to access Node APIs }, }); if (isDev) { mainWindow.loadURL('http://localhost:4200'); mainWindow.webContents.openDevTools({ mode: 'detach' }); } else { mainWindow.loadFile(path.join(__dirname, 'frontend_build', 'index.html')); } mainWindow.on('closed', () => { mainWindow = null; }); }