Revert "NF: Improve typing of AnkiWebView action (#3475)" (#3504)

This reverts commit 18889239d2.
This commit is contained in:
Damien Elmes 2024-10-16 03:02:22 +10:00 committed by GitHub
parent 25070c505a
commit c1a2b03871
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -8,9 +8,9 @@ import json
import os import os
import re import re
import sys import sys
from collections.abc import Callable from collections.abc import Callable, Sequence
from enum import Enum from enum import Enum
from typing import TYPE_CHECKING, Any, Optional, cast from typing import TYPE_CHECKING, Any, cast
import anki import anki
import anki.lang import anki.lang
@ -285,7 +285,7 @@ class AnkiWebView(QWebEngineView):
self.onBridgeCmd: Callable[[str], Any] = self.defaultOnBridgeCmd self.onBridgeCmd: Callable[[str], Any] = self.defaultOnBridgeCmd
self._domDone = True self._domDone = True
self._pendingActions: list[Callable[[], None]] = [] self._pendingActions: list[tuple[str, Sequence[Any]]] = []
self.requiresCol = True self.requiresCol = True
self.setPage(self._page) self.setPage(self._page)
self._disable_zoom = False self._disable_zoom = False
@ -395,13 +395,14 @@ class AnkiWebView(QWebEngineView):
def setHtml( # type: ignore[override] def setHtml( # type: ignore[override]
self, html: str, context: PageContext | None = None self, html: str, context: PageContext | None = None
) -> None: ) -> None:
from aqt.mediasrv import PageContext
# discard any previous pending actions # discard any previous pending actions
self._pendingActions = [] self._pendingActions = []
self._domDone = True self._domDone = True
if context is None: if context is None:
context = PageContext.UNKNOWN context = PageContext.UNKNOWN
self._queueAction(lambda: self._setHtml(html, context)) self._queueAction("setHtml", html, context)
self.set_open_links_externally(True) self.set_open_links_externally(True)
self.allow_drops = False self.allow_drops = False
self.show() self.show()
@ -630,10 +631,10 @@ html {{ {font} }}
def eval(self, js: str) -> None: def eval(self, js: str) -> None:
self.evalWithCallback(js, None) self.evalWithCallback(js, None)
def evalWithCallback(self, js: str, cb: Optional[Callable]) -> None: def evalWithCallback(self, js: str, cb: Callable) -> None:
self._queueAction(lambda: self._evalWithCallback(js, cb)) self._queueAction("eval", js, cb)
def _evalWithCallback(self, js: str, cb: Optional[Callable[[Any], Any]]) -> None: def _evalWithCallback(self, js: str, cb: Callable[[Any], Any]) -> None:
if cb: if cb:
def handler(val: Any) -> None: def handler(val: Any) -> None:
@ -646,16 +647,22 @@ html {{ {font} }}
else: else:
self.page().runJavaScript(js) self.page().runJavaScript(js)
def _queueAction(self, action: Callable[[], None]) -> None: def _queueAction(self, name: str, *args: Any) -> None:
self._pendingActions.append(action) self._pendingActions.append((name, args))
self._maybeRunActions() self._maybeRunActions()
def _maybeRunActions(self) -> None: def _maybeRunActions(self) -> None:
if sip.isdeleted(self): if sip.isdeleted(self):
return return
while self._pendingActions and self._domDone: while self._pendingActions and self._domDone:
action = self._pendingActions.pop(0) name, args = self._pendingActions.pop(0)
action()
if name == "eval":
self._evalWithCallback(*args)
elif name == "setHtml":
self._setHtml(*args)
else:
raise Exception(f"unknown action: {name}")
def _openLinksExternally(self, url: str) -> None: def _openLinksExternally(self, url: str) -> None:
openLink(url) openLink(url)