main.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Modules to control application life and create native browser window
  2. const { app, BrowserWindow, ipcMain, shell } = require('electron/main')
  3. function createWindow () {
  4. // Create the browser window.
  5. const mainWindow = new BrowserWindow({
  6. width: 800,
  7. height: 600,
  8. webPreferences: {
  9. contextIsolation: false,
  10. nodeIntegration: true
  11. }
  12. })
  13. // and load the index.html of the app.
  14. mainWindow.loadFile('index.html')
  15. // Open external links in the default browser
  16. mainWindow.webContents.on('will-navigate', (event, url) => {
  17. event.preventDefault()
  18. shell.openExternal(url)
  19. })
  20. let demoWindow
  21. ipcMain.on('show-demo-window', () => {
  22. if (demoWindow) {
  23. demoWindow.focus()
  24. return
  25. }
  26. demoWindow = new BrowserWindow({ width: 600, height: 400 })
  27. demoWindow.loadURL('https://electronjs.org')
  28. demoWindow.on('close', () => {
  29. mainWindow.webContents.send('window-close')
  30. })
  31. demoWindow.on('focus', () => {
  32. mainWindow.webContents.send('window-focus')
  33. })
  34. demoWindow.on('blur', () => {
  35. mainWindow.webContents.send('window-blur')
  36. })
  37. })
  38. ipcMain.on('focus-demo-window', () => {
  39. if (demoWindow) demoWindow.focus()
  40. })
  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.