nw.js: [mac] Need a way to differentiate Cmd-Q from closing window(s) [$500 awarded]

Common pattern for apps is to hide on window close but quit on Menu->Quit (Cmd-Q).

Currently, Quit menu item calls closeAllWindows, thus triggering close event for each window. There seems to be no straight way to distinguish between this two events.

<bountysource-plugin>

The $500 bounty on this issue has been claimed at Bountysource. </bountysource-plugin>

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Comments: 55 (13 by maintainers)

Commits related to this issue

Most upvoted comments

Hi @tommoor, 33c12555d6d67f75c393c7f4c9136c5bde13611d fix the quit from dock (and also hitting cmd+q while cmd+tabbing) so that the app does not terminate immediately and windows on close handlers are properly called (with 'quit' as the event parameter) .

In the v0.10.6-pre-osx-x64 version @rogerwang posted earlier in the thread you can try something like:

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

win.on('close', function(event) {
  if (event == 'quit') {
    win.close(true);
  } else { 
    // event is `undefined`
    win.hide();
  }
});

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

Right now the close event differentiates (only) between

  • a quit “request” from menu, dock, shortcut (with 'quit' as the event parameter being passed to the close handler) and
  • a close “request” from the window (with no event parameter -i.e. undefined- being passed to the close handler). Maybe a 'window' parameter would provide a clearer distinction from the other case.

By using NSApplication.currentEvent inside applicationShouldTerminate it should be possible to find out the source of the termination request (dock, menu, …) and pass a proper parameter to nw::App:CloseAllWindows() which should be modified (along with ShouldCloseWindow in Shell and the NativeWindowDelegate) to accept a parameter (enum? int?) other than the bool quit. What is your opinion @rogerwang on this?

That said, I think most of the use cases are having a simple differentiation between the close window versus quit application and the further differentiation for dock, menu should not hinder this simple case, i.e. being able to tell if (event = 'quit') { ... } independently from the source of the quit event.

I can’t help but wonder if this would be better just having a parameter on the exit event that tells you which of the places its being triggered from:

  • Menu
  • Dock
  • Window
  • Shortcut

It’s then upto the developer to choose whether the app should close in those situations? As some apps will want to close with the window and others not…