add extra hook for media sync start/stop
This commit is contained in:
parent
0f7fc1e960
commit
fdd850c0f0
@ -721,6 +721,30 @@ class _MediaSyncDidProgressHook:
|
|||||||
media_sync_did_progress = _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:
|
class _OverviewDidRefreshHook:
|
||||||
"""Allow to update the overview window. E.g. add the deck name in the
|
"""Allow to update the overview window. E.g. add the deck name in the
|
||||||
title."""
|
title."""
|
||||||
|
@ -76,6 +76,7 @@ class MediaSyncer:
|
|||||||
self._log: List[LogEntryWithTime] = []
|
self._log: List[LogEntryWithTime] = []
|
||||||
self._want_stop = False
|
self._want_stop = False
|
||||||
hooks.rust_progress_callback.append(self._on_rust_progress)
|
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:
|
def _on_rust_progress(self, proceed: bool, progress: Progress) -> bool:
|
||||||
if progress.kind != ProgressKind.MediaSyncProgress:
|
if progress.kind != ProgressKind.MediaSyncProgress:
|
||||||
@ -116,7 +117,7 @@ class MediaSyncer:
|
|||||||
self._log_and_notify(_("Media sync starting..."))
|
self._log_and_notify(_("Media sync starting..."))
|
||||||
self._sync_state = MediaSyncState()
|
self._sync_state = MediaSyncState()
|
||||||
self._want_stop = False
|
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)
|
(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:
|
def _on_finished(self, future: Future) -> None:
|
||||||
self._sync_state = None
|
self._sync_state = None
|
||||||
self._on_start_stop()
|
gui_hooks.media_sync_did_start_or_stop(False)
|
||||||
|
|
||||||
exc = future.exception()
|
exc = future.exception()
|
||||||
if exc is not None:
|
if exc is not None:
|
||||||
@ -200,8 +201,8 @@ class MediaSyncer:
|
|||||||
def is_syncing(self) -> bool:
|
def is_syncing(self) -> bool:
|
||||||
return self._sync_state is not None
|
return self._sync_state is not None
|
||||||
|
|
||||||
def _on_start_stop(self):
|
def _on_start_stop(self, running: bool):
|
||||||
self.mw.toolbar.set_sync_active(self.is_syncing())
|
self.mw.toolbar.set_sync_active(running)
|
||||||
|
|
||||||
def show_sync_log(self):
|
def show_sync_log(self):
|
||||||
aqt.dialogs.open("sync_log", self.mw, self)
|
aqt.dialogs.open("sync_log", self.mw, self)
|
||||||
|
@ -269,6 +269,7 @@ hooks = [
|
|||||||
Hook(
|
Hook(
|
||||||
name="media_sync_did_progress", args=["entry: aqt.mediasync.LogEntryWithTime"],
|
name="media_sync_did_progress", args=["entry: aqt.mediasync.LogEntryWithTime"],
|
||||||
),
|
),
|
||||||
|
Hook(name="media_sync_did_start_or_stop", args=["running: bool"]),
|
||||||
# Adding cards
|
# Adding cards
|
||||||
###################
|
###################
|
||||||
Hook(
|
Hook(
|
||||||
|
Loading…
Reference in New Issue
Block a user