問題描述
在我的 Electron 應用程序中,我想做一些在其他 OSX 應用程序中經常做的事情.那就是...我不想關閉右上角的紅色 X 的應用程序.但是,如果他們右鍵單擊 Dock 中的應用程序圖標,然后說退出,那么我想退出該應用程序.我該怎么做?
In my Electron app, I would like to do something that is done very often in other OSX apps. That is... I would like to NOT close the app of the red X is clicked in the top right. But, if they right click the app icon in the dock, and say Quit, then I would like to quit the app. How do I do this?
我已經嘗試使用 rendererProcess 中的 onbeforeunload
事件以及 browserWindow.on("close", fn)
事件來嘗試阻止這種情況.問題是他們都提交了 onbeforeunload
事件.而且我無法分辨紅色 X 被單擊和停靠圖標被右鍵單擊并被告知退出之間的區別.你能幫忙的話,我會很高興.有沒有其他人在 Electron for OSX 中這樣做過?
I have tried using the onbeforeunload
event from the rendererProcess, as well as the browserWindow.on("close", fn)
event to try and prevent this. The problem is that they both file the onbeforeunload
event. And I can't tell the different between the red X being clicked and the dock icon being right clicked and told to quit. Any help would be nice. Has anyone else done this in Electron for OSX?
推薦答案
試試這個
if (process.platform === 'darwin') {
var forceQuit = false;
app.on('before-quit', function() {
forceQuit = true;
});
mainWindow.on('close', function(event) {
if (!forceQuit) {
event.preventDefault();
/*
* your process here
*/
}
});
}
這篇關于電子:關閉 w X 與右鍵單擊停靠并退出的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!