main.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Modules to control application life and create native browser window
  2. const { app, BrowserWindow, ipcMain, shell } = require('electron/main')
  3. ipcMain.on('create-demo-window', (event) => {
  4. const win = new BrowserWindow({ width: 400, height: 275 })
  5. function updateReply () {
  6. event.sender.send('bounds-changed', {
  7. size: win.getSize(),
  8. position: win.getPosition()
  9. })
  10. }
  11. win.on('resize', updateReply)
  12. win.on('move', updateReply)
  13. win.loadURL('https://electronjs.org')
  14. })
  15. function createWindow () {
  16. // Create the browser window.
  17. const mainWindow = new BrowserWindow({
  18. width: 800,
  19. height: 600,
  20. webPreferences: {
  21. contextIsolation: false,
  22. nodeIntegration: true
  23. }
  24. })
  25. // and load the index.html of the app.
  26. mainWindow.loadFile('index.html')
  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(() => {
  37. createWindow()
  38. app.on('activate', function () {
  39. // On macOS it's common to re-create a window in the app when the
  40. // dock icon is clicked and there are no other windows open.
  41. if (BrowserWindow.getAllWindows().length === 0) createWindow()
  42. })
  43. })
  44. // Quit when all windows are closed, except on macOS. There, it's common
  45. // for applications and their menu bar to stay active until the user quits
  46. // explicitly with Cmd + Q.
  47. app.on('window-all-closed', function () {
  48. if (process.platform !== 'darwin') app.quit()
  49. })
  50. // In this file you can include the rest of your app's specific main process
  51. // code. You can also put them in separate files and require them here.