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

Allow menu options to be updatable #12

Open
wants to merge 5 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
28 changes: 19 additions & 9 deletions src/infi/systray/traybar.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,7 @@ def __init__(self,
self._hover_text = hover_text
self._on_quit = on_quit

menu_options = menu_options or ()
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._set_menu_options(menu_options)

window_class_name = window_class_name or ("SysTrayIconPy-%s" % (str(uuid.uuid4())))

Expand Down Expand Up @@ -122,14 +117,29 @@ 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):
""" update icon image and/or hover text and/or menu options """
icon_refresh_needed = False
if icon:
self._icon = icon
self._load_icon()
icon_refresh_needed = True
if hover_text:
self._hover_text = hover_text
self._refresh_icon()
icon_refresh_needed = True
if icon_refresh_needed:
self._refresh_icon()
if menu_options:
self._set_menu_options(menu_options)

def _set_menu_options(self, menu_options):
menu_options = menu_options or ()
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
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm concerned about leaking objects here. Shouldn't we call DestroyMenu to free "self._menu" if it is not None? We can also leak submenus in this case.


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