Update 'top_toolbar_did_init_links' hook

This commit is contained in:
Glutanimate 2020-02-20 16:23:33 +01:00
parent e13fee5aa3
commit dfefd67508
3 changed files with 28 additions and 15 deletions

View file

@ -1295,25 +1295,28 @@ style_did_init = _StyleDidInitFilter()
class _TopToolbarDidInitLinksHook: class _TopToolbarDidInitLinksHook:
_hooks: List[ """Used to modify or add links in the top toolbar of Anki's main window
Callable[[List[Tuple[str, str, str]], "aqt.toolbar.Toolbar"], None]
] = [] 'links' is a list of HTML link elements. Add-ons can generate their own links
by using aqt.toolbar.Toolbar.create_link. Links created in that way can then be
appended to the link list, e.g.:
def append( def on_top_toolbar_did_init_links(links, toolbar):
self, cb: Callable[[List[Tuple[str, str, str]], "aqt.toolbar.Toolbar"], None] my_link = toolbar.create_link(...)
) -> None: links.append(my_link)
"""(links: List[Tuple[str, str, str]], top_toolbar: aqt.toolbar.Toolbar)""" """
_hooks: List[Callable[[List[str], "aqt.toolbar.Toolbar"], None]] = []
def append(self, cb: Callable[[List[str], "aqt.toolbar.Toolbar"], None]) -> None:
"""(links: List[str], top_toolbar: aqt.toolbar.Toolbar)"""
self._hooks.append(cb) self._hooks.append(cb)
def remove( def remove(self, cb: Callable[[List[str], "aqt.toolbar.Toolbar"], None]) -> None:
self, cb: Callable[[List[Tuple[str, str, str]], "aqt.toolbar.Toolbar"], None]
) -> None:
if cb in self._hooks: if cb in self._hooks:
self._hooks.remove(cb) self._hooks.remove(cb)
def __call__( def __call__(self, links: List[str], top_toolbar: aqt.toolbar.Toolbar) -> None:
self, links: List[Tuple[str, str, str]], top_toolbar: aqt.toolbar.Toolbar
) -> None:
for hook in self._hooks: for hook in self._hooks:
try: try:
hook(links, top_toolbar) hook(links, top_toolbar)

View file

@ -109,7 +109,7 @@ class Toolbar:
links.append(self._create_sync_link()) links.append(self._create_sync_link())
# gui_hooks.top_toolbar_did_init_links(links, self) gui_hooks.top_toolbar_did_init_links(links, self)
return "\n".join(links) return "\n".join(links)

View file

@ -308,7 +308,17 @@ hooks = [
), ),
Hook( Hook(
name="top_toolbar_did_init_links", name="top_toolbar_did_init_links",
args=["links: List[Tuple[str, str, str]]", "top_toolbar: aqt.toolbar.Toolbar"], args=["links: List[str]", "top_toolbar: aqt.toolbar.Toolbar"],
doc="""Used to modify or add links in the top toolbar of Anki's main window
'links' is a list of HTML link elements. Add-ons can generate their own links
by using aqt.toolbar.Toolbar.create_link. Links created in that way can then be
appended to the link list, e.g.:
def on_top_toolbar_did_init_links(links, toolbar):
my_link = toolbar.create_link(...)
links.append(my_link)
""",
), ),
Hook( Hook(
name="media_sync_did_progress", args=["entry: aqt.mediasync.LogEntryWithTime"], name="media_sync_did_progress", args=["entry: aqt.mediasync.LogEntryWithTime"],