main.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Modules to control application life and create native browser window
  2. const { app, BrowserWindow, ipcMain, shell } = require('electron/main')
  3. ipcMain.on('new-window', (event, { url, width, height }) => {
  4. const win = new BrowserWindow({ width, height })
  5. win.loadURL(url)
  6. })
  7. function createWindow () {
  8. // Create the browser window.
  9. const mainWindow = new BrowserWindow({
  10. width: 800,
  11. height: 600,
  12. webPreferences: {
  13. contextIsolation: false,
  14. nodeIntegration: true
  15. }
  16. })
  17. // and load the index.html of the app.
  18. mainWindow.loadFile('index.html')
  19. // Open external links in the default browser
  20. mainWindow.webContents.on('will-navigate', (event, url) => {
  21. event.preventDefault()
  22. shell.openExternal(url)
  23. })
  24. }
  25. // This method will be called when Electron has finished
  26. // initialization and is ready to create browser windows.
  27. // Some APIs can only be used after this event occurs.
  28. app.whenReady().then(() => {
  29. createWindow()
  30. app.on('activate', function () {
  31. // On macOS it's common to re-create a window in the app when the
  32. // dock icon is clicked and there are no other windows open.
  33. if (BrowserWindow.getAllWindows().length === 0) createWindow()
  34. })
  35. })
  36. // Quit when all windows are closed, except on macOS. There, it's common
  37. // for applications and their menu bar to stay active until the user quits
  38. // explicitly with Cmd + Q.
  39. app.on('window-all-closed', function () {
  40. if (process.platform !== 'darwin') app.quit()
  41. })
  42. // In this file you can include the rest of your app's specific main process
  43. // code. You can also put them in separate files and require them here.