hook single_addon_selected

This commit is contained in:
Arthur Milchior 2020-03-06 22:21:42 +01:00
parent 4e1838bba8
commit 74b2b18966
3 changed files with 40 additions and 0 deletions

View file

@ -784,6 +784,7 @@ class AddonsDialog(QDialog):
or self.mgr.configAction(addon.dir_name)
)
)
gui_hooks.addons_dialog_did_change_selected_addon(self, addon)
return
def selectedAddons(self) -> List[str]:

View file

@ -135,6 +135,40 @@ class _AddonConfigEditorWillSaveJsonFilter:
addon_config_editor_will_save_json = _AddonConfigEditorWillSaveJsonFilter()
class _AddonsDialogDidChangeSelectedAddonHook:
"""Allows doing an action when a single add-on is selected."""
_hooks: List[
Callable[["aqt.addons.AddonsDialog", "aqt.addons.AddonMeta"], None]
] = []
def append(
self, cb: Callable[["aqt.addons.AddonsDialog", "aqt.addons.AddonMeta"], None]
) -> None:
"""(dialog: aqt.addons.AddonsDialog, add_on: aqt.addons.AddonMeta)"""
self._hooks.append(cb)
def remove(
self, cb: Callable[["aqt.addons.AddonsDialog", "aqt.addons.AddonMeta"], None]
) -> None:
if cb in self._hooks:
self._hooks.remove(cb)
def __call__(
self, dialog: aqt.addons.AddonsDialog, add_on: aqt.addons.AddonMeta
) -> None:
for hook in self._hooks:
try:
hook(dialog, add_on)
except:
# if the hook fails, remove it
self._hooks.remove(hook)
raise
addons_dialog_did_change_selected_addon = _AddonsDialogDidChangeSelectedAddonHook()
class _AddonsDialogWillShowHook:
"""Allows changing the add-on dialog before it is shown. E.g. add
buttons."""

View file

@ -494,6 +494,11 @@ def emptyNewCard():
doc="""Allows changing the add-on dialog before it is shown. E.g. add
buttons.""",
),
Hook(
name="addons_dialog_did_change_selected_addon",
args=["dialog: aqt.addons.AddonsDialog", "add_on: aqt.addons.AddonMeta"],
doc="""Allows doing an action when a single add-on is selected.""",
),
# Other
###################
Hook(