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>
This commit is contained in:
1 parent
7b84f89b65
commit
4303b33677
14 files changed
+5522
-164
No files matched your search
+138
-34
@@ -9,67 +9,171 @@ tags:
|
||||
|
||||
## Overview
|
||||
|
||||
Electron 34 wraps the Showy backend + frontend into a standalone desktop application for Windows.
|
||||
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] --> |child_process.spawn| B[Node.js Backend]
|
||||
E --> |BrowserWindow.loadFile| FE[Frontend Build]
|
||||
FE --> |HTTP requests| B
|
||||
E --> |IPC: backend-ready| FE
|
||||
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. Spawns backend as a **child process**: `node backend/server.js`
|
||||
2. Sets `PARENT_PROCESS` env var pointing to Electron user data path
|
||||
3. Backend auto-restarts if it crashes
|
||||
4. Creates `BrowserWindow` (1200×720) loading `frontend_build/browser/index.html`
|
||||
5. Sends `backend-ready` IPC event when backend starts listening
|
||||
6. On window close → kills backend child process
|
||||
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 | 1200 |
|
||||
| Height | 720 |
|
||||
| Context Isolation | `true` |
|
||||
| Sandbox | `true` |
|
||||
| Node Integration | `true` |
|
||||
| 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 `electron.ipcRenderer` to the renderer process via `contextBridge.exposeInMainWorld`:
|
||||
Exposes via `contextBridge.exposeInMainWorld('electron', ...)`:
|
||||
|
||||
- `send(channel, data)` — send to main
|
||||
- `on(channel, callback)` — listen from main
|
||||
| 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 |
|
||||
| Icon | From Angular frontend assets |
|
||||
| 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
|
||||
|
||||
- `electron` 34
|
||||
- `electron-builder` 25
|
||||
- All backend dependencies (bundled)
|
||||
| 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 entry |
|
||||
| `electron/preload.js` | Context bridge for IPC |
|
||||
| `electron/package.json` | Electron + backend deps |
|
||||
| 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
|
||||
Reference in new issue
Block a user