Merge pull request #641 from glutanimate/add-main-window-did-init-hook

Add main_window_did_init hook
This commit is contained in:
Damien Elmes 2020-05-29 08:56:47 +10:00 committed by GitHub
commit cfc2a549f1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 2 deletions

View file

@ -1475,6 +1475,38 @@ class _EmptyCardsWillShowHook:
empty_cards_will_show = _EmptyCardsWillShowHook() empty_cards_will_show = _EmptyCardsWillShowHook()
class _MainWindowDidInitHook:
"""Executed after the main window is fully initialized
A sample use case for this hook would be to delay actions until Anki objects
like the profile or collection are fully initialized. In contrast to
`profile_did_open`, this hook will only fire once per Anki session and
is thus suitable for single-shot subscribers.
"""
_hooks: List[Callable[[], None]] = []
def append(self, cb: Callable[[], None]) -> None:
"""()"""
self._hooks.append(cb)
def remove(self, cb: Callable[[], None]) -> None:
if cb in self._hooks:
self._hooks.remove(cb)
def __call__(self) -> None:
for hook in self._hooks:
try:
hook()
except:
# if the hook fails, remove it
self._hooks.remove(hook)
raise
main_window_did_init = _MainWindowDidInitHook()
class _MediaSyncDidProgressHook: class _MediaSyncDidProgressHook:
_hooks: List[Callable[["aqt.mediasync.LogEntryWithTime"], None]] = [] _hooks: List[Callable[["aqt.mediasync.LogEntryWithTime"], None]] = []
@ -1624,6 +1656,13 @@ overview_will_render_content = _OverviewWillRenderContentHook()
class _ProfileDidOpenHook: class _ProfileDidOpenHook:
"""Executed whenever a user profile has been opened
Please note that this hook will also be called on profile switches, so if you
are looking to simply delay an add-on action in a single-shot manner,
`main_window_did_init` is likely the more suitable choice.
"""
_hooks: List[Callable[[], None]] = [] _hooks: List[Callable[[], None]] = []
def append(self, cb: Callable[[], None]) -> None: def append(self, cb: Callable[[], None]) -> None:

View file

@ -118,7 +118,12 @@ class AnkiQt(QMainWindow):
fn = self.setupProfileAfterWebviewsLoaded fn = self.setupProfileAfterWebviewsLoaded
else: else:
fn = self.setupProfile fn = self.setupProfile
self.progress.timer(10, fn, False, requiresCollection=False)
def on_window_init():
fn()
gui_hooks.main_window_did_init()
self.progress.timer(10, on_window_init, False, requiresCollection=False)
def setupUI(self) -> None: def setupUI(self) -> None:
self.col = None self.col = None

View file

@ -400,8 +400,27 @@ hooks = [
), ),
# Main # Main
################### ###################
Hook(
name="main_window_did_init",
doc="""Executed after the main window is fully initialized
A sample use case for this hook would be to delay actions until Anki objects
like the profile or collection are fully initialized. In contrast to
`profile_did_open`, this hook will only fire once per Anki session and
is thus suitable for single-shot subscribers.
""",
),
Hook(name="backup_did_complete"), Hook(name="backup_did_complete"),
Hook(name="profile_did_open", legacy_hook="profileLoaded"), Hook(
name="profile_did_open",
legacy_hook="profileLoaded",
doc="""Executed whenever a user profile has been opened
Please note that this hook will also be called on profile switches, so if you
are looking to simply delay an add-on action in a single-shot manner,
`main_window_did_init` is likely the more suitable choice.
""",
),
Hook(name="profile_will_close", legacy_hook="unloadProfile"), Hook(name="profile_will_close", legacy_hook="unloadProfile"),
Hook( Hook(
name="collection_did_load", name="collection_did_load",