mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 06:22:22 -04:00
add reviewer_will_play_question_sounds and reviewer_will_play_answer_sounds hooks
This commit is contained in:
parent
215413ce25
commit
b4b12f1642
3 changed files with 74 additions and 4 deletions
|
@ -2112,6 +2112,60 @@ class _ReviewerWillEndHook:
|
||||||
reviewer_will_end = _ReviewerWillEndHook()
|
reviewer_will_end = _ReviewerWillEndHook()
|
||||||
|
|
||||||
|
|
||||||
|
class _ReviewerWillPlayAnswerSoundsHook:
|
||||||
|
_hooks: List[Callable[[Card, "List[anki.sound.AVTag]"], None]] = []
|
||||||
|
|
||||||
|
def append(self, cb: Callable[[Card, "List[anki.sound.AVTag]"], None]) -> None:
|
||||||
|
"""(card: Card, tags: List[anki.sound.AVTag])"""
|
||||||
|
self._hooks.append(cb)
|
||||||
|
|
||||||
|
def remove(self, cb: Callable[[Card, "List[anki.sound.AVTag]"], None]) -> None:
|
||||||
|
if cb in self._hooks:
|
||||||
|
self._hooks.remove(cb)
|
||||||
|
|
||||||
|
def count(self) -> int:
|
||||||
|
return len(self._hooks)
|
||||||
|
|
||||||
|
def __call__(self, card: Card, tags: List[anki.sound.AVTag]) -> None:
|
||||||
|
for hook in self._hooks:
|
||||||
|
try:
|
||||||
|
hook(card, tags)
|
||||||
|
except:
|
||||||
|
# if the hook fails, remove it
|
||||||
|
self._hooks.remove(hook)
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
reviewer_will_play_answer_sounds = _ReviewerWillPlayAnswerSoundsHook()
|
||||||
|
|
||||||
|
|
||||||
|
class _ReviewerWillPlayQuestionSoundsHook:
|
||||||
|
_hooks: List[Callable[[Card, "List[anki.sound.AVTag]"], None]] = []
|
||||||
|
|
||||||
|
def append(self, cb: Callable[[Card, "List[anki.sound.AVTag]"], None]) -> None:
|
||||||
|
"""(card: Card, tags: List[anki.sound.AVTag])"""
|
||||||
|
self._hooks.append(cb)
|
||||||
|
|
||||||
|
def remove(self, cb: Callable[[Card, "List[anki.sound.AVTag]"], None]) -> None:
|
||||||
|
if cb in self._hooks:
|
||||||
|
self._hooks.remove(cb)
|
||||||
|
|
||||||
|
def count(self) -> int:
|
||||||
|
return len(self._hooks)
|
||||||
|
|
||||||
|
def __call__(self, card: Card, tags: List[anki.sound.AVTag]) -> None:
|
||||||
|
for hook in self._hooks:
|
||||||
|
try:
|
||||||
|
hook(card, tags)
|
||||||
|
except:
|
||||||
|
# if the hook fails, remove it
|
||||||
|
self._hooks.remove(hook)
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
reviewer_will_play_question_sounds = _ReviewerWillPlayQuestionSoundsHook()
|
||||||
|
|
||||||
|
|
||||||
class _ReviewerWillShowContextMenuHook:
|
class _ReviewerWillShowContextMenuHook:
|
||||||
_hooks: List[Callable[["aqt.reviewer.Reviewer", QMenu], None]] = []
|
_hooks: List[Callable[["aqt.reviewer.Reviewer", QMenu], None]] = []
|
||||||
|
|
||||||
|
|
|
@ -183,10 +183,14 @@ class Reviewer:
|
||||||
q = c.q()
|
q = c.q()
|
||||||
# play audio?
|
# play audio?
|
||||||
if c.autoplay():
|
if c.autoplay():
|
||||||
av_player.play_tags(c.question_av_tags())
|
sounds = c.question_av_tags()
|
||||||
|
gui_hooks.reviewer_will_play_question_sounds(c, sounds)
|
||||||
|
av_player.play_tags(sounds)
|
||||||
else:
|
else:
|
||||||
av_player.clear_queue_and_maybe_interrupt()
|
av_player.clear_queue_and_maybe_interrupt()
|
||||||
|
sounds = []
|
||||||
|
gui_hooks.reviewer_will_play_question_sounds(c, sounds)
|
||||||
|
av_player.play_tags(sounds)
|
||||||
# render & update bottom
|
# render & update bottom
|
||||||
q = self._mungeQA(q)
|
q = self._mungeQA(q)
|
||||||
q = gui_hooks.card_will_show(q, c, "reviewQuestion")
|
q = gui_hooks.card_will_show(q, c, "reviewQuestion")
|
||||||
|
@ -225,10 +229,14 @@ class Reviewer:
|
||||||
a = c.a()
|
a = c.a()
|
||||||
# play audio?
|
# play audio?
|
||||||
if c.autoplay():
|
if c.autoplay():
|
||||||
av_player.play_tags(c.answer_av_tags())
|
sounds = c.answer_av_tags()
|
||||||
|
gui_hooks.reviewer_will_play_answer_sounds(c, sounds)
|
||||||
|
av_player.play_tags(sounds)
|
||||||
else:
|
else:
|
||||||
av_player.clear_queue_and_maybe_interrupt()
|
av_player.clear_queue_and_maybe_interrupt()
|
||||||
|
sounds = []
|
||||||
|
gui_hooks.reviewer_will_play_answer_sounds(c, sounds)
|
||||||
|
av_player.play_tags(sounds)
|
||||||
a = self._mungeQA(a)
|
a = self._mungeQA(a)
|
||||||
a = gui_hooks.card_will_show(a, c, "reviewAnswer")
|
a = gui_hooks.card_will_show(a, c, "reviewAnswer")
|
||||||
# render and update bottom
|
# render and update bottom
|
||||||
|
|
|
@ -90,6 +90,14 @@ hooks = [
|
||||||
legacy_hook="reviewCleanup",
|
legacy_hook="reviewCleanup",
|
||||||
doc="Called before Anki transitions from the review screen to another screen.",
|
doc="Called before Anki transitions from the review screen to another screen.",
|
||||||
),
|
),
|
||||||
|
Hook(
|
||||||
|
name="reviewer_will_play_question_sounds",
|
||||||
|
args=["card: Card", "tags: List[anki.sound.AVTag]"],
|
||||||
|
),
|
||||||
|
Hook(
|
||||||
|
name="reviewer_will_play_answer_sounds",
|
||||||
|
args=["card: Card", "tags: List[anki.sound.AVTag]"],
|
||||||
|
),
|
||||||
# Debug
|
# Debug
|
||||||
###################
|
###################
|
||||||
Hook(
|
Hook(
|
||||||
|
|
Loading…
Reference in a new issue