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

Add dictionary mapping command_id to its associated MenuCommands for each plugin #348

Merged
merged 7 commits into from
Jun 21, 2024

Conversation

DragaDoncila
Copy link
Contributor

Adding this dictionary to support quick mapping of widgets/commands to their menus for implementing contributable menus . Prior to this PR, you would have to search all menu contributions for the command of the contribution you were currently processing. This map allows you direct access to the menus this command needs to live in.

This is a precursor to potentially rearchitecting the manifest schema entirely to be "command-first".

Comment on lines 370 to 386
for command in manifest.contributions.commands or ():
# command IDs are keys in map
# each value is a dict menu_id: list of MenuCommands
# for the command and menu
associated = self._get_associated_menus(manifest, command.id)
self._command_menu_map[manifest.name][command.id] = associated

def _get_associated_menus(
self, manifest: PluginManifest, command_id: str
) -> Dict[str, List[MenuCommand]]:
menus = manifest.contributions.menus or dict()
associated_menus = defaultdict(list)
for menu_id, items in menus.items():
for item in items:
if getattr(item, "command", "") == command_id:
associated_menus[menu_id].append(item)
return associated_menus
Copy link
Member

@jni jni Jun 21, 2024

Choose a reason for hiding this comment

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

Ah this is backwards. You should be able to do this in a single pass of the menus:

  • iterate over the menus, but
  • map commands back to menus
  • since you use a defaultdict it's ok if some menu-less commands are missed, but you could add them by keeping a set of already-seen commands and then putting in the missed ones at the end.

So:

Suggested change
for command in manifest.contributions.commands or ():
# command IDs are keys in map
# each value is a dict menu_id: list of MenuCommands
# for the command and menu
associated = self._get_associated_menus(manifest, command.id)
self._command_menu_map[manifest.name][command.id] = associated
def _get_associated_menus(
self, manifest: PluginManifest, command_id: str
) -> Dict[str, List[MenuCommand]]:
menus = manifest.contributions.menus or dict()
associated_menus = defaultdict(list)
for menu_id, items in menus.items():
for item in items:
if getattr(item, "command", "") == command_id:
associated_menus[menu_id].append(item)
return associated_menus
menu_map = self._command_menu_map[manifest.name] # just for conciseness below
for menu_id, menu_items in manifest.contributions.menus.items() or ():
# command IDs are keys in map
# each value is a dict menu_id: list of MenuCommands
# for the command and menu
for item in menu_items:
if command_id := getattr(item, "command", None) is not None:
menu_map[command_id].append(item)

@jni jni merged commit eeeeacc into napari:main Jun 21, 2024
29 of 30 checks passed
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.

2 participants