Skip to content

Commit

Permalink
Add remove_item_from_application_menu method in main menu
Browse files Browse the repository at this point in the history
  • Loading branch information
andfoy committed Aug 31, 2021
1 parent 4e730e3 commit b866f3f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
4 changes: 4 additions & 0 deletions spyder/api/widgets/menus.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ def add_action(self: T, action: Union[SpyderAction, T],
self._dirty = True
self._actions_map[item_id] = action

def remove_action(self, item_id: str):
action = self._actions_map.pop(item_id)
self._actions.remove(action)

def get_title(self):
"""
Return the title for menu.
Expand Down
31 changes: 30 additions & 1 deletion spyder/plugins/application/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,36 @@ def _populate_help_menu_about_section(self):
section=HelpMenuSections.About)

def _depopulate_help_menu(self):
pass
self._depopulate_help_menu_documentation_section()
self._depopulate_help_menu_support_section()
self._depopulate_help_menu_about_section()

def _depopulate_help_menu_documentation_section(self):
mainmenu = self.get_plugin(Plugins.MainMenu)
for documentation_action in [
ApplicationActions.SpyderDocumentationAction,
ApplicationActions.SpyderDocumentationVideoAction]:
mainmenu.remove_item_from_application_menu(
documentation_action,
menu_id=ApplicationMenus.Help)

def _depopulate_help_menu_support_section(self):
"""Remove Spyder base support actions from the Help main menu."""
mainmenu = self.get_plugin(Plugins.MainMenu)
for support_action in [
ApplicationActions.SpyderTroubleshootingAction,
ApplicationActions.SpyderDependenciesAction,
ApplicationActions.SpyderCheckUpdatesAction,
ApplicationActions.SpyderSupportAction]:
mainmenu.remove_item_from_application_menu(
support_action,
menu_id=ApplicationMenus.Help)

def _depopulate_help_menu_about_section(self):
mainmenu = self.get_plugin(Plugins.MainMenu)
mainmenu.remove_item_from_application_menu(
ApplicationActions.SpyderAbout,
menu_id=ApplicationMenus.Help)

# ---- Public API
# ------------------------------------------------------------------------
Expand Down
46 changes: 46 additions & 0 deletions spyder/plugins/mainmenu/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,52 @@ def add_item_to_application_menu(self, item: ItemType,
menu.add_action(item, section=section, before=before,
before_section=before_section, omit_id=omit_id)

def remove_item_from_application_menu(self, item_id: str,
menu_id: Optional[str] = None):
"""
Remove action or widget from given application menu by id.
Parameters
----------
item_id: str
The item identifier to remove from the given menu.
menu_id: str or None
The application menu unique string identifier.
"""
if menu_id not in self._APPLICATION_MENUS:
raise SpyderAPIError('{} is not a valid menu_id'.format(menu_id))

# TODO: For now just add the item to the bottom for non-migrated menus.
# Temporal solution while migration is complete
app_menu_actions = {
ApplicationMenus.Edit: self._main.edit_menu_actions,
ApplicationMenus.Search: self._main.search_menu_actions,
ApplicationMenus.Source: self._main.source_menu_actions,
ApplicationMenus.Run: self._main.run_menu_actions,
ApplicationMenus.Debug: self._main.debug_menu_actions,
}

menu = self.get_application_menu(menu_id)

if menu_id in app_menu_actions:
actions = app_menu_actions[menu_id] # type: list
position = None
for i, action in enumerate(actions):
this_item_id = None
if (isinstance(action, SpyderAction) or
hasattr(action, 'action_id')):
this_item_id = action.action_id
elif (isinstance(action, SpyderMenu) or
hasattr(action, 'menu_id')):
this_item_id = action.menu_id
if this_item_id is not None and this_item_id == item_id:
position = i
break
if position is not None:
actions.pop(position)
else:
menu.remove_action(item_id)

def get_application_menu(self, menu_id: str) -> SpyderMenu:
"""
Return an application menu by menu unique id.
Expand Down

0 comments on commit b866f3f

Please sign in to comment.