7

Usually on mac, when I close the window it doesn't quit the app but when using node webkit it does quit the app.

Does anyone know a workaround so when I click the "x" it just closes the window but not the app?

Thanks in advance for your help. enter image description here

denislexic
  • 10,786
  • 23
  • 84
  • 128

4 Answers4

3

This works for me in MAC:

var gui = require('nw.gui');
var window = gui.Window.get();

gui.App.on('reopen', function(){
    window.show();
})

window.on('close', function(){
    window.hide();
});

On using above code, If user clicks on close button, it hides window instead of terminating application.
When user clicks on app icon from Dock, GUI reopen event is triggered which will show hidden window.
Note: reopen is a MAC specific feature currently.
More details provided in NodeWebkit App Features

Abhishek
  • 1,999
  • 5
  • 26
  • 52
  • An improvement, but in this way user can't never close the app neither from the menu – Fez Vrasta Jan 14 '15 at 18:43
  • @FezVrasta I think user can close/quit app from menu/dock by doing a right-click on icon and selecting `quit` – Abhishek Jan 15 '15 at 05:56
  • It is working for me in MAC including force quit of app from dock. If in case, app is not closing for you from dock by right-click, then you can add `gui.App.quit()` in close function as mentioned by @FezVrasta – Abhishek Jan 21 '15 at 06:38
3

Not a complete solution, all credits goes to Abhishek's answer.

I've improved it to allow user to close the app and not just quit the window.

  var hidden = false;
  gui.App.on('reopen', function(){
    hidden = false;
    win.show();
  })

  win.on('close', function(){
    if (hidden == true) {
      gui.App.quit();
    } else {
      win.hide();
      hidden = true;
    }
  });

With this trick the first time you click the red button, the window gets closed but still runs in the dock, then, right clicking the icon and clicking quit the app is completely closed.

Fez Vrasta
  • 14,110
  • 21
  • 98
  • 160
2

The window passes an argument to the callback function if "quit" was clicked, either from the menu bar, or the dock (also on command+Q). I'm using this code and it works as expected:

var gui = require('nw.gui'),
    app = gui.App,
    win = gui.Window.get();

win.on('close', function (action) {
    'quit' === action ?
        app.quit() :
        win.hide();
});

app.on('reopen', function(){
    win.show();
});
bm00
  • 36
  • 5
0

Yes, There is a way. I call it command+Q. Another way you can do it is command+control+power

Ro Yo Mi
  • 14,790
  • 5
  • 35
  • 43