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
This commit is contained in:
RumovZ 2022-01-18 10:12:57 +01:00 committed by GitHub
parent 0e06a1c679
commit fe7a8db231
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 5 deletions

View File

@ -869,12 +869,14 @@ title="{}" {}>{}</button>""".format(
# toolbar # toolbar
tweb = self.toolbarWeb = AnkiWebView(title="top toolbar") tweb = self.toolbarWeb = AnkiWebView(title="top toolbar")
tweb.setFocusPolicy(Qt.FocusPolicy.WheelFocus) tweb.setFocusPolicy(Qt.FocusPolicy.WheelFocus)
tweb.disable_zoom()
self.toolbar = aqt.toolbar.Toolbar(self, tweb) self.toolbar = aqt.toolbar.Toolbar(self, tweb)
# main area # main area
self.web = MainWebView(self) self.web = MainWebView(self)
# bottom area # bottom area
sweb = self.bottomWeb = AnkiWebView(title="bottom toolbar") sweb = self.bottomWeb = AnkiWebView(title="bottom toolbar")
sweb.setFocusPolicy(Qt.FocusPolicy.WheelFocus) sweb.setFocusPolicy(Qt.FocusPolicy.WheelFocus)
sweb.disable_zoom()
# add in a layout # add in a layout
self.mainLayout = QVBoxLayout() self.mainLayout = QVBoxLayout()
self.mainLayout.setContentsMargins(0, 0, 0, 0) self.mainLayout.setContentsMargins(0, 0, 0, 0)

View File

@ -1002,6 +1002,22 @@ def no_arg_trigger(func: Callable) -> Callable:
return pyqtSlot()(func) # type: ignore return pyqtSlot()(func) # type: ignore
def is_zoom_event(evt: QEvent) -> bool:
"""If the event will trigger zoom.
Includes zoom by pinching, Ctrl-scrolling, and Meta-scrolling,
where scrolling may be triggered by mouse wheel or gesture.
"""
return isinstance(evt, QNativeGestureEvent) or (
isinstance(evt, QWheelEvent)
and (
(is_mac and KeyboardModifiersPressed().meta)
or KeyboardModifiersPressed().control
)
)
class KeyboardModifiersPressed: class KeyboardModifiersPressed:
"Util for type-safe checks of currently-pressed modifier keys." "Util for type-safe checks of currently-pressed modifier keys."
@ -1022,6 +1038,10 @@ class KeyboardModifiersPressed:
def alt(self) -> bool: def alt(self) -> bool:
return bool(self._modifiers & Qt.KeyboardModifier.AltModifier) return bool(self._modifiers & Qt.KeyboardModifier.AltModifier)
@property
def meta(self) -> bool:
return bool(self._modifiers & Qt.KeyboardModifier.MetaModifier)
# add-ons attempting to import isMac from this module :-( # add-ons attempting to import isMac from this module :-(
_deprecated_names = DeprecatedNamesMixinForModule(globals()) _deprecated_names = DeprecatedNamesMixinForModule(globals())

View File

@ -13,7 +13,7 @@ from anki.utils import is_lin, is_mac, is_win
from aqt import colors, gui_hooks from aqt import colors, gui_hooks
from aqt.qt import * from aqt.qt import *
from aqt.theme import theme_manager from aqt.theme import theme_manager
from aqt.utils import askUser, openLink, showInfo, tr from aqt.utils import askUser, is_zoom_event, openLink, showInfo, tr
serverbaseurl = re.compile(r"^.+:\/\/[^\/]+") serverbaseurl = re.compile(r"^.+:\/\/[^\/]+")
@ -241,6 +241,7 @@ class AnkiWebView(QWebEngineView):
self._pendingActions: list[tuple[str, Sequence[Any]]] = [] self._pendingActions: list[tuple[str, Sequence[Any]]] = []
self.requiresCol = True self.requiresCol = True
self.setPage(self._page) self.setPage(self._page)
self._disable_zoom = False
self.resetHandlers() self.resetHandlers()
self._filterSet = False self._filterSet = False
@ -255,18 +256,21 @@ class AnkiWebView(QWebEngineView):
def set_title(self, title: str) -> None: def set_title(self, title: str) -> None:
self.title = title # type: ignore[assignment] self.title = title # type: ignore[assignment]
def disable_zoom(self) -> None:
self._disable_zoom = True
def eventFilter(self, obj: QObject, evt: QEvent) -> bool: def eventFilter(self, obj: QObject, evt: QEvent) -> bool:
# disable pinch to zoom gesture if self._disable_zoom and is_zoom_event(evt):
if isinstance(evt, QNativeGestureEvent):
return True return True
elif (
if (
isinstance(evt, QMouseEvent) isinstance(evt, QMouseEvent)
and evt.type() == QEvent.Type.MouseButtonRelease and evt.type() == QEvent.Type.MouseButtonRelease
): ):
if evt.button() == Qt.MouseButton.MiddleButton and is_lin: if evt.button() == Qt.MouseButton.MiddleButton and is_lin:
self.onMiddleClickPaste() self.onMiddleClickPaste()
return True return True
return False
return False return False
def set_open_links_externally(self, enable: bool) -> None: def set_open_links_externally(self, enable: bool) -> None: