Merge pull request #537 from Arthur-Milchior/add_card_did_init

hook add_card_did_init
This commit is contained in:
Damien Elmes 2020-04-03 19:34:33 +10:00 committed by GitHub
commit 2c99a4203f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 0 deletions

View file

@ -46,6 +46,7 @@ class AddCards(QDialog):
gui_hooks.state_did_reset.append(self.onReset) gui_hooks.state_did_reset.append(self.onReset)
gui_hooks.current_note_type_did_change.append(self.onModelChange) gui_hooks.current_note_type_did_change.append(self.onModelChange)
addCloseShortcut(self) addCloseShortcut(self)
gui_hooks.add_cards_did_init(self)
self.show() self.show()
def setupEditor(self) -> None: def setupEditor(self) -> None:

View file

@ -50,6 +50,30 @@ class _AddCardsDidAddNoteHook:
add_cards_did_add_note = _AddCardsDidAddNoteHook() add_cards_did_add_note = _AddCardsDidAddNoteHook()
class _AddCardsDidInitHook:
_hooks: List[Callable[["aqt.addcards.AddCards"], None]] = []
def append(self, cb: Callable[["aqt.addcards.AddCards"], None]) -> None:
"""(addcards: aqt.addcards.AddCards)"""
self._hooks.append(cb)
def remove(self, cb: Callable[["aqt.addcards.AddCards"], None]) -> None:
if cb in self._hooks:
self._hooks.remove(cb)
def __call__(self, addcards: aqt.addcards.AddCards) -> None:
for hook in self._hooks:
try:
hook(addcards)
except:
# if the hook fails, remove it
self._hooks.remove(hook)
raise
add_cards_did_init = _AddCardsDidInitHook()
class _AddCardsWillAddNoteFilter: class _AddCardsWillAddNoteFilter:
"""Decides whether the note should be added to the collection or """Decides whether the note should be added to the collection or
not. It is assumed to come from the addCards window. not. It is assumed to come from the addCards window.

View file

@ -432,6 +432,7 @@ def emptyNewCard():
args=["addcards: aqt.addcards.AddCards", "menu: QMenu"], args=["addcards: aqt.addcards.AddCards", "menu: QMenu"],
legacy_hook="AddCards.onHistory", legacy_hook="AddCards.onHistory",
), ),
Hook(name="add_cards_did_init", args=["addcards: aqt.addcards.AddCards"],),
Hook( Hook(
name="add_cards_did_add_note", name="add_cards_did_add_note",
args=["note: anki.notes.Note"], args=["note: anki.notes.Note"],