question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BrowserWindow OnClose event

See original GitHub issue

🚨 The issue tracker is not for questions 🚨

How to hide browser window and put application in Tray mode on close button clicked?

I tried as it shown below;

browserWindow.OnClose += () => OnClose(browserWindow);

and OnClose action;


private static void OnClose(BrowserWindow browserWindow)
{
    browserWindow.Hide();
    var menu = new MenuItem[]
    {
        new MenuItem
        {
            Label = "Show",
            Click = () =>
            {
                Electron.Tray.Destroy();
                browserWindow.Show();
            }
        },
        new MenuItem
        {
            Label = "Quit",
            Click = () => Electron.App.Quit()
        }
    };

    Electron.Tray.Show("favicon.ico", menu);
    Electron.Tray.SetToolTip("Electron Demo in the tray.");
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ruohkicommented, Oct 31, 2020

To piggyback on this problem.

In js you can do something like this in your app.js

    mainWindow.on('close', (event) => {
      if(!shouldQuit){
        event.returnValue = true;
        event.preventDefault()
        mainWindow.hide();
      } else {
        // close app
      }
    });

I noticed the OnClose do not hand over any arguments to do the same in c#. Is this a missing feature?

0reactions
AykutToprakcommented, May 7, 2020

Thank you @GregorBiswanger .

It solved the problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to catch the event of clicking the app window's close ...
You can catch it by using the close event of the browser-window api. You can try the following to verify this... var app...
Read more >
BrowserWindow
Calling event.preventDefault() will cancel the close. Usually you would want to use the beforeunload handler to decide whether the window should be closed, ......
Read more >
electron.BrowserWindow.close JavaScript and Node.js ...
How to use. close. function. in. BrowserWindow. Best JavaScript code snippets using electron.BrowserWindow.close(Showing top 15 results out of 315).
Read more >
Two ways to react on the Electron 'close' event
The BrowserWindow emits events when an App Command is invoked. The ones we are interested in are 'close' and 'resize'.
Read more >
Adding a Shutdown Dialog to Electron - NetworkSynapse
Electron's BrowserWindow 'close' event is perfect, as it is triggered anytime a user closes the Main Window. It is triggered with a hotkey,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found