main.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Modules to control application life and create native browser window
  2. const { app, BrowserWindow, ipcMain, dialog, shell } = require('electron/main')
  3. // Keep a global reference of the window object, if you don't, the window will
  4. // be closed automatically when the JavaScript object is garbage collected.
  5. let mainWindow
  6. function createWindow () {
  7. // Create the browser window.
  8. mainWindow = new BrowserWindow({
  9. width: 800,
  10. height: 600,
  11. webPreferences: {
  12. contextIsolation: false,
  13. nodeIntegration: true
  14. }
  15. })
  16. // and load the index.html of the app.
  17. mainWindow.loadFile('index.html')
  18. // Open the DevTools.
  19. // mainWindow.webContents.openDevTools()
  20. // Emitted when the window is closed.
  21. mainWindow.on('closed', function () {
  22. // Dereference the window object, usually you would store windows
  23. // in an array if your app supports multi windows, this is the time
  24. // when you should delete the corresponding element.
  25. mainWindow = null
  26. })
  27. // Open external links in the default browser
  28. mainWindow.webContents.on('will-navigate', (event, url) => {
  29. event.preventDefault()
  30. shell.openExternal(url)
  31. })
  32. }
  33. // This method will be called when Electron has finished
  34. // initialization and is ready to create browser windows.
  35. // Some APIs can only be used after this event occurs.
  36. app.whenReady().then(createWindow)
  37. // Quit when all windows are closed.
  38. app.on('window-all-closed', function () {
  39. // On macOS it is common for applications and their menu bar
  40. // to stay active until the user quits explicitly with Cmd + Q
  41. if (process.platform !== 'darwin') {
  42. app.quit()
  43. }
  44. })
  45. app.on('activate', function () {
  46. // On macOS it is common to re-create a window in the app when the
  47. // dock icon is clicked and there are no other windows open.
  48. if (mainWindow === null) {
  49. createWindow()
  50. }
  51. })
  52. ipcMain.on('open-information-dialog', event => {
  53. const options = {
  54. type: 'info',
  55. title: 'Information',
  56. message: "This is an information dialog. Isn't it nice?",
  57. buttons: ['Yes', 'No']
  58. }
  59. dialog.showMessageBox(options, index => {
  60. event.sender.send('information-dialog-selection', index)
  61. })
  62. })
  63. // In this file you can include the rest of your app's specific main process
  64. // code. You can also put them in separate files and require them here.