From eb42e901d7af59693da400121cf45248dea8097a Mon Sep 17 00:00:00 2001 From: junlu592 Date: Fri, 7 Nov 2025 12:47:32 +0100 Subject: [PATCH] moved monotoring of tooltip to be centralized --- qt/aqt/sync.py | 23 ----------------------- qt/aqt/utils.py | 39 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 25 deletions(-) diff --git a/qt/aqt/sync.py b/qt/aqt/sync.py index 0ce02866a..75bdeca89 100644 --- a/qt/aqt/sync.py +++ b/qt/aqt/sync.py @@ -121,29 +121,6 @@ def sync_collection(mw: aqt.main.AnkiQt, on_done: Callable[[], None]) -> None: showText(out.server_message, parent=mw) if out.required == out.NO_CHANGES: tooltip(parent=mw, msg=tr.sync_collection_complete()) - - # Monitor window focus and close tooltip if window loses focus - def check_focus() -> None: - from aqt.utils import closeTooltip - - # Close tooltip if window loses focus, becomes invisible, or is minimized - if ( - not mw.isActiveWindow() - or not mw.isVisible() - or (mw.windowState() & Qt.WindowState.WindowMinimized) - ): - closeTooltip() - focus_timer.stop() - - focus_timer = QTimer(mw) - qconnect(focus_timer.timeout, check_focus) - focus_timer.start(100) # Check every 100ms - - # Stop monitoring after tooltip's natural expiry time - def stop_monitoring() -> None: - focus_timer.stop() - - QTimer.singleShot(3000, stop_monitoring) # all done; track media progress mw.media_syncer.start_monitoring() return on_done() diff --git a/qt/aqt/utils.py b/qt/aqt/utils.py index 43efc513f..68054e128 100644 --- a/qt/aqt/utils.py +++ b/qt/aqt/utils.py @@ -1061,6 +1061,7 @@ def send_to_trash(path: Path) -> None: ###################################################################### _tooltipTimer: QTimer | None = None +_tooltipFocusTimer: QTimer | None = None _tooltipLabel: QLabel | None = None @@ -1071,7 +1072,7 @@ def tooltip( x_offset: int = 0, y_offset: int = 100, ) -> None: - global _tooltipTimer, _tooltipLabel + global _tooltipTimer, _tooltipLabel, _tooltipFocusTimer class CustomLabel(QLabel): silentlyClose = True @@ -1101,6 +1102,33 @@ def tooltip( lab.setPalette(p) lab.move(aw.mapToGlobal(QPoint(0 + x_offset, aw.height() - y_offset))) lab.show() + window = aw.window() if hasattr(aw, "window") else aw + if window is None: + window = aw + + def close_if_parent_inactive() -> None: + if not _tooltipLabel or window is None: + return + if ( + not window.isActiveWindow() + or not window.isVisible() + or (window.windowState() & Qt.WindowState.WindowMinimized) + ): + closeTooltip() + + if _tooltipFocusTimer: + try: + _tooltipFocusTimer.stop() + _tooltipFocusTimer.deleteLater() + except RuntimeError: + pass + _tooltipFocusTimer = None + + focus_timer = QTimer(lab) + focus_timer.setInterval(100) + qconnect(focus_timer.timeout, close_if_parent_inactive) + focus_timer.start() + _tooltipFocusTimer = focus_timer _tooltipTimer = aqt.mw.progress.timer( period, closeTooltip, False, requiresCollection=False, parent=aw ) @@ -1108,7 +1136,7 @@ def tooltip( def closeTooltip() -> None: - global _tooltipLabel, _tooltipTimer + global _tooltipLabel, _tooltipTimer, _tooltipFocusTimer if _tooltipLabel: try: _tooltipLabel.deleteLater() @@ -1122,6 +1150,13 @@ def closeTooltip() -> None: except RuntimeError: pass _tooltipTimer = None + if _tooltipFocusTimer: + try: + _tooltipFocusTimer.stop() + _tooltipFocusTimer.deleteLater() + except RuntimeError: + pass + _tooltipFocusTimer = None # true if invalid; print warning