Add top_toolbar_did_redraw hook

Notifies add-ons of the top toolbar being redrawn
This commit is contained in:
Glutanimate 2020-07-10 17:38:40 +02:00
parent df319c7c58
commit 15fd3a4856
3 changed files with 35 additions and 0 deletions

View File

@ -2437,6 +2437,35 @@ class _TopToolbarDidInitLinksHook:
top_toolbar_did_init_links = _TopToolbarDidInitLinksHook()
class _TopToolbarDidRedrawHook:
"""Executed when the top toolbar is redrawn"""
_hooks: List[Callable[["aqt.toolbar.Toolbar"], None]] = []
def append(self, cb: Callable[["aqt.toolbar.Toolbar"], None]) -> None:
"""(top_toolbar: aqt.toolbar.Toolbar)"""
self._hooks.append(cb)
def remove(self, cb: Callable[["aqt.toolbar.Toolbar"], None]) -> None:
if cb in self._hooks:
self._hooks.remove(cb)
def count(self) -> int:
return len(self._hooks)
def __call__(self, top_toolbar: aqt.toolbar.Toolbar) -> None:
for hook in self._hooks:
try:
hook(top_toolbar)
except:
# if the hook fails, remove it
self._hooks.remove(hook)
raise
top_toolbar_did_redraw = _TopToolbarDidRedrawHook()
class _UndoStateDidChangeHook:
_hooks: List[Callable[[bool], None]] = []

View File

@ -57,6 +57,7 @@ class Toolbar:
def redraw(self) -> None:
self.set_sync_active(self.mw.media_syncer.is_syncing())
self.update_sync_status()
gui_hooks.top_toolbar_did_redraw(self)
# Available links
######################################################################

View File

@ -451,6 +451,11 @@ hooks = [
links.append(my_link)
""",
),
Hook(
name="top_toolbar_did_redraw",
args=["top_toolbar: aqt.toolbar.Toolbar"],
doc="""Executed when the top toolbar is redrawn""",
),
Hook(
name="media_sync_did_progress", args=["entry: aqt.mediasync.LogEntryWithTime"],
),