Я хочу поймать событие нажатия кнопки закрытия окна приложения в приложении Electron.
Я пытаюсь разработать приложение Electron для Mac OSX. Я хочу скрыть окно приложения, а не прерывать приложение, когда пользователь нажимает кнопку закрытия окна, как и другие приложения Mac.
Однако я не могу обнаружить, что система должна быть завершена или она должна быть скрыта, потому что в любом случае событие close
browser-window
вызывается при нажатии кнопки закрытия, OS выключается или приложение завершается командой quit, Cmd+Q
.
Есть ли способ поймать событие нажатия кнопки закрытия окна приложения в приложении Electron?
Благодарим вас за помощь.
Postscript
Чтобы обнаружить событие нажатия кнопки закрытия, я пробовал этот код
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();
});
});
С помощью этого кода приложение скрывается при нажатии кнопки закрытия окна приложения, и приложение заканчивается при вводе Cmd+Q
. Однако, когда я пытаюсь отключить ОС, событие завершения работы предотвращается.