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 __future__ import annotations
|
||||||
|
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
from enum import Enum
|
from dataclasses import dataclass
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import aqt
|
import aqt
|
||||||
@ -24,10 +24,20 @@ class DeckBrowserBottomBar:
|
|||||||
self.deck_browser = deck_browser
|
self.deck_browser = deck_browser
|
||||||
|
|
||||||
|
|
||||||
class DeckBrowserSection(Enum):
|
@dataclass
|
||||||
TREE = 0
|
class DeckBrowserContent:
|
||||||
STATS = 1
|
"""Stores sections of HTML content that the deck browser will be
|
||||||
WARN = 2
|
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:
|
class DeckBrowser:
|
||||||
@ -110,17 +120,14 @@ class DeckBrowser:
|
|||||||
gui_hooks.deck_browser_did_render(self)
|
gui_hooks.deck_browser_did_render(self)
|
||||||
|
|
||||||
def __renderPage(self, offset):
|
def __renderPage(self, offset):
|
||||||
tree = gui_hooks.deck_browser_will_render_section(
|
content = DeckBrowserContent(
|
||||||
self._renderDeckTree(self._dueTree), DeckBrowserSection.TREE, self
|
tree=self._renderDeckTree(self._dueTree),
|
||||||
)
|
stats=self._renderStats(),
|
||||||
stats = gui_hooks.deck_browser_will_render_section(
|
countwarn=self._countWarn(),
|
||||||
self._renderStats(), DeckBrowserSection.STATS, self
|
|
||||||
)
|
|
||||||
warn = gui_hooks.deck_browser_will_render_section(
|
|
||||||
self._countWarn(), DeckBrowserSection.WARN, self
|
|
||||||
)
|
)
|
||||||
|
gui_hooks.deck_browser_will_render_content(self, content)
|
||||||
self.web.stdHtml(
|
self.web.stdHtml(
|
||||||
self._body % dict(tree=tree, stats=stats, countwarn=warn),
|
self._body % content.__dict__,
|
||||||
css=["deckbrowser.css"],
|
css=["deckbrowser.css"],
|
||||||
js=["jquery.js", "jquery-ui.js", "deckbrowser.js"],
|
js=["jquery.js", "jquery-ui.js", "deckbrowser.js"],
|
||||||
context=self,
|
context=self,
|
||||||
|
@ -435,51 +435,41 @@ class _DeckBrowserDidRenderHook:
|
|||||||
deck_browser_did_render = _DeckBrowserDidRenderHook()
|
deck_browser_did_render = _DeckBrowserDidRenderHook()
|
||||||
|
|
||||||
|
|
||||||
class _DeckBrowserWillRenderSectionFilter:
|
class _DeckBrowserWillRenderContentHook:
|
||||||
"""Used to modify HTML content sections in the deck browser body
|
"""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
|
When modifying the content of a particular section, please make sure your
|
||||||
of all the possible values please see aqt.deckbrowser.DeckBrowserSection.
|
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.:
|
||||||
|
|
||||||
If you do not want to modify the content of a particular section,
|
def on_deck_browser_will_render_content(deck_browser, content):
|
||||||
return 'html' unmodified, e.g.:
|
content.stats += "
|
||||||
|
<div>my html</div>"
|
||||||
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[
|
_hooks: List[
|
||||||
Callable[
|
Callable[
|
||||||
[str, "aqt.deckbrowser.DeckBrowserSection", "aqt.deckbrowser.DeckBrowser"],
|
["aqt.deckbrowser.DeckBrowser", "aqt.deckbrowser.DeckBrowserContent"], None
|
||||||
str,
|
|
||||||
]
|
]
|
||||||
] = []
|
] = []
|
||||||
|
|
||||||
def append(
|
def append(
|
||||||
self,
|
self,
|
||||||
cb: Callable[
|
cb: Callable[
|
||||||
[str, "aqt.deckbrowser.DeckBrowserSection", "aqt.deckbrowser.DeckBrowser"],
|
["aqt.deckbrowser.DeckBrowser", "aqt.deckbrowser.DeckBrowserContent"], None
|
||||||
str,
|
|
||||||
],
|
],
|
||||||
) -> 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)
|
self._hooks.append(cb)
|
||||||
|
|
||||||
def remove(
|
def remove(
|
||||||
self,
|
self,
|
||||||
cb: Callable[
|
cb: Callable[
|
||||||
[str, "aqt.deckbrowser.DeckBrowserSection", "aqt.deckbrowser.DeckBrowser"],
|
["aqt.deckbrowser.DeckBrowser", "aqt.deckbrowser.DeckBrowserContent"], None
|
||||||
str,
|
|
||||||
],
|
],
|
||||||
) -> None:
|
) -> None:
|
||||||
if cb in self._hooks:
|
if cb in self._hooks:
|
||||||
@ -487,21 +477,19 @@ class _DeckBrowserWillRenderSectionFilter:
|
|||||||
|
|
||||||
def __call__(
|
def __call__(
|
||||||
self,
|
self,
|
||||||
html: str,
|
|
||||||
section: aqt.deckbrowser.DeckBrowserSection,
|
|
||||||
deck_browser: aqt.deckbrowser.DeckBrowser,
|
deck_browser: aqt.deckbrowser.DeckBrowser,
|
||||||
) -> str:
|
content: aqt.deckbrowser.DeckBrowserContent,
|
||||||
for filter in self._hooks:
|
) -> None:
|
||||||
|
for hook in self._hooks:
|
||||||
try:
|
try:
|
||||||
html = filter(html, section, deck_browser)
|
hook(deck_browser, content)
|
||||||
except:
|
except:
|
||||||
# if the hook fails, remove it
|
# if the hook fails, remove it
|
||||||
self._hooks.remove(filter)
|
self._hooks.remove(hook)
|
||||||
raise
|
raise
|
||||||
return html
|
|
||||||
|
|
||||||
|
|
||||||
deck_browser_will_render_section = _DeckBrowserWillRenderSectionFilter()
|
deck_browser_will_render_content = _DeckBrowserWillRenderContentHook()
|
||||||
|
|
||||||
|
|
||||||
class _DeckBrowserWillShowOptionsMenuHook:
|
class _DeckBrowserWillShowOptionsMenuHook:
|
||||||
|
@ -31,33 +31,23 @@ hooks = [
|
|||||||
doc="""Allow to update the deck browser window. E.g. change its title.""",
|
doc="""Allow to update the deck browser window. E.g. change its title.""",
|
||||||
),
|
),
|
||||||
Hook(
|
Hook(
|
||||||
name="deck_browser_will_render_section",
|
name="deck_browser_will_render_content",
|
||||||
args=[
|
args=[
|
||||||
"html: str",
|
|
||||||
"section: aqt.deckbrowser.DeckBrowserSection",
|
|
||||||
"deck_browser: aqt.deckbrowser.DeckBrowser",
|
"deck_browser: aqt.deckbrowser.DeckBrowser",
|
||||||
|
"content: aqt.deckbrowser.DeckBrowserContent",
|
||||||
],
|
],
|
||||||
return_type="str",
|
|
||||||
doc="""Used to modify HTML content sections in the deck browser body
|
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
|
When modifying the content of a particular section, please make sure your
|
||||||
of all the possible values please see aqt.deckbrowser.DeckBrowserSection.
|
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.:
|
||||||
|
|
||||||
If you do not want to modify the content of a particular section,
|
def on_deck_browser_will_render_content(deck_browser, content):
|
||||||
return 'html' unmodified, e.g.:
|
content.stats += "\n<div>my html</div>"
|
||||||
|
|
||||||
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(
|
Hook(
|
||||||
|
Loading…
Reference in New Issue
Block a user