Files
Showy/electron/main.js
Mohammed AbadandCopilot 4303b33677 feat(electron/frontend) Electron Full Rework
- **Electron Full Rework**: Completely rewrote `electron/main.js` — removed child process spawn, backend now runs in-process via `require()`; frameless window with custom title bar; dual-mode support (dev loads Vite at `:4200`, production loads built files)
- **`electron/preload.js`**: Enhanced contextBridge to expose `windowControls` (minimize/maximize/close) and `isElectron` flag alongside existing `ipcRenderer` bridge
- **`electron/package.json`**: Updated scripts (`start`, `dev`, `build-front` → calls React build), removed duplicated backend dependencies (backend now required in-process), updated electron-builder config with proper `files` and `extraResources`
- **`TitleBar` component**: New `react_frontend/src/components/layout/TitleBar.tsx` — custom frameless window title bar with drag region, app branding, and IPC-powered minimize/maximize/close buttons
- **`AppLayout`**: Conditionally renders `<TitleBar>` when built for Electron (`VITE_ELECTRON=true`); wrapped content in an extra flex container to accommodate title bar height
- **`vite.config.ts`**: Added `VITE_ELECTRON` env detection — sets `base: './'` for correct asset resolution in `file://` context
- **`api.ts`**: Dynamic base URL — uses `http://localhost:4000/api/v1` in Electron mode, `/api/v1` (proxy-relative) otherwise
- **`router.tsx`**: Conditional router factory — uses `createHashRouter` in Electron mode (required for `file://` navigation) and `createBrowserRouter` for web
- **`electron.d.ts`**: Added TypeScript declarations for `window.electron` global (windowControls, ipcRenderer, isElectron)
- **`package.json`**: Added `build:electron` script with `cross-env VITE_ELECTRON=true`; added `cross-env` devDependency
- Updated `Electron.md`

Co-authored-by: Copilot <copilot@github.com>
2026-05-03 02:00:14 +01:00

79 lines
1.8 KiB
JavaScript

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;
});
}