mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 14:02:21 -04:00
Add a couple of hooks to trigger add-on actions before showing stats
This commit is contained in:
parent
61017a2e21
commit
b89811bfba
3 changed files with 73 additions and 0 deletions
|
@ -2342,6 +2342,64 @@ class _StateWillChangeHook:
|
|||
state_will_change = _StateWillChangeHook()
|
||||
|
||||
|
||||
class _StatsDialogOldWillShowHook:
|
||||
"""Allows changing the old stats dialog before it is shown."""
|
||||
|
||||
_hooks: List[Callable[["aqt.stats.NewDeckStats"], None]] = []
|
||||
|
||||
def append(self, cb: Callable[["aqt.stats.NewDeckStats"], None]) -> None:
|
||||
"""(dialog: aqt.stats.NewDeckStats)"""
|
||||
self._hooks.append(cb)
|
||||
|
||||
def remove(self, cb: Callable[["aqt.stats.NewDeckStats"], None]) -> None:
|
||||
if cb in self._hooks:
|
||||
self._hooks.remove(cb)
|
||||
|
||||
def count(self) -> int:
|
||||
return len(self._hooks)
|
||||
|
||||
def __call__(self, dialog: aqt.stats.NewDeckStats) -> None:
|
||||
for hook in self._hooks:
|
||||
try:
|
||||
hook(dialog)
|
||||
except:
|
||||
# if the hook fails, remove it
|
||||
self._hooks.remove(hook)
|
||||
raise
|
||||
|
||||
|
||||
stats_dialog_old_will_show = _StatsDialogOldWillShowHook()
|
||||
|
||||
|
||||
class _StatsDialogWillShowHook:
|
||||
"""Allows changing the stats dialog before it is shown."""
|
||||
|
||||
_hooks: List[Callable[["aqt.stats.NewDeckStats"], None]] = []
|
||||
|
||||
def append(self, cb: Callable[["aqt.stats.NewDeckStats"], None]) -> None:
|
||||
"""(dialog: aqt.stats.NewDeckStats)"""
|
||||
self._hooks.append(cb)
|
||||
|
||||
def remove(self, cb: Callable[["aqt.stats.NewDeckStats"], None]) -> None:
|
||||
if cb in self._hooks:
|
||||
self._hooks.remove(cb)
|
||||
|
||||
def count(self) -> int:
|
||||
return len(self._hooks)
|
||||
|
||||
def __call__(self, dialog: aqt.stats.NewDeckStats) -> None:
|
||||
for hook in self._hooks:
|
||||
try:
|
||||
hook(dialog)
|
||||
except:
|
||||
# if the hook fails, remove it
|
||||
self._hooks.remove(hook)
|
||||
raise
|
||||
|
||||
|
||||
stats_dialog_will_show = _StatsDialogWillShowHook()
|
||||
|
||||
|
||||
class _StyleDidInitFilter:
|
||||
_hooks: List[Callable[[str], str]] = []
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ import time
|
|||
|
||||
import aqt
|
||||
from anki.lang import _
|
||||
from aqt import gui_hooks
|
||||
from aqt.qt import *
|
||||
from aqt.theme import theme_manager
|
||||
from aqt.utils import (
|
||||
|
@ -43,6 +44,7 @@ class NewDeckStats(QDialog):
|
|||
b.setAutoDefault(False)
|
||||
maybeHideClose(self.form.buttonBox)
|
||||
addCloseShortcut(self)
|
||||
gui_hooks.stats_dialog_will_show(self)
|
||||
self.show()
|
||||
self.refresh()
|
||||
self.activateWindow()
|
||||
|
@ -125,6 +127,7 @@ class DeckStats(QDialog):
|
|||
qconnect(f.life.clicked, lambda: self.changePeriod(2))
|
||||
maybeHideClose(self.form.buttonBox)
|
||||
addCloseShortcut(self)
|
||||
gui_hooks.stats_dialog_old_will_show(self)
|
||||
self.show()
|
||||
self.refresh()
|
||||
self.activateWindow()
|
||||
|
|
|
@ -601,6 +601,18 @@ hooks = [
|
|||
# Model
|
||||
###################
|
||||
Hook(name="models_advanced_will_show", args=["advanced: QDialog"],),
|
||||
# Stats
|
||||
###################
|
||||
Hook(
|
||||
name="stats_dialog_will_show",
|
||||
args=["dialog: aqt.stats.NewDeckStats"],
|
||||
doc="""Allows changing the stats dialog before it is shown.""",
|
||||
),
|
||||
Hook(
|
||||
name="stats_dialog_old_will_show",
|
||||
args=["dialog: aqt.stats.NewDeckStats"],
|
||||
doc="""Allows changing the old stats dialog before it is shown.""",
|
||||
),
|
||||
# Other
|
||||
###################
|
||||
Hook(
|
||||
|
|
Loading…
Reference in a new issue