Explorar el Código

chore: no need to hold on to mainWindow reference (#357)

Jeremy Apthorp hace 5 años
padre
commit
6cf4eeb47d
Se han modificado 1 ficheros con 2 adiciones y 14 borrados
  1. 2 14
      main.js

+ 2 - 14
main.js

@@ -2,13 +2,9 @@
 const {app, BrowserWindow} = require('electron')
 const path = require('path')
 
-// Keep a global reference of the window object, if you don't, the window will
-// be closed automatically when the JavaScript object is garbage collected.
-let mainWindow
-
 function createWindow () {
   // Create the browser window.
-  mainWindow = new BrowserWindow({
+  const mainWindow = new BrowserWindow({
     width: 800,
     height: 600,
     webPreferences: {
@@ -21,14 +17,6 @@ function createWindow () {
 
   // Open the DevTools.
   // mainWindow.webContents.openDevTools()
-
-  // Emitted when the window is closed.
-  mainWindow.on('closed', function () {
-    // Dereference the window object, usually you would store windows
-    // in an array if your app supports multi windows, this is the time
-    // when you should delete the corresponding element.
-    mainWindow = null
-  })
 }
 
 // This method will be called when Electron has finished
@@ -46,7 +34,7 @@ app.on('window-all-closed', function () {
 app.on('activate', function () {
   // On macOS it's common to re-create a window in the app when the
   // dock icon is clicked and there are no other windows open.
-  if (mainWindow === null) createWindow()
+  if (BrowserWindow.getAllWindows().length === 0) createWindow()
 })
 
 // In this file you can include the rest of your app's specific main process