Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature added: update of menu options #18

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

MagTun
Copy link

@MagTun MagTun commented Apr 5, 2019

No description provided.

if icon:
self._icon = icon
self._load_icon()
if hover_text:
self._hover_text = hover_text
# "if menu_options" added to be allow the update of the menu options
if menu_options:
menu_options = menu_options or ()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this line, since we are already checking if menu_options is not empty in the line above.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I removed it!

@@ -122,15 +122,25 @@ def shutdown(self):
PostMessage(self._hwnd, WM_CLOSE, 0, 0)
self._message_loop_thread.join()

def update(self, icon=None, hover_text=None):
""" update icon image and/or hover text """
def update(self, icon=None, hover_text=None, menu_options=None): # "menu_options=None" added to be allow the update of the menu options
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the comment. It's making the line too long and it isn't necessary.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

self._menu_actions_by_id = set()
self._menu_options = self._add_ids_to_menu_options(list(menu_options))
self._menu_actions_by_id = dict(self._menu_actions_by_id)
self._menu = None # detroy the old menu created by right clicking the icon
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I'm afraid of here is that we're not freeing the previous menu (and submenus and icons, maybe?), potentially causing resource leaks.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible but I don't know how to prevent this.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would DestroyMenu do the trick?
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-destroymenu

You'll probably have to add an alias for it in win32_adapter.py but I managed to get this working by doing:

if menu_options:
    [the menu update code]
    ctypes.windll.user32.DestroyMenu(self._menu)
    self._menu = None

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants