电子:关闭w X vs右键单击停靠并退出

Electron: Close w X vs right click dock and quit

本文关键字:单击 停靠 退出 右键 vs 关闭 电子      更新时间:2023-09-26

在我的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();
    }
});