denislexic
denislexic

Reputation: 11352

Close window but not app on click of "x" on Mac OS with node webkit

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

Upvotes: 7

Views: 975

Answers (4)

Bob the larry
Bob the larry

Reputation: 9

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

Upvotes: 0

bm00
bm00

Reputation: 36

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

Upvotes: 2

Fez Vrasta
Fez Vrasta

Reputation: 14825

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.

Upvotes: 4

Abhishek
Abhishek

Reputation: 2019

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

Upvotes: 3

Related Questions