Monkeytype qt/aqt/webview.py
This commit is contained in:
parent
8960d12aac
commit
cd570eef24
@ -5,7 +5,7 @@ import dataclasses
|
|||||||
import json
|
import json
|
||||||
import math
|
import math
|
||||||
import sys
|
import sys
|
||||||
from typing import Any, List, Optional, Tuple
|
from typing import Any, Callable, List, Optional, Sequence, Tuple
|
||||||
|
|
||||||
from anki.lang import _
|
from anki.lang import _
|
||||||
from anki.utils import isLin, isMac, isWin
|
from anki.utils import isLin, isMac, isWin
|
||||||
@ -176,7 +176,7 @@ class AnkiWebView(QWebEngineView): # type: ignore
|
|||||||
self.onBridgeCmd: Callable[[str], Any] = self.defaultOnBridgeCmd
|
self.onBridgeCmd: Callable[[str], Any] = self.defaultOnBridgeCmd
|
||||||
|
|
||||||
self._domDone = True
|
self._domDone = True
|
||||||
self._pendingActions: List[Tuple[str, List[Any]]] = []
|
self._pendingActions: List[Tuple[str, Sequence[Any]]] = []
|
||||||
self.requiresCol = True
|
self.requiresCol = True
|
||||||
self.setPage(self._page)
|
self.setPage(self._page)
|
||||||
|
|
||||||
@ -258,13 +258,13 @@ class AnkiWebView(QWebEngineView): # type: ignore
|
|||||||
def dropEvent(self, evt):
|
def dropEvent(self, evt):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def setHtml(self, html):
|
def setHtml(self, html: str) -> None:
|
||||||
# discard any previous pending actions
|
# discard any previous pending actions
|
||||||
self._pendingActions = []
|
self._pendingActions = []
|
||||||
self._domDone = True
|
self._domDone = True
|
||||||
self._queueAction("setHtml", html)
|
self._queueAction("setHtml", html)
|
||||||
|
|
||||||
def _setHtml(self, html):
|
def _setHtml(self, html: str) -> None:
|
||||||
app = QApplication.instance()
|
app = QApplication.instance()
|
||||||
oldFocus = app.focusWidget()
|
oldFocus = app.focusWidget()
|
||||||
self._domDone = False
|
self._domDone = False
|
||||||
@ -273,7 +273,7 @@ class AnkiWebView(QWebEngineView): # type: ignore
|
|||||||
if oldFocus:
|
if oldFocus:
|
||||||
oldFocus.setFocus()
|
oldFocus.setFocus()
|
||||||
|
|
||||||
def zoomFactor(self):
|
def zoomFactor(self) -> float:
|
||||||
# overridden scale factor?
|
# overridden scale factor?
|
||||||
webscale = os.environ.get("ANKI_WEBSCALE")
|
webscale = os.environ.get("ANKI_WEBSCALE")
|
||||||
if webscale:
|
if webscale:
|
||||||
@ -295,7 +295,7 @@ class AnkiWebView(QWebEngineView): # type: ignore
|
|||||||
newFactor = desiredScale / qtIntScale
|
newFactor = desiredScale / qtIntScale
|
||||||
return max(1, newFactor)
|
return max(1, newFactor)
|
||||||
|
|
||||||
def _getQtIntScale(self, screen):
|
def _getQtIntScale(self, screen) -> int:
|
||||||
# try to detect if Qt has scaled the screen
|
# try to detect if Qt has scaled the screen
|
||||||
# - qt will round the scale factor to a whole number, so a dpi of 125% = 1x,
|
# - qt will round the scale factor to a whole number, so a dpi of 125% = 1x,
|
||||||
# and a dpi of 150% = 2x
|
# and a dpi of 150% = 2x
|
||||||
@ -430,13 +430,13 @@ body {{ zoom: {}; background: {}; {} }}
|
|||||||
fname
|
fname
|
||||||
)
|
)
|
||||||
|
|
||||||
def eval(self, js):
|
def eval(self, js: str) -> None:
|
||||||
self.evalWithCallback(js, None)
|
self.evalWithCallback(js, None)
|
||||||
|
|
||||||
def evalWithCallback(self, js, cb):
|
def evalWithCallback(self, js: str, cb: Callable) -> None:
|
||||||
self._queueAction("eval", js, cb)
|
self._queueAction("eval", js, cb)
|
||||||
|
|
||||||
def _evalWithCallback(self, js, cb):
|
def _evalWithCallback(self, js: str, cb: Callable[[Any], Any]) -> None:
|
||||||
if cb:
|
if cb:
|
||||||
|
|
||||||
def handler(val):
|
def handler(val):
|
||||||
@ -449,11 +449,11 @@ body {{ zoom: {}; background: {}; {} }}
|
|||||||
else:
|
else:
|
||||||
self.page().runJavaScript(js)
|
self.page().runJavaScript(js)
|
||||||
|
|
||||||
def _queueAction(self, name, *args):
|
def _queueAction(self, name: str, *args: Any) -> None:
|
||||||
self._pendingActions.append((name, args))
|
self._pendingActions.append((name, args))
|
||||||
self._maybeRunActions()
|
self._maybeRunActions()
|
||||||
|
|
||||||
def _maybeRunActions(self):
|
def _maybeRunActions(self) -> None:
|
||||||
while self._pendingActions and self._domDone:
|
while self._pendingActions and self._domDone:
|
||||||
name, args = self._pendingActions.pop(0)
|
name, args = self._pendingActions.pop(0)
|
||||||
|
|
||||||
@ -464,10 +464,10 @@ body {{ zoom: {}; background: {}; {} }}
|
|||||||
else:
|
else:
|
||||||
raise Exception("unknown action: {}".format(name))
|
raise Exception("unknown action: {}".format(name))
|
||||||
|
|
||||||
def _openLinksExternally(self, url):
|
def _openLinksExternally(self, url: str) -> None:
|
||||||
openLink(url)
|
openLink(url)
|
||||||
|
|
||||||
def _shouldIgnoreWebEvent(self):
|
def _shouldIgnoreWebEvent(self) -> bool:
|
||||||
# async web events may be received after the profile has been closed
|
# async web events may be received after the profile has been closed
|
||||||
# or the underlying webview has been deleted
|
# or the underlying webview has been deleted
|
||||||
from aqt import mw
|
from aqt import mw
|
||||||
@ -499,18 +499,18 @@ body {{ zoom: {}; background: {}; {} }}
|
|||||||
else:
|
else:
|
||||||
return self.onBridgeCmd(cmd)
|
return self.onBridgeCmd(cmd)
|
||||||
|
|
||||||
def defaultOnBridgeCmd(self, cmd: str) -> Any:
|
def defaultOnBridgeCmd(self, cmd: str) -> None:
|
||||||
print("unhandled bridge cmd:", cmd)
|
print("unhandled bridge cmd:", cmd)
|
||||||
|
|
||||||
# legacy
|
# legacy
|
||||||
def resetHandlers(self):
|
def resetHandlers(self) -> None:
|
||||||
self.onBridgeCmd = self.defaultOnBridgeCmd
|
self.onBridgeCmd = self.defaultOnBridgeCmd
|
||||||
self._bridge_context = None
|
self._bridge_context = None
|
||||||
|
|
||||||
def adjustHeightToFit(self):
|
def adjustHeightToFit(self) -> None:
|
||||||
self.evalWithCallback("$(document.body).height()", self._onHeight)
|
self.evalWithCallback("$(document.body).height()", self._onHeight)
|
||||||
|
|
||||||
def _onHeight(self, qvar):
|
def _onHeight(self, qvar: Optional[int]) -> None:
|
||||||
from aqt import mw
|
from aqt import mw
|
||||||
|
|
||||||
if qvar is None:
|
if qvar is None:
|
||||||
|
Loading…
Reference in New Issue
Block a user