Bundle individual section hooks together into one
Uses new dataclass 'DeckBrowserContent'
This commit is contained in:
parent
d02de28f21
commit
8ff1a2e770
@ -5,7 +5,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from copy import deepcopy
|
||||
from enum import Enum
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
import aqt
|
||||
@ -24,10 +24,20 @@ class DeckBrowserBottomBar:
|
||||
self.deck_browser = deck_browser
|
||||
|
||||
|
||||
class DeckBrowserSection(Enum):
|
||||
TREE = 0
|
||||
STATS = 1
|
||||
WARN = 2
|
||||
@dataclass
|
||||
class DeckBrowserContent:
|
||||
"""Stores sections of HTML content that the deck browser will be
|
||||
populated with.
|
||||
|
||||
Attributes:
|
||||
tree {str} -- HTML of the deck tree section
|
||||
stats {str} -- HTML of the stats section
|
||||
countwarn {str} -- HTML of the deck count warning section
|
||||
"""
|
||||
|
||||
tree: str
|
||||
stats: str
|
||||
countwarn: str
|
||||
|
||||
|
||||
class DeckBrowser:
|
||||
@ -110,17 +120,14 @@ class DeckBrowser:
|
||||
gui_hooks.deck_browser_did_render(self)
|
||||
|
||||
def __renderPage(self, offset):
|
||||
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
|
||||
content = DeckBrowserContent(
|
||||
tree=self._renderDeckTree(self._dueTree),
|
||||
stats=self._renderStats(),
|
||||
countwarn=self._countWarn(),
|
||||
)
|
||||
gui_hooks.deck_browser_will_render_content(self, content)
|
||||
self.web.stdHtml(
|
||||
self._body % dict(tree=tree, stats=stats, countwarn=warn),
|
||||
self._body % content.__dict__,
|
||||
css=["deckbrowser.css"],
|
||||
js=["jquery.js", "jquery-ui.js", "deckbrowser.js"],
|
||||
context=self,
|
||||
|
@ -435,51 +435,41 @@ class _DeckBrowserDidRenderHook:
|
||||
deck_browser_did_render = _DeckBrowserDidRenderHook()
|
||||
|
||||
|
||||
class _DeckBrowserWillRenderSectionFilter:
|
||||
class _DeckBrowserWillRenderContentHook:
|
||||
"""Used to modify HTML content sections in the deck browser body
|
||||
|
||||
'html' is the content a particular section will be populated with
|
||||
'content' contains the sections of HTML content the deck browser body
|
||||
will be updated 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.:
|
||||
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_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
|
||||
def on_deck_browser_will_render_content(deck_browser, content):
|
||||
content.stats += "
|
||||
<div>my html</div>"
|
||||
"""
|
||||
|
||||
_hooks: List[
|
||||
Callable[
|
||||
[str, "aqt.deckbrowser.DeckBrowserSection", "aqt.deckbrowser.DeckBrowser"],
|
||||
str,
|
||||
["aqt.deckbrowser.DeckBrowser", "aqt.deckbrowser.DeckBrowserContent"], None
|
||||
]
|
||||
] = []
|
||||
|
||||
def append(
|
||||
self,
|
||||
cb: Callable[
|
||||
[str, "aqt.deckbrowser.DeckBrowserSection", "aqt.deckbrowser.DeckBrowser"],
|
||||
str,
|
||||
["aqt.deckbrowser.DeckBrowser", "aqt.deckbrowser.DeckBrowserContent"], None
|
||||
],
|
||||
) -> None:
|
||||
"""(html: str, section: aqt.deckbrowser.DeckBrowserSection, deck_browser: aqt.deckbrowser.DeckBrowser)"""
|
||||
"""(deck_browser: aqt.deckbrowser.DeckBrowser, content: aqt.deckbrowser.DeckBrowserContent)"""
|
||||
self._hooks.append(cb)
|
||||
|
||||
def remove(
|
||||
self,
|
||||
cb: Callable[
|
||||
[str, "aqt.deckbrowser.DeckBrowserSection", "aqt.deckbrowser.DeckBrowser"],
|
||||
str,
|
||||
["aqt.deckbrowser.DeckBrowser", "aqt.deckbrowser.DeckBrowserContent"], None
|
||||
],
|
||||
) -> None:
|
||||
if cb in self._hooks:
|
||||
@ -487,21 +477,19 @@ class _DeckBrowserWillRenderSectionFilter:
|
||||
|
||||
def __call__(
|
||||
self,
|
||||
html: str,
|
||||
section: aqt.deckbrowser.DeckBrowserSection,
|
||||
deck_browser: aqt.deckbrowser.DeckBrowser,
|
||||
) -> str:
|
||||
for filter in self._hooks:
|
||||
content: aqt.deckbrowser.DeckBrowserContent,
|
||||
) -> None:
|
||||
for hook in self._hooks:
|
||||
try:
|
||||
html = filter(html, section, deck_browser)
|
||||
hook(deck_browser, content)
|
||||
except:
|
||||
# if the hook fails, remove it
|
||||
self._hooks.remove(filter)
|
||||
self._hooks.remove(hook)
|
||||
raise
|
||||
return html
|
||||
|
||||
|
||||
deck_browser_will_render_section = _DeckBrowserWillRenderSectionFilter()
|
||||
deck_browser_will_render_content = _DeckBrowserWillRenderContentHook()
|
||||
|
||||
|
||||
class _DeckBrowserWillShowOptionsMenuHook:
|
||||
|
@ -31,33 +31,23 @@ hooks = [
|
||||
doc="""Allow to update the deck browser window. E.g. change its title.""",
|
||||
),
|
||||
Hook(
|
||||
name="deck_browser_will_render_section",
|
||||
name="deck_browser_will_render_content",
|
||||
args=[
|
||||
"html: str",
|
||||
"section: aqt.deckbrowser.DeckBrowserSection",
|
||||
"deck_browser: aqt.deckbrowser.DeckBrowser",
|
||||
"content: aqt.deckbrowser.DeckBrowserContent",
|
||||
],
|
||||
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
|
||||
'content' contains the sections of HTML content the deck browser body
|
||||
will be updated 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.:
|
||||
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_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
|
||||
def on_deck_browser_will_render_content(deck_browser, content):
|
||||
content.stats += "\n<div>my html</div>"
|
||||
""",
|
||||
),
|
||||
Hook(
|
||||
|
Loading…
Reference in New Issue
Block a user