- **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>
180 lines
7.1 KiB
Markdown
180 lines
7.1 KiB
Markdown
---
|
||
title: Electron
|
||
tags:
|
||
- electron
|
||
- desktop
|
||
---
|
||
|
||
# Electron
|
||
|
||
## Overview
|
||
|
||
Electron 34 wraps the Showy backend + React frontend into a standalone desktop application for Windows. The backend runs **in-process** (no child process spawn), and the app uses a custom frameless title bar with IPC-driven window controls.
|
||
|
||
## Architecture
|
||
|
||
```mermaid
|
||
graph TD
|
||
E[Electron Main Process] --> |require in-process| B[Node.js Backend]
|
||
E --> |BrowserWindow| FE[React Frontend Build]
|
||
FE --> |HTTP localhost:4000| B
|
||
FE --> |IPC: windowControls| E
|
||
```
|
||
|
||
## Main Process — `electron/main.js`
|
||
|
||
1. Sets `process.env.PARENT_PROCESS` with Electron user data path
|
||
2. Starts backend **in-process** via `require('../backend/server.js')`
|
||
3. Creates frameless `BrowserWindow` (1280×800)
|
||
4. **Dev mode** (`!app.isPackaged` or `ELECTRON_DEV=true`): loads `http://localhost:4200` (Vite dev server) + opens DevTools
|
||
5. **Production mode**: loads `frontend_build/index.html` (hash-routed React app)
|
||
6. Registers IPC handlers for window controls
|
||
|
||
### Window Configuration
|
||
|
||
| Setting | Value |
|
||
| ----------------- | ------------------------------ |
|
||
| Width | 1280 |
|
||
| Height | 800 |
|
||
| Min Width | 900 |
|
||
| Min Height | 600 |
|
||
| Frame | `false` (frameless) |
|
||
| Context Isolation | `true` |
|
||
| Node Integration | `false` |
|
||
| Sandbox | `false` (required for preload) |
|
||
|
||
## Preload Script — `electron/preload.js`
|
||
|
||
Exposes via `contextBridge.exposeInMainWorld('electron', ...)`:
|
||
|
||
| API | Methods |
|
||
| ---------------- | -------------------------------------------------- |
|
||
| `isElectron` | `true` (flag for frontend detection) |
|
||
| `windowControls` | `minimize()`, `maximize()`, `close()` |
|
||
| `ipcRenderer` | `send()`, `on()`, `once()`, `removeAllListeners()` |
|
||
|
||
## IPC Channels
|
||
|
||
| Channel | Direction | Purpose |
|
||
| ----------------- | --------------- | ----------------------- |
|
||
| `window-minimize` | Renderer → Main | Minimize window |
|
||
| `window-maximize` | Renderer → Main | Toggle maximize/restore |
|
||
| `window-close` | Renderer → Main | Close window |
|
||
|
||
## Custom Title Bar
|
||
|
||
The React frontend renders a `<TitleBar>` component when `VITE_ELECTRON=true`:
|
||
|
||
- Draggable region (`-webkit-app-region: drag`)
|
||
- App branding ("SHOWY")
|
||
- Min/Max/Close buttons (no-drag region, sends IPC)
|
||
|
||
See: `react_frontend/src/components/layout/TitleBar.tsx`
|
||
|
||
## Dev Mode Workflow
|
||
|
||
1. Start Vite dev server: `cd react_frontend && pnpm dev` (port 4200)
|
||
2. Run Electron in dev: `cd electron && pnpm dev`
|
||
3. Electron detects `ELECTRON_DEV=true` → loads from Vite HMR server
|
||
|
||
## Production Build Workflow
|
||
|
||
1. Build React for Electron: `cd react_frontend && pnpm build:electron`
|
||
- Sets `VITE_ELECTRON=true` → hash router, `base: './'`, API points to `localhost:4000`
|
||
- Outputs to `electron/frontend_build/`
|
||
2. Package with electron-builder: `cd electron && pnpm generate-exe`
|
||
- Bundles `frontend_build/`, backend code, and electron files
|
||
- Outputs to `dist/electron/`
|
||
|
||
## Build Configuration
|
||
|
||
| Field | Value |
|
||
| ------- | ---------------------------- |
|
||
| `appId` | `x4lo.showy` |
|
||
| Target | Windows x64 directory output |
|
||
| Output | `../dist/electron` |
|
||
|
||
## Frontend Adaptations for Electron
|
||
|
||
| Concern | Web Mode | Electron Mode |
|
||
| ------------ | ----------------------- | ------------------------------------------ |
|
||
| Router | `createBrowserRouter` | `createHashRouter` |
|
||
| API Base URL | `/api/v1` (Vite proxy) | `http://localhost:4000/api/v1` |
|
||
| Asset Paths | `/` base | `./` relative base |
|
||
| Title Bar | Hidden (native browser) | Custom `<TitleBar>` component |
|
||
| Detection | — | `import.meta.env.VITE_ELECTRON === 'true'` |
|
||
|
||
## Package Dependencies
|
||
|
||
| Package | Purpose |
|
||
| ---------------------- | ------------------ |
|
||
| `electron` ^34 | Runtime |
|
||
| `electron-builder` ^25 | Packaging (devDep) |
|
||
|
||
Backend dependencies live in `backend/package.json` (required in-process at runtime).
|
||
|
||
## Important Files
|
||
|
||
| File | Role |
|
||
| --------------------------------------------------- | ------------------------------------------------------- |
|
||
| `electron/main.js` | Main process — in-process backend, window creation, IPC |
|
||
| `electron/preload.js` | Context bridge (window controls + IPC) |
|
||
| `electron/package.json` | Scripts + builder config |
|
||
| `react_frontend/src/components/layout/TitleBar.tsx` | Custom frameless title bar |
|
||
| `react_frontend/src/electron.d.ts` | TypeScript types for `window.electron` |
|
||
|
||
## Folder Structure
|
||
|
||
### Running from source (`npm run start` in `electron/`)
|
||
|
||
Before running from source, you need:
|
||
1. `cd react_frontend && pnpm build:electron` → builds the React app into `electron/frontend_build/`
|
||
2. `cd backend && npm install` → install backend deps
|
||
3. `cd electron && npm install` → install electron dep
|
||
4. `cd electron && npm start`
|
||
|
||
Expected structure:
|
||
|
||
```
|
||
Showy/
|
||
├── backend/
|
||
│ ├── server.js
|
||
│ ├── package.json
|
||
│ └── node_modules/ ← npm install in backend/
|
||
├── electron/
|
||
│ ├── main.js
|
||
│ ├── preload.js
|
||
│ ├── package.json
|
||
│ ├── node_modules/ ← npm install in electron/
|
||
│ └── frontend_build/ ← created by pnpm build:electron
|
||
│ ├── index.html
|
||
│ └── assets/
|
||
```
|
||
|
||
### After packaging (`npm run generate-exe`)
|
||
|
||
```
|
||
dist/electron/win-unpacked/
|
||
├── Showy.exe
|
||
├── resources/
|
||
│ ├── app/
|
||
│ │ ├── main.js
|
||
│ │ ├── preload.js
|
||
│ │ └── frontend_build/
|
||
│ │ ├── index.html
|
||
│ │ └── assets/
|
||
│ └── backend/ ← copied via extraResources (no node_modules!)
|
||
│ ├── server.js
|
||
│ └── ...
|
||
└── (Electron runtime DLLs)
|
||
```
|
||
|
||
> **⚠️ Packaging caveat**: The current config excludes `backend/node_modules` from the package via `extraResources`. The packaged app will be missing backend dependencies (Express, Sequelize, etc.) and will crash at runtime. To fix this for distribution, either include `backend/node_modules` in `extraResources`, or move all backend dependencies into `electron/package.json`.
|
||
|
||
## See Also
|
||
|
||
- [[Architecture]] — System overview
|
||
- [[Frontend]] — React frontend details
|
||
- [[Backend]] — API and services
|
||
- [[Deployment]] — Docker alternative
|