diff --git a/qt/aqt/gui_hooks.py b/qt/aqt/gui_hooks.py index 71aec7506..3cc7673ca 100644 --- a/qt/aqt/gui_hooks.py +++ b/qt/aqt/gui_hooks.py @@ -1737,6 +1737,41 @@ class _MainWindowDidInitHook: main_window_did_init = _MainWindowDidInitHook() +class _MainWindowWillRequireResetFilter: + """Executed before the main window will require a reset + + This hook can be used to change the behavior of the main window, + when other dialogs, like the AddCards or Browser, require a reset + from the main window. + """ + + _hooks: List[Callable[[bool], bool]] = [] + + def append(self, cb: Callable[[bool], bool]) -> None: + """(will_reset: bool)""" + self._hooks.append(cb) + + def remove(self, cb: Callable[[bool], bool]) -> None: + if cb in self._hooks: + self._hooks.remove(cb) + + def count(self) -> int: + return len(self._hooks) + + def __call__(self, will_reset: bool) -> bool: + for filter in self._hooks: + try: + will_reset = filter(will_reset) + except: + # if the hook fails, remove it + self._hooks.remove(filter) + raise + return will_reset + + +main_window_will_require_reset = _MainWindowWillRequireResetFilter() + + class _MediaSyncDidProgressHook: _hooks: List[Callable[["aqt.mediasync.LogEntryWithTime"], None]] = [] diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index a9d5b1096..5c7264656 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -440,6 +440,17 @@ hooks = [ is thus suitable for single-shot subscribers. """, ), + Hook( + name="main_window_will_require_reset", + args=["will_reset: bool"], + return_type="bool", + doc="""Executed before the main window will require a reset + + This hook can be used to change the behavior of the main window, + when other dialogs, like the AddCards or Browser, require a reset + from the main window. + """, + ), Hook(name="backup_did_complete"), Hook( name="profile_did_open",