Skip to content

Commit

Permalink
Migrate history and layouts to use the new teardown mmechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
andfoy committed Aug 31, 2021
1 parent cccfdb8 commit 6a9aa90
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 2 deletions.
10 changes: 10 additions & 0 deletions spyder/api/widgets/toolbars.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@ def add_item(self, action_or_widget: ToolbarItem,
self.add_item(item, section=section, before=before,
before_section=before_section)

def remove_item(self, item_id: str):
"""Remove action or widget from toolbar by id."""
item = self._item_map.pop(item_id)
for section in list(self._section_items.keys()):
section_items = self._section_items[section]
if item in section_items:
section_items.remove(item)
if len(section_items) == 0:
self._section_items.pop(section)

def _render(self):
"""
Create the toolbar taking into account sections and locations.
Expand Down
13 changes: 12 additions & 1 deletion spyder/plugins/history/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

# Local imports
from spyder.api.plugins import Plugins, SpyderDockablePlugin
from spyder.api.plugin_registration.decorators import on_plugin_available
from spyder.api.plugin_registration.decorators import (
on_plugin_available, on_plugin_teardown)
from spyder.api.translations import get_translation
from spyder.plugins.history.confpage import HistoryConfigPage
from spyder.plugins.history.widgets import HistoryWidget
Expand Down Expand Up @@ -68,6 +69,16 @@ def on_console_available(self):
console = self.get_plugin(Plugins.Console)
console.sig_refreshed.connect(self.refresh)

@on_plugin_teardown(plugin=Plugins.Preferences)
def on_preferences_teardown(self):
preferences = self.get_plugin(Plugins.Preferences)
preferences.deregister_plugin_preferences(self)

@on_plugin_teardown(plugin=Plugins.Console)
def on_console_teardown(self):
console = self.get_plugin(Plugins.Console)
console.sig_refreshed.disconnect(self.refresh)

def update_font(self):
color_scheme = self.get_color_scheme()
font = self.get_font()
Expand Down
46 changes: 45 additions & 1 deletion spyder/plugins/layout/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
# Local imports
from spyder.api.exceptions import SpyderAPIError
from spyder.api.plugins import Plugins, SpyderPluginV2
from spyder.api.plugin_registration.decorators import on_plugin_available
from spyder.api.plugin_registration.decorators import (
on_plugin_available, on_plugin_teardown)
from spyder.api.translations import get_translation
from spyder.api.utils import get_class_values
from spyder.plugins.mainmenu.api import ApplicationMenus, ViewMenuSections
Expand Down Expand Up @@ -146,6 +147,49 @@ def on_toolbar_available(self):
before=PreferencesActions.Show
)

@on_plugin_teardown(plugin=Plugins.MainMenu)
def on_main_menu_teardown(self):
mainmenu = self.get_plugin(Plugins.MainMenu)
container = self.get_container()
# Remove Panes related actions from the View application menu
panes_items = [
container._plugins_menu,
container._lock_interface_action,
container._close_dockwidget_action,
container._maximize_dockwidget_action]
for panes_item in panes_items:
mainmenu.remove_item_from_application_menu(
panes_item,
menu_id=ApplicationMenus.View)
# Remove layouts menu from the View application menu
layout_items = [
container._layouts_menu,
container._toggle_next_layout_action,
container._toggle_previous_layout_action]
for layout_item in layout_items:
mainmenu.remove_item_from_application_menu(
layout_item,
menu_id=ApplicationMenus.View)
# Remove fullscreen action from the View application menu
mainmenu.remove_item_from_application_menu(
container._fullscreen_action,
menu_id=ApplicationMenus.View)

@on_plugin_teardown(plugin=Plugins.Toolbar)
def on_toolbar_teardown(self):
container = self.get_container()
toolbars = self.get_plugin(Plugins.Toolbar)
# Remove actions from the Main application toolbar
before_action = self.get_action(
PreferencesActions.Show,
plugin=Plugins.Preferences
)

toolbars.remove_item_from_application_toolbar(
container._maximize_dockwidget_action,
toolbar_id=ApplicationToolbars.Main
)

def before_mainwindow_visible(self):
# Update layout menu
self.update_layout_menu_actions()
Expand Down
19 changes: 19 additions & 0 deletions spyder/plugins/toolbar/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,25 @@ def add_item_to_application_toolbar(self,
toolbar.add_item(item, section=section, before=before,
before_section=before_section, omit_id=omit_id)

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

toolbar = self.get_application_toolbar(toolbar_id)
toolbar.remove_item(item_id)

def get_application_toolbar(self, toolbar_id: str) -> ApplicationToolbar:
"""
Return an application toolbar by toolbar_id.
Expand Down
24 changes: 24 additions & 0 deletions spyder/plugins/toolbar/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,30 @@ def add_item_to_application_toolbar(self,
omit_id=omit_id
)

def remove_item_from_application_toolbar(self, item, toolbar=None,
toolbar_id=None):
"""
Remove action or widget `item` from given application menu.
Parameters
----------
item: SpyderAction or QWidget
The item to add to the `toolbar`.
toolbar: ApplicationToolbar or None
Instance of a Spyder application toolbar.
toolbar_id: str or None
The application toolbar unique string identifier.
Notes
-----
Must provide a `toolbar` or a `toolbar_id`.
"""
self.get_container().remove_item_from_application_toolbar(
item,
toolbar=toolbar,
toolbar_id=toolbar_id
)

def get_application_toolbar(self, toolbar_id):
"""
Return an application toolbar by toolbar_id.
Expand Down

0 comments on commit 6a9aa90

Please sign in to comment.