diff options
author | Mitch Curtis <mitch.curtis@theqtcompany.com> | 2016-03-16 15:49:04 +0100 |
---|---|---|
committer | J-P Nurmi <jpnurmi@theqtcompany.com> | 2016-03-22 07:56:21 +0000 |
commit | 7a96f2c6f184af87b6669a84a804d4ea4ff02578 (patch) | |
tree | 06a89d577063214b63c4482be2a8d456feb57727 | |
parent | f61ddb30690f5d1112980b058ed5cec749396c63 (diff) |
Drawer: respect swipe velocity
A fast enough swipe should open/close the drawer,
even if it doesn't cross the position threshold.
Change-Id: Iede4ec669f7d42d197e8a42623714c977b0c8a03
Reviewed-by: J-P Nurmi <jpnurmi@theqtcompany.com>
-rw-r--r-- | src/templates/qquickdrawer.cpp | 22 | ||||
-rw-r--r-- | tests/auto/controls/data/tst_drawer.qml | 35 |
2 files changed, 48 insertions, 9 deletions
diff --git a/src/templates/qquickdrawer.cpp b/src/templates/qquickdrawer.cpp index 7f0ef44c..58fe2d87 100644 --- a/src/templates/qquickdrawer.cpp +++ b/src/templates/qquickdrawer.cpp @@ -42,6 +42,7 @@ #include <QtQuick/private/qquickanimation_p.h> #include <QtQuick/private/qquickitemchangelistener_p.h> #include <QtQuickTemplates/private/qquickcontrol_p_p.h> +#include <QtQuickTemplates/private/qquickvelocitycalculator_p_p.h> QT_BEGIN_NAMESPACE @@ -110,6 +111,7 @@ public: qreal offset; qreal position; QPointF pressPoint; + QQuickVelocityCalculator velocityCalculator; QQuickItem *content; QQuickPropertyAnimation *animation; }; @@ -167,6 +169,8 @@ bool QQuickDrawerPrivate::handleMousePressEvent(QQuickItem *item, QMouseEvent *e event->accept(); } + velocityCalculator.startMeasuring(pressPoint, event->timestamp()); + return item == q; } @@ -204,21 +208,20 @@ bool QQuickDrawerPrivate::handleMouseMoveEvent(QQuickItem *item, QMouseEvent *ev return q->keepMouseGrab(); } +static const qreal openCloseVelocityThreshold = 300; + bool QQuickDrawerPrivate::handleMouseReleaseEvent(QQuickItem *item, QMouseEvent *event) { Q_Q(QQuickDrawer); bool wasGrabbed = q->keepMouseGrab(); if (wasGrabbed) { -// int startDragVelocity = QGuiApplication::styleHints()->startDragVelocity(); -// if (startDragVelocity && QGuiApplicationPrivate::mouseEventCaps(event) & QTouchDevice::Velocity) { -// QVector2D velocity = QGuiApplicationPrivate::mouseEventVelocity(event); -// qreal vel = (edge == Qt::LeftEdge || edge == Qt::RightEdge) ? velocity.x() : velocity.y(); -// qDebug() << vel << "vs." << startDragVelocity; -// } - if (position < 0.3) { - q->close(); - } else if (position > 0.7) { + velocityCalculator.stopMeasuring(event->pos(), event->timestamp()); + const qreal velocity = velocityCalculator.velocity().x(); + + if (position > 0.7 || velocity > openCloseVelocityThreshold) { q->open(); + } else if (position < 0.3 || velocity < -openCloseVelocityThreshold) { + q->close(); } else { switch (edge) { case Qt::LeftEdge: @@ -465,6 +468,7 @@ void QQuickDrawer::mouseUngrabEvent() Q_D(QQuickDrawer); QQuickControl::mouseUngrabEvent(); d->pressPoint = QPoint(); + d->velocityCalculator.reset(); } void QQuickDrawer::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) diff --git a/tests/auto/controls/data/tst_drawer.qml b/tests/auto/controls/data/tst_drawer.qml index 28db11c8..fff8ec49 100644 --- a/tests/auto/controls/data/tst_drawer.qml +++ b/tests/auto/controls/data/tst_drawer.qml @@ -63,4 +63,39 @@ TestCase { verify(control.animation) control.destroy() } + + Component { + id: rectDrawer + + Drawer { + Rectangle { + width: 200 + height: 400 + color: "steelblue" + } + } + } + + function test_swipeVelocity() { + skip("QTBUG-52003"); + + var control = rectDrawer.createObject(testCase) + verify(control.contentItem) + compare(control.edge, Qt.LeftEdge) + compare(control.position, 0.0) + + var dragDistance = Math.max(20, Qt.styleHints.startDragDistance + 5) + var distance = dragDistance * 1.1 + if (distance >= control.width * 0.7) + skip("This test requires a startDragDistance that is less than the opening threshold of the drawer") + + mousePress(control, 0, 0, Qt.LeftButton) + mouseMove(control, distance, 0, Qt.LeftButton) + verify(control.position > 0) + tryCompare(control, "position", distance / control.contentItem.width) + mouseRelease(control, distance, 0, Qt.LeftButton) + tryCompare(control, "position", 1.0) + + control.destroy() + } } |