最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Electron - Catch when window is closed using the "x" button - Stack Overflow

programmeradmin5浏览0评论

The setup

Looking at the BrowserWindow Events Docs, the close event is...

Emitted when the window is going to be closed

and the closed event is...

Emitted when the window is closed.

The problem

Both events are triggered when I close the window manually (i.e. clicking the "x" in the menu bar), as well as when I programmatically close the window (i.e. window.close());

This window requires different handling, depending on whether I am closing the window programmatically, or manually closing it (quit the app entirely if using the "x", or do some stuffs otherwise).

So, how do I...

How can I differentiate between the two?

The setup

Looking at the BrowserWindow Events Docs, the close event is...

Emitted when the window is going to be closed

and the closed event is...

Emitted when the window is closed.

The problem

Both events are triggered when I close the window manually (i.e. clicking the "x" in the menu bar), as well as when I programmatically close the window (i.e. window.close());

This window requires different handling, depending on whether I am closing the window programmatically, or manually closing it (quit the app entirely if using the "x", or do some stuffs otherwise).

So, how do I...

How can I differentiate between the two?

Share Improve this question asked Jan 23, 2018 at 17:56 BirrelBirrel 4,8547 gold badges41 silver badges79 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

As the documentation states, you cannot make difference between window.close() and clicking on X

win.close() Try to close the window. This has the same effect as a user manually clicking the close button of the window.

However, you can use window.destroy(), which generates slightly different events

win.destroy() Force closing the window, the unload and beforeunload event won't be emitted for the web page, and close event will also not be emitted for this window, but it guarantees the closed event will be emitted.

A simple example to exploit this difference would be something like the following:

const { app, BrowserWindow } = require('electron')

app.once('ready', () => {
  let win = new BrowserWindow()
  let isRegularClose = false
  setTimeout(() => {
    if (win) win.destroy()
  }, 5000)
  win.on('close', (event) => {
    isRegularClose = true
  })
  win.on('closed', (event) => {
    console.log(isRegularClose
      ? 'win closed (X)'
      : 'win destroyed (code)')
    win = null
  })
})

okay so:

  1. win.destroy() is equal to the X system native application close button according to the docs and it will always make sure the closed event is fired and close event is NOT
  2. when clicking the X system native application close button you will have the close event fired first then the closed event es after.
  3. close event es before the beforeunload and unload DOM events

TIPS:

  1. as per dosc use the closed event to get rid of the BrowserWindow instance (your main window or any window created using new BrowserWindow()). this is etter than garbage collection. see this here

5. I used the closed event to shutdown a local database server before quiting the app. So you can use it to acplish similar step, like this:

mainWindow.on('closed', () => {
  shutdownDatabase();
  mainWindow = null; 
});

IPC is the solution for you answer.

const {app, BrowserWindow, Menu, ipcMain} = electron;

let mainApp;

app.on('ready', () => {
  mainApp = new BrowserWindow({ frame: false });
  mainApp.loadURL(url.format({
    pathname: path.join(__dirname, 'public/mainApp2.html'),
    protocol: 'file:',
    slashes: true
  }));
  mainApp.on('closed', () => {
    app.quit();
  });
});

ipcMain.on('mand:close', (event, arg) => {
  app.quit();
});

And on click

const {ipcRenderer} = require('electron');
let $ = require("jquery");



// Click Events
$("#action-close").click(() => {
    ipcRenderer.send("mand:close");
});
发布评论

评论列表(0)

  1. 暂无评论