Skip to content

Commit

Permalink
API: Add plugin_name kwarg to register_shortcut_for_widget
Browse files Browse the repository at this point in the history
This is to have feature parity of kwargs with the other methods of
SpyderShortcutsMixin.
  • Loading branch information
ccordoba12 committed Nov 21, 2024
1 parent fde2435 commit b18dc1a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
2 changes: 2 additions & 0 deletions changelogs/Spyder-6.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### API changes

* Add `plugin_name` kwarg to the `register_shortcut_for_widget` method of
`SpyderShortcutsMixin`.
* The `add_configuration_observer` method was added to `SpyderConfigurationObserver`.
* Add `items_elide_mode` kwarg to the constructors of `SpyderComboBox` and
`SpyderComboBoxWithIcons`.
Expand Down
17 changes: 14 additions & 3 deletions spyder/api/shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def register_shortcut_for_widget(
triggered: Callable,
widget: Optional[QWidget] = None,
context: Optional[str] = None,
plugin_name: Optional[str] = None,
):
"""
Register a shortcut for a widget that inherits this mixin.
Expand All @@ -128,6 +129,10 @@ def register_shortcut_for_widget(
Name of the shortcut context, e.g. "editor" for shortcuts that have
effect when the Editor is focused or "_" for global shortcuts. If
not set, the widget's CONF_SECTION will be used as context.
plugin_name: str, optional
Name of the plugin where the shortcut is defined. This is only
necessary for third-party plugins that have shortcuts with several
contexts.
"""
context = self.CONF_SECTION if context is None else context
widget = self if widget is None else widget
Expand All @@ -140,6 +145,7 @@ def register_shortcut_for_widget(
triggered=triggered,
context=context,
widget=widget,
plugin_name=plugin_name,
)

self.add_configuration_observer(
Expand All @@ -159,6 +165,7 @@ def _register_shortcut(
triggered: Callable,
context: str,
widget: QWidget,
plugin_name: Optional[str]
):
"""
Auxiliary function to register a shortcut for a widget.
Expand All @@ -178,18 +185,22 @@ def _register_shortcut(
context: str, optional
Name of the shortcut context, e.g. "editor" for shortcuts that have
effect when the Editor is focused or "_" for global shortcuts.
plugin_name: str, optional
Name of the plugin where the shortcut is defined. This is only
necessary for third-party plugins that have shortcuts with several
contexts.
"""
# Disable current shortcut, if available
current_shortcut = self._shortcuts.get((context, name))
current_shortcut = self._shortcuts.get((context, name, plugin_name))
if current_shortcut:
current_shortcut.setEnabled(False)
current_shortcut.deleteLater()
self._shortcuts.pop((context, name))
self._shortcuts.pop((context, name, plugin_name))

# Create a new shortcut
new_shortcut = QShortcut(QKeySequence(keystr), widget)
new_shortcut.activated.connect(triggered)
new_shortcut.setContext(Qt.WidgetWithChildrenShortcut)

# Save shortcut
self._shortcuts[(context, name)] = new_shortcut
self._shortcuts[(context, name, plugin_name)] = new_shortcut

0 comments on commit b18dc1a

Please sign in to comment.