Skip to content

Commit

Permalink
Merge pull request Stvad#129 from aplaice/crashing_export_all_decks
Browse files Browse the repository at this point in the history
Prevent crash when trying to export all decks
  • Loading branch information
Stvad authored Jun 4, 2021
2 parents 117a294 + b7cb571 commit b25eace
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions crowd_anki/utils/notifier.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from abc import ABC, abstractmethod

import aqt.utils
import aqt


class Notifier(ABC):
Expand All @@ -16,16 +17,27 @@ def warning(self, title: str, message: str):
def error(self, title: str, message: str):
pass

def run_closure_in_main(closure):
"""Run the closure in the main thread.
This is necessary, because GUI operations in a background thread cause
a crash. For instance, export is now run in a background thread.
"""
if aqt.mw.inMainThread():
closure()
else:
aqt.mw.taskman.run_on_main(lambda: aqt.mw.progress.timer(0, closure, False))

class AnkiModalNotifier(Notifier):
def info(self, title: str, message: str):
aqt.utils.showInfo(message, title=title)
run_closure_in_main(lambda: aqt.utils.showInfo(message, title=title))

def warning(self, title: str, message: str):
aqt.utils.showWarning(message, title=title)
run_closure_in_main(lambda: aqt.utils.showWarning(message, title=title))

def error(self, title: str, message: str):
aqt.utils.showCritical(message, title=title)
run_closure_in_main(lambda: aqt.utils.showCritical(message, title=title))


class AnkiTooltipNotifier(Notifier):
Expand Down

0 comments on commit b25eace

Please sign in to comment.