I am developing a windows desktop application using node.js and backbone.js. I want to perform an action when the user closes the app by clicking on the close button on the title bar or right clicking on the application from windows taskbar.
My app.js looks like this,
var app = module.exports = require('appjs');
app.serveFilesFrom(__dirname + '/content/assets/');
var menubar = app.createMenu([ {
label : '&File',
submenu : [ {
label : 'E&xit',
action : function() {
window.close();
}
},{
label : 'New',
action : function() {
window.test();
}
} ]
}, {
label : '&Window',
submenu : [ {
label : 'Fullscreen',
action : function(item) {
window.frame.fullscreen();
console.log(item.label + " called.");
}
}, {
label : 'Minimize',
action : function() {
console.log("df");
window.frame.minimize();
}
}, {
label : 'Maximize',
action : function() {
console.log("nnnnnnlaaaaaaaaaaaaaaa");
window.frame.maximize();
}
}, {
label : ''// separator
}, {
label : 'Restore',
action : function() {
window.frame.restore();
}
} ]
} ]);
menubar.on('select', function(item) {
console.log("menu item " + item.label + " clicked");
});
var trayMenu = app.createMenu([ {
label : 'Show',
action : function() {
window.frame.show();
},
}, {
label : 'Minimize',
action : function() {
window.frame.hide();
}
}, {
label : 'Exit',
action : function() {
window.close();
}
} ]);
var statusIcon = app.createStatusIcon({
icon : './data/content/icons/32.png',
tooltip : 'AppJS Hello World',
menu : trayMenu
});
var window = app.createWindow({
width : 1024,// 640
height : 768,
showChrome: true,
icons : __dirname + '/content/icons'
});
window.on('create', function() {
console.log("Window Created");
window.frame.show();
window.frame.center();
window.frame.maximize();
window.frame.setMenuBar(menubar);
});
window.on('ready', function() {
console.log("Window Ready");
window.require = require;
window.process = process;
window.module = module;
//window.frame.openDevTools();
window.fileAssoc = process.mainModule.filename;
//window.readMyFile();
function F12(e) {
return e.keyIdentifier === 'F12'
}
function Command_Option_J(e) {
return e.keyCode === 74 && e.metaKey && e.altKey
}
});*/
window.addEventListener('keydown', function(e) {
console.log("hi");
if (F12(e) || Command_Option_J(e)) {
window.frame.openDevTools();
}
});
});
Please find the attached screenshot. I am able to perform actions on custom added functionalities inside "File" & "Windows".
But i don't know how to capture the event when the default app close button in the title bar is clicked or closed by right clicking on the application from windows task bar. Please help.
Thanks in Advance
UPDATED (added a few more code lines to show proper way for event to fire)
You should do like this:
var gui = require("nw.gui");
var win_main = gui.Window.get();
win_main.on('close', function () {
this.hide(); // Pretend to be closed already
alert("Closing...");
// here you detect if data is saved, and if not, ask user if they want to save
this.close(true); // if this line executes the app closes, if not,
// app stays opened
});
I tried the above and it works perfect. It catches both "Close button" clicks and key shortcuts like "Ctrl+F4" in Windows.
For further reading (moved from comments):
http://tutorialzine.com/2015/01/your-first-node-webkit-app/
https://nodesource.com/blog/node-desktop-applications
https://gist.github.com/LeCoupa/80eca2716a2b13c37cce
https://github.com/nwjs/nw.js/
https://github.com/nwjs/nw.js/wiki/window
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With