diff --git a/qt/aqt/clayout.py b/qt/aqt/clayout.py index 87084cd8b..d95ef635d 100644 --- a/qt/aqt/clayout.py +++ b/qt/aqt/clayout.py @@ -65,6 +65,7 @@ class CardLayout(QDialog): v1.addLayout(self.buttons) v1.setContentsMargins(12, 12, 12, 12) self.setLayout(v1) + gui_hooks.card_layout_will_show(self) self.redraw() restoreGeom(self, "CardLayout") self.setWindowModality(Qt.ApplicationModal) diff --git a/qt/aqt/gui_hooks.py b/qt/aqt/gui_hooks.py index 5a001251e..a41a12d36 100644 --- a/qt/aqt/gui_hooks.py +++ b/qt/aqt/gui_hooks.py @@ -328,6 +328,33 @@ class _BrowserWillShowContextMenuHook: browser_will_show_context_menu = _BrowserWillShowContextMenuHook() +class _CardLayoutWillShowHook: + """Allow to change the display of the card layout. After most values are + set and before the window is actually shown.""" + + _hooks: List[Callable[["aqt.clayout.CardLayout"], None]] = [] + + def append(self, cb: Callable[["aqt.clayout.CardLayout"], None]) -> None: + """(clayout: aqt.clayout.CardLayout)""" + self._hooks.append(cb) + + def remove(self, cb: Callable[["aqt.clayout.CardLayout"], None]) -> None: + if cb in self._hooks: + self._hooks.remove(cb) + + def __call__(self, clayout: aqt.clayout.CardLayout) -> None: + for hook in self._hooks: + try: + hook(clayout) + except: + # if the hook fails, remove it + self._hooks.remove(hook) + raise + + +card_layout_will_show = _CardLayoutWillShowHook() + + class _CardWillShowFilter: """Can modify card text before review/preview.""" diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index cda84a43a..fd63660c0 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -114,6 +114,14 @@ hooks = [ legacy_hook="reviewCleanup", doc="Called before Anki transitions from the review screen to another screen.", ), + # Card layout + ################### + Hook( + name="card_layout_will_show", + args=["clayout: aqt.clayout.CardLayout"], + doc="""Allow to change the display of the card layout. After most values are + set and before the window is actually shown.""", + ), # Multiple windows ################### # reviewer, clayout and browser