Add deck_browser_will_render_section hook

Allows add-on authors to specifcally target and modify individual
sections of the deck browser HTML body at string composition time.
This commit is contained in:
Glutanimate 2020-02-16 19:29:01 +01:00
parent 7894ab3312
commit d02de28f21
3 changed files with 116 additions and 3 deletions

View file

@ -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,

View file

@ -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 += "<div>my code</div>"
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]] = []

View file

@ -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 += "<div>my code</div>"
return html
""",
),
Hook(
name="reviewer_did_show_question",
args=["card: Card"],