main.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Modules to control application life and create native browser window
  2. const { app, BrowserWindow, Menu } = require('electron');
  3. const path = require('node:path');
  4. function createWindow () {
  5. const mainWindow = new BrowserWindow({
  6. width: 800,
  7. height: 600,
  8. webPreferences: {
  9. preload: path.join(__dirname, 'preload.js')
  10. }
  11. });
  12. mainWindow.loadFile('./dist/index.html');
  13. const template = [
  14. {
  15. label: '窗口',
  16. // submenu 代表下一级菜单
  17. submenu: [
  18. {
  19. label: '刷新窗口',
  20. accelerator: 'F5',
  21. // 添加点击事件
  22. click: () => {
  23. mainWindow.reload();
  24. },
  25. },
  26. {
  27. label: '关闭窗口',
  28. accelerator: 'ctrl+q',
  29. // 添加点击事件
  30. click: () => {
  31. mainWindow.close();
  32. },
  33. }
  34. ],
  35. }
  36. ]
  37. // 3.从模板中创建菜单
  38. const myMenu = Menu.buildFromTemplate(template);
  39. // 4.设置为应用程序菜单
  40. Menu.setApplicationMenu(myMenu);
  41. }
  42. // This method will be called when Electron has finished
  43. // initialization and is ready to create browser windows.
  44. // Some APIs can only be used after this event occurs.
  45. app.whenReady().then(() => {
  46. createWindow()
  47. app.on('activate', function () {
  48. // On macOS it's common to re-create a window in the app when the
  49. // dock icon is clicked and there are no other windows open.
  50. if (BrowserWindow.getAllWindows().length === 0) createWindow()
  51. })
  52. })
  53. // Quit when all windows are closed, except on macOS. There, it's common
  54. // for applications and their menu bar to stay active until the user quits
  55. // explicitly with Cmd + Q.
  56. app.on('window-all-closed', function () {
  57. if (process.platform !== 'darwin') app.quit()
  58. })
  59. // In this file you can include the rest of your app's specific main process
  60. // code. You can also put them in separate files and require them here.