moved monotoring of tooltip to be centralized

This commit is contained in:
junlu592 2025-11-07 12:47:32 +01:00
parent 0ecf708421
commit eb42e901d7
2 changed files with 37 additions and 25 deletions

View file

@ -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()

View file

@ -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