【问题标题】:How to catch the event of clicking the app window's close button in Electron app如何在 Electron 应用程序中捕获单击应用程序窗口关闭按钮的事件
【发布时间】:2015-12-29 09:27:39
【问题描述】:

我希望在 Electron 应用中捕捉点击应用窗口关闭按钮的事件。

我正在尝试为 Mac OSX 开发 Electron 应用程序。 我想隐藏应用程序窗口,而不是像其他 Mac 应用程序一样在用户单击窗口的关闭按钮时终止应用程序。

但是,我无法检测到系统应该终止还是应该隐藏,因为在任何情况下,当单击关闭按钮、操作系统关闭或应用程序通过退出命令终止,Cmd+Q

有什么方法可以捕捉到在 Electron 应用中点击应用窗口关闭按钮的事件?

感谢您的帮助。


后记

为了检测点击关闭按钮的事件,我尝试了这段代码

var app = require('app');
var BrowserWindow = require('browser-window');
var Menu = require('menu');

var force_quit = false;

var menu = Menu.buildFromTemplate([
  {
    label: 'Sample',
    submenu: [
      {label: 'About App', selector: 'orderFrontStandardAboutPanel:'},
      {label: 'Quit', accelerator: 'CmdOrCtrl+Q', click: function() {force_quit=true; app.quit();}}
    ]
  }]);

app.on('window-all-closed', function(){
    if(process.platform != 'darwin')
        app.quit();
});

app.on('ready', function(){

    Menu.setApplicationMenu(menu);

    mainWindow = new BrowserWindow({width:800, height:600});

    mainWindow.on('close', function(e){
        if(!force_quit){
            e.preventDefault();
            mainWindow.hide();
        }
    });

    mainWindow.on('closed', function(){
        console.log("closed");
        mainWindow = null;
        app.quit();
    });

    app.on('activate-with-no-open-windows', function(){
        mainWindow.show();
    });
});

使用此代码,当单击应用程序窗口的关闭按钮时隐藏应用程序,并在键入Cmd+Q 时终止应用程序。但是,当我尝试关闭操作系统时,会阻止关闭事件。

【问题讨论】:

    标签: javascript electron


    【解决方案1】:

    您可以使用browser-window api 的close 事件来捕获它。您可以尝试以下方法来验证这一点...

    var app = require('app');
    
    var force_quit = false;
    
    app.on('ready', function () {        
        mainWindow = new BrowserWindow({ width: 800, height: 600 });
    
        mainWindow.on('close', function() { //   <---- Catch close event
    
            // The dialog box below will open, instead of your app closing.
            require('dialog').showMessageBox({
                message: "Close button has been pressed!",
                buttons: ["OK"]
            });
        });
    });
    

    更新:

    要分离功能,您可以执行以下操作...

    var app = require('app');
    var BrowserWindow = require('browser-window');
    var Menu = require('menu');
    
    var force_quit = false;
    var menu = Menu.buildFromTemplate([
    {
        label: 'Sample',
        submenu: [
            {label: 'About App', selector: 'orderFrontStandardAboutPanel:'},
            {
                label: 'Quit', 
                accelerator: 'CmdOrCtrl+Q', 
                click: function() { 
                    force_quit=true; app.quit();
                }
            }
        ]
    }]);
    
    app.on('window-all-closed', function(){
        if(process.platform != 'darwin')
            app.quit();
    });
    
    app.on('ready', function(){
    
        Menu.setApplicationMenu(menu);
    
        mainWindow = new BrowserWindow({width:800, height:600});
    
        // Continue to handle mainWindow "close" event here
        mainWindow.on('close', function(e){
            if(!force_quit){
                e.preventDefault();
                mainWindow.hide();
            }
        });
    
        // You can use 'before-quit' instead of (or with) the close event
        app.on('before-quit', function (e) {
            // Handle menu-item or keyboard shortcut quit here
            if(!force_quit){
                e.preventDefault();
                mainWindow.hide();
            }
        });
    
        // Remove mainWindow.on('closed'), as it is redundant
    
        app.on('activate-with-no-open-windows', function(){
            mainWindow.show();
        });
    });
    
    // This is another place to handle events after all windows are closed
    app.on('will-quit', function () {
        // This is a good place to add tests insuring the app is still
        // responsive and all windows are closed.
        console.log("will-quit");
        mainWindow = null;
    });
    

    上面的代码使用before-quitevent handler来处理应用程序api上的应用程序“关闭”事件。浏览器窗口“关闭”事件仍由 mainWindow.on('close') 在浏览器窗口 api 上处理。

    此外,will-quit 事件是在应用完全关闭之前测试问题的更好场所。

    【讨论】:

    • 感谢您的回复!!我尝试了您的代码,但是我发现单击关闭按钮或键入 Cmd+Q 时会捕获 close 事件。此外,当操作系统关闭时,此事件会被缓存...我只想检测when a close button is clicked...
    • 当您调用 app.quit() 时,应用程序会在终止前尝试关闭所有窗口。您在 mainWindow 的“关闭”事件上有第二个 app.quit()。这可能会导致问题。使用您的代码查看我的更新答案。
    • 再次感谢您的帮助!我试过你的代码,发现只有close'' event is called when a close button is clicked, and before-quit''事件和“close”事件在操作系统关闭时被调用。非常感谢!
    • 'activate-with-no-open-windows' 已替换为 'activate'
    【解决方案2】:
    app.on('ready', ()=> {
    let win = new BrowserWindow({width:800, height:600}) 
    win.loadURL('file://'+__dirname+'/index.html')
    win.on('closed', () => {
        console.log(' ---- Bye Bye Electron ---- ')
      });
    })
    

    这样你就可以捕捉到关闭事件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 2011-06-18
      • 2020-01-30
      • 2016-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多