From 15fd3a4856379d251bfb8f5031e632142f50dd34 Mon Sep 17 00:00:00 2001 From: Glutanimate Date: Fri, 10 Jul 2020 17:38:40 +0200 Subject: [PATCH] Add top_toolbar_did_redraw hook Notifies add-ons of the top toolbar being redrawn --- qt/aqt/gui_hooks.py | 29 +++++++++++++++++++++++++++++ qt/aqt/toolbar.py | 1 + qt/tools/genhooks_gui.py | 5 +++++ 3 files changed, 35 insertions(+) diff --git a/qt/aqt/gui_hooks.py b/qt/aqt/gui_hooks.py index 138919fa7..2f9828bf9 100644 --- a/qt/aqt/gui_hooks.py +++ b/qt/aqt/gui_hooks.py @@ -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]] = [] diff --git a/qt/aqt/toolbar.py b/qt/aqt/toolbar.py index 5cfa9e1e2..fc5eb5e23 100644 --- a/qt/aqt/toolbar.py +++ b/qt/aqt/toolbar.py @@ -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 ###################################################################### diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index 17ffa6720..29a166f2e 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -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"], ),