Merge pull request #682 from glutanimate/add-top-toolbar-did-redraw-hook

Add top_toolbar_did_redraw hook
This commit is contained in:
Damien Elmes 2020-07-11 10:20:41 +10:00 committed by GitHub
commit c34a99871b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 0 deletions

View file

@ -2437,6 +2437,35 @@ class _TopToolbarDidInitLinksHook:
top_toolbar_did_init_links = _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: class _UndoStateDidChangeHook:
_hooks: List[Callable[[bool], None]] = [] _hooks: List[Callable[[bool], None]] = []

View file

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

View file

@ -451,6 +451,11 @@ hooks = [
links.append(my_link) 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( Hook(
name="media_sync_did_progress", args=["entry: aqt.mediasync.LogEntryWithTime"], name="media_sync_did_progress", args=["entry: aqt.mediasync.LogEntryWithTime"],
), ),