From 75a8ef832b1968b99ca9f7c28ca178b33cc55af8 Mon Sep 17 00:00:00 2001 From: Arthur Milchior Date: Fri, 7 Feb 2020 21:31:41 -0800 Subject: [PATCH] Allow to change main window when overview/deckbrowser is shown --- qt/aqt/deckbrowser.py | 1 + qt/aqt/gui_hooks.py | 53 ++++++++++++++++++++++++++++++++++++++++ qt/aqt/overview.py | 2 ++ qt/tools/genhooks_gui.py | 11 +++++++++ 4 files changed, 67 insertions(+) diff --git a/qt/aqt/deckbrowser.py b/qt/aqt/deckbrowser.py index d4555dc8d..f4cdc8dfe 100644 --- a/qt/aqt/deckbrowser.py +++ b/qt/aqt/deckbrowser.py @@ -92,6 +92,7 @@ class DeckBrowser: self.__renderPage(None) return self.web.evalWithCallback("window.pageYOffset", self.__renderPage) + gui_hooks.deck_browser_did_render(self) def __renderPage(self, offset): tree = self._renderDeckTree(self._dueTree) diff --git a/qt/aqt/gui_hooks.py b/qt/aqt/gui_hooks.py index 3ef1709f9..bb752c83c 100644 --- a/qt/aqt/gui_hooks.py +++ b/qt/aqt/gui_hooks.py @@ -310,6 +310,32 @@ class _CurrentNoteTypeDidChangeHook: current_note_type_did_change = _CurrentNoteTypeDidChangeHook() +class _DeckBrowserDidRenderHook: + """Allow to update the deck browser window. E.g. change its title.""" + + _hooks: List[Callable[["aqt.deckbrowser.DeckBrowser"], None]] = [] + + def append(self, cb: Callable[["aqt.deckbrowser.DeckBrowser"], None]) -> None: + """(deck_browser: aqt.deckbrowser.DeckBrowser)""" + self._hooks.append(cb) + + def remove(self, cb: Callable[["aqt.deckbrowser.DeckBrowser"], None]) -> None: + if cb in self._hooks: + self._hooks.remove(cb) + + def __call__(self, deck_browser: aqt.deckbrowser.DeckBrowser) -> None: + for hook in self._hooks: + try: + hook(deck_browser) + except: + # if the hook fails, remove it + self._hooks.remove(hook) + raise + + +deck_browser_did_render = _DeckBrowserDidRenderHook() + + class _DeckBrowserWillShowOptionsMenuHook: _hooks: List[Callable[[QMenu, int], None]] = [] @@ -572,6 +598,33 @@ class _EditorWillUseFontForFieldFilter: editor_will_use_font_for_field = _EditorWillUseFontForFieldFilter() +class _OverviewDidRefreshHook: + """Allow to update the overview window. E.g. add the deck name in the + title.""" + + _hooks: List[Callable[["aqt.overview.Overview"], None]] = [] + + def append(self, cb: Callable[["aqt.overview.Overview"], None]) -> None: + """(overview: aqt.overview.Overview)""" + self._hooks.append(cb) + + def remove(self, cb: Callable[["aqt.overview.Overview"], None]) -> None: + if cb in self._hooks: + self._hooks.remove(cb) + + def __call__(self, overview: aqt.overview.Overview) -> None: + for hook in self._hooks: + try: + hook(overview) + except: + # if the hook fails, remove it + self._hooks.remove(hook) + raise + + +overview_did_refresh = _OverviewDidRefreshHook() + + class _ProfileDidOpenHook: _hooks: List[Callable[[], None]] = [] diff --git a/qt/aqt/overview.py b/qt/aqt/overview.py index 3a75eb8a7..6b0b98dd1 100644 --- a/qt/aqt/overview.py +++ b/qt/aqt/overview.py @@ -6,6 +6,7 @@ from __future__ import annotations import aqt from anki.lang import _ +from aqt import gui_hooks from aqt.sound import av_player from aqt.toolbar import BottomBar from aqt.utils import askUserDialog, openLink, shortcut, tooltip @@ -30,6 +31,7 @@ class Overview: self._renderPage() self._renderBottom() self.mw.web.setFocus() + gui_hooks.overview_did_refresh(self) # Handlers ############################################################ diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index c3ebf0272..a146519e9 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -19,6 +19,17 @@ from tools.hookslib import Hook, update_file hooks = [ # Reviewing ################### + Hook( + name="overview_did_refresh", + args=["overview: aqt.overview.Overview"], + doc="""Allow to update the overview window. E.g. add the deck name in the + title.""", + ), + Hook( + name="deck_browser_did_render", + args=["deck_browser: aqt.deckbrowser.DeckBrowser"], + doc="""Allow to update the deck browser window. E.g. change its title.""", + ), Hook( name="reviewer_did_show_question", args=["card: Card"],