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.
我正在使用node开发一个windows桌面应用程序。js和backbone.js。当用户通过单击标题栏上的close按钮或通过windows任务栏右键单击应用程序时,我想执行一个操作。
My app.js looks like this,
我的app。js是这样的,
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.
但我不知道如何捕获事件,当默认的应用程序关闭按钮在标题栏点击或关闭时,从windows任务栏右键单击应用程序。请帮助。
Thanks in Advance
谢谢提前
2 个解决方案
#1
5
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.
我试过上面的方法,效果很好。它同时捕获窗口中的“关闭按钮”点击和快捷键,如“Ctrl+F4”。
For further reading (moved from comments):
进一步阅读(请参阅评论):
http://tutorialzine.com/2015/01/your-first-node-webkit-app/
http://tutorialzine.com/2015/01/your-first-node-webkit-app/
https://nodesource.com/blog/node-desktop-applications
https://nodesource.com/blog/node-desktop-applications
https://gist.github.com/LeCoupa/80eca2716a2b13c37cce
https://gist.github.com/LeCoupa/80eca2716a2b13c37cce
https://github.com/nwjs/nw.js/
https://github.com/nwjs/nw.js/
https://github.com/nwjs/nw.js/wiki/window
https://github.com/nwjs/nw.js/wiki/window
#2
0
Finally I have done this by hiding the default title bar and adding a custom one.
最后,我隐藏了默认的标题栏,并添加了一个自定义的标题栏。
For that, I set "showChrome" attribute in appJs to false during the window creation, which is by default true.
为此,我在创建窗口时将appJs中的“showChrome”属性设置为false,默认为true。
The code change is
代码变更
var window = app.createWindow({
width : 1024,// 640
height : 768,
**showChrome: false**,
icons : __dirname + '/content/icons'
});
But when the 'showChrome' attribute is set to false task bar also will be hidden until we minimise or restore the app.
但当“showChrome”属性设置为false时,也会隐藏任务栏,直到我们最小化或恢复应用程序。
#1
5
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.
我试过上面的方法,效果很好。它同时捕获窗口中的“关闭按钮”点击和快捷键,如“Ctrl+F4”。
For further reading (moved from comments):
进一步阅读(请参阅评论):
http://tutorialzine.com/2015/01/your-first-node-webkit-app/
http://tutorialzine.com/2015/01/your-first-node-webkit-app/
https://nodesource.com/blog/node-desktop-applications
https://nodesource.com/blog/node-desktop-applications
https://gist.github.com/LeCoupa/80eca2716a2b13c37cce
https://gist.github.com/LeCoupa/80eca2716a2b13c37cce
https://github.com/nwjs/nw.js/
https://github.com/nwjs/nw.js/
https://github.com/nwjs/nw.js/wiki/window
https://github.com/nwjs/nw.js/wiki/window
#2
0
Finally I have done this by hiding the default title bar and adding a custom one.
最后,我隐藏了默认的标题栏,并添加了一个自定义的标题栏。
For that, I set "showChrome" attribute in appJs to false during the window creation, which is by default true.
为此,我在创建窗口时将appJs中的“showChrome”属性设置为false,默认为true。
The code change is
代码变更
var window = app.createWindow({
width : 1024,// 640
height : 768,
**showChrome: false**,
icons : __dirname + '/content/icons'
});
But when the 'showChrome' attribute is set to false task bar also will be hidden until we minimise or restore the app.
但当“showChrome”属性设置为false时,也会隐藏任务栏,直到我们最小化或恢复应用程序。