Commit Graph

107 Commits

Author SHA1 Message Date
RumovZ
f61126a40c
Fix button focus highlight on Windows (#1627)
* Fix button focus highlight on Windows

* Use none instead of none for outline and box-shadow

* Unnest selectors in reviewer-bottom

Co-authored-by: Henrik Giesel <hengiesel@gmail.com>
2022-02-07 19:41:19 +10:00
Danish Prakash
2c357a6c94
qt/aqt/webview: handle opening href with target=_blank (#1623)
* qt/aqt/webview: handle opening href with target=_blank

* CONTRIBUTORS: add username as part of first PR
2022-01-24 12:23:01 +10:00
Damien Elmes
5c100b4189 fall back on a sans-serif font if the system Linux font is unavailable
The GNOME theme may return "Cantarell", which may not be installed.
2022-01-22 16:43:58 +10:00
RumovZ
a51232debe
Fix is_zoom_event() (#1607) 2022-01-20 11:56:12 +10:00
RumovZ
fe7a8db231
Selectively disable zoom (#1602)
* Make webview zoom optional

Also suppress mouse wheel zooming.

* Disable zoom for top and bottom bars in main view

* Factor in macos zoom by scrolling and refactor
2022-01-18 19:12:57 +10:00
Abdo
1120939648
Support drag & drop in main window to import files (#1562)
* Support drag & drop in main window to import files

* Pass drag & drop events to super when not in deck browser

Seems this is required so that JS in the reviewer receives dragover

* Remove unused allowDrops
2021-12-20 20:22:21 +10:00
Damien Elmes
db5117ce1a fix webviews sometimes failing to load, by enabling cache
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
```
2021-12-08 21:20:39 +10:00
nwwt
9becb4c11f
Allow <audio> to play without user interaction in accordance to autoplay setting v2 (#1539)
Adds back setting Chromium's autoplay policy according to Anki's, this time without globals.
2021-12-08 08:08:56 +10:00
Abdo
b3ea7288ab
Flip arrows of Bootstrap-styled <select>s for RTL langs (#1526)
* 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
2021-12-06 18:40:26 +10:00
Hikaru Y
d2cbd15bbe
Fix memory leak in AnkiWebView (#1510) 2021-11-29 12:31:37 +10:00
Damien Elmes
ebad6ad379 update platform checks (eg isWin -> is_win) + devMode 2021-11-25 09:06:16 +10:00
RumovZ
f2173fddb0
Live theme changes (#1497)
* 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>
2021-11-25 07:17:41 +10:00
RumovZ
9dc3cf216a
PEP8 for rest of pylib (#1451)
* PEP8 dbproxy.py

* PEP8 errors.py

* PEP8 httpclient.py

* PEP8 lang.py

* PEP8 latex.py

* Add decorator to deprectate key words

* Make replacement for deprecated attribute optional

* Use new helper `_print_replacement_warning()`

* PEP8 media.py

* PEP8 rsbackend.py

* PEP8 sound.py

* PEP8 stdmodels.py

* PEP8 storage.py

* PEP8 sync.py

* PEP8 tags.py

* PEP8 template.py

* PEP8 types.py

* Fix DeprecatedNamesMixinForModule

The class methods need to be overridden with instance methods, so every
module has its own dicts.

* Use `# pylint: disable=invalid-name` instead of id

* PEP8 utils.py

* Only decorate `__getattr__` with `@no_type_check`

* Fix mypy issue with snakecase

Importing it from `anki._vendor` raises attribute errors.

* Format

* Remove inheritance of DeprecatedNamesMixin

There's almost no shared code now and overriding classmethods with
instance methods raises mypy issues.

* Fix traceback frames of deprecation warnings

* remove fn/TimedLog (dae)

Neither Anki nor add-ons appear to have been using it

* fix some issues with stringcase use (dae)

- the wheel was depending on the PyPI version instead of our vendored
version
- _vendor:stringcase should not have been listed in the anki py_library.
We already include the sources in py_srcs, and need to refer to them
directly. By listing _vendor:stringcase as well, we were making a
top-level stringcase library available, which would have only worked for
distributing because the wheel definition was also incorrect.
- mypy errors are what caused me to mistakenly add the above - they
were because the type: ignore at the top of stringcase.py was causing
mypy to completely ignore the file, so it was not aware of any attributes
it contained.
2021-10-25 14:50:13 +10:00
Damien Elmes
a7812dedc0 switch to new-style PyQt scoped enums and Qt6
The enum changes should work on PyQt 5.x, and are required in PyQt 6.x.
They are not supported by the PyQt5 typings however, so we need to run
our tests with PyQt6.
2021-10-15 12:57:19 +10:00
Damien Elmes
dfcefaebe3 miscellaneous pyqt6 compat fixes
- add a few gates for qt5-specific behaviour
- prepare for some changes to the typings in qt6
- map pickled Qt5 ByteArrays to Qt6 when running Qt6
2021-10-15 12:57:19 +10:00
Damien Elmes
a403b957cc fix error in webview __del__ on shutdown 2021-10-12 17:07:07 +10:00
Damien Elmes
d33c66e195 change setHtml() to serve content via media server
- fixes https://forums.ankiweb.net/t/deck-list-is-blank/2241/2
- fixes the security warnings on Qt 6, by ensuring our pages and
resources are coming from the same origin
2021-10-12 16:27:03 +10:00
Damien Elmes
70dbd06be3 add qtmajor qualification to version checks; update outdated min ver 2021-10-12 16:20:33 +10:00
Damien Elmes
aaceeec25a Revert "Allow copying of images using context menu"
This reverts commit d5b3927d0b.

This approach no longer appears to work in Qt6. There is a
QWebEngineView.lastContextMenuRequest() that may work, though whether
onCopy() qualifies as "during contextMenuEvent" is not clear.

https://doc.qt.io/qt-6/qwebengineview.html#lastContextMenuRequest
2021-10-12 16:18:24 +10:00
Damien Elmes
aabf693bb8 Revert "Allow <audio> to play without user interaction in accordance to autoplay setting"
This reverts commit 54f51da944.

This breaks in the PyQt6 upgrade. There are no globals anymore, only
page profiles - but the code should probably be modifying a specific
webview instead of globals anyway.
2021-10-12 16:18:24 +10:00
Damien Elmes
b9251290ca run pyupgrade over codebase [python upgrade required]
This adds Python 3.9 and 3.10 typing syntax to files that import
attributions from __future___. Python 3.9 should be able to cope with
the 3.10 syntax, but Python 3.8 will no longer work.

On Windows/Mac, install the latest Python 3.9 version from python.org.
There are currently no orjson wheels for Python 3.10 on Windows/Mac,
which will break the build unless you have Rust installed separately.

On Linux, modern distros should have Python 3.9 available already. If
you're on an older distro, you'll need to build Python from source first.
2021-10-04 15:05:48 +10:00
Damien Elmes
9a103b3716 use extra variable in stdHtml() to work around a pylint crash 2021-10-02 22:47:13 +10:00
hikaru-y
dd9528aa73 Silence 'link preload' warning on the first card
Due to 207e425, a harmless warning about 'link preload' will be output
to the console on the first card.
2021-08-18 02:25:51 +09:00
Damien Elmes
e7189f4af5 fix ugly js confirm box
https://forums.ankiweb.net/t/2-1-45-release-candidate/11362/2
2021-07-24 22:04:06 +10:00
Damien Elmes
a54f181ae0 remove old Mac shortcut code completely
The shortcuts seem to work correctly without it on Qt 5.14, so this
code seems to have only been required for older Qt releases.
2021-07-04 16:00:23 +10:00
Henrik Giesel
5989fb8ef6 Satisfy qt formatter 2021-07-03 01:54:10 +02:00
Henrik Giesel
7f7e5c2702 Execute Ctrl+C/V/X/A through execCommand in editor (so we can customize it) 2021-07-03 01:48:49 +02:00
hikaru-y
4bff3f4854 Set --window-bg of day/night mode to proper colors
including the following changes:
- rename "_getWindowColor" to "get_window_bg_color" to follow PEP8
- add "night_mode" argument to "get_window_bg_color"
- make it possible to get the correct colors regardless of the current theme
2021-06-24 23:46:48 +09:00
Damien Elmes
d448bd9cb6 prevent cmd+c in webview with empty selection from crashing on macOS
https://forums.ankiweb.net/t/command-c-on-crashes-anki-2-1-41-on-mac/10684
2021-06-11 19:58:20 +10:00
Damien Elmes
fdd162a7b9 simplify previous patch
The default 1 rem is fine, but we need to apply the default font size
to the html element instead of the body for the definition of rem to
update.

Fixes #1174
2021-05-19 19:06:20 +10:00
Damien Elmes
fc8a5759d5
Merge pull request #1124 from hgiesel/dropjquerypy2
Remove jQuery from remaining qt/aqt files
2021-04-14 12:43:11 +10:00
Damien Elmes
e530b96998 add missing type annotation 2021-04-14 11:28:40 +10:00
Henrik Giesel
280f78535b Replace jQuery in webview.py 2021-04-13 21:32:53 +02:00
Henrik Giesel
191e3cf69b Satisfy formatter 2021-04-13 19:47:03 +02:00
Henrik Giesel
df7c597a8b Add option not include default css in stdHtml 2021-04-13 19:47:03 +02:00
Damien Elmes
12a3d9ec87 ensure page is shown when setHtml() called
https://forums.ankiweb.net/t/looked-into-the-bug-when-pressing-e-and-r-together-in-filtered-deck/8951
2021-04-03 16:59:23 +10:00
abdo
d7aca8845f Fix deprecated implicit conversion from float to int using __int__() 2021-03-31 00:32:20 +03:00
Damien Elmes
5a094e78fa enable type checking of aqt/forms, and fix the new typing issues
Referencing an invalid translation should now break the build
2021-03-26 16:06:02 +10:00
Damien Elmes
0c338bfd53 update no-arg tr references in qt/ 2021-03-26 13:48:26 +10:00
Damien Elmes
0c59c8b591 fix a bunch of qt typing issues uncovered by the following commit 2021-03-19 19:45:21 +10:00
Damien Elmes
88c002f4eb convert qt strings to f-strings with flynt
Also revealed an incorrect type def in editor.py that mypy wasn't
noticing before :-(
2021-02-11 10:09:06 +10:00
Damien Elmes
bb29ce88f3 minor code cleanups with pyupgrade
- pyupgrade --py38-plus --keep-runtime-typing --keep-percent-format
- third-party mpv and winpaths excluded
2021-02-11 09:43:40 +10:00
Damien Elmes
e30b7fc36a handle remote links in ts pages
Required to avoid loading remote links embedded in deck descriptions
in the webview
2021-02-06 15:03:21 +10:00
Damien Elmes
bb30e8f7bc use constants for other color references
str_color/qcolor() doesn't appear to have been used by any add-ons
except one of mine, so changing the signature should be safe
2021-02-05 18:58:22 +10:00
Damien Elmes
6426edb0ac more typing updates 2021-02-02 23:31:55 +10:00
Damien Elmes
a56b09b987 add a bunch of return types 2021-02-01 23:53:23 +10:00
Henrik Giesel
083cd36e55 Fix formatting 2020-12-28 14:56:41 +01:00
Henrik Giesel
98407c3fea Avoid building jquery to its own directory 2020-12-28 14:18:07 +01:00
Henrik Giesel
37dcdcc4c0 Rename references from vendor/jquery.js to vendor/jquery/jquery.min.js 2020-12-28 13:16:12 +01:00
Damien Elmes
c1d15a8e1f quieten MathJax 2020-11-18 21:34:44 +10:00