main.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. const { app, BrowserWindow } = require('electron')
  2. const path = require('path')
  3. const config = require('../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. mainWindow.loadURL('http://localhost:5173')
  17. // 根据配置文件决定是否打开调试侧边栏
  18. if (config.devTools.enabled) {
  19. mainWindow.webContents.openDevTools()
  20. }
  21. } else {
  22. mainWindow.loadFile(path.join(__dirname, '../dist/index.html'))
  23. }
  24. }
  25. app.whenReady().then(() => {
  26. createWindow()
  27. app.on('activate', () => {
  28. if (BrowserWindow.getAllWindows().length === 0) {
  29. createWindow()
  30. }
  31. })
  32. })
  33. app.on('window-all-closed', () => {
  34. if (process.platform !== 'darwin') {
  35. app.quit()
  36. }
  37. })