main.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. const { app, BrowserWindow } = require('electron');
  2. const path = require('path');
  3. const url = require('url');
  4. let win = null;
  5. // 创建窗口
  6. const createWindow = () => {
  7. win = new BrowserWindow({
  8. // width: 1200,
  9. // height: 800,
  10. show: false, // 是否显示窗口
  11. autoHideMenuBar: true, // 隐藏菜单栏
  12. webPreferences: {
  13. nodeIntegration: true, // 是否集成 Node.js
  14. // enableRemoteModule: true, // 是否启用 remote 模块
  15. webSecurity: false, // 是否禁用同源策略
  16. preload: path.join(__dirname, 'preload.js'), // 预加载脚本
  17. },
  18. });
  19. win.loadURL(
  20. url.format({
  21. pathname: path.join(__dirname, './dist', 'index.html'),
  22. protocol: 'file:',
  23. slashes: true, // true: file://, false: file:
  24. hash: '/login',
  25. }),
  26. );
  27. win.once('ready-to-show', () => {
  28. win.maximize();
  29. win.show();
  30. });
  31. };
  32. // 当 Electron 完成初始化并准备创建浏览器窗口时调用此方法
  33. app.whenReady().then(() => {
  34. createWindow();
  35. app.on('activate', () => {
  36. if (BrowserWindow.getAllWindows().length === 0) {
  37. createWindow();
  38. }
  39. });
  40. });
  41. // 当所有窗口都已关闭时退出
  42. app.on('window-all-closed', () => {
  43. if (process.platform !== 'darwin') {
  44. app.quit();
  45. }
  46. });
  47. app.on('activate', () => {
  48. if (BrowserWindow.getAllWindows().length === 0) {
  49. createWindow();
  50. }
  51. });