fyne-io / systray

a cross platfrom Go library to place an icon and menu in the notification area
Apache License 2.0
223 stars 41 forks source link

Deleting Menu Items #33

Closed bdwyertech closed 1 year ago

bdwyertech commented 1 year ago

I’m not well-versed enough in C/CGO to determine if hiding and nulling a menu or submenu item will trigger it for garbage collection.

If not, this is a feature request to be able to delete a menu/submenu item properly. Use case is multiple dynamic submenus with a background goroutine populating/pruning items.

andydotxyz commented 1 year ago

C does not have garbage collection, so I am not sure what this issue is really about. Perhaps try what you think should work, then open an issue if you find it not going as expected, or if you discover a memory leak?

bdwyertech commented 1 year ago

Sure, I'll try and rig up a test harness for this scenario and monitor memory usage. Concern is simply removed menu items consuming resources.

andydotxyz commented 1 year ago

If that approach makes you nervous you could call ResetMenu and build a new menu instead?

bdwyertech commented 1 year ago

If that approach makes you nervous you could call ResetMenu and build a new menu instead?

In this case, these are submenu items. But yes, a reset menu function scoped to a specific submenu would work perfectly. Given the number of menu items and frequency I am updating this submenu (every two seconds) it would be overkill to reload the entire app.

This submenu is monitoring mutagen sessions and their health via icon and status text as the tooltip. Clicking the item will pause/unpause the sync.

bdwyertech commented 1 year ago

Did some testing on my Mac and yes, just nulling things out did cause a memory leak.

It appears that hiding an item on Windows is good enough, so long as it is a leaf (no children).

Windows: RemoveMenu vs DeleteMenu RemoveMenu does not destroy the menu or its handle, allowing the menu to be reused. DeleteMenu deletes an item from the specified menu. If the menu item opens a menu or submenu, this function destroys the handle to the menu or submenu and frees the memory used by the menu or submenu.