Skip to content

ensure that menuhook.register creates menus #15

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
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
29 changes: 20 additions & 9 deletions src/packages/menuhook/menuhook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,31 +77,44 @@ def deep_menu(menu):
like deep_menu(["&Scripting", "Python Developer", "HowTos"]) would make sure
there is a "Python Developer -> HowTos" sub menu item under scripting
"""
found = rt.menuman.findMenu(menu[-1])
submenu = rt.menuman.findMenu(menu[-1])
name = menu[-1]
if found is not None:
return found
if len(menu) == 1:
inmenu = rt.menuman.getMainMenuBar()
else:
inmenu = deep_menu(menu[0:-1])
submenu = rt.menuman.createMenu(name)
if submenu is None:
submenu = rt.menuman.createMenu(name)
else:
# Although the menu was found, it may be by itself and need to be added to
# its parent's (inmenu's) items.
is_in_inmenu = False
for item_index in range(1, inmenu.numItems() + 1):
if inmenu.getItem(item_index).getTitle() == submenu.getTitle():
is_in_inmenu = True
if is_in_inmenu:
return submenu
print(f"adding menu {name} to {inmenu.getTitle()}")
submenuitem = rt.menuman.createSubMenuItem(name, submenu)
index = inmenu.numItems() - 1
inmenu.addItem(submenuitem, index)
return submenu

def add_menu_item(menu, action, category):
def add_menu_item(menu, action, category, text):
"""
Add a menu item for an action.
The menu item will be re added even if it is already there.
If its text matches an existing menu item in menu then it won't be re-added.
"""
if not isinstance(menu, list):
menu = [menu]
targetmenu = deep_menu(menu)
if targetmenu:
newaction = rt.menuman.createActionItem(action, category)
if newaction:
for item_index in range(1, targetmenu.numItems() + 1):
if targetmenu.getItem(item_index).getTitle() == text:
return
print(f"adding menu item {text} to {targetmenu.getTitle()}")
targetmenu.addItem(newaction, -1)
rt.menuman.updateMenuBar()

Expand All @@ -117,8 +130,6 @@ def register(action, category, fcn, menu=None, text=None, tooltip=None):
- assign the macro function if the macro is already there
- create a menu item for the macro if it is not already there
"""
defined = macro_defined(action, category)
add_macro(action, category, text or action, tooltip or action, fcn)
if not defined and not menu is None:
add_menu_item(menu, action, category)
add_menu_item(menu, action, category, text)
#pylint: enable=too-many-arguments