【问题标题】:Electron: Close w X vs right click dock and quit电子:关闭 w X 与右键单击停靠并退出
【发布时间】:2016-05-02 16:36:18
【问题描述】:

在我的 Electron 应用程序中,我想做一些在其他 OSX 应用程序中经常做的事情。那就是...我不想关闭右上角的红色 X 的应用程序。但是,如果他们右键单击 Dock 中的应用程序图标并说退出,那么我想退出该应用程序。我该怎么做?

我尝试使用来自 rendererProcess 的 onbeforeunload 事件以及 browserWindow.on("close", fn) 事件来尝试阻止这种情况。问题是他们都提交了onbeforeunload 事件。而且我无法分辨红色 X 被单击和停靠图标被右键单击并被告知退出之间的区别。你能帮忙的话,我会很高兴。有没有其他人在 Electron for OSX 中这样做过?

【问题讨论】:

    标签: javascript electron


    【解决方案1】:

    试试这个

    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
           */
        }
      });
    }
    

    【讨论】:

    • 这个答案正确而简洁。感谢那。对正在发生的事情进行一些解释以帮助澄清:应用程序的 before-quit 事件在 浏览器窗口关闭之前触发,因此如果您退出浏览器窗口以外的其他操作(停靠上下文菜单、应用程序菜单等)before-quit 将在主窗口的close 事件之前触发。如果您通过单击主窗口上的 x 来关闭,则窗口的 close 事件将首先触发。 electron.atom.io/docs/api/app/#event-before-quit
    【解决方案2】:

    这是唯一对我有用的答案:

    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

    【讨论】:

      【解决方案3】:

      经过一番寻找,我找到了以下解决方案。当您右键单击停靠栏并选择Quit 时,在渲染器进程中触发onbeforeunload 之前,它将首先在应用程序本身上触发close 事件。因此,在 rendererProcess 中有一个 onbeforeunload 监听器。你告诉它总是返回 false 。从该事件返回 false 将阻止窗口永远卸载/关闭。然后在你的 mainProcess 中添加app.on('close',fn) 监听器。该侦听器可以向 rendererProcess 发送一个事件,告诉它允许关闭。也许您可以设置一个全局allowClose = true 或其他东西。然后在您的onbeforeunload 中,添加逻辑以在allowClose 为真时不返回真。

      【讨论】:

        【解决方案4】:

        看一下主进程中appwindow-all-closed事件。此事件通常用于在 Linux 和 Windows 上退出应用程序,但在 OS X 上不可用(例如,请参阅 Electron 的 Quick Start Tutorial)。在 OS X 上,如果当前没有打开窗口,您可能还应该处理 activate 事件以打开新窗口。

        【讨论】:

        • 你知道吗。阅读您的回复,我意识到我的应用程序可能与许多 Electron 应用程序不同。我的应用程序不是围绕作为电子主进程的节点应用程序,而是围绕在我包装的远程 url 中加载的应用程序。因此......如果窗户全部关闭,那么我的应用程序的主要部分就死了。而大多数电子应用程序仍在运行。所以...考虑到这一点,这个答案非常好。
        【解决方案5】:

        看看电子quick start guide

        请注意以下两个解决方案需要在main.js中实现,而不是在您的html页面上执行的JS上。

        特定窗口关闭

        如果您想在特定的BrowserWindow 关闭时执行代码:

        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
        });
        

        【讨论】:

          【解决方案6】:

          您需要在window-all-closed 事件上检查您的main.js 文件是否为darwin 平台,然后在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

          【讨论】:

            【解决方案7】:

            这就是我解决它的方法,而且效果很好。

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

            【讨论】:

              猜你喜欢
              • 2016-12-12
              • 2011-02-18
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多