main.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. const { app, BrowserWindow } = require('electron')
  2. const path = require('path')
  3. const config = require('../configs/config.js')
  4. const isDev = process.env.NODE_ENV === 'development' || !app.isPackaged
  5. function createWindow() {
  6. const mainWindow = new BrowserWindow({
  7. width: config.window.width,
  8. height: config.window.height,
  9. autoHideMenuBar: config.window.autoHideMenuBar, // 从配置文件读取
  10. webPreferences: {
  11. nodeIntegration: false,
  12. contextIsolation: true
  13. }
  14. })
  15. if (isDev) {
  16. // 从配置文件读取 Vite 开发服务器端口
  17. const vitePort = config.vite?.port || 5173
  18. mainWindow.loadURL(`http://${config.vite?.host || 'localhost'}:${vitePort}`)
  19. // 根据配置文件决定是否打开调试侧边栏
  20. if (config.devTools.enabled) {
  21. mainWindow.webContents.openDevTools()
  22. }
  23. } else {
  24. mainWindow.loadFile(path.join(__dirname, '../dist/index.html'))
  25. }
  26. }
  27. app.whenReady().then(() => {
  28. createWindow()
  29. app.on('activate', () => {
  30. if (BrowserWindow.getAllWindows().length === 0) {
  31. createWindow()
  32. }
  33. })
  34. })
  35. app.on('window-all-closed', () => {
  36. if (process.platform !== 'darwin') {
  37. app.quit()
  38. }
  39. })