2019-02-05 04:59:03 +01:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
2012-12-21 08:51:59 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
2018-03-08 08:48:14 +01:00
|
|
|
import json
|
2017-08-28 13:15:38 +02:00
|
|
|
import math
|
2019-12-20 10:19:03 +01:00
|
|
|
import sys
|
2020-01-22 01:46:35 +01:00
|
|
|
from typing import Any, List, Optional, Tuple
|
2019-12-20 10:19:03 +01:00
|
|
|
|
|
|
|
from anki.lang import _
|
|
|
|
from anki.utils import isLin, isMac, isWin
|
2020-01-14 23:53:57 +01:00
|
|
|
from aqt import gui_hooks
|
2012-12-21 08:51:59 +01:00
|
|
|
from aqt.qt import *
|
2020-01-23 06:08:10 +01:00
|
|
|
from aqt.theme import theme_manager
|
2019-06-27 01:49:05 +02:00
|
|
|
from aqt.utils import openLink
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Page for debug messages
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
class AnkiWebPage(QWebEnginePage): # type: ignore
|
2016-06-06 07:50:03 +02:00
|
|
|
def __init__(self, onBridgeCmd):
|
2016-05-31 10:51:40 +02:00
|
|
|
QWebEnginePage.__init__(self)
|
2016-06-06 07:50:03 +02:00
|
|
|
self._onBridgeCmd = onBridgeCmd
|
|
|
|
self._setupBridge()
|
|
|
|
|
|
|
|
def _setupBridge(self):
|
|
|
|
class Bridge(QObject):
|
2018-03-08 08:48:14 +01:00
|
|
|
@pyqtSlot(str, result=str)
|
2016-06-06 07:50:03 +02:00
|
|
|
def cmd(self, str):
|
2018-03-08 08:48:14 +01:00
|
|
|
return json.dumps(self.onCmd(str))
|
2016-06-06 07:50:03 +02:00
|
|
|
|
|
|
|
self._bridge = Bridge()
|
|
|
|
self._bridge.onCmd = self._onCmd
|
|
|
|
|
|
|
|
self._channel = QWebChannel(self)
|
|
|
|
self._channel.registerObject("py", self._bridge)
|
|
|
|
self.setWebChannel(self._channel)
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
js = QFile(":/qtwebchannel/qwebchannel.js")
|
2016-06-06 07:50:03 +02:00
|
|
|
assert js.open(QIODevice.ReadOnly)
|
2019-12-23 01:34:10 +01:00
|
|
|
js = bytes(js.readAll()).decode("utf-8")
|
2016-06-06 07:50:03 +02:00
|
|
|
|
|
|
|
script = QWebEngineScript()
|
2019-12-23 01:34:10 +01:00
|
|
|
script.setSourceCode(
|
|
|
|
js
|
|
|
|
+ """
|
2016-06-06 07:50:03 +02:00
|
|
|
var pycmd;
|
|
|
|
new QWebChannel(qt.webChannelTransport, function(channel) {
|
2018-03-08 08:48:14 +01:00
|
|
|
pycmd = function (arg, cb) {
|
|
|
|
var resultCB = function (res) {
|
|
|
|
// pass result back to user-provided callback
|
|
|
|
if (cb) {
|
|
|
|
cb(JSON.parse(res));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 08:47:01 +02:00
|
|
|
channel.objects.py.cmd(arg, resultCB);
|
|
|
|
return false;
|
2018-03-08 08:48:14 +01:00
|
|
|
}
|
2016-06-06 07:50:03 +02:00
|
|
|
pycmd("domDone");
|
|
|
|
});
|
2019-12-23 01:34:10 +01:00
|
|
|
"""
|
|
|
|
)
|
2016-06-06 07:50:03 +02:00
|
|
|
script.setWorldId(QWebEngineScript.MainWorld)
|
|
|
|
script.setInjectionPoint(QWebEngineScript.DocumentReady)
|
|
|
|
script.setRunsOnSubFrames(False)
|
|
|
|
self.profile().scripts().insert(script)
|
2016-05-31 10:51:40 +02:00
|
|
|
|
|
|
|
def javaScriptConsoleMessage(self, lvl, msg, line, srcID):
|
2017-08-06 05:10:51 +02:00
|
|
|
# not translated because console usually not visible,
|
|
|
|
# and may only accept ascii text
|
2019-12-23 01:34:10 +01:00
|
|
|
buf = "JS error on line %(a)d: %(b)s" % dict(a=line, b=msg + "\n")
|
2019-05-17 00:43:25 +02:00
|
|
|
# ensure we don't try to write characters the terminal can't handle
|
2019-12-23 01:34:10 +01:00
|
|
|
buf = buf.encode(sys.stdout.encoding, "backslashreplace").decode(
|
|
|
|
sys.stdout.encoding
|
|
|
|
)
|
2019-05-17 00:43:25 +02:00
|
|
|
sys.stdout.write(buf)
|
2016-05-31 10:51:40 +02:00
|
|
|
|
|
|
|
def acceptNavigationRequest(self, url, navType, isMainFrame):
|
2017-07-17 04:40:38 +02:00
|
|
|
if not isMainFrame:
|
|
|
|
return True
|
2018-10-23 08:11:08 +02:00
|
|
|
# data: links generated by setHtml()
|
|
|
|
if url.scheme() == "data":
|
|
|
|
return True
|
2018-10-23 08:47:01 +02:00
|
|
|
# catch buggy <a href='#' onclick='func()'> links
|
2017-08-11 12:59:15 +02:00
|
|
|
from aqt import mw
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2018-10-23 08:47:01 +02:00
|
|
|
if url.matches(QUrl(mw.serverURL()), QUrl.RemoveFragment):
|
2019-04-10 04:44:01 +02:00
|
|
|
print("onclick handler needs to return false")
|
2017-08-11 12:59:15 +02:00
|
|
|
return False
|
2016-06-06 07:50:03 +02:00
|
|
|
# load all other links in browser
|
|
|
|
openLink(url)
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _onCmd(self, str):
|
2018-03-08 08:48:14 +01:00
|
|
|
return self._onBridgeCmd(str)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Main web view
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
class AnkiWebView(QWebEngineView): # type: ignore
|
2020-01-22 01:46:35 +01:00
|
|
|
def __init__(self, parent: Optional[QWidget] = None) -> None:
|
|
|
|
QWebEngineView.__init__(self, parent=parent) # type: ignore
|
2016-07-07 09:23:13 +02:00
|
|
|
self.title = "default"
|
2016-06-06 07:50:03 +02:00
|
|
|
self._page = AnkiWebPage(self._onBridgeCmd)
|
2019-02-25 11:44:27 +01:00
|
|
|
self._page.setBackgroundColor(self._getWindowColor()) # reduce flicker
|
2016-06-06 07:50:03 +02:00
|
|
|
|
2020-01-22 01:46:35 +01:00
|
|
|
# in new code, use .set_bridge_command() instead of setting this directly
|
|
|
|
self.onBridgeCmd: Callable[[str], Any] = self.defaultOnBridgeCmd
|
|
|
|
|
2017-08-01 06:30:04 +02:00
|
|
|
self._domDone = True
|
2020-01-22 01:46:35 +01:00
|
|
|
self._pendingActions: List[Tuple[str, List[Any]]] = []
|
2018-10-12 04:08:19 +02:00
|
|
|
self.requiresCol = True
|
2012-12-21 08:51:59 +01:00
|
|
|
self.setPage(self._page)
|
2017-01-08 11:02:49 +01:00
|
|
|
|
2020-01-22 01:46:35 +01:00
|
|
|
self._page.profile().setHttpCacheType(QWebEngineProfile.NoCache) # type: ignore
|
2016-05-31 10:51:40 +02:00
|
|
|
self.resetHandlers()
|
2012-12-21 08:51:59 +01:00
|
|
|
self.allowDrops = False
|
2018-10-23 08:06:56 +02:00
|
|
|
self._filterSet = False
|
2020-01-22 07:54:18 +01:00
|
|
|
QShortcut( # type: ignore
|
2019-12-23 01:34:10 +01:00
|
|
|
QKeySequence("Esc"),
|
|
|
|
self,
|
|
|
|
context=Qt.WidgetWithChildrenShortcut,
|
|
|
|
activated=self.onEsc,
|
2020-01-22 07:54:18 +01:00
|
|
|
)
|
2017-06-22 08:36:54 +02:00
|
|
|
if isMac:
|
|
|
|
for key, fn in [
|
|
|
|
(QKeySequence.Copy, self.onCopy),
|
|
|
|
(QKeySequence.Paste, self.onPaste),
|
|
|
|
(QKeySequence.Cut, self.onCut),
|
|
|
|
(QKeySequence.SelectAll, self.onSelectAll),
|
|
|
|
]:
|
2020-01-22 07:54:18 +01:00
|
|
|
QShortcut( # type: ignore
|
2019-12-23 01:34:10 +01:00
|
|
|
key, self, context=Qt.WidgetWithChildrenShortcut, activated=fn
|
2020-01-22 07:54:18 +01:00
|
|
|
)
|
|
|
|
QShortcut( # type: ignore
|
2019-12-23 01:34:10 +01:00
|
|
|
QKeySequence("ctrl+shift+v"),
|
|
|
|
self,
|
|
|
|
context=Qt.WidgetWithChildrenShortcut,
|
|
|
|
activated=self.onPaste,
|
2020-01-22 07:54:18 +01:00
|
|
|
)
|
2017-06-22 08:36:54 +02:00
|
|
|
|
2017-06-23 06:34:56 +02:00
|
|
|
def eventFilter(self, obj, evt):
|
|
|
|
# disable pinch to zoom gesture
|
|
|
|
if isinstance(evt, QNativeGestureEvent):
|
|
|
|
return True
|
2018-03-02 02:16:02 +01:00
|
|
|
elif evt.type() == QEvent.MouseButtonRelease:
|
|
|
|
if evt.button() == Qt.MidButton and isLin:
|
|
|
|
self.onMiddleClickPaste()
|
|
|
|
return True
|
|
|
|
return False
|
2017-06-23 06:34:56 +02:00
|
|
|
return False
|
|
|
|
|
2017-06-22 08:36:54 +02:00
|
|
|
def onEsc(self):
|
|
|
|
w = self.parent()
|
|
|
|
while w:
|
|
|
|
if isinstance(w, QDialog) or isinstance(w, QMainWindow):
|
|
|
|
from aqt import mw
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2017-06-22 08:36:54 +02:00
|
|
|
# esc in a child window closes the window
|
|
|
|
if w != mw:
|
|
|
|
w.close()
|
|
|
|
else:
|
|
|
|
# in the main window, removes focus from type in area
|
|
|
|
self.parent().setFocus()
|
|
|
|
break
|
|
|
|
w = w.parent()
|
2016-06-06 09:54:39 +02:00
|
|
|
|
|
|
|
def onCopy(self):
|
|
|
|
self.triggerPageAction(QWebEnginePage.Copy)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2016-06-06 09:54:39 +02:00
|
|
|
def onCut(self):
|
|
|
|
self.triggerPageAction(QWebEnginePage.Cut)
|
|
|
|
|
|
|
|
def onPaste(self):
|
|
|
|
self.triggerPageAction(QWebEnginePage.Paste)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2018-03-02 02:16:02 +01:00
|
|
|
def onMiddleClickPaste(self):
|
|
|
|
self.triggerPageAction(QWebEnginePage.Paste)
|
|
|
|
|
2017-06-22 08:36:54 +02:00
|
|
|
def onSelectAll(self):
|
|
|
|
self.triggerPageAction(QWebEnginePage.SelectAll)
|
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
def contextMenuEvent(self, evt) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
m = QMenu(self)
|
|
|
|
a = m.addAction(_("Copy"))
|
2020-01-15 22:53:12 +01:00
|
|
|
a.triggered.connect(self.onCopy) # type: ignore
|
2020-01-15 08:45:35 +01:00
|
|
|
gui_hooks.webview_will_show_context_menu(self, m)
|
2012-12-21 08:51:59 +01:00
|
|
|
m.popup(QCursor.pos())
|
|
|
|
|
|
|
|
def dropEvent(self, evt):
|
|
|
|
pass
|
|
|
|
|
2016-05-31 10:51:40 +02:00
|
|
|
def setHtml(self, html):
|
2019-04-29 08:41:47 +02:00
|
|
|
# discard any previous pending actions
|
|
|
|
self._pendingActions = []
|
|
|
|
self._domDone = True
|
2017-08-07 08:01:35 +02:00
|
|
|
self._queueAction("setHtml", html)
|
|
|
|
|
|
|
|
def _setHtml(self, html):
|
2016-05-31 10:51:40 +02:00
|
|
|
app = QApplication.instance()
|
|
|
|
oldFocus = app.focusWidget()
|
2017-08-01 06:30:04 +02:00
|
|
|
self._domDone = False
|
2016-05-31 10:51:40 +02:00
|
|
|
self._page.setHtml(html)
|
|
|
|
# work around webengine stealing focus on setHtml()
|
|
|
|
if oldFocus:
|
|
|
|
oldFocus.setFocus()
|
|
|
|
|
2016-06-07 06:27:33 +02:00
|
|
|
def zoomFactor(self):
|
2017-11-27 02:01:15 +01:00
|
|
|
# overridden scale factor?
|
|
|
|
webscale = os.environ.get("ANKI_WEBSCALE")
|
|
|
|
if webscale:
|
|
|
|
return float(webscale)
|
|
|
|
|
2017-08-15 06:47:03 +02:00
|
|
|
if isMac:
|
2017-08-10 07:02:46 +02:00
|
|
|
return 1
|
2016-06-07 06:27:33 +02:00
|
|
|
screen = QApplication.desktop().screen()
|
|
|
|
dpi = screen.logicalDpiX()
|
2017-08-15 06:47:03 +02:00
|
|
|
factor = dpi / 96.0
|
|
|
|
if isLin:
|
|
|
|
factor = max(1, factor)
|
|
|
|
return factor
|
2019-12-17 09:43:32 +01:00
|
|
|
# compensate for qt's integer scaling on windows?
|
|
|
|
if qtminor >= 14:
|
|
|
|
return 1
|
2017-10-05 08:42:46 +02:00
|
|
|
qtIntScale = self._getQtIntScale(screen)
|
2017-08-15 06:47:03 +02:00
|
|
|
desiredScale = factor * qtIntScale
|
|
|
|
newFactor = desiredScale / qtIntScale
|
2017-10-05 08:42:46 +02:00
|
|
|
return max(1, newFactor)
|
|
|
|
|
|
|
|
def _getQtIntScale(self, 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,
|
|
|
|
# and a dpi of 150% = 2x
|
|
|
|
# - a screen with a normal physical dpi of 72 will have a dpi of 32
|
|
|
|
# if the scale factor has been rounded to 2x
|
|
|
|
# - different screens have different physical DPIs (eg 72, 93, 102)
|
|
|
|
# - until a better solution presents itself, assume a physical DPI at
|
|
|
|
# or above 70 is unscaled
|
|
|
|
if screen.physicalDpiX() > 70:
|
|
|
|
return 1
|
|
|
|
elif screen.physicalDpiX() > 35:
|
|
|
|
return 2
|
|
|
|
else:
|
|
|
|
return 3
|
2016-06-07 06:27:33 +02:00
|
|
|
|
2019-02-25 11:44:27 +01:00
|
|
|
def _getWindowColor(self):
|
2020-01-23 06:27:30 +01:00
|
|
|
if theme_manager.night_mode:
|
|
|
|
return theme_manager.qcolor("window-bg")
|
2019-02-25 11:44:27 +01:00
|
|
|
if isMac:
|
|
|
|
# standard palette does not return correct window color on macOS
|
|
|
|
return QColor("#ececec")
|
|
|
|
return self.style().standardPalette().color(QPalette.Window)
|
|
|
|
|
2019-03-04 07:01:10 +01:00
|
|
|
def stdHtml(self, body, css=None, js=None, head=""):
|
|
|
|
if css is None:
|
|
|
|
css = []
|
|
|
|
if js is None:
|
|
|
|
js = ["jquery.js"]
|
2019-04-10 07:16:06 +02:00
|
|
|
|
|
|
|
palette = self.style().standardPalette()
|
|
|
|
color_hl = palette.color(QPalette.Highlight).name()
|
|
|
|
|
2016-07-08 08:17:06 +02:00
|
|
|
if isWin:
|
2019-12-23 01:34:10 +01:00
|
|
|
# T: include a font for your language on Windows, eg: "Segoe UI", "MS Mincho"
|
2019-03-06 06:28:49 +01:00
|
|
|
family = _('"Segoe UI"')
|
2020-01-23 09:05:55 +01:00
|
|
|
widgetspec = "button { font-family:%s; }" % family
|
2019-04-10 07:16:06 +02:00
|
|
|
widgetspec += "\n:focus { outline: 1px solid %s; }" % color_hl
|
2019-12-23 01:34:10 +01:00
|
|
|
fontspec = "font-size:12px;font-family:%s;" % family
|
2016-07-08 08:17:06 +02:00
|
|
|
elif isMac:
|
2019-12-23 01:34:10 +01:00
|
|
|
family = "Helvetica"
|
|
|
|
fontspec = 'font-size:15px;font-family:"%s";' % family
|
2018-08-30 22:07:06 +02:00
|
|
|
widgetspec = """
|
2020-01-23 09:05:55 +01:00
|
|
|
button { -webkit-appearance: none; background: #fff; border: 1px solid #ccc;
|
2016-07-26 02:34:16 +02:00
|
|
|
border-radius:5px; font-family: Helvetica }"""
|
2016-07-08 08:17:06 +02:00
|
|
|
else:
|
|
|
|
family = self.font().family()
|
2018-08-30 22:07:06 +02:00
|
|
|
color_hl_txt = palette.color(QPalette.HighlightedText).name()
|
|
|
|
color_btn = palette.color(QPalette.Button).name()
|
2019-12-23 01:34:10 +01:00
|
|
|
fontspec = 'font-size:14px;font-family:"%s";' % family
|
2018-08-30 22:07:06 +02:00
|
|
|
widgetspec = """
|
|
|
|
/* Buttons */
|
2020-01-26 01:18:24 +01:00
|
|
|
button{
|
|
|
|
background-color: %(color_btn)s;
|
|
|
|
font-family:"%(family)s"; }
|
2018-08-30 22:07:06 +02:00
|
|
|
button:focus{ border-color: %(color_hl)s }
|
|
|
|
button:active, button:active:hover { background-color: %(color_hl)s; color: %(color_hl_txt)s;}
|
|
|
|
/* Input field focus outline */
|
|
|
|
textarea:focus, input:focus, input[type]:focus, .uneditable-input:focus,
|
|
|
|
div[contenteditable="true"]:focus {
|
|
|
|
outline: 0 none;
|
|
|
|
border-color: %(color_hl)s;
|
2019-12-23 01:34:10 +01:00
|
|
|
}""" % {
|
|
|
|
"family": family,
|
|
|
|
"color_btn": color_btn,
|
|
|
|
"color_hl": color_hl,
|
|
|
|
"color_hl_txt": color_hl_txt,
|
|
|
|
}
|
|
|
|
|
|
|
|
csstxt = "\n".join(
|
|
|
|
[self.bundledCSS("webview.css")] + [self.bundledCSS(fname) for fname in css]
|
|
|
|
)
|
|
|
|
jstxt = "\n".join(
|
|
|
|
[self.bundledScript("webview.js")]
|
|
|
|
+ [self.bundledScript(fname) for fname in js]
|
|
|
|
)
|
2017-08-11 12:59:15 +02:00
|
|
|
from aqt import mw
|
2019-12-23 01:34:10 +01:00
|
|
|
|
|
|
|
head = mw.baseHTML() + head + csstxt + jstxt
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-01-23 06:08:10 +01:00
|
|
|
body_class = theme_manager.body_class()
|
|
|
|
|
2018-01-16 07:07:30 +01:00
|
|
|
html = """
|
2017-08-10 11:02:32 +02:00
|
|
|
<!doctype html>
|
|
|
|
<html><head>
|
2018-01-16 07:07:30 +01:00
|
|
|
<title>{}</title>
|
2017-08-10 11:02:32 +02:00
|
|
|
|
|
|
|
<style>
|
2020-01-23 09:36:42 +01:00
|
|
|
body {{ zoom: {}; background: {}; {} }}
|
2018-01-16 07:07:30 +01:00
|
|
|
{}
|
2017-08-10 11:02:32 +02:00
|
|
|
</style>
|
|
|
|
|
2018-01-16 07:07:30 +01:00
|
|
|
{}
|
2012-12-21 08:51:59 +01:00
|
|
|
</head>
|
2017-08-10 11:02:32 +02:00
|
|
|
|
2020-01-23 06:08:10 +01:00
|
|
|
<body class="{}">{}</body>
|
2019-12-23 01:34:10 +01:00
|
|
|
</html>""".format(
|
2020-01-23 09:36:42 +01:00
|
|
|
self.title,
|
|
|
|
self.zoomFactor(),
|
|
|
|
self._getWindowColor().name(),
|
|
|
|
fontspec,
|
|
|
|
widgetspec,
|
|
|
|
head,
|
|
|
|
body_class,
|
|
|
|
body,
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
|
|
|
# print(html)
|
2017-06-22 10:01:01 +02:00
|
|
|
self.setHtml(html)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2017-07-28 08:19:06 +02:00
|
|
|
def webBundlePath(self, path):
|
|
|
|
from aqt import mw
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2018-10-11 07:45:55 +02:00
|
|
|
return "http://127.0.0.1:%d/_anki/%s" % (mw.mediaServer.getPort(), path)
|
2017-07-28 08:19:06 +02:00
|
|
|
|
|
|
|
def bundledScript(self, fname):
|
|
|
|
return '<script src="%s"></script>' % self.webBundlePath(fname)
|
|
|
|
|
|
|
|
def bundledCSS(self, fname):
|
2019-12-23 01:34:10 +01:00
|
|
|
return '<link rel="stylesheet" type="text/css" href="%s">' % self.webBundlePath(
|
|
|
|
fname
|
|
|
|
)
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def eval(self, js):
|
2017-08-02 07:39:49 +02:00
|
|
|
self.evalWithCallback(js, None)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2016-06-07 06:27:33 +02:00
|
|
|
def evalWithCallback(self, js, cb):
|
2017-08-07 08:01:35 +02:00
|
|
|
self._queueAction("eval", js, cb)
|
|
|
|
|
|
|
|
def _evalWithCallback(self, js, cb):
|
|
|
|
if cb:
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2018-09-28 08:27:41 +02:00
|
|
|
def handler(val):
|
|
|
|
if self._shouldIgnoreWebEvent():
|
|
|
|
print("ignored late js callback", cb)
|
|
|
|
return
|
|
|
|
cb(val)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2018-09-28 08:27:41 +02:00
|
|
|
self.page().runJavaScript(js, handler)
|
2017-08-02 07:39:49 +02:00
|
|
|
else:
|
2017-08-07 08:01:35 +02:00
|
|
|
self.page().runJavaScript(js)
|
|
|
|
|
|
|
|
def _queueAction(self, name, *args):
|
|
|
|
self._pendingActions.append((name, args))
|
|
|
|
self._maybeRunActions()
|
|
|
|
|
|
|
|
def _maybeRunActions(self):
|
|
|
|
while self._pendingActions and self._domDone:
|
|
|
|
name, args = self._pendingActions.pop(0)
|
|
|
|
|
|
|
|
if name == "eval":
|
|
|
|
self._evalWithCallback(*args)
|
|
|
|
elif name == "setHtml":
|
|
|
|
self._setHtml(*args)
|
|
|
|
else:
|
|
|
|
raise Exception("unknown action: {}".format(name))
|
2016-06-07 06:27:33 +02:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def _openLinksExternally(self, url):
|
|
|
|
openLink(url)
|
|
|
|
|
2018-09-28 08:27:41 +02:00
|
|
|
def _shouldIgnoreWebEvent(self):
|
|
|
|
# async web events may be received after the profile has been closed
|
|
|
|
# or the underlying webview has been deleted
|
|
|
|
from aqt import mw
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2018-10-12 04:08:19 +02:00
|
|
|
if sip.isdeleted(self):
|
|
|
|
return True
|
|
|
|
if not mw.col and self.requiresCol:
|
|
|
|
return True
|
|
|
|
return False
|
2018-09-28 08:27:41 +02:00
|
|
|
|
2020-01-22 01:46:35 +01:00
|
|
|
def _onBridgeCmd(self, cmd: str) -> Any:
|
2018-09-28 08:27:41 +02:00
|
|
|
if self._shouldIgnoreWebEvent():
|
|
|
|
print("ignored late bridge cmd", cmd)
|
2017-08-07 05:12:47 +02:00
|
|
|
return
|
|
|
|
|
2019-04-21 11:12:04 +02:00
|
|
|
if not self._filterSet:
|
|
|
|
self.focusProxy().installEventFilter(self)
|
|
|
|
self._filterSet = True
|
|
|
|
|
2016-06-06 07:50:03 +02:00
|
|
|
if cmd == "domDone":
|
2017-08-01 06:30:04 +02:00
|
|
|
self._domDone = True
|
2017-08-07 08:01:35 +02:00
|
|
|
self._maybeRunActions()
|
2016-06-06 07:50:03 +02:00
|
|
|
else:
|
2020-01-22 01:46:35 +01:00
|
|
|
handled, result = gui_hooks.webview_did_receive_js_message(
|
2020-02-08 23:59:29 +01:00
|
|
|
(False, None), cmd, self._bridge_context
|
2020-01-22 01:46:35 +01:00
|
|
|
)
|
|
|
|
if handled:
|
|
|
|
return result
|
|
|
|
else:
|
|
|
|
return self.onBridgeCmd(cmd)
|
2016-05-31 10:51:40 +02:00
|
|
|
|
2020-01-22 01:46:35 +01:00
|
|
|
def defaultOnBridgeCmd(self, cmd: str) -> Any:
|
2016-06-06 07:50:03 +02:00
|
|
|
print("unhandled bridge cmd:", cmd)
|
2016-05-31 10:51:40 +02:00
|
|
|
|
2020-01-22 01:46:35 +01:00
|
|
|
# legacy
|
2016-05-31 10:51:40 +02:00
|
|
|
def resetHandlers(self):
|
2016-06-06 07:50:03 +02:00
|
|
|
self.onBridgeCmd = self.defaultOnBridgeCmd
|
2020-02-08 23:59:29 +01:00
|
|
|
self._bridge_context = None
|
2017-08-02 08:22:54 +02:00
|
|
|
|
|
|
|
def adjustHeightToFit(self):
|
2018-10-29 12:08:16 +01:00
|
|
|
self.evalWithCallback("$(document.body).height()", self._onHeight)
|
2017-08-02 08:22:54 +02:00
|
|
|
|
|
|
|
def _onHeight(self, qvar):
|
2019-12-24 11:15:47 +01:00
|
|
|
from aqt import mw
|
2019-12-24 11:23:36 +01:00
|
|
|
|
2018-06-29 09:17:13 +02:00
|
|
|
if qvar is None:
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2019-06-27 01:49:05 +02:00
|
|
|
mw.progress.timer(1000, mw.reset, False)
|
2018-06-29 09:17:13 +02:00
|
|
|
return
|
|
|
|
|
2019-12-24 11:15:47 +01:00
|
|
|
scaleFactor = self.zoomFactor()
|
|
|
|
if scaleFactor == 1:
|
|
|
|
scaleFactor = mw.pm.uiScale()
|
|
|
|
|
|
|
|
height = math.ceil(qvar * scaleFactor)
|
2017-08-02 08:22:54 +02:00
|
|
|
self.setFixedHeight(height)
|
2020-01-22 01:46:35 +01:00
|
|
|
|
2020-02-08 23:59:29 +01:00
|
|
|
def set_bridge_command(self, func: Callable[[str], Any], context: Any) -> None:
|
2020-01-22 01:46:35 +01:00
|
|
|
"""Set a handler for pycmd() messages received from Javascript.
|
|
|
|
|
2020-02-08 23:59:29 +01:00
|
|
|
Context is the object calling this routine, eg an instance of
|
|
|
|
aqt.reviewer.Reviewer or aqt.deckbrowser.DeckBrowser."""
|
2020-01-22 01:46:35 +01:00
|
|
|
self.onBridgeCmd = func
|
2020-02-08 23:59:29 +01:00
|
|
|
self._bridge_context = context
|