diff --git a/qt/aqt/deckbrowser.py b/qt/aqt/deckbrowser.py index a57647d62..356d616fc 100644 --- a/qt/aqt/deckbrowser.py +++ b/qt/aqt/deckbrowser.py @@ -5,6 +5,7 @@ from __future__ import annotations from copy import deepcopy +from enum import Enum from typing import Any import aqt @@ -23,6 +24,12 @@ class DeckBrowserBottomBar: self.deck_browser = deck_browser +class DeckBrowserSection(Enum): + TREE = 0 + STATS = 1 + WARN = 2 + + class DeckBrowser: _dueTree: Any @@ -103,10 +110,17 @@ class DeckBrowser: gui_hooks.deck_browser_did_render(self) def __renderPage(self, offset): - tree = self._renderDeckTree(self._dueTree) - stats = self._renderStats() + tree = gui_hooks.deck_browser_will_render_section( + self._renderDeckTree(self._dueTree), DeckBrowserSection.TREE, self + ) + stats = gui_hooks.deck_browser_will_render_section( + self._renderStats(), DeckBrowserSection.STATS, self + ) + warn = gui_hooks.deck_browser_will_render_section( + self._countWarn(), DeckBrowserSection.WARN, self + ) self.web.stdHtml( - self._body % dict(tree=tree, stats=stats, countwarn=self._countWarn()), + self._body % dict(tree=tree, stats=stats, countwarn=warn), css=["deckbrowser.css"], js=["jquery.js", "jquery-ui.js", "deckbrowser.js"], context=self, diff --git a/qt/aqt/gui_hooks.py b/qt/aqt/gui_hooks.py index 026f0bc2f..a4922de36 100644 --- a/qt/aqt/gui_hooks.py +++ b/qt/aqt/gui_hooks.py @@ -435,6 +435,75 @@ class _DeckBrowserDidRenderHook: deck_browser_did_render = _DeckBrowserDidRenderHook() +class _DeckBrowserWillRenderSectionFilter: + """Used to modify HTML content sections in the deck browser body + + 'html' is the content a particular section will be populated with + + 'section' is an enum describing the current section. For an overview + of all the possible values please see aqt.deckbrowser.DeckBrowserSection. + + If you do not want to modify the content of a particular section, + return 'html' unmodified, e.g.: + + def on_deck_browser_will_render_section(html, section, deck_browser): + + if section != DeckBrowserSection.TREE: + # not the tree section we want to modify, return unchanged + return html + + # tree section, perform changes to html + html += "
my code
" + + return html + """ + + _hooks: List[ + Callable[ + [str, "aqt.deckbrowser.DeckBrowserSection", "aqt.deckbrowser.DeckBrowser"], + str, + ] + ] = [] + + def append( + self, + cb: Callable[ + [str, "aqt.deckbrowser.DeckBrowserSection", "aqt.deckbrowser.DeckBrowser"], + str, + ], + ) -> None: + """(html: str, section: aqt.deckbrowser.DeckBrowserSection, deck_browser: aqt.deckbrowser.DeckBrowser)""" + self._hooks.append(cb) + + def remove( + self, + cb: Callable[ + [str, "aqt.deckbrowser.DeckBrowserSection", "aqt.deckbrowser.DeckBrowser"], + str, + ], + ) -> None: + if cb in self._hooks: + self._hooks.remove(cb) + + def __call__( + self, + html: str, + section: aqt.deckbrowser.DeckBrowserSection, + deck_browser: aqt.deckbrowser.DeckBrowser, + ) -> str: + for filter in self._hooks: + try: + html = filter(html, section, deck_browser) + except: + # if the hook fails, remove it + self._hooks.remove(filter) + raise + return html + + +deck_browser_will_render_section = _DeckBrowserWillRenderSectionFilter() + + class _DeckBrowserWillShowOptionsMenuHook: _hooks: List[Callable[[QMenu, int], None]] = [] diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index bd8226132..b3efb16f3 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -30,6 +30,36 @@ hooks = [ args=["deck_browser: aqt.deckbrowser.DeckBrowser"], doc="""Allow to update the deck browser window. E.g. change its title.""", ), + Hook( + name="deck_browser_will_render_section", + args=[ + "html: str", + "section: aqt.deckbrowser.DeckBrowserSection", + "deck_browser: aqt.deckbrowser.DeckBrowser", + ], + return_type="str", + doc="""Used to modify HTML content sections in the deck browser body + + 'html' is the content a particular section will be populated with + + 'section' is an enum describing the current section. For an overview + of all the possible values please see aqt.deckbrowser.DeckBrowserSection. + + If you do not want to modify the content of a particular section, + return 'html' unmodified, e.g.: + + def on_deck_browser_will_render_section(html, section, deck_browser): + + if section != DeckBrowserSection.TREE: + # not the tree section we want to modify, return unchanged + return html + + # tree section, perform changes to html + html += "
my code
" + + return html + """, + ), Hook( name="reviewer_did_show_question", args=["card: Card"],