Add top_toolbar_did_init_links hook

Allows extending the links in the top toolbar, in a similar
fashion to editor_did_init_shortcuts
This commit is contained in:
Glutanimate 2020-02-15 23:21:23 +01:00
parent d0284f759d
commit 18ae9e612e
3 changed files with 43 additions and 5 deletions

View File

@ -1041,6 +1041,38 @@ class _StyleDidInitFilter:
style_did_init = _StyleDidInitFilter()
class _TopToolbarDidInitLinksHook:
_hooks: List[
Callable[[List[Tuple[str, str, str]], "aqt.toolbar.Toolbar"], None]
] = []
def append(
self, cb: Callable[[List[Tuple[str, str, str]], "aqt.toolbar.Toolbar"], None]
) -> None:
"""(links: List[Tuple[str, str, str]], top_toolbar: aqt.toolbar.Toolbar)"""
self._hooks.append(cb)
def remove(
self, cb: Callable[[List[Tuple[str, str, str]], "aqt.toolbar.Toolbar"], None]
) -> None:
if cb in self._hooks:
self._hooks.remove(cb)
def __call__(
self, links: List[Tuple[str, str, str]], top_toolbar: aqt.toolbar.Toolbar
) -> None:
for hook in self._hooks:
try:
hook(links, top_toolbar)
except:
# if the hook fails, remove it
self._hooks.remove(hook)
raise
top_toolbar_did_init_links = _TopToolbarDidInitLinksHook()
class _UndoStateDidChangeHook:
_hooks: List[Callable[[bool], None]] = []

View File

@ -6,6 +6,7 @@ from __future__ import annotations
import aqt
from anki.lang import _
from aqt import gui_hooks
from aqt.qt import *
from aqt.webview import AnkiWebView
@ -47,12 +48,13 @@ class Toolbar:
def _centerLinks(self):
links = [
["decks", _("Decks"), _("Shortcut key: %s") % "D"],
["add", _("Add"), _("Shortcut key: %s") % "A"],
["browse", _("Browse"), _("Shortcut key: %s") % "B"],
["stats", _("Stats"), _("Shortcut key: %s") % "T"],
["sync", _("Sync"), _("Shortcut key: %s") % "Y"],
("decks", _("Decks"), _("Shortcut key: %s") % "D"),
("add", _("Add"), _("Shortcut key: %s") % "A"),
("browse", _("Browse"), _("Shortcut key: %s") % "B"),
("stats", _("Stats"), _("Shortcut key: %s") % "T"),
("sync", _("Sync"), _("Shortcut key: %s") % "Y"),
]
gui_hooks.top_toolbar_did_init_links(links, self)
return self._linkHTML(links)
def _linkHTML(self, links):

View File

@ -187,6 +187,10 @@ hooks = [
return_type="str",
legacy_hook="setupStyle",
),
Hook(
name="top_toolbar_did_init_links",
args=["links: List[Tuple[str, str, str]]", "top_toolbar: aqt.toolbar.Toolbar",],
),
# Adding cards
###################
Hook(