Wait for macOS focus restoration to land

This commit is contained in:
firat
2026-08-18 18:26:15 +02:00
parent 45b18722be
commit 0be64c2ef9
2 changed files with 50 additions and 5 deletions
+15 -3
View File
@@ -637,12 +637,24 @@ class Dikte:
# inactive: at forty the title bar visibly blinks, at ten it does not.
# Two messages to AppKit per tick, for at most a second and a half.
watch.setInterval(10)
# activateWithOptions: answers whether macOS accepted the request, not
# whether the other application is already back in front. Keep the
# watch alive until that asynchronous handoff is observable; on Intel
# Macs it can take hundreds of milliseconds after the call returned.
restore_requested = False
def look():
if mac_window.is_frontmost():
mac_window.activate(was_in_front)
nonlocal restore_requested
if time.monotonic() > deadline:
self._stop_watching_the_front()
elif time.monotonic() > deadline:
return
if mac_window.is_frontmost():
if not restore_requested:
restore_requested = mac_window.activate(was_in_front)
elif restore_requested:
# The request has landed. Stop only now, rather than as soon
# as AppKit accepted it, so a delayed or failed handoff stays
# under observation until the deadline guard above.
self._stop_watching_the_front()
watch.timeout.connect(look)