diff options
author | Shawn Rutledge <[email protected]> | 2023-11-14 10:35:56 -0700 |
---|---|---|
committer | Shawn Rutledge <[email protected]> | 2023-11-17 07:28:57 -0700 |
commit | 1b166c87d06ef53de9ccc76218dfae7b0359c5e0 (patch) | |
tree | b4d1dccc70c336fd6a68b67e955e6f69777acf62 | |
parent | 096f3c3097cb2f60c2f44d4d8c734fc33ffd0a3e (diff) |
Make TapHandler longPressed/tapped exclusive and reliable; fix example
The back button in the examples' LauncherList.qml has been flaky.
As described in the docs, a TapHandler used to implement a Button
should have `gesturePolicy: TapHandler.ReleaseWithinBounds` to get
the common behavior that you can drag out of the button to cancel
the click, and you can also drag back into the button to change your
mind and let it click after all. But when trying to test this behavior,
another problem became evident: if you spend a longer time than
longPressThreshold for the whole gesture, then at the time of release
you could see the debug output "long press threshold exceeded" and the
tapped signal was not emitted. Our intention was that if you are
dragging around, the TapHandler is not eligible to emit the longPressed
signal; it follows that it should not become ineligible to emit tapped,
either (tapped can be emitted if other constraints are satisfied).
The intention of the ReleaseWithinBounds policy is that it doesn't
matter how much you drag, as long as the point is within the bounds
of the parent at the time of release.
So we begin keeping track of whether we have actually emitted the
longPressed signal, rather than merely looking at the time difference.
This changed behavior in tst_qquickdeliveryagent::passiveGrabberOrder:
1 second is more than enough time for long press with the default
longPressThreshold, and now the tapped signals are no longer emitted
after longPressed. So we just wait for pressed state rather than
waiting so long. qWaits in tests are best avoided anyway (although
I think the intention in 152e12dc22cc0fd07cf90bcd35ae0e05b8b46fa0
might have been to wait long enough to ensure that nothing undesired
would occur, rather than waiting for something specific to occur).
Task-number: QTBUG-65012
Task-number: QTBUG-105810
Pick-to: 6.5 6.6
Change-Id: If6a86d955e19810cb06de659f5e39b50a72fa762
Reviewed-by: Richard Moe Gustavsen <[email protected]>
-rw-r--r-- | examples/quick/shared/LauncherList.qml | 1 | ||||
-rw-r--r-- | src/quick/handlers/qquicktaphandler.cpp | 10 | ||||
-rw-r--r-- | src/quick/handlers/qquicktaphandler_p.h | 1 | ||||
-rw-r--r-- | tests/auto/quick/qquickdeliveryagent/tst_qquickdeliveryagent.cpp | 6 |
4 files changed, 13 insertions, 5 deletions
diff --git a/examples/quick/shared/LauncherList.qml b/examples/quick/shared/LauncherList.qml index ee8fc3984e..82fcf0c194 100644 --- a/examples/quick/shared/LauncherList.qml +++ b/examples/quick/shared/LauncherList.qml @@ -181,6 +181,7 @@ Rectangle { TapHandler { id: tapHandler enabled: root.activePageCount > 0 + gesturePolicy: TapHandler.ReleaseWithinBounds onTapped: { pageContainer.children[pageContainer.children.length - 1].exit() } diff --git a/src/quick/handlers/qquicktaphandler.cpp b/src/quick/handlers/qquicktaphandler.cpp index 5c1b59cf61..43c761f5fd 100644 --- a/src/quick/handlers/qquicktaphandler.cpp +++ b/src/quick/handlers/qquicktaphandler.cpp @@ -78,6 +78,8 @@ bool QQuickTapHandler::wantsEventPoint(const QPointerEvent *event, const QEventP bool ret = false; bool overThreshold = d_func()->dragOverThreshold(point); if (overThreshold && m_gesturePolicy != DragWithinBounds) { + if (m_longPressTimer.isActive()) + qCDebug(lcTapHandler) << objectName() << "drag threshold exceeded"; m_longPressTimer.stop(); m_holdTimer.invalidate(); } @@ -176,6 +178,7 @@ void QQuickTapHandler::timerEvent(QTimerEvent *event) if (event->timerId() == m_longPressTimer.timerId()) { m_longPressTimer.stop(); qCDebug(lcTapHandler) << objectName() << "longPressed"; + m_longPressed = true; emit longPressed(); } else if (event->timerId() == m_doubleTapTimer.timerId()) { m_doubleTapTimer.stop(); @@ -364,7 +367,9 @@ void QQuickTapHandler::setPressed(bool press, bool cancel, QPointerEvent *event, setExclusiveGrab(event, point, press); } if (!cancel && !press && parentContains(point)) { - if (point.timeHeld() < longPressThreshold()) { + if (m_longPressed) { + qCDebug(lcTapHandler) << objectName() << "long press threshold" << longPressThreshold() << "exceeded:" << point.timeHeld(); + } else { // Assuming here that pointerEvent()->timestamp() is in ms. const quint64 ts = event->timestamp(); const quint64 interval = ts - m_lastTapTimestamp; @@ -410,10 +415,9 @@ void QQuickTapHandler::setPressed(bool press, bool cancel, QPointerEvent *event, m_lastTapTimestamp = ts; m_lastTapPos = point.scenePosition(); - } else { - qCDebug(lcTapHandler) << objectName() << "tap threshold" << longPressThreshold() << "exceeded:" << point.timeHeld(); } } + m_longPressed = false; emit pressedChanged(); if (!press && m_gesturePolicy != DragThreshold) { // on release, ungrab after emitting changed signals diff --git a/src/quick/handlers/qquicktaphandler_p.h b/src/quick/handlers/qquicktaphandler_p.h index 8c6b6d162d..5e57e81a29 100644 --- a/src/quick/handlers/qquicktaphandler_p.h +++ b/src/quick/handlers/qquicktaphandler_p.h @@ -109,6 +109,7 @@ private: GesturePolicy m_gesturePolicy = GesturePolicy::DragThreshold; ExclusiveSignals m_exclusiveSignals = NotExclusive; bool m_pressed = false; + bool m_longPressed = false; static quint64 m_multiTapInterval; static int m_mouseMultiClickDistanceSquared; diff --git a/tests/auto/quick/qquickdeliveryagent/tst_qquickdeliveryagent.cpp b/tests/auto/quick/qquickdeliveryagent/tst_qquickdeliveryagent.cpp index fc457666e6..2302e216f8 100644 --- a/tests/auto/quick/qquickdeliveryagent/tst_qquickdeliveryagent.cpp +++ b/tests/auto/quick/qquickdeliveryagent/tst_qquickdeliveryagent.cpp @@ -179,7 +179,8 @@ void tst_qquickdeliveryagent::passiveGrabberOrder() QPoint pos(75, 75); QTest::mousePress(&view, Qt::LeftButton, Qt::NoModifier, pos); - QTest::qWait(1000); + QTRY_VERIFY(rootTap->isPressed()); + QTRY_VERIFY(subsceneTap->isPressed()); auto devPriv = QPointingDevicePrivate::get(QPointingDevice::primaryPointingDevice()); const auto &persistentPoint = devPriv->activePoints.values().first(); qCDebug(lcTests) << "passive grabbers" << persistentPoint.passiveGrabbers << "contexts" << persistentPoint.passiveGrabbersContext; @@ -189,7 +190,8 @@ void tst_qquickdeliveryagent::passiveGrabberOrder() QCOMPARE(persistentPoint.passiveGrabbers.last(), rootTap); QTest::mouseRelease(&view, Qt::LeftButton); - QTest::qWait(100); + QTRY_COMPARE(rootTap->isPressed(), false); + QTRY_COMPARE(subsceneTap->isPressed(), false); // QQuickWindow::event() has failsafe: clear all grabbers after release QCOMPARE(persistentPoint.passiveGrabbers.size(), 0); |