Electron.NET icon indicating copy to clipboard operation
Electron.NET copied to clipboard

Add EventArgs to BrowserWindow.OnClose event to prevent closing the window

Open niklasstich opened this issue 1 year ago • 3 comments

Currently, the OnClose event on BrowserWindow does not provide any event args to callees of the event. This makes it impossible to prevent closing the window from C# code directly without involving Javascript that catches the 'onbeforeunload' event.

This fact was mentioned in several previous issues: https://github.com/ElectronNET/Electron.NET/issues/391#issuecomment-719928364 https://github.com/ElectronNET/Electron.NET/issues/567

I would therefore request that EventArgs be added to this event that allow preventing the window from being closed, analogous to the Electron BrowserWindow close event: https://www.electronjs.org/docs/latest/api/browser-window#event-close

niklasstich avatar Apr 21 '23 15:04 niklasstich

Hello! OnClose event still doesnot has event args to prevent closing. I know how to prevent App closing, but browserWindow still doesnot has this feature. At https://github.com/ElectronNET/Electron.NET/issues/567#issuecomment-1487165152 u said "With native Electron 23 and .NET 6 support. Your problem should be fixed here. " but seems it doesnot. Can u explain, pls?

ssrdop avatar Jun 05 '23 19:06 ssrdop

Good day! I've also been facing this problem. Any information on this would be very appreciated.

terryjbutler avatar Jun 19 '23 15:06 terryjbutler

Good day! I've also been facing this problem. Any information on this would be very appreciated.

Hey ive found some solution - u can use window.onbeforeunload

so when ur app starts u can add ur callback (even async) to window.onbeforeunload

I have react app, so i wrote something like this:

useEffect(() => {
        window.onbeforeunload = async () => {
            await beforeApplicationClosed()
            if (!canCloseApp)
                return
        }
}, [])

So u can make it with any javasript framework, react just for example

It prevents closing app and it always calls ur callback function u wrote inside window.onbeforeunload.

But there are some problem - even if u want to close app now - u cant.

But for this problem i also have a solution - if u know that u want to close ur app then do next: window.onbeforeunload = null

and call after i: Electron.App.Quit(); // inside ur back, using websockets or via http api, doesnot matter.

How it works - when ur app starts u assing to window.onbeforeunload some callback thats calling everytime ur application or window is closing and its prevents closing at all (event if u press alt+f4, or from window toolbar). So often u assign this callback for window.onbeforeunload when u want to save some data before or ask user confirmation or something else. If u did this job and u ready to close app - just doint "window.onbeforeunload = null" resets any callback on closing and u need to call "Electron.App.Quit();" and after it there are no callback for window.onbeforeunload and ur app closing.

Sorry i am not native english speaker, but i hope u will understand.

ssrdop avatar Jun 20 '23 19:06 ssrdop