无法防止电子窗关闭

Cas*_*sen 5 javascript electron

我有一个渲染器过程,我们称它为RP。单击按钮后,将打开浏览器窗口(BW)。

现在,当单击BW上的close时,我想在RP中捕获此close事件并阻止它。

我遇到的问题是,close在RP中调用该事件之前,该窗口已关闭。

码:

bw.on("close", (e: Electron.Event) => hideWindow(e));
let hideWindow = (e: Electron.Event) => {
    e.preventDefault();
    bw.hide();
    return false;
  }
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

我知道,在bw中,我可以使用beforeunload,这有效。但是我希望RP控制窗口是否关闭。

chi*_*zui 7

以下是防止关闭的方法:

经过多次试验,我想出了一个解决方案:

// Prevent Closing when work is running
window.onbeforeunload = (e) => {
  e.returnValue = false;  // this will *prevent* the closing no matter what value is passed

  if(confirm('Do you really want to close the application?')) { 
    win.destroy();  // this will bypass onbeforeunload and close the app
  }  
};
Run Code Online (Sandbox Code Playgroud)

在文档中找到:事件关闭销毁


Cas*_*sen 3

这是不可能的,因为打开浏览器窗口的进程是渲染器进程,它是通过 electro.remote 调用的,因此是异步处理的。因此,窗口会在关闭事件处理之前关闭。如果打开浏览器窗口的进程是主进程,那就没问题了。

这显示了这种情况:https://github.com/CThuleHansen/windowHide 这是对该问题的更长讨论:https://discuss.atom.io/t/close-event-for-window-being-fired-窗口已关闭后/37863/4