anki/qt/aqt/webview.py

547 lines
18 KiB
Python
Raw Normal View History

2019-02-05 04:59:03 +01:00
# Copyright: Ankitects Pty Ltd and contributors
# -*- coding: utf-8 -*-
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import dataclasses
import json
import math
2019-12-20 10:19:03 +01:00
import sys
2020-02-27 03:08:38 +01:00
from typing import Any, Callable, List, Optional, Sequence, Tuple
2019-12-20 10:19:03 +01:00
from anki.lang import _
from anki.utils import isLin, isMac, isWin
from aqt import gui_hooks
from aqt.qt import *
from aqt.theme import theme_manager
from aqt.utils import openLink
# Page for debug messages
##########################################################################
2019-12-23 01:34:10 +01:00
class AnkiWebPage(QWebEnginePage): # type: ignore
def __init__(self, onBridgeCmd):
QWebEnginePage.__init__(self)
self._onBridgeCmd = onBridgeCmd
self._setupBridge()
def _setupBridge(self):
class Bridge(QObject):
@pyqtSlot(str, result=str)
def cmd(self, str):
return json.dumps(self.onCmd(str))
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")
assert js.open(QIODevice.ReadOnly)
2019-12-23 01:34:10 +01:00
js = bytes(js.readAll()).decode("utf-8")
script = QWebEngineScript()
2019-12-23 01:34:10 +01:00
script.setSourceCode(
js
+ """
var pycmd;
new QWebChannel(qt.webChannelTransport, function(channel) {
pycmd = function (arg, cb) {
var resultCB = function (res) {
// pass result back to user-provided callback
if (cb) {
cb(JSON.parse(res));
}
}
channel.objects.py.cmd(arg, resultCB);
return false;
}
pycmd("domDone");
});
2019-12-23 01:34:10 +01:00
"""
)
script.setWorldId(QWebEngineScript.MainWorld)
script.setInjectionPoint(QWebEngineScript.DocumentReady)
script.setRunsOnSubFrames(False)
self.profile().scripts().insert(script)
def javaScriptConsoleMessage(self, level, msg, line, srcID):
2017-08-06 05:10:51 +02:00
# not translated because console usually not visible,
# and may only accept ascii text
srcID = srcID.replace(AnkiWebView.webBundlePath("/"), "")
if level == QWebEnginePage.InfoMessageLevel:
level = "info"
elif level == QWebEnginePage.WarningMessageLevel:
level = "warning"
elif level == QWebEnginePage.ErrorMessageLevel:
level = "error"
buf = "JS %(t)s /%(f)s:%(a)d %(b)s" % dict(
t=level, a=line, f=srcID, b=msg + "\n"
)
# ensure we don't try to write characters the terminal can't handle
buf = buf.encode(sys.stdout.encoding, "backslashreplace").decode(
sys.stdout.encoding
2019-12-23 01:34:10 +01:00
)
# output to stdout because it may raise error messages on the anki GUI
# https://github.com/ankitects/anki/pull/560
sys.stdout.write(buf)
def acceptNavigationRequest(self, url, navType, isMainFrame):
if not isMainFrame:
return True
# data: links generated by setHtml()
if url.scheme() == "data":
return True
# catch buggy <a href='#' onclick='func()'> links
from aqt import mw
2019-12-23 01:34:10 +01:00
if url.matches(QUrl(mw.serverURL()), QUrl.RemoveFragment):
print("onclick handler needs to return false")
return False
# load all other links in browser
openLink(url)
return False
def _onCmd(self, str):
return self._onBridgeCmd(str)
2019-12-23 01:34:10 +01:00
# Add-ons
##########################################################################
@dataclasses.dataclass
class WebContent:
"""Stores all dynamically modified content that a particular web view
will be populated with.
Attributes:
body {str} -- HTML body
head {str} -- HTML head
css {List[str]} -- List of media server subpaths,
each pointing to a CSS file
js {List[str]} -- List of media server subpaths,
each pointing to a JS file
Important Notes:
- When modifying the attributes specified above, please make sure your
changes only perform the minimum requried edits to make your add-on work.
You should avoid overwriting or interfering with existing data as much
as possible, instead opting to append your own changes, e.g.:
def on_webview_will_set_content(web_content: WebContent, context):
web_content.body += "<my_html>"
web_content.head += "<my_head>"
- The paths specified in `css` and `js` need to be accessible by Anki's
media server. All list members without a specified subpath are assumed
to be located under `/_anki`, which is the media server subpath used
for all web assets shipped with Anki.
Add-ons may expose their own web assets by utilizing
aqt.addons.AddonManager.setWebExports(). Web exports registered
in this manner may then be accessed under the `/_addons` subpath.
E.g., to allow access to a `my-addon.js` and `my-addon.css` residing
in a "web" subfolder in your add-on package, first register the
corresponding web export:
> from aqt import mw
> mw.addonManager.setWebExports(__name__, r"web/.*(css|js)")
Then append the subpaths to the corresponding web_content fields
within a function subscribing to gui_hooks.webview_will_set_content:
def on_webview_will_set_content(web_content: WebContent, context):
addon_package = mw.addonManager.addonFromModule(__name__)
web_content.css.append(
f"/_addons/{addon_package}/web/my-addon.css")
web_content.js.append(
f"/_addons/{addon_package}/web/my-addon.js")
Note that '/' will also match the os specific path separator.
"""
body: str = ""
head: str = ""
css: List[str] = dataclasses.field(default_factory=lambda: [])
js: List[str] = dataclasses.field(default_factory=lambda: [])
# Main web view
##########################################################################
2019-12-23 01:34:10 +01:00
class AnkiWebView(QWebEngineView): # type: ignore
def __init__(
self, parent: Optional[QWidget] = None, title: str = "default"
) -> None:
QWebEngineView.__init__(self, parent=parent) # type: ignore
self.title = title
self._page = AnkiWebPage(self._onBridgeCmd)
self._page.setBackgroundColor(self._getWindowColor()) # reduce flicker
# in new code, use .set_bridge_command() instead of setting this directly
self.onBridgeCmd: Callable[[str], Any] = self.defaultOnBridgeCmd
self._domDone = True
2020-02-27 03:08:38 +01:00
self._pendingActions: List[Tuple[str, Sequence[Any]]] = []
self.requiresCol = True
self.setPage(self._page)
self._page.profile().setHttpCacheType(QWebEngineProfile.NoCache) # type: ignore
self.resetHandlers()
self.allowDrops = False
self._filterSet = False
QShortcut( # type: ignore
2019-12-23 01:34:10 +01:00
QKeySequence("Esc"),
self,
context=Qt.WidgetWithChildrenShortcut,
activated=self.onEsc,
)
if isMac:
for key, fn in [
(QKeySequence.Copy, self.onCopy),
(QKeySequence.Paste, self.onPaste),
(QKeySequence.Cut, self.onCut),
(QKeySequence.SelectAll, self.onSelectAll),
]:
QShortcut( # type: ignore
2019-12-23 01:34:10 +01:00
key, self, context=Qt.WidgetWithChildrenShortcut, activated=fn
)
QShortcut( # type: ignore
2019-12-23 01:34:10 +01:00
QKeySequence("ctrl+shift+v"),
self,
context=Qt.WidgetWithChildrenShortcut,
activated=self.onPaste,
)
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
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
# 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()
def onCopy(self):
self.triggerPageAction(QWebEnginePage.Copy)
def onCut(self):
self.triggerPageAction(QWebEnginePage.Cut)
def onPaste(self):
self.triggerPageAction(QWebEnginePage.Paste)
2018-03-02 02:16:02 +01:00
def onMiddleClickPaste(self):
self.triggerPageAction(QWebEnginePage.Paste)
def onSelectAll(self):
self.triggerPageAction(QWebEnginePage.SelectAll)
def contextMenuEvent(self, evt) -> None:
m = QMenu(self)
a = m.addAction(_("Copy"))
a.triggered.connect(self.onCopy) # type: ignore
gui_hooks.webview_will_show_context_menu(self, m)
m.popup(QCursor.pos())
def dropEvent(self, evt):
pass
2020-02-27 03:08:38 +01:00
def setHtml(self, html: str) -> None:
# discard any previous pending actions
self._pendingActions = []
self._domDone = True
self._queueAction("setHtml", html)
2020-02-27 03:08:38 +01:00
def _setHtml(self, html: str) -> None:
app = QApplication.instance()
oldFocus = app.focusWidget()
self._domDone = False
self._page.setHtml(html)
# work around webengine stealing focus on setHtml()
if oldFocus:
oldFocus.setFocus()
2020-02-27 03:08:38 +01:00
def zoomFactor(self) -> float:
# overridden scale factor?
webscale = os.environ.get("ANKI_WEBSCALE")
if webscale:
return float(webscale)
if isMac:
return 1
2016-06-07 06:27:33 +02:00
screen = QApplication.desktop().screen()
dpi = screen.logicalDpiX()
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
qtIntScale = self._getQtIntScale(screen)
desiredScale = factor * qtIntScale
newFactor = desiredScale / qtIntScale
return max(1, newFactor)
2020-02-27 03:08:38 +01:00
def _getQtIntScale(self, screen) -> int:
# 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
def _getWindowColor(self):
2020-01-23 06:27:30 +01:00
if theme_manager.night_mode:
return theme_manager.qcolor("window-bg")
if isMac:
# standard palette does not return correct window color on macOS
return QColor("#ececec")
return self.style().standardPalette().color(QPalette.Window)
def stdHtml(
self,
body: str,
css: Optional[List[str]] = None,
js: Optional[List[str]] = None,
head: str = "",
context: Optional[Any] = None,
):
web_content = WebContent(
body=body,
head=head,
js=["webview.js"] + (["jquery.js"] if js is None else js),
css=["webview.css"] + ([] if css is None else css),
)
gui_hooks.webview_will_set_content(web_content, context)
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"
family = _('"Segoe UI"')
widgetspec = "button { font-family:%s; }" % family
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
widgetspec = """
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()
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
widgetspec = """
/* Buttons */
button{
background-color: %(color_btn)s;
font-family:"%(family)s"; }
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(fname) for fname in web_content.css)
jstxt = "\n".join(self.bundledScript(fname) for fname in web_content.js)
from aqt import mw
2019-12-23 01:34:10 +01:00
head = mw.baseHTML() + csstxt + jstxt + web_content.head
body_class = theme_manager.body_class()
html = """
<!doctype html>
<html><head>
<title>{}</title>
<style>
2020-01-23 09:36:42 +01:00
body {{ zoom: {}; background: {}; {} }}
{}
</style>
{}
</head>
<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,
web_content.body,
2019-12-23 01:34:10 +01:00
)
# print(html)
self.setHtml(html)
@classmethod
def webBundlePath(cls, path: str) -> str:
from aqt import mw
2019-12-23 01:34:10 +01:00
if path.startswith("/"):
subpath = ""
else:
subpath = "/_anki/"
return f"http://127.0.0.1:{mw.mediaServer.getPort()}{subpath}{path}"
def bundledScript(self, fname: str) -> str:
return '<script src="%s"></script>' % self.webBundlePath(fname)
def bundledCSS(self, fname: str) -> str:
2019-12-23 01:34:10 +01:00
return '<link rel="stylesheet" type="text/css" href="%s">' % self.webBundlePath(
fname
)
2020-02-27 03:08:38 +01:00
def eval(self, js: str) -> None:
self.evalWithCallback(js, None)
2020-02-27 03:08:38 +01:00
def evalWithCallback(self, js: str, cb: Callable) -> None:
self._queueAction("eval", js, cb)
2020-02-27 03:08:38 +01:00
def _evalWithCallback(self, js: str, cb: Callable[[Any], Any]) -> None:
if cb:
2019-12-23 01:34:10 +01:00
def handler(val):
if self._shouldIgnoreWebEvent():
print("ignored late js callback", cb)
return
cb(val)
2019-12-23 01:34:10 +01:00
self.page().runJavaScript(js, handler)
else:
self.page().runJavaScript(js)
2020-02-27 03:08:38 +01:00
def _queueAction(self, name: str, *args: Any) -> None:
self._pendingActions.append((name, args))
self._maybeRunActions()
2020-02-27 03:08:38 +01:00
def _maybeRunActions(self) -> None:
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
2020-02-27 03:08:38 +01:00
def _openLinksExternally(self, url: str) -> None:
openLink(url)
2020-02-27 03:08:38 +01:00
def _shouldIgnoreWebEvent(self) -> bool:
# 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
if sip.isdeleted(self):
return True
if not mw.col and self.requiresCol:
return True
return False
def _onBridgeCmd(self, cmd: str) -> Any:
if self._shouldIgnoreWebEvent():
print("ignored late bridge cmd", cmd)
return
if not self._filterSet:
self.focusProxy().installEventFilter(self)
self._filterSet = True
if cmd == "domDone":
self._domDone = True
self._maybeRunActions()
else:
handled, result = gui_hooks.webview_did_receive_js_message(
(False, None), cmd, self._bridge_context
)
if handled:
return result
else:
return self.onBridgeCmd(cmd)
2020-02-27 03:08:38 +01:00
def defaultOnBridgeCmd(self, cmd: str) -> None:
print("unhandled bridge cmd:", cmd)
# legacy
2020-02-27 03:08:38 +01:00
def resetHandlers(self) -> None:
self.onBridgeCmd = self.defaultOnBridgeCmd
self._bridge_context = None
2020-02-27 03:08:38 +01:00
def adjustHeightToFit(self) -> None:
self.evalWithCallback("$(document.body).height()", self._onHeight)
2020-02-27 03:08:38 +01:00
def _onHeight(self, qvar: Optional[int]) -> None:
from aqt import mw
if qvar is None:
2019-12-23 01:34:10 +01:00
mw.progress.timer(1000, mw.reset, False)
return
scaleFactor = self.zoomFactor()
if scaleFactor == 1:
scaleFactor = mw.pm.uiScale()
height = math.ceil(qvar * scaleFactor)
self.setFixedHeight(height)
def set_bridge_command(self, func: Callable[[str], Any], context: Any) -> None:
"""Set a handler for pycmd() messages received from Javascript.
Context is the object calling this routine, eg an instance of
aqt.reviewer.Reviewer or aqt.deckbrowser.DeckBrowser."""
self.onBridgeCmd = func
self._bridge_context = context