yuk*_*kib 17 javascript electron
我希望能够在Electron应用程序中点击应用程序窗口的关闭按钮.
我正在尝试为Mac OSX开发Electron应用程序.我想隐藏应用程序窗口,而不是在用户像其他Mac应用程序一样单击窗口的关闭按钮时终止应用程序.
但是,我无法检测到系统应该被终止还是应该被隐藏,因为在任何情况下,当单击关闭按钮时调用close事件browser-window,操作系统将关闭或应用程序以quit命令终止Cmd+Q.
有没有办法在Electron应用程序中点击应用程序窗口的关闭按钮?
谢谢您的帮助.
要检测单击关闭按钮的事件,我尝试了此代码
var app = require('app');
var BrowserWindow = require('browser-window');
var Menu = require('menu');
var force_quit = false;
var menu = Menu.buildFromTemplate([
{
label: 'Sample',
submenu: [
{label: 'About App', selector: 'orderFrontStandardAboutPanel:'},
{label: 'Quit', accelerator: 'CmdOrCtrl+Q', click: function() {force_quit=true; app.quit();}}
]
}]);
app.on('window-all-closed', function(){
if(process.platform != 'darwin')
app.quit();
});
app.on('ready', function(){
Menu.setApplicationMenu(menu);
mainWindow = new BrowserWindow({width:800, height:600});
mainWindow.on('close', function(e){
if(!force_quit){
e.preventDefault();
mainWindow.hide();
}
});
mainWindow.on('closed', function(){
console.log("closed");
mainWindow = null;
app.quit();
});
app.on('activate-with-no-open-windows', function(){
mainWindow.show();
});
});
Run Code Online (Sandbox Code Playgroud)
使用此代码,当单击应用程序窗口的关闭按钮时,将隐藏应用程序,并在Cmd+Q键入时终止应用程序.但是,当我尝试关闭操作系统时,会阻止关闭事件.
Jos*_*osh 22
您可以使用浏览器窗口 api 的close事件来捕获它.您可以尝试以下方法来验证这一点......
var app = require('app');
var force_quit = false;
app.on('ready', function () {
mainWindow = new BrowserWindow({ width: 800, height: 600 });
mainWindow.on('close', function() { // <---- Catch close event
// The dialog box below will open, instead of your app closing.
require('dialog').showMessageBox({
message: "Close button has been pressed!",
buttons: ["OK"]
});
});
});
Run Code Online (Sandbox Code Playgroud)
要分离功能,您可以执行以下操作...
var app = require('app');
var BrowserWindow = require('browser-window');
var Menu = require('menu');
var force_quit = false;
var menu = Menu.buildFromTemplate([
{
label: 'Sample',
submenu: [
{label: 'About App', selector: 'orderFrontStandardAboutPanel:'},
{
label: 'Quit',
accelerator: 'CmdOrCtrl+Q',
click: function() {
force_quit=true; app.quit();
}
}
]
}]);
app.on('window-all-closed', function(){
if(process.platform != 'darwin')
app.quit();
});
app.on('ready', function(){
Menu.setApplicationMenu(menu);
mainWindow = new BrowserWindow({width:800, height:600});
// Continue to handle mainWindow "close" event here
mainWindow.on('close', function(e){
if(!force_quit){
e.preventDefault();
mainWindow.hide();
}
});
// You can use 'before-quit' instead of (or with) the close event
app.on('before-quit', function (e) {
// Handle menu-item or keyboard shortcut quit here
if(!force_quit){
e.preventDefault();
mainWindow.hide();
}
});
// Remove mainWindow.on('closed'), as it is redundant
app.on('activate-with-no-open-windows', function(){
mainWindow.show();
});
});
// This is another place to handle events after all windows are closed
app.on('will-quit', function () {
// This is a good place to add tests insuring the app is still
// responsive and all windows are closed.
console.log("will-quit");
mainWindow = null;
});
Run Code Online (Sandbox Code Playgroud)
上面的代码使用before-quit 事件处理程序来处理app api上的app"close"事件.浏览器窗口"关闭"事件仍然在浏览器窗口api上处理mainWindow.on('close').
此外,在will-quit应用完全关闭之前,事件是测试问题的更好地方.
| 归档时间: |
|
| 查看次数: |
21523 次 |
| 最近记录: |