Kept the favicon, but have reverted the rest, as it unfortunately did
not seem to prevent the issue from occurring.
Original discussion: https://github.com/ankitects/anki/pull/1369
This reverts commit 6d0f7e7f05.
Fixes#983
This has been a long-standing issue that was infrequent enough on
developer machines that we weren't able to get to the bottom of it before
now. As luck would have it, the new ARM build had just the right timing
for this to occur every few invocations, and I was able to narrow it down
to the call that turns off the cache.
We don't really want the cache, so this is not a great solution. But I
ran into trouble when trying to figure out a better solution:
- Calling setHttpCacheType() earlier (eg immediately after creating the
page) fails.
- It even fails if you attempt to change the setting in the shared
default profile before any webviews are loaded:
```
def setupMainWindow(self) -> None:
QWebEngineProfile.defaultProfile().setHttpCacheType(QWebEngineProfile.HttpCacheType.NoCache)
```
- Creating a profile separately, and passing it into QWebEnginePage()
does work. But it introduces a warning each time a webview is deallocated,
and I fear it may lead to crashes:
```
Release of profile requested but WebEnginePage still not deleted. Expect troubles !
```
I tried various combinations of parents for the profile and page, and
turning web._page into an unretained property, but could not figure it out.
Some Googling pulls up a bunch of other people who seem to have hit similar
issues with PyQt. If anyone has any ideas, they'd be welcome; in the mean
time, I guess we're better off using up some of the user's disk
space than sometimes failing to load.
The profile-in-advance code I tried is below:
```
diff --git a/qt/aqt/webview.py b/qt/aqt/webview.py
index 1c96112d8..4f3e91284 100644
--- a/qt/aqt/webview.py
+++ b/qt/aqt/webview.py
@@ -23,9 +23,49 @@ serverbaseurl = re.compile(r"^.+:\/\/[^\/]+")
BridgeCommandHandler = Callable[[str], Any]
+def _create_profile(parent: QObject) -> QWebEngineProfile:
+ qwebchannel = ":/qtwebchannel/qwebchannel.js"
+ jsfile = QFile(qwebchannel)
+ if not jsfile.open(QIODevice.OpenModeFlag.ReadOnly):
+ print(f"Error opening '{qwebchannel}': {jsfile.error()}", file=sys.stderr)
+ jstext = bytes(cast(bytes, jsfile.readAll())).decode("utf-8")
+ jsfile.close()
+
+ script = QWebEngineScript()
+ script.setSourceCode(
+ jstext
+ + """
+ var pycmd, bridgeCommand;
+ new QWebChannel(qt.webChannelTransport, function(channel) {
+ bridgeCommand = 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");
+ });
+ """
+ )
+ script.setWorldId(QWebEngineScript.ScriptWorldId.MainWorld)
+ script.setInjectionPoint(QWebEngineScript.InjectionPoint.DocumentReady)
+ script.setRunsOnSubFrames(False)
+
+ profile = QWebEngineProfile(parent)
+ profile.setHttpCacheType(QWebEngineProfile.HttpCacheType.NoCache)
+ profile.scripts().insert(script)
+ return profile
+
+
class AnkiWebPage(QWebEnginePage):
- def __init__(self, onBridgeCmd: BridgeCommandHandler) -> None:
- QWebEnginePage.__init__(self)
+ def __init__(self, onBridgeCmd: BridgeCommandHandler, parent: QObject) -> None:
+ profile = _create_profile(parent)
+ QWebEnginePage.__init__(self, profile, parent)
self._onBridgeCmd = onBridgeCmd
self._setupBridge()
self.open_links_externally = True
@@ -46,39 +86,6 @@ class AnkiWebPage(QWebEnginePage):
self._channel.registerObject("py", self._bridge)
self.setWebChannel(self._channel)
- qwebchannel = ":/qtwebchannel/qwebchannel.js"
- jsfile = QFile(qwebchannel)
- if not jsfile.open(QIODevice.OpenModeFlag.ReadOnly):
- print(f"Error opening '{qwebchannel}': {jsfile.error()}", file=sys.stderr)
- jstext = bytes(cast(bytes, jsfile.readAll())).decode("utf-8")
- jsfile.close()
-
- script = QWebEngineScript()
- script.setSourceCode(
- jstext
- + """
- var pycmd, bridgeCommand;
- new QWebChannel(qt.webChannelTransport, function(channel) {
- bridgeCommand = 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");
- });
- """
- )
- script.setWorldId(QWebEngineScript.ScriptWorldId.MainWorld)
- script.setInjectionPoint(QWebEngineScript.InjectionPoint.DocumentReady)
- script.setRunsOnSubFrames(False)
- self.profile().scripts().insert(script)
-
def javaScriptConsoleMessage(
self,
level: QWebEnginePage.JavaScriptConsoleMessageLevel,
@@ -228,7 +235,7 @@ class AnkiWebView(QWebEngineView):
) -> None:
QWebEngineView.__init__(self, parent=parent)
self.set_title(title)
- self._page = AnkiWebPage(self._onBridgeCmd)
+ self._page = AnkiWebPage(self._onBridgeCmd, self)
self._page.setBackgroundColor(
self.get_window_bg_color(theme_manager.night_mode)
@@ -242,7 +249,6 @@ class AnkiWebView(QWebEngineView):
self.requiresCol = True
self.setPage(self._page)
- self._page.profile().setHttpCacheType(QWebEngineProfile.HttpCacheType.NoCache)
self.resetHandlers()
self.allowDrops = False
self._filterSet = False
```
This was motivated by the fact that recording was crashing on the native
M1 build. That ended up being mostly a PEBKAC problem - turns out the
Mac Mini has no built-in microphone 🤦.
I still thinks this has some value though - it doesn't crash in such
cases, and probably doesn't suffer from the problem shown in this thread
either:
https://forums.ankiweb.net/t/anki-crashes-when-trying-to-record-on-mac/14764
For now, this is only enabled when running on arm64. If it turns out to
be reliable, it could be offered as an option on amd64 as well.
More pleasant to work with than ObjectiveC, which will help with the
following commit.
Swift libraries weren't added to macOS until 10.14.4, so theme
autodetection will fail on 10.14.0-10.14.3. The Qt6 build will have its
minimum version bumped to 10.14.4; the Qt5 build will remain on 10.13.4.
Bazel's rules_swift doesn't currently support building Swift dylibs, so
we need to invoke swiftc directly via a genrule().
* Update about.py
* Add cqg to list
for his contributions to the community (testing, support, suggestions).
* Add the AnKing to list
for his efforts to keep add-ons compatible with new versions and popularizing Anki in the medical community via YouTube, Reddit and other social media.
* Flip arrows of Bootstrap-styled <select>s for RTL langs
* Use the dir attribute to set document direction
* Remove unused variable and fix use of CSS var
* Remove unneeded old.note_type() call
Fixes TypeError thrown after deleting a notetype that's currently selected in the editor.
* Handle IndexError on notetype change
Occurs in the Add window when changing the notetype via NotetypeChooser from
- the notetype that's auto-selected after deleting the currently selected notetype
- to a notetype with fewer fields than the auto-selected one
* Add return to exception handler
to properly ignore the command.
The packaged builds of 2.1.50 use python -OO, which means our assertion
statements won't be run. This is not an issue for unit tests (as we
don't run them from a packaged build), or for type assertions (which are
added for mypy's benefit), but we do need to ensure that invariant checks
are still run.
* Allow theme change at runtime and add hook
* Save or restore default palette on theme change
* Update aqt widget styles on theme change
* styling fixes
- drop _light_palette, as default_palette serves the same purpose
- save default platform theme, and restore it when switching away
from nightmode
- update macOS light/dark mode on theme switch
- fix unreadable menus on Windows
* update night-mode classes on theme change
This is the easy part - CSS styling that uses standard_css or our
css variables should update automatically. The main remaining issue
is JS code that sets colors based on the theme at the time it's run -
eg the graph code, and the editor.
* switch night mode value on toggle
* expose current theme via a store; switch graphs to use it
https://github.com/ankitects/anki/issues/1471#issuecomment-972402492
* start using currentTheme in editor/components
This fixes basic editing - there are still components that need updating.
* add simple xcodeproj for code completion
* add helper to get currently-active system theme on macOS
* fix setCurrentTheme not being immediately available
* live update tag color
* style().name() doesn't work on Qt5
* automatic theme switching on Windows/Mac
* currentTheme -> pageTheme
* Replace `nightModeKey` with `pageTheme`
Co-authored-by: Damien Elmes <gpg@ankiweb.net>
* Enable access to old notetype name
* Set minimum height for ChangeNotetypeDialog
* Add bootstrap icons to change-notetype
* Move alert up and make it collapsible
* Tweak some CSS
- Add variables --sticky-bg and --sticky-border to StickyContainer
- Tweak base.css
* Add translatable string "(Nothing)"
* Rework ChangeNotetype screen
* Initially load option at newIndex and remaining options on focus
Optimization for big notetypes:
Should increase efficiency from O(n²) to O(n). Test on notetype with 500 templates shows significant improvement in load time (~10s down to ~1s).
* Try to satisfy rust test
* Change arrow direction depending on reading direction
+ add 0.5em top padding to main
* Create Alert.svelte
* Introduce CSS variable --pane-bg
* Revert "Initially load option at newIndex and remaining options on focus"
This reverts commit f42beee45c27dba9433d76217fb583b117fb5231.
* Final cleanup
* Refine padding/gutter