| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- // 使用 CommonJS 格式
- const { app, BrowserWindow, ipcMain, shell } = require('electron');
- const { join } = require('path');
- let mainWindow: typeof BrowserWindow.prototype | null = null;
- const VITE_DEV_SERVER_URL = process.env.VITE_DEV_SERVER_URL;
- function createWindow() {
- mainWindow = new BrowserWindow({
- width: 1400,
- height: 900,
- minWidth: 1200,
- minHeight: 700,
- webPreferences: {
- preload: join(__dirname, 'preload.js'),
- nodeIntegration: false,
- contextIsolation: true,
- },
- titleBarStyle: 'hiddenInset',
- frame: process.platform !== 'darwin',
- show: false,
- });
- // 窗口准备好后再显示,避免白屏
- mainWindow.once('ready-to-show', () => {
- mainWindow?.show();
- });
- // 加载页面
- if (VITE_DEV_SERVER_URL) {
- mainWindow.loadURL(VITE_DEV_SERVER_URL);
- mainWindow.webContents.openDevTools();
- } else {
- mainWindow.loadFile(join(__dirname, '../dist/index.html'));
- }
- // 处理外部链接
- mainWindow.webContents.setWindowOpenHandler(({ url }: { url: string }) => {
- shell.openExternal(url);
- return { action: 'deny' };
- });
- mainWindow.on('closed', () => {
- mainWindow = null;
- });
- }
- // 单实例锁定
- const gotTheLock = app.requestSingleInstanceLock();
- if (!gotTheLock) {
- app.quit();
- } else {
- app.on('second-instance', () => {
- if (mainWindow) {
- if (mainWindow.isMinimized()) mainWindow.restore();
- mainWindow.focus();
- }
- });
- app.whenReady().then(() => {
- createWindow();
- app.on('activate', () => {
- if (BrowserWindow.getAllWindows().length === 0) {
- createWindow();
- }
- });
- });
- }
- app.on('window-all-closed', () => {
- if (process.platform !== 'darwin') {
- app.quit();
- }
- });
- // IPC 处理
- ipcMain.handle('get-app-version', () => {
- return app.getVersion();
- });
- ipcMain.handle('get-platform', () => {
- return process.platform;
- });
- // 窗口控制
- ipcMain.on('window-minimize', () => {
- mainWindow?.minimize();
- });
- ipcMain.on('window-maximize', () => {
- if (mainWindow?.isMaximized()) {
- mainWindow.unmaximize();
- } else {
- mainWindow?.maximize();
- }
- });
- ipcMain.on('window-close', () => {
- mainWindow?.close();
- });
|