电子:关闭 w X vs 右键单击停靠并退出
Electron: Close w X vs right click dock and quit
在我的 Electron 应用程序中,我想做一些在其他 OSX 应用程序中经常做的事情。那就是...我不想关闭在右上角单击红色 X 的应用程序。但是,如果他们右键单击停靠栏中的应用程序图标,然后说退出,那么我想退出该应用程序。我该怎么做?
我已经尝试使用来自 rendererProcess 的 onbeforeunload
事件以及 browserWindow.on("close", fn)
事件来尝试防止这种情况发生。问题是他们都提交了 onbeforeunload
事件。而且我无法区分单击红色 X 和右键单击并告知退出的停靠图标之间的区别。你能帮忙的话,我会很高兴。 OSX 有没有其他人在 Electron 中这样做过?
经过多方查找,我找到了以下解决方案。当您右键单击 dock 和 select Quit
时,在触发 rendererProcess 中的 onbeforeunload
之前,它将首先触发应用程序本身的 close
事件。所以,在 rendererProcess 中你有一个 onbeforeunload
监听器。你总是告诉 return false。从该事件返回 false 将阻止 window 从 unloading/closing 永远。然后在您的 mainProcess 中添加 app.on('close',fn)
侦听器。该侦听器可以向 rendererProcess 发送一个事件,告诉它允许关闭。或许你可以设置一个全局 allowClose = true
之类的。然后在您的 onbeforeunload
中添加逻辑,如果 allowClose
为真,则不 return 为真。
如果当前没有 window 打开,请查看 window-all-closed event of app
in the main process. This event is typically used to quit the app on Linux and Windows but not on OS X (for an example, see Electron's Quick Start Tutorial). On OS X you should then probably also handle the activate 事件以打开一个新的 window。
请注意,以下两个解决方案需要在 main.js
中实施,而不是在 html 页面上执行的 JS 上实施。
具体window关闭
如果要在特定 BrowserWindow 关闭时执行代码:
mainWindow.on('closed', function() {
// Your code to be executed before "really" stopping the app
});
全部window关闭
如果要在所有 windows 关闭时执行代码 (app
API):
app.on('window-all-closed', function() {
// do stuff here
});
您需要在 main.js
文件中处理此问题,方法是在 window-all-closed
事件上检查它是否是达尔文平台,并在 activate
上重新创建 window事件。
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow();
}
});
更多info/Example:https://github.com/atom/electron-quick-start/blob/master/main.js
试试这个
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
*/
}
});
}
这是唯一对我有用的答案:
const electron = require('electron');
const app = electron.app;
let willQuitApp = false;
let window;
app.on('ready', () => {
window = new electron.BrowserWindow();
window.on('close', (e) => {
if (willQuitApp) {
/* the user tried to quit the app */
window = null;
} else {
/* the user only tried to close the window */
e.preventDefault();
window.hide();
}
});
window.loadURL('foobar'); /* load your page */
});
/* 'activate' is emitted when the user clicks the Dock icon (OS X) */
app.on('activate', () => window.show());
/* 'before-quit' is emitted when Electron receives
* the signal to exit and wants to start closing windows */
app.on('before-quit', () => willQuitApp = true);
我就是这样解决的,效果很好。
import { app } from "electron";
let window: any;
let forceQuit = false;
app.on("ready", () => {
window = //YOUR BROWSER WINDOW
window.on("close", e => {
if (process.platform === "darwin" && forceQuit) {
window = null;
} else {
e.preventDefault();
app.hide();
}
});
app.on("activate", function() {
app.show();
});
app.on("before-quit", function(event) {
if (!forceQuit) {
event.preventDefault();
forceQuit = true;
app.quit();
}
});
在我的 Electron 应用程序中,我想做一些在其他 OSX 应用程序中经常做的事情。那就是...我不想关闭在右上角单击红色 X 的应用程序。但是,如果他们右键单击停靠栏中的应用程序图标,然后说退出,那么我想退出该应用程序。我该怎么做?
我已经尝试使用来自 rendererProcess 的 onbeforeunload
事件以及 browserWindow.on("close", fn)
事件来尝试防止这种情况发生。问题是他们都提交了 onbeforeunload
事件。而且我无法区分单击红色 X 和右键单击并告知退出的停靠图标之间的区别。你能帮忙的话,我会很高兴。 OSX 有没有其他人在 Electron 中这样做过?
经过多方查找,我找到了以下解决方案。当您右键单击 dock 和 select Quit
时,在触发 rendererProcess 中的 onbeforeunload
之前,它将首先触发应用程序本身的 close
事件。所以,在 rendererProcess 中你有一个 onbeforeunload
监听器。你总是告诉 return false。从该事件返回 false 将阻止 window 从 unloading/closing 永远。然后在您的 mainProcess 中添加 app.on('close',fn)
侦听器。该侦听器可以向 rendererProcess 发送一个事件,告诉它允许关闭。或许你可以设置一个全局 allowClose = true
之类的。然后在您的 onbeforeunload
中添加逻辑,如果 allowClose
为真,则不 return 为真。
如果当前没有 window 打开,请查看 window-all-closed event of app
in the main process. This event is typically used to quit the app on Linux and Windows but not on OS X (for an example, see Electron's Quick Start Tutorial). On OS X you should then probably also handle the activate 事件以打开一个新的 window。
请注意,以下两个解决方案需要在 main.js
中实施,而不是在 html 页面上执行的 JS 上实施。
具体window关闭
如果要在特定 BrowserWindow 关闭时执行代码:
mainWindow.on('closed', function() {
// Your code to be executed before "really" stopping the app
});
全部window关闭
如果要在所有 windows 关闭时执行代码 (app
API):
app.on('window-all-closed', function() {
// do stuff here
});
您需要在 main.js
文件中处理此问题,方法是在 window-all-closed
事件上检查它是否是达尔文平台,并在 activate
上重新创建 window事件。
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow();
}
});
更多info/Example:https://github.com/atom/electron-quick-start/blob/master/main.js
试试这个
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
*/
}
});
}
这是唯一对我有用的答案:
const electron = require('electron');
const app = electron.app;
let willQuitApp = false;
let window;
app.on('ready', () => {
window = new electron.BrowserWindow();
window.on('close', (e) => {
if (willQuitApp) {
/* the user tried to quit the app */
window = null;
} else {
/* the user only tried to close the window */
e.preventDefault();
window.hide();
}
});
window.loadURL('foobar'); /* load your page */
});
/* 'activate' is emitted when the user clicks the Dock icon (OS X) */
app.on('activate', () => window.show());
/* 'before-quit' is emitted when Electron receives
* the signal to exit and wants to start closing windows */
app.on('before-quit', () => willQuitApp = true);
我就是这样解决的,效果很好。
import { app } from "electron";
let window: any;
let forceQuit = false;
app.on("ready", () => {
window = //YOUR BROWSER WINDOW
window.on("close", e => {
if (process.platform === "darwin" && forceQuit) {
window = null;
} else {
e.preventDefault();
app.hide();
}
});
app.on("activate", function() {
app.show();
});
app.on("before-quit", function(event) {
if (!forceQuit) {
event.preventDefault();
forceQuit = true;
app.quit();
}
});