index.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. 'use strict'
  2. import { app, BrowserWindow } from 'electron'
  3. const isDevelopment = process.env.NODE_ENV !== 'production'
  4. // Global reference to mainWindow
  5. // Necessary to prevent win from being garbage collected
  6. let mainWindow
  7. function createMainWindow() {
  8. // Construct new BrowserWindow
  9. const window = new BrowserWindow()
  10. // Set url for `win`
  11. // points to `webpack-dev-server` in development
  12. // points to `index.html` in production
  13. const url = isDevelopment
  14. ? `http://localhost:${process.env.ELECTRON_WEBPACK_WDS_PORT}`
  15. : `file://${__dirname}/index.html`
  16. if (isDevelopment) {
  17. window.webContents.openDevTools()
  18. }
  19. window.loadURL(url)
  20. window.on('closed', () => {
  21. mainWindow = null
  22. })
  23. window.webContents.on('devtools-opened', () => {
  24. window.focus()
  25. setImmediate(() => {
  26. window.focus()
  27. })
  28. })
  29. return window
  30. }
  31. // Quit application when all windows are closed
  32. app.on('window-all-closed', () => {
  33. // On macOS it is common for applications to stay open
  34. // until the user explicitly quits
  35. if (process.platform !== 'darwin') app.quit()
  36. })
  37. app.on('activate', () => {
  38. // On macOS it is common to re-create a window
  39. // even after all windows have been closed
  40. if (mainWindow === null) mainWindow = createMainWindow()
  41. })
  42. // Create main BrowserWindow when electron is ready
  43. app.on('ready', () => {
  44. mainWindow = createMainWindow()
  45. })