电子:关闭w X vs右键单击停靠并退出
Electron: Close w X vs right click dock and quit
在我的Electron应用程序中,我想做一些在其他OSX应用程序中经常做的事情。 也就是说...我不想关闭右上角单击红色X的应用程序。但是,如果他们右键单击扩展坞中的应用程序图标,然后说退出,那么我想退出该应用程序。我该怎么做?
我尝试使用渲染器进程中的onbeforeunload
事件以及browserWindow.on("close", fn)
事件来尝试防止这种情况。问题是它们都提交了onbeforeunload
事件。而且我无法分辨单击红色 X 和右键单击并被告知退出的停靠图标之间的区别。任何帮助都会很好。还有其他人在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
*/
}
});
}
这是对我有用的唯一答案:
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);
通过 https://discuss.atom.io/t/how-to-catch-the-event-of-clicking-the-app-windows-close-button-in-electron-app/21425/8
经过多次查找,我找到了以下解决方案。当您右键单击停靠并选择 Quit
时,在渲染器进程中触发onbeforeunload
之前,它将首先在应用程序本身上触发 close
事件。因此,在渲染器进程中,您有一个onbeforeunload
侦听器。你告诉它总是返回假。从该事件返回 false 将阻止窗口永远卸载/关闭。然后在主进程中添加侦听器app.on('close',fn)
。该侦听器可以将事件发送到渲染器进程,告诉它允许关闭。也许您可以设置全局allowClose = true
或其他东西。然后在您的onbeforeunload
中,您添加逻辑以在allowClose
为真时不返回 true。
看看主进程中app
的窗口全部关闭事件。此事件通常用于在 Linux 和 Windows 上退出应用程序,但不用于在 OS X 上退出应用程序(有关示例,请参阅 Electron 的快速入门教程)。在OS X上,如果当前没有打开窗口,您可能还应该处理激活事件以打开新窗口。
查看电子快速入门指南
请注意,以下两个解决方案需要在 main.js
中实现,而不是在 html 页面上执行的 JS 上实现。
特定窗口关闭
如果要在特定浏览器窗口关闭时执行代码:
mainWindow.on('closed', function() {
// Your code to be executed before "really" stopping the app
});
所有窗口关闭
如果你想在所有窗口都关闭时执行代码(app
API):
app.on('window-all-closed', function() {
// do stuff here
});
从main.js
文件中处理此问题,方法是检查它是否是达尔文平台,window-all-closed
事件并在activate
事件上重新创建窗口。
// 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();
}
});
更多信息/示例:https://github.com/atom/electron-quick-start/blob/master/main.js
这就是我解决它的方式,这非常有效。
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();
}
});
- 使用clickToggle并在单击另一个元素时关闭元素
- 单击时将焦点更改为元素
- 单击F5时如何停止页面加载
- 通过单击表单中的按钮,在代码生成中使用javascript生成字母数字代码
- 在单击任何位置时隐藏元素,而不检查每次DOM单击
- 单击jquery清除输入值
- 单击按钮以等待单击按钮
- 在单击href链接的同时下载文件
- 单击顶部导航时如何进行向下滚动效果(向下滑动).
- 使图像在单击时展开到不大于浏览器
- 单击按钮后如何逐个调用分区,上一个分区将隐藏
- 如何在单击复选框后调用控制器方法
- 单击页面上的链接后高度发生变化
- 使用jquery在单击时在单元格中输入值
- 将纯文本URL转换为可单击链接
- ASP.NET通过单击JavaScript按钮触发c#事件
- 单击更改图标并通过javascript添加一个css类
- 阻止在select2单击时调用ajax
- 复制图像路径以单击它
- 电子:关闭w X vs右键单击停靠并退出