diff --git a/qt/aqt/browser.py b/qt/aqt/browser.py index 4a1f7b317..ecdcbe1ab 100644 --- a/qt/aqt/browser.py +++ b/qt/aqt/browser.py @@ -596,6 +596,7 @@ class Browser(QMainWindow): self.updateFont() self.onUndoState(self.mw.form.actionUndo.isEnabled()) self.setupSearch() + gui_hooks.browser_will_show(self) self.show() def setupMenus(self) -> None: diff --git a/qt/aqt/gui_hooks.py b/qt/aqt/gui_hooks.py index 5a001251e..08746896e 100644 --- a/qt/aqt/gui_hooks.py +++ b/qt/aqt/gui_hooks.py @@ -302,6 +302,30 @@ class _BrowserWillBuildTreeFilter: browser_will_build_tree = _BrowserWillBuildTreeFilter() +class _BrowserWillShowHook: + _hooks: List[Callable[["aqt.browser.Browser"], None]] = [] + + def append(self, cb: Callable[["aqt.browser.Browser"], None]) -> None: + """(browser: aqt.browser.Browser)""" + self._hooks.append(cb) + + def remove(self, cb: Callable[["aqt.browser.Browser"], None]) -> None: + if cb in self._hooks: + self._hooks.remove(cb) + + def __call__(self, browser: aqt.browser.Browser) -> None: + for hook in self._hooks: + try: + hook(browser) + except: + # if the hook fails, remove it + self._hooks.remove(hook) + raise + + +browser_will_show = _BrowserWillShowHook() + + class _BrowserWillShowContextMenuHook: _hooks: List[Callable[["aqt.browser.Browser", QMenu], None]] = [] diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index 39b334e4c..849ed1212 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -147,6 +147,7 @@ hooks = [ ), # Browser ################### + Hook(name="browser_will_show", args=["browser: aqt.browser.Browser"]), Hook( name="browser_menus_did_init", args=["browser: aqt.browser.Browser"],