main.js 1014 B

1234567891011121314151617181920212223242526272829303132333435363738
  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. })
  17. win.loadURL('http://localhost:5173')
  18. win.webContents.openDevTools()
  19. winState.manage(win)
  20. }
  21. app.whenReady().then(() => {
  22. createWindow()
  23. // darwin系统关闭窗口但是它还在后台运行
  24. // 所以检测APP是否在活动,如果还在就重新创建
  25. app.on('activate', () => {
  26. if (BrowserWindow.getAllWindows().length ===0){
  27. createWindow()
  28. }
  29. })
  30. })
  31. app.on('window-all-closed', () => {
  32. app.quit()
  33. })