diff --git a/qt/aqt/gui_hooks.py b/qt/aqt/gui_hooks.py index a6813d54c..3a85b50a5 100644 --- a/qt/aqt/gui_hooks.py +++ b/qt/aqt/gui_hooks.py @@ -781,6 +781,55 @@ class _OverviewDidRefreshHook: overview_did_refresh = _OverviewDidRefreshHook() +class _OverviewWillRenderContentHook: + """Used to modify HTML content sections in the overview body + + 'content' contains the sections of HTML content the overview body + will be updated with. + + When modifying the content of a particular section, please make sure your + changes only perform the minimum required edits to make your add-on work. + You should avoid overwriting or interfering with existing data as much + as possible, instead opting to append your own changes, e.g.: + + def on_overview_will_render_content(overview, content): + content.table += " +
my html
" + """ + + _hooks: List[ + Callable[["aqt.overview.Overview", "aqt.overview.OverviewContent"], None] + ] = [] + + def append( + self, + cb: Callable[["aqt.overview.Overview", "aqt.overview.OverviewContent"], None], + ) -> None: + """(overview: aqt.overview.Overview, content: aqt.overview.OverviewContent)""" + self._hooks.append(cb) + + def remove( + self, + cb: Callable[["aqt.overview.Overview", "aqt.overview.OverviewContent"], None], + ) -> None: + if cb in self._hooks: + self._hooks.remove(cb) + + def __call__( + self, overview: aqt.overview.Overview, content: aqt.overview.OverviewContent + ) -> None: + for hook in self._hooks: + try: + hook(overview, content) + except: + # if the hook fails, remove it + self._hooks.remove(hook) + raise + + +overview_will_render_content = _OverviewWillRenderContentHook() + + class _ProfileDidOpenHook: _hooks: List[Callable[[], None]] = [] diff --git a/qt/aqt/overview.py b/qt/aqt/overview.py index e94b69953..0b06d27b8 100644 --- a/qt/aqt/overview.py +++ b/qt/aqt/overview.py @@ -4,6 +4,8 @@ from __future__ import annotations +from dataclasses import dataclass + import aqt from anki.lang import _ from aqt import gui_hooks @@ -17,6 +19,14 @@ class OverviewBottomBar: self.overview = overview +@dataclass +class OverviewContent: + deck: str + shareLink: str + desc: str + table: str + + class Overview: "Deck overview." @@ -141,14 +151,15 @@ class Overview: shareLink = 'Reviews and Updates' else: shareLink = "" + content = OverviewContent( + deck=deck["name"], + shareLink=shareLink, + desc=self._desc(deck), + table=self._table(), + ) + gui_hooks.overview_will_render_content(self, content) self.web.stdHtml( - self._body - % dict( - deck=deck["name"], - shareLink=shareLink, - desc=self._desc(deck), - table=self._table(), - ), + self._body % content.__dict__, css=["overview.css"], js=["jquery.js", "overview.js"], context=self, diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index 3d9b198bc..e377a727b 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -25,6 +25,26 @@ hooks = [ doc="""Allow to update the overview window. E.g. add the deck name in the title.""", ), + Hook( + name="overview_will_render_content", + args=[ + "overview: aqt.overview.Overview", + "content: aqt.overview.OverviewContent", + ], + doc="""Used to modify HTML content sections in the overview body + + 'content' contains the sections of HTML content the overview body + will be updated with. + + When modifying the content of a particular section, please make sure your + changes only perform the minimum required edits to make your add-on work. + You should avoid overwriting or interfering with existing data as much + as possible, instead opting to append your own changes, e.g.: + + def on_overview_will_render_content(overview, content): + content.table += "\n
my html
" + """, + ), Hook( name="deck_browser_did_render", args=["deck_browser: aqt.deckbrowser.DeckBrowser"],