From fdd850c0f0ef858c9df002082be30a5447715039 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Wed, 5 Feb 2020 11:55:14 +1000 Subject: [PATCH] add extra hook for media sync start/stop --- qt/aqt/gui_hooks.py | 24 ++++++++++++++++++++++++ qt/aqt/mediasync.py | 9 +++++---- qt/tools/genhooks_gui.py | 1 + 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/qt/aqt/gui_hooks.py b/qt/aqt/gui_hooks.py index 3fa21a96f..b9fc915b4 100644 --- a/qt/aqt/gui_hooks.py +++ b/qt/aqt/gui_hooks.py @@ -721,6 +721,30 @@ class _MediaSyncDidProgressHook: media_sync_did_progress = _MediaSyncDidProgressHook() +class _MediaSyncDidStartOrStopHook: + _hooks: List[Callable[[bool], None]] = [] + + def append(self, cb: Callable[[bool], None]) -> None: + """(running: bool)""" + self._hooks.append(cb) + + def remove(self, cb: Callable[[bool], None]) -> None: + if cb in self._hooks: + self._hooks.remove(cb) + + def __call__(self, running: bool) -> None: + for hook in self._hooks: + try: + hook(running) + except: + # if the hook fails, remove it + self._hooks.remove(hook) + raise + + +media_sync_did_start_or_stop = _MediaSyncDidStartOrStopHook() + + class _OverviewDidRefreshHook: """Allow to update the overview window. E.g. add the deck name in the title.""" diff --git a/qt/aqt/mediasync.py b/qt/aqt/mediasync.py index 3218e1814..0ab9bea2b 100644 --- a/qt/aqt/mediasync.py +++ b/qt/aqt/mediasync.py @@ -76,6 +76,7 @@ class MediaSyncer: self._log: List[LogEntryWithTime] = [] self._want_stop = False hooks.rust_progress_callback.append(self._on_rust_progress) + gui_hooks.media_sync_did_start_or_stop.append(self._on_start_stop) def _on_rust_progress(self, proceed: bool, progress: Progress) -> bool: if progress.kind != ProgressKind.MediaSyncProgress: @@ -116,7 +117,7 @@ class MediaSyncer: self._log_and_notify(_("Media sync starting...")) self._sync_state = MediaSyncState() self._want_stop = False - self._on_start_stop() + gui_hooks.media_sync_did_start_or_stop(True) (media_folder, media_db) = media_paths_from_col_path(self.mw.col.path) @@ -144,7 +145,7 @@ class MediaSyncer: def _on_finished(self, future: Future) -> None: self._sync_state = None - self._on_start_stop() + gui_hooks.media_sync_did_start_or_stop(False) exc = future.exception() if exc is not None: @@ -200,8 +201,8 @@ class MediaSyncer: def is_syncing(self) -> bool: return self._sync_state is not None - def _on_start_stop(self): - self.mw.toolbar.set_sync_active(self.is_syncing()) + def _on_start_stop(self, running: bool): + self.mw.toolbar.set_sync_active(running) def show_sync_log(self): aqt.dialogs.open("sync_log", self.mw, self) diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index 4ffa76c12..cd2c8d471 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -269,6 +269,7 @@ hooks = [ Hook( name="media_sync_did_progress", args=["entry: aqt.mediasync.LogEntryWithTime"], ), + Hook(name="media_sync_did_start_or_stop", args=["running: bool"]), # Adding cards ################### Hook(