Nw.js: File dialogs cancel event

Created on 24 Dec 2012  Â·  4Comments  Â·  Source: nwjs/nw.js

Is there a way to detect "Cancel" button event in file dialog?

var chooserEntered = function() {
//do some staff. Ok
};
var chooserCanceled = function() {
//do some staff. How to call???
}

var chooser = $('');
chooser.change(function(e) {
console.log(chooser.val());
chooserEntered(chooser.val());
});
chooser.click();

Most helpful comment

This is a hack but works:
before opening the dialog, you have to change the input's value like
$("#file-dialog")[0].files.append(new File("", ""))
When the user cancels the dialog, $("#file-dialog").val() is empty

All 4 comments

One more issue:

  • Focus is lost from currently active element (document.activeElement is set to body after fileDialog closed)

This is a hack but works:
before opening the dialog, you have to change the input's value like
$("#file-dialog")[0].files.append(new File("", ""))
When the user cancels the dialog, $("#file-dialog").val() is empty

The hack proposed by @kochelmonster causes nwjs 0.12.1 to crash under Mac OS X 10.9.2.
I've managed to find another one – handle the nwjs Window focus event. Please see here: https://github.com/sompylasar/node-webkit-fdialogs/commit/fb7a606f5c13eb6e03e1342a3e8baa3a1767ff43#diff-025b14d5d2e91d029d15c4e4a0dbbcb8R100

Another approach is to add a cancel event listener:

$('#fileDialog')).addEventListener('cancel', (evt) => {
  console.log('DISMISS DIALOG');
  const path = (evt.target as HTMLInputElement).value;
  console.log(`Path: ${path}`);
}, false);

This works for me, and variable path is empty.

Was this page helpful?
0 / 5 - 0 ratings