Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions src/toad/qt/composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
QPlainTextEdit,
QPushButton,
QScrollArea,
QScroller,
QSizePolicy,
QVBoxLayout,
QWidget,
Expand Down Expand Up @@ -918,10 +917,6 @@ def __init__(self, host: ComposerHost, renderer: QtLineRenderer, placeholder: st
self.attached_chips.setSpacing(UiGap.tight)
self.attached_chips.setAlignment(Qt.AlignmentFlag.AlignLeft)
self.attached_scroll.setWidget(self.attached_chips_host)
QScroller.grabGesture(
self.attached_scroll.viewport(),
QScroller.ScrollerGestureType.LeftMouseButtonGesture,
)
self.compress_button: QtCompressImagesButton | None = None
row_layout.addWidget(self.attached_scroll, 1)
layout = self.layout()
Expand Down
8 changes: 6 additions & 2 deletions src/toad/qt/finger_scrolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from abc import ABC, abstractmethod

from PySide6.QtCore import QObject
from PySide6.QtCore import QObject, Qt
from PySide6.QtGui import QGuiApplication
from PySide6.QtWidgets import QAbstractScrollArea, QApplication, QProxyStyle, QScroller, QWidget

Expand Down Expand Up @@ -45,7 +45,11 @@ class TouchAsMouse(FingerScrollGesture):
drag, then delivers it, so a tap still presses the row or button under the finger."""

def grab(self, viewport: QWidget) -> None:
QScroller.grabGesture(viewport, QScroller.ScrollerGestureType.LeftMouseButtonGesture)
"""Grab once per viewport: calling `QScroller.grabGesture` again unregisters and
deletes the live `QFlickGestureRecognizer`, leaving `QGestureManager::getState`
with a dangling recognizer pointer when a widget repolishes during gesture delivery."""
if QScroller.grabbedGesture(viewport).value <= Qt.GestureType.CustomGesture.value:
QScroller.grabGesture(viewport, QScroller.ScrollerGestureType.LeftMouseButtonGesture)


class MouseAndWheel(FingerScrollGesture):
Expand Down
26 changes: 25 additions & 1 deletion tests/model/test_qt_finger_scrolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
with zero application-wide event filter overhead (`toad.qt.finger_scrolling`)."""
from __future__ import annotations

from PySide6.QtWidgets import QApplication, QListWidget, QWidget
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QApplication, QListWidget, QScrollArea, QScroller, QWidget

from toad.qt.finger_scrolling import FingerScrolling, MouseAndWheel, TouchAsMouse

Expand Down Expand Up @@ -69,3 +70,26 @@ def installEventFilter(self, flt):
assert base_styles == [app._style]
assert styled == [marker]
assert filtered == []


def test_touch_grab_is_idempotent_across_repolish(monkeypatch):
"""Repeated `polish()` calls must keep the same QScroller gesture ID instead of
unregistering and deleting the active `QFlickGestureRecognizer`."""
monkeypatch.setenv("QT_QPA_PLATFORM", "offscreen")
application = QApplication.instance() or QApplication([])
gesture = TouchAsMouse()
granted = FingerScrolling(gesture, application)
area = QScrollArea()
try:
vp = area.viewport()
granted.polish(area)
first = QScroller.grabbedGesture(vp)
assert first.value > Qt.GestureType.CustomGesture.value
for _ in range(5):
granted.polish(area)
gesture.grab(vp)
assert QScroller.grabbedGesture(vp) == first
finally:
QScroller.ungrabGesture(area.viewport())
area.deleteLater()
granted.deleteLater()
Loading