main.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Modules to control application life and create native browser window
  2. const {app, BrowserWindow} = require('electron')
  3. const path = require('path')
  4. const fs = require('fs')
  5. // 读取配置文件
  6. const config = JSON.parse(fs.readFileSync('./config.json', 'utf-8'))
  7. function createWindow () {
  8. // Create the browser window.
  9. let webConfig = {
  10. width: config.width,
  11. height: config.height,
  12. webPreferences: {}
  13. }
  14. if (config.preload) {
  15. webConfig.webPreferences.preload = config.preload
  16. }
  17. const mainWindow = new BrowserWindow(webConfig)
  18. // and load the index.html of the app.
  19. // mainWindow.loadFile('index.html')
  20. mainWindow.loadURL(config.url)
  21. // Open the DevTools.
  22. // mainWindow.webContents.openDevTools()
  23. }
  24. // This method will be called when Electron has finished
  25. // initialization and is ready to create browser windows.
  26. // Some APIs can only be used after this event occurs.
  27. app.whenReady().then(() => {
  28. createWindow()
  29. app.on('activate', function () {
  30. // On macOS it's common to re-create a window in the app when the
  31. // dock icon is clicked and there are no other windows open.
  32. if (BrowserWindow.getAllWindows().length === 0) createWindow()
  33. })
  34. })
  35. // Quit when all windows are closed, except on macOS. There, it's common
  36. // for applications and their menu bar to stay active until the user quits
  37. // explicitly with Cmd + Q.
  38. app.on('window-all-closed', function () {
  39. if (process.platform !== 'darwin') app.quit()
  40. })
  41. // In this file you can include the rest of your app's specific main process
  42. // code. You can also put them in separate files and require them here.