main.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. const { app, BrowserWindow} = require('electron')
  2. const WinState = require('electron-win-state').default
  3. const path = require('path')
  4. const createWindow = () => {
  5. const winState = new WinState({
  6. defaultWidth: 1000,
  7. defaultHeight:800
  8. })
  9. const win = new BrowserWindow({
  10. ...winState.winOptions,
  11. webPreferences:{
  12. // 新版 electron 必须关闭沙盒模式才能使用 clipboard
  13. sandbox: false,
  14. preload: path.resolve(__dirname, './preload/index.js')
  15. },
  16. show: false
  17. })
  18. win.loadURL('http://localhost:5173')
  19. win.webContents.openDevTools()
  20. winState.manage(win)
  21. // 等页面加载渲染完后,才显示窗口,避免刚启动时看到页面空白
  22. win.on('ready-to-show', () => {
  23. win.show()
  24. })
  25. }
  26. app.whenReady().then(() => {
  27. createWindow()
  28. // darwin系统关闭窗口但是它还在后台运行
  29. // 所以检测APP是否在活动,如果还在就重新创建
  30. app.on('activate', () => {
  31. if (BrowserWindow.getAllWindows().length ===0){
  32. createWindow()
  33. }
  34. })
  35. })
  36. app.on('window-all-closed', () => {
  37. app.quit()
  38. })