Electron OSX app behavior on close


#1

Hello, I am facing the problem where after I click on “X” in the title bar to close the app, it correctly minimizes to the dock of OS X (Yosemite) but does not recover its window after that.

the main.js code for my electron app is as follows (Taken from the example):

// Quit when all windows are closed.
app.on('window-all-closed', function() {
  // On OS X it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform != 'darwin') {
    app.quit();
  }
});

When pressing X on OS X this correctly does not kill the app, but then the window disappears and never re-appears on tapping the icon till I kill the app which defeats the purpose.

I tried adding a darwin check to this event, but it does not make a difference

 // 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.
     if (process.platform != 'darwin') {
          mainWindow = null;
    }
  });

So the question is, how do I make sure my window re-appears after I close it on OS X?

thanks


#2

Actually, when you close the last window of an OS X application the window doesn’t minimize … the window is destroyed. There is nothing to “recover”. What you should do when you’re in the state of “no windows but still running” and you need a new window is to create a new window.


#3

Thank you. So you mean repeat the app.on(ready) code ?

if (process.platform != 'darwin') {
    app.quit();
  }
  else
  {
     // Create the browser window.
     mainWindow = new BrowserWindow({width:1024, height:900});

     // and load the index.html of the app.
     mainWindow.loadUrl('file://' + __dirname + '/index.html');

    // 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;
    });

  }



#4

okay, that did not work as intended.
With the code above, on clicking the “X” button, the app spawns a new window and reloads. What happens in other OS X apps is it minimizes and on tapping it again, restores the window. I’ll dig around a bit more. I’m one day old as far as Electron goes :smiley:


#5

I’m not sure what “other OS X apps” you’re talking about, but it isn’t “minimizing” and “restoring”. It is “closing” and “creating”. Try the TextEdit app for an example. If you close the window by clicking the red button, it closes the document.

And no, I didn’t mean repeat the app.on('ready') code … at least not in the close window method. If the user wants to open a new window, then typically they should press Cmd+N or select File > New for a new window.


#6

What I was after is what say, iTunes does - you click on the close, the app is still alive, just that the UI gets removed. You tap it again and the UI reloads.

I achieved that by trapping on active I check if mainWindow is null and if so, I re-create it
seems to work fine.


#7

FWIW, you can keep a reference to the main window by

win.on('close', function(event){
    event.preventDefault();
})

This prevents the ‘closed’ event from triggering, which I believe is where the window gets destroyed.
I had a similar problem on windows, where the app needed to minimize to the system tray, and reloading the page was not an option because the app would have to make http requests and rebuild the state of the view. preventDefault() allowed me to call win.show() or win.restore() to show the window as it was when the user closed it, without Electron throwing the ‘Object has been destroyed’ error. However, I have not tested this on OS X.


#8

For cross platform behaviour you can also decide based on the platform.

In main.js we use:

// Quit when all windows are closed. Except on darwin.
app.on("window-all-closed", () => {
    // On macOS it is common for applications and their menu bar
    // to stay active until the user quits explicitly with Cmd + Q.
    // Exceptions include System Preferences, App Store,
    // Though there is no way to re-open the main window
    // through the menu, but you can click on the dock icon.
    // Noted in the README
    if (process.platform !== "darwin") {
        app.quit();
    }
});