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
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/infi/systray/traybar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!

""" update icon image and/or hover text and/or menu options"""
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!

menu_options = menu_options + (('Quit', None, SysTrayIcon.QUIT),)
self._next_action_id = SysTrayIcon.FIRST_ID
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

self._refresh_icon()


def _add_ids_to_menu_options(self, menu_options):
result = []
for menu_option in menu_options:
Expand Down