From e6fcfb73792fc9dd446c165f9bd00bcf9a23aa00 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Thu, 31 May 2018 13:20:10 +1000 Subject: [PATCH] need to record active window prior to playing in mpv --- anki/sound.py | 2 ++ aqt/main.py | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/anki/sound.py b/anki/sound.py index 16e8ccf87..46ad37e8d 100644 --- a/anki/sound.py +++ b/anki/sound.py @@ -102,6 +102,8 @@ class MpvManager(MPV): super().__init__(window_id=None, debug=False) def queueFile(self, file): + runHook("mpvWillPlay") + path = os.path.join(os.getcwd(), file) self.command("loadfile", path, "append-play") diff --git a/aqt/main.py b/aqt/main.py index a0b6d0dd5..0ad101716 100644 --- a/aqt/main.py +++ b/aqt/main.py @@ -960,17 +960,26 @@ Difference to correct time: %s.""") % diffText addHook("modSchema", self.onSchemaMod) addHook("remNotes", self.onRemNotes) addHook("odueInvalid", self.onOdueInvalid) + + addHook("mpvWillPlay", self.onMpvWillPlay) addHook("mpvIdleHook", self.onMpvIdle) + self._activeWindowOnPlay = None def onOdueInvalid(self): showWarning(_("""\ Invalid property found on card. Please use Tools>Check Database, \ and if the problem comes up again, please ask on the support site.""")) + def onMpvWillPlay(self): + if not self._activeWindowOnPlay: + self._activeWindowOnPlay = self.app.activeWindow() + def onMpvIdle(self): - # when audio player finishes playing, restore focus to anki window - self.activateWindow() - self.raise_() + w = self._activeWindowOnPlay + if w and not sip.isdeleted(w) and w.isVisible(): + w.activateWindow() + w.raise_() + self._activeWindowOnPlay = None # Log note deletion ##########################################################################