2019-02-05 04:59:03 +01:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
2012-12-21 08:51:59 +01:00
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
2021-03-17 05:51:59 +01:00
|
|
|
|
2020-02-12 22:00:13 +01:00
|
|
|
import dataclasses
|
2018-03-08 08:48:14 +01:00
|
|
|
import json
|
2020-04-15 21:21:47 +02:00
|
|
|
import re
|
2019-12-20 10:19:03 +01:00
|
|
|
import sys
|
2021-02-02 14:30:53 +01:00
|
|
|
from typing import Any, Callable, List, Optional, Sequence, Tuple, cast
|
2019-12-20 10:19:03 +01:00
|
|
|
|
2020-07-21 03:52:25 +02:00
|
|
|
import anki
|
2020-11-18 02:53:33 +01:00
|
|
|
from anki.lang import is_rtl
|
2019-12-20 10:19:03 +01:00
|
|
|
from anki.utils import isLin, isMac, isWin
|
2021-02-05 09:50:01 +01:00
|
|
|
from aqt import colors, 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
|
2021-03-26 04:48:26 +01:00
|
|
|
from aqt.utils import openLink, showInfo, tr
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-04-17 08:42:57 +02:00
|
|
|
serverbaseurl = re.compile(r"^.+:\/\/[^\/]+")
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Page for debug messages
|
|
|
|
##########################################################################
|
|
|
|
|
2021-02-02 14:30:53 +01:00
|
|
|
BridgeCommandHandler = Callable[[str], Any]
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-06-02 09:56:15 +02:00
|
|
|
class AnkiWebPage(QWebEnginePage):
|
2021-02-02 14:30:53 +01:00
|
|
|
def __init__(self, onBridgeCmd: BridgeCommandHandler) -> None:
|
2016-05-31 10:51:40 +02:00
|
|
|
QWebEnginePage.__init__(self)
|
2016-06-06 07:50:03 +02:00
|
|
|
self._onBridgeCmd = onBridgeCmd
|
|
|
|
self._setupBridge()
|
2020-06-29 05:49:35 +02:00
|
|
|
self.open_links_externally = True
|
2016-06-06 07:50:03 +02:00
|
|
|
|
2020-07-31 00:49:05 +02:00
|
|
|
def _setupBridge(self) -> None:
|
2016-06-06 07:50:03 +02:00
|
|
|
class Bridge(QObject):
|
2021-03-17 05:51:59 +01:00
|
|
|
def __init__(self, bridge_handler: Callable[[str], Any]) -> None:
|
|
|
|
super().__init__()
|
|
|
|
self.onCmd = bridge_handler
|
|
|
|
|
2020-07-31 00:49:05 +02:00
|
|
|
@pyqtSlot(str, result=str) # type: ignore
|
2021-02-02 14:30:53 +01:00
|
|
|
def cmd(self, str: str) -> Any:
|
2018-03-08 08:48:14 +01:00
|
|
|
return json.dumps(self.onCmd(str))
|
2016-06-06 07:50:03 +02:00
|
|
|
|
2021-03-17 05:51:59 +01:00
|
|
|
self._bridge = Bridge(self._onCmd)
|
2016-06-06 07:50:03 +02:00
|
|
|
|
|
|
|
self._channel = QWebChannel(self)
|
|
|
|
self._channel.registerObject("py", self._bridge)
|
|
|
|
self.setWebChannel(self._channel)
|
|
|
|
|
2020-05-10 02:58:42 +02:00
|
|
|
qwebchannel = ":/qtwebchannel/qwebchannel.js"
|
|
|
|
jsfile = QFile(qwebchannel)
|
|
|
|
if not jsfile.open(QIODevice.ReadOnly):
|
|
|
|
print(f"Error opening '{qwebchannel}': {jsfile.error()}", file=sys.stderr)
|
2021-03-17 05:51:59 +01:00
|
|
|
jstext = bytes(cast(bytes, jsfile.readAll())).decode("utf-8")
|
2020-05-10 02:58:42 +02:00
|
|
|
jsfile.close()
|
2016-06-06 07:50:03 +02:00
|
|
|
|
|
|
|
script = QWebEngineScript()
|
2019-12-23 01:34:10 +01:00
|
|
|
script.setSourceCode(
|
2020-05-10 02:58:42 +02:00
|
|
|
jstext
|
2019-12-23 01:34:10 +01:00
|
|
|
+ """
|
2020-08-27 13:46:34 +02:00
|
|
|
var pycmd, bridgeCommand;
|
2016-06-06 07:50:03 +02:00
|
|
|
new QWebChannel(qt.webChannelTransport, function(channel) {
|
2020-08-27 13:46:34 +02:00
|
|
|
bridgeCommand = pycmd = function (arg, cb) {
|
2018-03-08 08:48:14 +01:00
|
|
|
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
|
|
|
|
2021-02-02 14:30:53 +01:00
|
|
|
def javaScriptConsoleMessage(
|
|
|
|
self,
|
|
|
|
level: QWebEnginePage.JavaScriptConsoleMessageLevel,
|
|
|
|
msg: str,
|
|
|
|
line: int,
|
|
|
|
srcID: str,
|
|
|
|
) -> None:
|
2017-08-06 05:10:51 +02:00
|
|
|
# not translated because console usually not visible,
|
|
|
|
# and may only accept ascii text
|
2020-04-17 02:49:48 +02:00
|
|
|
if srcID.startswith("data"):
|
|
|
|
srcID = ""
|
|
|
|
else:
|
2020-04-17 08:42:57 +02:00
|
|
|
srcID = serverbaseurl.sub("", srcID[:80], 1)
|
2020-04-15 09:00:32 +02:00
|
|
|
if level == QWebEnginePage.InfoMessageLevel:
|
2021-02-02 14:30:53 +01:00
|
|
|
level_str = "info"
|
2020-04-15 09:00:32 +02:00
|
|
|
elif level == QWebEnginePage.WarningMessageLevel:
|
2021-02-02 14:30:53 +01:00
|
|
|
level_str = "warning"
|
2020-04-15 09:00:32 +02:00
|
|
|
elif level == QWebEnginePage.ErrorMessageLevel:
|
2021-02-02 14:30:53 +01:00
|
|
|
level_str = "error"
|
|
|
|
else:
|
|
|
|
level_str = str(level)
|
2020-04-15 21:21:47 +02:00
|
|
|
buf = "JS %(t)s %(f)s:%(a)d %(b)s" % dict(
|
2021-02-11 01:09:06 +01:00
|
|
|
t=level_str, a=line, f=srcID, b=f"{msg}\n"
|
2020-04-15 09:00:32 +02:00
|
|
|
)
|
2020-11-18 12:34:44 +01:00
|
|
|
if "MathJax localStorage" in buf:
|
|
|
|
# silence localStorage noise
|
|
|
|
return
|
2019-05-17 00:43:25 +02:00
|
|
|
# ensure we don't try to write characters the terminal can't handle
|
2020-04-15 21:04:57 +02:00
|
|
|
buf = buf.encode(sys.stdout.encoding, "backslashreplace").decode(
|
|
|
|
sys.stdout.encoding
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2020-04-15 21:04:57 +02: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)
|
2016-05-31 10:51:40 +02:00
|
|
|
|
2021-02-02 14:30:53 +01:00
|
|
|
def acceptNavigationRequest(
|
|
|
|
self, url: QUrl, navType: Any, isMainFrame: bool
|
|
|
|
) -> bool:
|
2021-02-06 06:03:21 +01:00
|
|
|
if not self.open_links_externally or "_anki/pages" in url.path():
|
2020-06-29 05:49:35 +02:00
|
|
|
return super().acceptNavigationRequest(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
|
|
|
|
2021-02-02 14:30:53 +01:00
|
|
|
if url.matches(QUrl(mw.serverURL()), cast(Any, 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
|
|
|
|
|
2021-03-17 05:51:59 +01:00
|
|
|
def _onCmd(self, str: str) -> Any:
|
2018-03-08 08:48:14 +01:00
|
|
|
return self._onBridgeCmd(str)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def javaScriptAlert(self, url: QUrl, text: str) -> None:
|
2020-08-06 01:50:12 +02:00
|
|
|
showInfo(text)
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2020-02-12 22:00:13 +01:00
|
|
|
# Add-ons
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
|
|
|
|
@dataclasses.dataclass
|
|
|
|
class WebContent:
|
2020-02-15 15:03:58 +01:00
|
|
|
"""Stores all dynamically modified content that a particular web view
|
|
|
|
will be populated with.
|
2020-02-12 22:00:13 +01:00
|
|
|
|
|
|
|
Attributes:
|
|
|
|
body {str} -- HTML body
|
|
|
|
head {str} -- HTML head
|
2020-02-15 15:03:58 +01:00
|
|
|
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
|
2020-02-12 22:00:13 +01:00
|
|
|
|
|
|
|
Important Notes:
|
2020-02-15 15:03:58 +01:00
|
|
|
- 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.:
|
2020-08-31 05:29:28 +02:00
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def on_webview_will_set_content(web_content: WebContent, context) -> None:
|
2020-02-12 22:00:13 +01:00
|
|
|
web_content.body += "<my_html>"
|
|
|
|
web_content.head += "<my_head>"
|
|
|
|
|
2020-02-15 15:03:58 +01:00
|
|
|
- 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.
|
2020-08-31 05:29:28 +02:00
|
|
|
|
2020-02-15 15:03:58 +01:00
|
|
|
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.
|
2020-08-31 05:29:28 +02:00
|
|
|
|
2020-02-15 15:03:58 +01:00
|
|
|
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:
|
2020-08-31 05:29:28 +02:00
|
|
|
|
2020-02-12 22:00:13 +01:00
|
|
|
> from aqt import mw
|
|
|
|
> mw.addonManager.setWebExports(__name__, r"web/.*(css|js)")
|
2020-08-31 05:29:28 +02:00
|
|
|
|
2020-02-15 15:03:58 +01:00
|
|
|
Then append the subpaths to the corresponding web_content fields
|
|
|
|
within a function subscribing to gui_hooks.webview_will_set_content:
|
2020-08-31 05:29:28 +02:00
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def on_webview_will_set_content(web_content: WebContent, context) -> None:
|
2020-02-12 22:00:13 +01:00
|
|
|
addon_package = mw.addonManager.addonFromModule(__name__)
|
|
|
|
web_content.css.append(
|
2020-02-15 15:03:58 +01:00
|
|
|
f"/_addons/{addon_package}/web/my-addon.css")
|
2020-02-12 22:00:13 +01:00
|
|
|
web_content.js.append(
|
2020-02-15 15:03:58 +01:00
|
|
|
f"/_addons/{addon_package}/web/my-addon.js")
|
2020-08-31 05:29:28 +02:00
|
|
|
|
2020-02-29 04:36:10 +01:00
|
|
|
Note that '/' will also match the os specific path separator.
|
2020-02-12 22:00:13 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
body: str = ""
|
|
|
|
head: str = ""
|
|
|
|
css: List[str] = dataclasses.field(default_factory=lambda: [])
|
|
|
|
js: List[str] = dataclasses.field(default_factory=lambda: [])
|
|
|
|
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Main web view
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
|
2020-06-02 09:56:15 +02:00
|
|
|
class AnkiWebView(QWebEngineView):
|
2020-02-12 21:03:11 +01:00
|
|
|
def __init__(
|
2021-07-03 01:54:10 +02:00
|
|
|
self,
|
|
|
|
parent: Optional[QWidget] = None,
|
|
|
|
title: str = "default",
|
|
|
|
*,
|
|
|
|
mac_default_shortcuts: bool = True,
|
2020-02-12 21:03:11 +01:00
|
|
|
) -> None:
|
2020-06-02 09:56:15 +02:00
|
|
|
QWebEngineView.__init__(self, parent=parent)
|
2021-03-26 07:06:02 +01:00
|
|
|
self.set_title(title)
|
2016-06-06 07:50:03 +02:00
|
|
|
self._page = AnkiWebPage(self._onBridgeCmd)
|
2021-06-19 20:04:24 +02:00
|
|
|
# reduce flicker
|
|
|
|
self._page.setBackgroundColor(
|
|
|
|
self.get_window_bg_color(theme_manager.night_mode)
|
|
|
|
)
|
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-02-27 03:08:38 +01:00
|
|
|
self._pendingActions: List[Tuple[str, Sequence[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-06-02 09:56:15 +02:00
|
|
|
self._page.profile().setHttpCacheType(QWebEngineProfile.NoCache)
|
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
|
|
|
)
|
2021-07-02 17:30:49 +02:00
|
|
|
if isMac and mac_default_shortcuts:
|
2017-06-22 08:36:54 +02:00
|
|
|
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
|
|
|
)
|
2021-07-02 17:30:49 +02:00
|
|
|
|
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
|
|
|
|
2021-03-26 07:06:02 +01:00
|
|
|
def set_title(self, title: str) -> None:
|
|
|
|
self.title = title # type: ignore[assignment]
|
|
|
|
|
2020-05-26 10:08:55 +02:00
|
|
|
def eventFilter(self, obj: QObject, evt: QEvent) -> bool:
|
2017-06-23 06:34:56 +02:00
|
|
|
# disable pinch to zoom gesture
|
|
|
|
if isinstance(evt, QNativeGestureEvent):
|
|
|
|
return True
|
2021-03-17 05:51:59 +01:00
|
|
|
elif isinstance(evt, QMouseEvent) and evt.type() == QEvent.MouseButtonRelease:
|
2018-03-02 02:16:02 +01:00
|
|
|
if evt.button() == Qt.MidButton and isLin:
|
|
|
|
self.onMiddleClickPaste()
|
|
|
|
return True
|
|
|
|
return False
|
2017-06-23 06:34:56 +02:00
|
|
|
return False
|
|
|
|
|
2020-06-29 05:49:35 +02:00
|
|
|
def set_open_links_externally(self, enable: bool) -> None:
|
|
|
|
self._page.open_links_externally = enable
|
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def onEsc(self) -> None:
|
2017-06-22 08:36:54 +02:00
|
|
|
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
|
2021-03-17 05:51:59 +01:00
|
|
|
parent = self.parent()
|
|
|
|
assert isinstance(parent, QWidget)
|
|
|
|
parent.setFocus()
|
2017-06-22 08:36:54 +02:00
|
|
|
break
|
|
|
|
w = w.parent()
|
2016-06-06 09:54:39 +02:00
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def onCopy(self) -> None:
|
2020-09-22 05:32:37 +02:00
|
|
|
if not self.selectedText():
|
2021-06-11 11:58:20 +02:00
|
|
|
if not isMac:
|
|
|
|
# crashes on a mac when clicking in the main window and pressing cmd+c
|
|
|
|
ctx = self._page.contextMenuData()
|
|
|
|
if ctx and ctx.mediaType() == QWebEngineContextMenuData.MediaTypeImage:
|
|
|
|
self.triggerPageAction(QWebEnginePage.CopyImageToClipboard)
|
2020-09-22 05:32:37 +02:00
|
|
|
else:
|
|
|
|
self.triggerPageAction(QWebEnginePage.Copy)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def onCut(self) -> None:
|
2016-06-06 09:54:39 +02:00
|
|
|
self.triggerPageAction(QWebEnginePage.Cut)
|
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def onPaste(self) -> None:
|
2016-06-06 09:54:39 +02:00
|
|
|
self.triggerPageAction(QWebEnginePage.Paste)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def onMiddleClickPaste(self) -> None:
|
2018-03-02 02:16:02 +01:00
|
|
|
self.triggerPageAction(QWebEnginePage.Paste)
|
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def onSelectAll(self) -> None:
|
2017-06-22 08:36:54 +02:00
|
|
|
self.triggerPageAction(QWebEnginePage.SelectAll)
|
|
|
|
|
2020-05-26 10:12:39 +02:00
|
|
|
def contextMenuEvent(self, evt: QContextMenuEvent) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
m = QMenu(self)
|
2021-03-26 04:48:26 +01:00
|
|
|
a = m.addAction(tr.actions_copy())
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(a.triggered, self.onCopy)
|
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())
|
|
|
|
|
2021-02-02 14:30:53 +01:00
|
|
|
def dropEvent(self, evt: QDropEvent) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
pass
|
|
|
|
|
2020-06-02 09:56:15 +02:00
|
|
|
def setHtml(self, html: str) -> None: # type: ignore
|
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)
|
2020-09-27 06:36:55 +02:00
|
|
|
self.set_open_links_externally(True)
|
2021-04-03 08:59:23 +02:00
|
|
|
self.show()
|
2017-08-07 08:01:35 +02:00
|
|
|
|
2020-02-27 03:08:38 +01:00
|
|
|
def _setHtml(self, html: str) -> None:
|
2021-03-17 05:51:59 +01:00
|
|
|
from aqt import mw
|
|
|
|
|
|
|
|
oldFocus = mw.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()
|
|
|
|
|
2021-03-17 05:51:59 +01:00
|
|
|
def load_url(self, url: QUrl) -> None:
|
2020-07-16 19:56:57 +02:00
|
|
|
# allow queuing actions when loading url directly
|
|
|
|
self._domDone = False
|
|
|
|
super().load(url)
|
|
|
|
|
2020-02-27 03:08:38 +01:00
|
|
|
def zoomFactor(self) -> float:
|
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()
|
2020-06-02 01:48:51 +02:00
|
|
|
if screen is None:
|
|
|
|
return 1
|
|
|
|
|
2016-06-07 06:27:33 +02:00
|
|
|
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)
|
|
|
|
|
2020-10-14 02:10:16 +02:00
|
|
|
@staticmethod
|
|
|
|
def setPlaybackRequiresGesture(value: bool) -> None:
|
|
|
|
QWebEngineSettings.globalSettings().setAttribute(
|
|
|
|
QWebEngineSettings.PlaybackRequiresUserGesture, value
|
|
|
|
)
|
|
|
|
|
2021-02-02 14:30:53 +01:00
|
|
|
def _getQtIntScale(self, screen: QWidget) -> int:
|
2017-10-05 08:42:46 +02:00
|
|
|
# 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
|
|
|
|
2021-06-19 20:04:24 +02:00
|
|
|
def get_window_bg_color(self, night_mode: bool) -> QColor:
|
|
|
|
if night_mode:
|
|
|
|
return QColor(colors.WINDOW_BG[1])
|
|
|
|
elif isMac:
|
2019-02-25 11:44:27 +01:00
|
|
|
# standard palette does not return correct window color on macOS
|
|
|
|
return QColor("#ececec")
|
2021-06-19 20:04:24 +02:00
|
|
|
else:
|
|
|
|
return theme_manager.default_palette.color(QPalette.Window)
|
2019-02-25 11:44:27 +01:00
|
|
|
|
2020-08-27 07:57:24 +02:00
|
|
|
def standard_css(self) -> str:
|
2021-06-19 20:04:24 +02:00
|
|
|
palette = theme_manager.default_palette
|
2019-04-10 07:16:06 +02:00
|
|
|
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"
|
2021-03-26 04:48:26 +01:00
|
|
|
family = tr.qt_misc_segoe_ui()
|
2020-08-27 07:57:24 +02:00
|
|
|
button_style = "button { font-family:%s; }" % family
|
|
|
|
button_style += "\n:focus { outline: 1px solid %s; }" % color_hl
|
2021-02-11 01:09:06 +01:00
|
|
|
font = f"font-size:12px;font-family:{family};"
|
2016-07-08 08:17:06 +02:00
|
|
|
elif isMac:
|
2019-12-23 01:34:10 +01:00
|
|
|
family = "Helvetica"
|
2021-02-11 01:09:06 +01:00
|
|
|
font = f'font-size:15px;font-family:"{family}";'
|
2020-08-27 07:57:24 +02:00
|
|
|
button_style = """
|
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()
|
2021-02-11 01:09:06 +01:00
|
|
|
font = f'font-size:14px;font-family:"{family}";'
|
2020-08-27 07:57:24 +02:00
|
|
|
button_style = """
|
2018-08-30 22:07:06 +02:00
|
|
|
/* 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,
|
|
|
|
}
|
|
|
|
|
2020-08-27 07:57:24 +02:00
|
|
|
zoom = self.zoomFactor()
|
2021-06-19 20:04:24 +02:00
|
|
|
|
|
|
|
window_bg_day = self.get_window_bg_color(False).name()
|
|
|
|
window_bg_night = self.get_window_bg_color(True).name()
|
|
|
|
body_bg = window_bg_night if theme_manager.night_mode else window_bg_day
|
2020-08-27 07:57:24 +02:00
|
|
|
|
|
|
|
if is_rtl(anki.lang.currentLang):
|
|
|
|
lang_dir = "rtl"
|
|
|
|
else:
|
|
|
|
lang_dir = "ltr"
|
|
|
|
|
|
|
|
return f"""
|
2021-06-19 20:04:24 +02:00
|
|
|
body {{ zoom: {zoom}; background-color: {body_bg}; direction: {lang_dir}; }}
|
2021-05-19 11:06:20 +02:00
|
|
|
html {{ {font} }}
|
2020-08-27 07:57:24 +02:00
|
|
|
{button_style}
|
2021-06-19 20:04:24 +02:00
|
|
|
:root {{ --window-bg: {window_bg_day} }}
|
|
|
|
:root[class*=night-mode] {{ --window-bg: {window_bg_night} }}
|
2020-08-27 07:57:24 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
def stdHtml(
|
|
|
|
self,
|
|
|
|
body: str,
|
|
|
|
css: Optional[List[str]] = None,
|
|
|
|
js: Optional[List[str]] = None,
|
|
|
|
head: str = "",
|
|
|
|
context: Optional[Any] = None,
|
2021-04-14 03:28:40 +02:00
|
|
|
default_css: bool = True,
|
2021-02-02 14:30:53 +01:00
|
|
|
) -> None:
|
2020-08-27 07:57:24 +02:00
|
|
|
|
|
|
|
web_content = WebContent(
|
|
|
|
body=body,
|
|
|
|
head=head,
|
2020-12-28 14:56:41 +01:00
|
|
|
js=["js/webview.js"] + (["js/vendor/jquery.min.js"] if js is None else js),
|
2021-04-13 18:24:21 +02:00
|
|
|
css=(["css/webview.css"] if default_css else [])
|
|
|
|
+ ([] if css is None else css),
|
2020-08-27 07:57:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
gui_hooks.webview_will_set_content(web_content, context)
|
|
|
|
|
|
|
|
csstxt = ""
|
2020-11-01 05:26:58 +01:00
|
|
|
if "css/webview.css" in web_content.css:
|
2020-08-27 07:57:24 +02:00
|
|
|
# we want our dynamic styling to override the defaults in
|
2020-11-01 05:26:58 +01:00
|
|
|
# css/webview.css, but come before user-provided stylesheets so that
|
2020-08-27 07:57:24 +02:00
|
|
|
# they can override us if necessary
|
2020-11-01 05:26:58 +01:00
|
|
|
web_content.css.remove("css/webview.css")
|
|
|
|
csstxt = self.bundledCSS("css/webview.css")
|
2020-08-27 07:57:24 +02:00
|
|
|
csstxt += f"<style>{self.standard_css()}</style>"
|
|
|
|
|
|
|
|
csstxt += "\n".join(self.bundledCSS(fname) for fname in web_content.css)
|
2020-02-12 22:00:13 +01:00
|
|
|
jstxt = "\n".join(self.bundledScript(fname) for fname in web_content.js)
|
|
|
|
|
2017-08-11 12:59:15 +02:00
|
|
|
from aqt import mw
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2020-02-16 23:38:59 +01:00
|
|
|
head = mw.baseHTML() + csstxt + jstxt + web_content.head
|
2020-01-23 06:08:10 +01:00
|
|
|
body_class = theme_manager.body_class()
|
|
|
|
|
2020-08-27 07:57:24 +02:00
|
|
|
if theme_manager.night_mode:
|
|
|
|
doc_class = "night-mode"
|
2020-07-21 03:52:25 +02:00
|
|
|
else:
|
2020-08-27 07:57:24 +02:00
|
|
|
doc_class = ""
|
2020-07-21 03:52:25 +02:00
|
|
|
|
2020-08-27 07:57:24 +02:00
|
|
|
html = f"""
|
2017-08-10 11:02:32 +02:00
|
|
|
<!doctype html>
|
2020-08-27 07:57:24 +02:00
|
|
|
<html class="{doc_class}">
|
|
|
|
<head>
|
|
|
|
<title>{self.title}</title>
|
|
|
|
{head}
|
2012-12-21 08:51:59 +01:00
|
|
|
</head>
|
2017-08-10 11:02:32 +02:00
|
|
|
|
2020-08-27 07:57:24 +02:00
|
|
|
<body class="{body_class}">{web_content.body}</body>
|
|
|
|
</html>"""
|
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
|
|
|
|
2020-04-15 09:00:32 +02:00
|
|
|
@classmethod
|
|
|
|
def webBundlePath(cls, path: str) -> str:
|
2017-07-28 08:19:06 +02:00
|
|
|
from aqt import mw
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2020-02-15 15:03:43 +01:00
|
|
|
if path.startswith("/"):
|
|
|
|
subpath = ""
|
|
|
|
else:
|
|
|
|
subpath = "/_anki/"
|
|
|
|
|
|
|
|
return f"http://127.0.0.1:{mw.mediaServer.getPort()}{subpath}{path}"
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2020-02-12 22:00:13 +01:00
|
|
|
def bundledScript(self, fname: str) -> str:
|
2021-02-11 01:09:06 +01:00
|
|
|
return f'<script src="{self.webBundlePath(fname)}"></script>'
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2020-02-12 22:00:13 +01:00
|
|
|
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
|
|
|
|
)
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2020-02-27 03:08:38 +01:00
|
|
|
def eval(self, js: str) -> None:
|
2017-08-02 07:39:49 +02:00
|
|
|
self.evalWithCallback(js, None)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-02-27 03:08:38 +01:00
|
|
|
def evalWithCallback(self, js: str, cb: Callable) -> None:
|
2017-08-07 08:01:35 +02:00
|
|
|
self._queueAction("eval", js, cb)
|
|
|
|
|
2020-02-27 03:08:38 +01:00
|
|
|
def _evalWithCallback(self, js: str, cb: Callable[[Any], Any]) -> None:
|
2017-08-07 08:01:35 +02:00
|
|
|
if cb:
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2021-02-02 14:30:53 +01:00
|
|
|
def handler(val: Any) -> None:
|
2018-09-28 08:27:41 +02:00
|
|
|
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)
|
|
|
|
|
2020-02-27 03:08:38 +01:00
|
|
|
def _queueAction(self, name: str, *args: Any) -> None:
|
2017-08-07 08:01:35 +02:00
|
|
|
self._pendingActions.append((name, args))
|
|
|
|
self._maybeRunActions()
|
|
|
|
|
2020-02-27 03:08:38 +01:00
|
|
|
def _maybeRunActions(self) -> None:
|
2017-08-07 08:01:35 +02:00
|
|
|
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:
|
2021-02-11 00:37:38 +01:00
|
|
|
raise Exception(f"unknown action: {name}")
|
2016-06-07 06:27:33 +02:00
|
|
|
|
2020-02-27 03:08:38 +01:00
|
|
|
def _openLinksExternally(self, url: str) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
openLink(url)
|
|
|
|
|
2020-02-27 03:08:38 +01:00
|
|
|
def _shouldIgnoreWebEvent(self) -> bool:
|
2018-09-28 08:27:41 +02:00
|
|
|
# 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-02-27 03:08:38 +01:00
|
|
|
def defaultOnBridgeCmd(self, cmd: str) -> None:
|
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
|
2020-02-27 03:08:38 +01:00
|
|
|
def resetHandlers(self) -> None:
|
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
|
|
|
|
2020-02-27 03:08:38 +01:00
|
|
|
def adjustHeightToFit(self) -> None:
|
2021-04-13 21:32:53 +02:00
|
|
|
self.evalWithCallback("document.documentElement.offsetHeight", self._onHeight)
|
2017-08-02 08:22:54 +02:00
|
|
|
|
2020-02-27 03:08:38 +01:00
|
|
|
def _onHeight(self, qvar: Optional[int]) -> None:
|
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
|
|
|
|
|
2021-03-30 23:32:20 +02:00
|
|
|
self.setFixedHeight(int(qvar))
|
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
|
2020-08-27 10:39:27 +02:00
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def hide_while_preserving_layout(self) -> None:
|
2020-08-27 10:39:27 +02:00
|
|
|
"Hide but keep existing size."
|
|
|
|
sp = self.sizePolicy()
|
|
|
|
sp.setRetainSizeWhenHidden(True)
|
|
|
|
self.setSizePolicy(sp)
|
|
|
|
self.hide()
|
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def inject_dynamic_style_and_show(self) -> None:
|
2020-08-27 10:39:27 +02:00
|
|
|
"Add dynamic styling, and reveal."
|
|
|
|
css = self.standard_css()
|
2020-08-29 14:00:28 +02:00
|
|
|
|
2021-02-02 14:30:53 +01:00
|
|
|
def after_style(arg: Any) -> None:
|
2020-08-28 09:10:35 +02:00
|
|
|
gui_hooks.webview_did_inject_style_into_page(self)
|
|
|
|
self.show()
|
2020-08-29 14:00:28 +02:00
|
|
|
|
2020-08-27 13:46:34 +02:00
|
|
|
self.evalWithCallback(
|
|
|
|
f"""
|
2020-08-27 10:39:27 +02:00
|
|
|
const style = document.createElement('style');
|
|
|
|
style.innerHTML = `{css}`;
|
|
|
|
document.head.appendChild(style);
|
2020-08-29 14:00:28 +02:00
|
|
|
""",
|
|
|
|
after_style,
|
|
|
|
)
|
2020-08-27 13:53:28 +02:00
|
|
|
|
|
|
|
def load_ts_page(self, name: str) -> None:
|
|
|
|
from aqt import mw
|
|
|
|
|
2021-02-06 06:03:21 +01:00
|
|
|
self.set_open_links_externally(True)
|
2020-08-27 13:53:28 +02:00
|
|
|
if theme_manager.night_mode:
|
|
|
|
extra = "#night"
|
|
|
|
else:
|
|
|
|
extra = ""
|
|
|
|
self.hide_while_preserving_layout()
|
2021-03-17 05:51:59 +01:00
|
|
|
self.load_url(QUrl(f"{mw.serverURL()}_anki/pages/{name}.html{extra}"))
|
2020-08-27 13:53:28 +02:00
|
|
|
self.inject_dynamic_style_and_show()
|
2021-03-17 05:51:59 +01:00
|
|
|
|
|
|
|
def force_load_hack(self) -> None:
|
|
|
|
"""Force process to initialize.
|
|
|
|
Must be done on Windows prior to changing current working directory."""
|
|
|
|
self.requiresCol = False
|
|
|
|
self._domReady = False
|
|
|
|
self._page.setContent(bytes("", "ascii"))
|