Merge pull request #472 from Arthur-Milchior/hook_init_clayout

Hook init clayout
This commit is contained in:
Damien Elmes 2020-03-02 15:39:23 +10:00 committed by GitHub
commit 3d7f643184
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 0 deletions

View File

@ -65,6 +65,7 @@ class CardLayout(QDialog):
v1.addLayout(self.buttons) v1.addLayout(self.buttons)
v1.setContentsMargins(12, 12, 12, 12) v1.setContentsMargins(12, 12, 12, 12)
self.setLayout(v1) self.setLayout(v1)
gui_hooks.card_layout_will_show(self)
self.redraw() self.redraw()
restoreGeom(self, "CardLayout") restoreGeom(self, "CardLayout")
self.setWindowModality(Qt.ApplicationModal) self.setWindowModality(Qt.ApplicationModal)

View File

@ -328,6 +328,33 @@ class _BrowserWillShowContextMenuHook:
browser_will_show_context_menu = _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: class _CardWillShowFilter:
"""Can modify card text before review/preview.""" """Can modify card text before review/preview."""

View File

@ -114,6 +114,17 @@ hooks = [
legacy_hook="reviewCleanup", legacy_hook="reviewCleanup",
doc="Called before Anki transitions from the review screen to another screen.", 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
Hook( Hook(
name="card_will_show", name="card_will_show",
args=["text: str", "card: Card", "kind: str"], args=["text: str", "card: Card", "kind: str"],