gpt4 book ai didi

javascript - Electron - 使用 "x"按钮关闭窗口时捕获

转载 作者:行者123 更新时间:2023-12-04 01:40:07 26 4
gpt4 key购买 nike

设置

看着BrowserWindow Events Docs , close事件是...

Emitted when the window is going to be closed



closed事件是...

Emitted when the window is closed.



问题

当我手动关闭窗口(即单击菜单栏中的“x”)以及以编程方式关闭窗口(即 window.close() )时,都会触发这两个事件;

这个窗口需要不同的处理,这取决于我是以编程方式关闭窗口,还是手动关闭它(如果使用“x”,则完全退出应用程序,否则做一些事情)。

那么,我该如何...

我怎样才能区分这两者?

最佳答案

正如文档所述,您无法区分 window.close() 并点击 X

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



但是,您可以使用 window.destroy() ,这会产生略有不同的事件

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.



利用这种差异的一个简单示例如下所示:
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
})
})

关于javascript - Electron - 使用 "x"按钮关闭窗口时捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48408014/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com