Double click events not being called after deleting tray icon and creating it again #899

Open
opened 2026-01-29 16:51:34 +00:00 by claunia · 0 comments
Owner

Originally created by @Arthurgroll997 on GitHub (Jun 21, 2023).

Specifications

Electron.NET CLI version: 23.6.1
Electron.NET API version: 23.6.1

.NET Core version: 6.0
Node.JS version: 18

Build target: Windows

Error description

I was trying to create a tray icon so that when the user closes the window it would be possible to show the window again by double clicking the tray icon.
I wanted to show the tray icon only when the window is hidden, so I would have to keep destroying and creating the tray icon again.
I noticed that the first time I closed the window the tray icon would work without any problems, but when I tried a second time it wouldn't work anymore.

The code I was using:

private readonly WindowManager _winMgr;

// ...

private void OnTrayIconDoubleClick(TrayClickEventArgs args, Rectangle rect)
    {
        Electron.Tray.Destroy();
        _winMgr.BrowserWindows.First().Show();
        _winMgr.BrowserWindows.First().Focus();
    }

public void CloseWindow()
    {
        _winMgr.BrowserWindows.First().Hide();

        Electron.Tray.Show("");

        Electron.Tray.OnDoubleClick -= OnTrayIconDoubleClick;
        Electron.Tray.OnDoubleClick += OnTrayIconDoubleClick;
    }

Steps to Reproduce:

  1. Create tray icon
  2. Assign a double click event
  3. Delete the tray icon
  4. Create tray icon again
  5. Done. The double click event will not be called from now on.

Workaround for now

I actually found a workaroud for this (which I'm using right now), but it would be nice to have a permanent fix.

When analysing the code for the double click event, I noticed that it only registers the double click event when the _doubleClick event is null, and it does not make _doubleClick null again when the Destroy method is called.

The OnDoubleClick event code:

        /// <summary>
        /// macOS, Windows: Emitted when the tray icon is double clicked.
        /// </summary>
        public event Action<TrayClickEventArgs, Rectangle> OnDoubleClick
        {
            add
            {
                if (_doubleClick == null)
                {
                    BridgeConnector.Socket.On<dynamic>("tray-double-click-event" + GetHashCode(), (result) =>
                    {
                        var args = ((JArray)result).ToObject<object[]>();
                        var trayClickEventArgs = ((JObject)args[0]).ToObject<TrayClickEventArgs>();
                        var bounds = ((JObject)args[1]).ToObject<Rectangle>();
                        _doubleClick(trayClickEventArgs, bounds);
                    });

                    BridgeConnector.Socket.Emit("register-tray-double-click", GetHashCode());
                }
                _doubleClick += value;
            }
            remove
            {
                _doubleClick -= value;

                if (_doubleClick == null)
                    BridgeConnector.Socket.Off("tray-double-click-event" + GetHashCode());
            }
        }

The Destroy code:

public async Task Destroy()
        {
            await BridgeConnector.Socket.Emit("tray-destroy");
            _items.Clear();
        }

So I tried my luck using reflection and setting the _doubleClick property to null in the CloseWindow method, and as expected it worked as a charm.

public void CloseWindow()
    {
        _winMgr.BrowserWindows.First().Hide();

        FieldInfo field = typeof(Tray).GetField("_doubleClick",
            BindingFlags.NonPublic | BindingFlags.Instance)!;
        
        field!.SetValue(Electron.Tray, null);

        Electron.Tray.Show("");

        Electron.Tray.OnDoubleClick -= OnTrayIconDoubleClick;
        Electron.Tray.OnDoubleClick += OnTrayIconDoubleClick;
    }

Conclusion

So that is it.
I'm not entirely sure if this is happening to other events such as the click event, but it might aswell be happening to them, and it would be nice to check it out too

Originally created by @Arthurgroll997 on GitHub (Jun 21, 2023). # Specifications Electron.NET CLI version: 23.6.1 Electron.NET API version: 23.6.1 <!-- Which version of .NET Core and Node.js are you using (if applicable)? --> .NET Core version: 6.0 Node.JS version: 18 Build target: Windows # Error description I was trying to create a tray icon so that when the user closes the window it would be possible to show the window again by double clicking the tray icon. I wanted to show the tray icon only when the window is hidden, so I would have to keep destroying and creating the tray icon again. I noticed that the first time I closed the window the tray icon would work without any problems, but when I tried a second time it wouldn't work anymore. The code I was using: ```csharp private readonly WindowManager _winMgr; // ... private void OnTrayIconDoubleClick(TrayClickEventArgs args, Rectangle rect) { Electron.Tray.Destroy(); _winMgr.BrowserWindows.First().Show(); _winMgr.BrowserWindows.First().Focus(); } public void CloseWindow() { _winMgr.BrowserWindows.First().Hide(); Electron.Tray.Show(""); Electron.Tray.OnDoubleClick -= OnTrayIconDoubleClick; Electron.Tray.OnDoubleClick += OnTrayIconDoubleClick; } ``` # Steps to Reproduce: 1. Create tray icon 2. Assign a double click event 3. Delete the tray icon 4. Create tray icon again 5. Done. The double click event will not be called from now on. # Workaround for now I actually found a workaroud for this (which I'm using right now), but it would be nice to have a permanent fix. When analysing the code for the double click event, I noticed that it only registers the double click event when the _doubleClick event is null, and it does not make _doubleClick null again when the Destroy method is called. The OnDoubleClick event code: ```csharp /// <summary> /// macOS, Windows: Emitted when the tray icon is double clicked. /// </summary> public event Action<TrayClickEventArgs, Rectangle> OnDoubleClick { add { if (_doubleClick == null) { BridgeConnector.Socket.On<dynamic>("tray-double-click-event" + GetHashCode(), (result) => { var args = ((JArray)result).ToObject<object[]>(); var trayClickEventArgs = ((JObject)args[0]).ToObject<TrayClickEventArgs>(); var bounds = ((JObject)args[1]).ToObject<Rectangle>(); _doubleClick(trayClickEventArgs, bounds); }); BridgeConnector.Socket.Emit("register-tray-double-click", GetHashCode()); } _doubleClick += value; } remove { _doubleClick -= value; if (_doubleClick == null) BridgeConnector.Socket.Off("tray-double-click-event" + GetHashCode()); } } ``` The Destroy code: ```csharp public async Task Destroy() { await BridgeConnector.Socket.Emit("tray-destroy"); _items.Clear(); } ``` So I tried my luck using reflection and setting the _doubleClick property to null in the CloseWindow method, and as expected it worked as a charm. ```csharp public void CloseWindow() { _winMgr.BrowserWindows.First().Hide(); FieldInfo field = typeof(Tray).GetField("_doubleClick", BindingFlags.NonPublic | BindingFlags.Instance)!; field!.SetValue(Electron.Tray, null); Electron.Tray.Show(""); Electron.Tray.OnDoubleClick -= OnTrayIconDoubleClick; Electron.Tray.OnDoubleClick += OnTrayIconDoubleClick; } ``` # Conclusion So that is it. I'm not entirely sure if this is happening to other events such as the click event, but it might aswell be happening to them, and it would be nice to check it out too
claunia added the bug label 2026-01-29 16:51:34 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/Electron.NET#899