|
| 1 | +import { app, BrowserWindow, Menu, ipcMain } from 'electron' |
| 2 | +import { createRequire } from 'node:module' |
| 3 | +import { fileURLToPath } from 'node:url' |
| 4 | +import path from 'node:path' |
| 5 | + |
| 6 | +const require = createRequire(import.meta.url) |
| 7 | +require; |
| 8 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
| 9 | + |
| 10 | +// The built directory structure |
| 11 | +// |
| 12 | +// ├─┬─┬ dist |
| 13 | +// │ │ └── index.html |
| 14 | +// │ │ |
| 15 | +// │ ├─┬ dist-electron |
| 16 | +// │ │ ├── main.js |
| 17 | +// │ │ └── preload.mjs |
| 18 | +// │ |
| 19 | +process.env.APP_ROOT = path.join(__dirname, '..') |
| 20 | + |
| 21 | +// 🚧 Use ['ENV_NAME'] avoid vite:define plugin - [email protected] |
| 22 | +export const VITE_DEV_SERVER_URL = process.env['VITE_DEV_SERVER_URL'] |
| 23 | +export const MAIN_DIST = path.join(process.env.APP_ROOT, 'dist-electron') |
| 24 | +export const RENDERER_DIST = path.join(process.env.APP_ROOT, 'dist') |
| 25 | + |
| 26 | +process.env.VITE_PUBLIC = VITE_DEV_SERVER_URL ? path.join(process.env.APP_ROOT, 'public') : RENDERER_DIST |
| 27 | + |
| 28 | +let win: BrowserWindow | null |
| 29 | + |
| 30 | +function createWindow() { |
| 31 | + Menu.setApplicationMenu(null) ; // Hide menu bar |
| 32 | + win = new BrowserWindow({ |
| 33 | + icon: path.join(process.env.VITE_PUBLIC, 'icons/icon.ico'), |
| 34 | + webPreferences: { |
| 35 | + preload: path.join(__dirname, 'preload.mjs'), |
| 36 | + }, |
| 37 | + frame: true, |
| 38 | + }) |
| 39 | + |
| 40 | + // Test active push message to Renderer-process. |
| 41 | + win.webContents.on('did-finish-load', () => { |
| 42 | + win?.webContents.send('main-process-message', (new Date).toLocaleString()) |
| 43 | + }) |
| 44 | + |
| 45 | + if (VITE_DEV_SERVER_URL) { |
| 46 | + win.loadURL(VITE_DEV_SERVER_URL) |
| 47 | + win.webContents.openDevTools() |
| 48 | + } else { |
| 49 | + // win.loadFile('dist/index.html') |
| 50 | + win.loadFile(path.join(RENDERER_DIST, 'index.html')) |
| 51 | + } |
| 52 | + |
| 53 | + ipcMain.on('resize-window', (event, width:number, height:number) => { |
| 54 | + if (win) { |
| 55 | + win.setSize(width, height) |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + ipcMain.on('set-window-frame-visible', (event, isFrameVisible:boolean) => { |
| 60 | + if (win) { |
| 61 | + win.setMenuBarVisibility(isFrameVisible) |
| 62 | + win.setResizable(isFrameVisible) |
| 63 | + } |
| 64 | + }); |
| 65 | + ipcMain.on('set-window-title', (event, title:string) => { |
| 66 | + if (win) { |
| 67 | + win.setTitle(title) |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | +} |
| 72 | + |
| 73 | +// Quit when all windows are closed, except on macOS. There, it's common |
| 74 | +// for applications and their menu bar to stay active until the user quits |
| 75 | +// explicitly with Cmd + Q. |
| 76 | +app.on('window-all-closed', () => { |
| 77 | + if (process.platform !== 'darwin') { |
| 78 | + app.quit() |
| 79 | + win = null |
| 80 | + } |
| 81 | +}) |
| 82 | + |
| 83 | +app.on('activate', () => { |
| 84 | + // On OS X it's common to re-create a window in the app when the |
| 85 | + // dock icon is clicked and there are no other windows open. |
| 86 | + if (BrowserWindow.getAllWindows().length === 0) { |
| 87 | + createWindow() |
| 88 | + } |
| 89 | +}) |
| 90 | + |
| 91 | +app.whenReady().then(createWindow) |
0 commit comments