Add deck_conf_did_setup_ui_form hook

Called earlier than deck_conf_will_show, allowing add-on authors
to perform UI modifications before the deck config is loaded.
This commit is contained in:
Glutanimate 2020-02-24 15:29:23 +01:00
parent 335047187a
commit 7cc9311b79
3 changed files with 32 additions and 0 deletions

View File

@ -29,6 +29,7 @@ class DeckConf(QDialog):
self._origNewOrder = None
self.form = aqt.forms.dconf.Ui_Dialog()
self.form.setupUi(self)
gui_hooks.deck_conf_did_setup_ui_form(self)
self.mw.checkpoint(_("Options"))
self.setupCombos()
self.setupConfs()

View File

@ -544,6 +544,32 @@ class _DeckConfDidLoadConfigHook:
deck_conf_did_load_config = _DeckConfDidLoadConfigHook()
class _DeckConfDidSetupUiFormHook:
"""Allows modifying or adding widgets in the deck options UI form"""
_hooks: List[Callable[["aqt.deckconf.DeckConf"], None]] = []
def append(self, cb: Callable[["aqt.deckconf.DeckConf"], None]) -> None:
"""(deck_conf: aqt.deckconf.DeckConf)"""
self._hooks.append(cb)
def remove(self, cb: Callable[["aqt.deckconf.DeckConf"], None]) -> None:
if cb in self._hooks:
self._hooks.remove(cb)
def __call__(self, deck_conf: aqt.deckconf.DeckConf) -> None:
for hook in self._hooks:
try:
hook(deck_conf)
except:
# if the hook fails, remove it
self._hooks.remove(hook)
raise
deck_conf_did_setup_ui_form = _DeckConfDidSetupUiFormHook()
class _DeckConfWillSaveConfigHook:
"""Called before widget state is saved to config"""

View File

@ -123,6 +123,11 @@ hooks = [
),
# Deck options
###################
Hook(
name="deck_conf_did_setup_ui_form",
args=["deck_conf: aqt.deckconf.DeckConf"],
doc="Allows modifying or adding widgets in the deck options UI form",
),
Hook(
name="deck_conf_will_show",
args=["deck_conf: aqt.deckconf.DeckConf"],