Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 372
denislexic Avatar asked Aug 19 '14 18:08

denislexic


2 Answers

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

like image 195
Abhishek Avatar answered Sep 25 '22 15:09

Abhishek


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.

like image 34
Fez Vrasta Avatar answered Sep 22 '22 15:09

Fez Vrasta