main.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // main.js
  2. // Modules to control application life and create native browser window
  3. const {app, BrowserWindow} = require('electron')
  4. const path = require('node:path')
  5. const createWindow = () => {
  6. // Create the browser window.
  7. const mainWindow = new BrowserWindow({
  8. width: 800,
  9. height: 600,
  10. webPreferences: {
  11. preload: path.join(__dirname, 'preload.js')
  12. }
  13. })
  14. //监听
  15. function handleSetTitle(event, title) {
  16. const webContents = event.sender
  17. const win = BrowserWindow.fromWebContents(webContents)
  18. win.setTitle(title)
  19. }
  20. // 加载 index.html
  21. mainWindow.loadFile('./dist/index.html')
  22. // 打开开发工具
  23. // mainWindow.webContents.openDevTools()
  24. }
  25. // 这段程序将会在 Electron 结束初始化
  26. // 和创建浏览器窗口的时候调用
  27. // 部分 API 在 ready 事件触发后才能使用。
  28. app.whenReady().then(() => {
  29. createWindow()
  30. ipcMain.on('set-title', handleSetTitle)
  31. app.on('activate', () => {
  32. // 在 macOS 系统内, 如果没有已开启的应用窗口
  33. // 点击托盘图标时通常会重新创建一个新窗口
  34. if (BrowserWindow.getAllWindows().length === 0) createWindow()
  35. })
  36. })
  37. // 除了 macOS 外,当所有窗口都被关闭的时候退出程序。 因此, 通常
  38. // 对应用程序和它们的菜单栏来说应该时刻保持激活状态,
  39. // 直到用户使用 Cmd + Q 明确退出
  40. app.on('window-all-closed', () => {
  41. if (process.platform !== 'darwin') app.quit()
  42. })
  43. // 在当前文件中你可以引入所有的主进程代码
  44. // 也可以拆分成几个文件,然后用 require 导入。