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 in152e12dc22
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 Change-Id: If6a86d955e19810cb06de659f5e39b50a72fa762 Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io> (cherry picked from commit1b166c87d0
) Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
This commit is contained in:
parent
187d7afb4e
commit
6e3c25f944
|
@ -181,6 +181,7 @@ Rectangle {
|
|||
TapHandler {
|
||||
id: tapHandler
|
||||
enabled: root.activePageCount > 0
|
||||
gesturePolicy: TapHandler.ReleaseWithinBounds
|
||||
onTapped: {
|
||||
pageContainer.children[pageContainer.children.length - 1].exit()
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -177,7 +177,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;
|
||||
|
@ -187,7 +188,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);
|
||||
|
||||
|
|
Loading…
Reference in New Issue