diff options
| author | Shawn Rutledge <shawn.rutledge@qt.io> | 2023-02-20 10:25:33 +0100 |
|---|---|---|
| committer | Shawn Rutledge <shawn.rutledge@qt.io> | 2023-03-03 04:49:46 +0000 |
| commit | dd3484343b294e830cf517b72cbbc167ca58e660 (patch) | |
| tree | 21855d2c6503d93b375bf9cfc1fb1c6a64057888 | |
| parent | 528d34d075c3ca673bf53e4e3dfe5d9912caf102 (diff) | |
doc: Add snippets and animations illustrating TapHandler.GesturePolicy
People are constantly confused by GesturePolicy and its default value,
so we really need a "glanceable" reference in the docs to show the
differences between use cases.
Also clarify the pitfalls with the default DragThreshold value.
We switch from the \value tag to a 2-column \table because the \image
would otherwise break the table, and also because it saves space and
acts as a meaningful reminder to have the animation right under the
enum value that is being documented.
Task-number: QTBUG-70397
Task-number: QTBUG-73262
Task-number: QTBUG-100534
Task-number: QTBUG-107239
Task-number: QTBUG-111310
Change-Id: I1ff45f58a8a8edf55f4a8696d881aa9e0bedcfe3
Reviewed-by: Oliver Eftevaag <oliver.eftevaag@qt.io>
(cherry picked from commit a6e196ce9cd327df53ef9b9db3020f7775ee1754)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit ae7069bfa7cc476d13dfa4e1eec9ddb960cd5557)
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
| -rw-r--r-- | src/quick/doc/images/pointerHandlers/dragReleaseMenu.webp | bin | 0 -> 86252 bytes | |||
| -rw-r--r-- | src/quick/doc/images/pointerHandlers/tapHandlerButtonReleaseWithinBounds.webp | bin | 0 -> 53154 bytes | |||
| -rw-r--r-- | src/quick/doc/images/pointerHandlers/tapHandlerButtonWithinBounds.webp | bin | 0 -> 27426 bytes | |||
| -rw-r--r-- | src/quick/doc/images/pointerHandlers/tapHandlerOverlappingButtons.webp | bin | 0 -> 51008 bytes | |||
| -rw-r--r-- | src/quick/doc/snippets/pointerHandlers/dragReleaseMenu.qml | 72 | ||||
| -rw-r--r-- | src/quick/doc/snippets/pointerHandlers/tapHandlerButtonReleaseWithinBounds.qml | 51 | ||||
| -rw-r--r-- | src/quick/doc/snippets/pointerHandlers/tapHandlerButtonWithinBounds.qml | 51 | ||||
| -rw-r--r-- | src/quick/doc/snippets/pointerHandlers/tapHandlerOverlappingButtons.qml | 51 | ||||
| -rw-r--r-- | src/quick/doc/src/concepts/inputhandlers/qtquickhandlers-index.qdoc | 3 | ||||
| -rw-r--r-- | src/quick/handlers/qquicktaphandler.cpp | 131 |
10 files changed, 315 insertions, 44 deletions
diff --git a/src/quick/doc/images/pointerHandlers/dragReleaseMenu.webp b/src/quick/doc/images/pointerHandlers/dragReleaseMenu.webp Binary files differnew file mode 100644 index 0000000000..16aaca4d86 --- /dev/null +++ b/src/quick/doc/images/pointerHandlers/dragReleaseMenu.webp diff --git a/src/quick/doc/images/pointerHandlers/tapHandlerButtonReleaseWithinBounds.webp b/src/quick/doc/images/pointerHandlers/tapHandlerButtonReleaseWithinBounds.webp Binary files differnew file mode 100644 index 0000000000..3edab7d7a1 --- /dev/null +++ b/src/quick/doc/images/pointerHandlers/tapHandlerButtonReleaseWithinBounds.webp diff --git a/src/quick/doc/images/pointerHandlers/tapHandlerButtonWithinBounds.webp b/src/quick/doc/images/pointerHandlers/tapHandlerButtonWithinBounds.webp Binary files differnew file mode 100644 index 0000000000..05cb2f2276 --- /dev/null +++ b/src/quick/doc/images/pointerHandlers/tapHandlerButtonWithinBounds.webp diff --git a/src/quick/doc/images/pointerHandlers/tapHandlerOverlappingButtons.webp b/src/quick/doc/images/pointerHandlers/tapHandlerOverlappingButtons.webp Binary files differnew file mode 100644 index 0000000000..0455097159 --- /dev/null +++ b/src/quick/doc/images/pointerHandlers/tapHandlerOverlappingButtons.webp diff --git a/src/quick/doc/snippets/pointerHandlers/dragReleaseMenu.qml b/src/quick/doc/snippets/pointerHandlers/dragReleaseMenu.qml new file mode 100644 index 0000000000..e6d2266408 --- /dev/null +++ b/src/quick/doc/snippets/pointerHandlers/dragReleaseMenu.qml @@ -0,0 +1,72 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +//![0] +import QtQuick + +Rectangle { + id: rect + width: 140; height: 100 + + //![1] + TapHandler { + id: menuPopupHandler + gesturePolicy: TapHandler.DragWithinBounds + onPressedChanged: + if (pressed) { + menu.x = point.position.x - menu.width / 2 + menu.y = point.position.y - menu.height / 2 + } else { + feedback.text = menu.highlightedMenuItem + selectFlash.start() + } + onCanceled: feedback.text = "canceled" + } + //![1] + + Column { + id: menu + visible: menuPopupHandler.pressed + opacity: Math.min(1, menuPopupHandler.timeHeld) + property string highlightedMenuItem: "" + Repeater { + model: [ "top", "middle", "bottom" ] + delegate: Rectangle { + property bool highlighted: menuPopupHandler.pressed && + contains(mapFromItem(rect, menuPopupHandler.point.position)) + onHighlightedChanged: { + if (highlighted) + menu.highlightedMenuItem = menuItemText.text + else if (menu.highlightedMenuItem === menuItemText.text) + menu.highlightedMenuItem = "" + } + width: 100 + height: 20 + color: highlighted ? "lightsteelblue" : "aliceblue" + Text { + id: menuItemText + anchors.centerIn: parent + text: modelData + } + } + } + } + + Text { + id: feedback + y: 6; anchors.horizontalCenter: parent.horizontalCenter + textFormat: Text.MarkdownText + text: "hold for context menu" + + SequentialAnimation on font.weight { + id: selectFlash + running: false + loops: 3 + PropertyAction { value: Font.Black } + PauseAnimation { duration: 100 } + PropertyAction { value: Font.Normal } + PauseAnimation { duration: 100 } + } + } +} +//![0] diff --git a/src/quick/doc/snippets/pointerHandlers/tapHandlerButtonReleaseWithinBounds.qml b/src/quick/doc/snippets/pointerHandlers/tapHandlerButtonReleaseWithinBounds.qml new file mode 100644 index 0000000000..3e8634a605 --- /dev/null +++ b/src/quick/doc/snippets/pointerHandlers/tapHandlerButtonReleaseWithinBounds.qml @@ -0,0 +1,51 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +//![0] +import QtQuick + +Item { + width: 120; height: 80 + + component Button : Rectangle { + id: button + signal clicked + property alias text: buttonLabel.text + + width: 80 + height: 40 + radius: 3 + property color dark: Qt.darker(palette.button, 1.3) + gradient: Gradient { + GradientStop { position: 0.0; color: tapHandler.pressed ? dark : palette.button } + GradientStop { position: 1.0; color: dark } + } + + SequentialAnimation on border.width { + id: tapFlash + running: false + loops: 3 + PropertyAction { value: 2 } + PauseAnimation { duration: 100 } + PropertyAction { value: 0 } + PauseAnimation { duration: 100 } + } + + //![1] + TapHandler { + id: tapHandler + gesturePolicy: TapHandler.ReleaseWithinBounds + onTapped: tapFlash.start() + } + //![1] + + Text { + id: buttonLabel + text: "Click Me" + color: palette.buttonText + anchors.centerIn: parent + } + } + + Button { x: 10; y: 10 } +} +//![0] diff --git a/src/quick/doc/snippets/pointerHandlers/tapHandlerButtonWithinBounds.qml b/src/quick/doc/snippets/pointerHandlers/tapHandlerButtonWithinBounds.qml new file mode 100644 index 0000000000..4563b44014 --- /dev/null +++ b/src/quick/doc/snippets/pointerHandlers/tapHandlerButtonWithinBounds.qml @@ -0,0 +1,51 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +//![0] +import QtQuick + +Item { + width: 120; height: 80 + + component Button : Rectangle { + id: button + signal clicked + property alias text: buttonLabel.text + + width: 80 + height: 40 + radius: 3 + property color dark: Qt.darker(palette.button, 1.3) + gradient: Gradient { + GradientStop { position: 0.0; color: tapHandler.pressed ? dark : palette.button } + GradientStop { position: 1.0; color: dark } + } + + SequentialAnimation on border.width { + id: tapFlash + running: false + loops: 3 + PropertyAction { value: 2 } + PauseAnimation { duration: 100 } + PropertyAction { value: 0 } + PauseAnimation { duration: 100 } + } + + //![1] + TapHandler { + id: tapHandler + gesturePolicy: TapHandler.WithinBounds + onTapped: tapFlash.start() + } + //![1] + + Text { + id: buttonLabel + text: "Click Me" + color: palette.buttonText + anchors.centerIn: parent + } + } + + Button { x: 10; y: 10 } +} +//![0] diff --git a/src/quick/doc/snippets/pointerHandlers/tapHandlerOverlappingButtons.qml b/src/quick/doc/snippets/pointerHandlers/tapHandlerOverlappingButtons.qml new file mode 100644 index 0000000000..917c863e53 --- /dev/null +++ b/src/quick/doc/snippets/pointerHandlers/tapHandlerOverlappingButtons.qml @@ -0,0 +1,51 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +//![0] +import QtQuick + +Item { + width: 120; height: 80 + + component Button : Rectangle { + signal clicked + property alias text: buttonLabel.text + + width: 80 + height: 40 + radius: 3 + property color dark: Qt.darker(palette.button, 1.3) + gradient: Gradient { + GradientStop { position: 0.0; color: tapHandler.pressed ? dark : palette.button } + GradientStop { position: 1.0; color: dark } + } + + SequentialAnimation on border.width { + id: tapFlash + running: false + loops: 3 + PropertyAction { value: 2 } + PauseAnimation { duration: 100 } + PropertyAction { value: 0 } + PauseAnimation { duration: 100 } + } + + //![1] + TapHandler { + id: tapHandler + gesturePolicy: TapHandler.DragThreshold // the default + onTapped: tapFlash.start() + } + //![1] + + Text { + id: buttonLabel + text: "Click Me" + color: palette.buttonText + anchors.centerIn: parent + } + } + + Button { x: 10; y: 10 } + Button { x: 30; y: 30 } +} +//![0] diff --git a/src/quick/doc/src/concepts/inputhandlers/qtquickhandlers-index.qdoc b/src/quick/doc/src/concepts/inputhandlers/qtquickhandlers-index.qdoc index 182773b0f5..1157364df8 100644 --- a/src/quick/doc/src/concepts/inputhandlers/qtquickhandlers-index.qdoc +++ b/src/quick/doc/src/concepts/inputhandlers/qtquickhandlers-index.qdoc @@ -70,7 +70,8 @@ PointHandler) can work only with passive grabs; others require exclusive grabs; and others can "lurk" with passive grabs until they detect that a gesture is being performed, and then make the transition from passive to - exclusive grab. + exclusive grab. TapHandler's grabbing behavior is + \l {TapHandler::gesturePolicy}{configurable}. When a grab transition is requested, \l PointerHandler::grabPermissions, \l QQuickItem::keepMouseGrab() and \l QQuickItem::keepTouchGrab() control diff --git a/src/quick/handlers/qquicktaphandler.cpp b/src/quick/handlers/qquicktaphandler.cpp index 018602ddff..68d0ae0200 100644 --- a/src/quick/handlers/qquicktaphandler.cpp +++ b/src/quick/handlers/qquicktaphandler.cpp @@ -199,49 +199,94 @@ void QQuickTapHandler::timerEvent(QTimerEvent *event) The \c gesturePolicy also affects grab behavior as described below. - \value TapHandler.DragThreshold - (the default value) The \l eventPoint must not move significantly. - If the mouse, finger or stylus moves past the system-wide drag - threshold (QStyleHints::startDragDistance), the tap gesture is - canceled, even if the button or finger is still pressed. This policy - can be useful whenever TapHandler needs to cooperate with other - input handlers (for example \l DragHandler) or event-handling Items - (for example QtQuick Controls), because in this case TapHandler - will not take the exclusive grab, but merely a - \l {QPointerEvent::addPassiveGrabber()}{passive grab}. - - \value TapHandler.WithinBounds - If the \l eventPoint leaves the bounds of the \c parent Item, the tap - gesture is canceled. The TapHandler will take the - \l {QPointerEvent::setExclusiveGrabber}{exclusive grab} on - press, but will release the grab as soon as the boundary constraint - is no longer satisfied. - - \value TapHandler.ReleaseWithinBounds - At the time of release (the mouse button is released or the finger - is lifted), if the \l eventPoint is outside the bounds of the - \c parent Item, a tap gesture is not recognized. This corresponds to - typical behavior for button widgets: you can cancel a click by - dragging outside the button, and you can also change your mind by - dragging back inside the button before release. Note that it's - necessary for TapHandler to take the - \l {QPointerEvent::setExclusiveGrabber}{exclusive grab} on press - and retain it until release in order to detect this gesture. - - \value TapHandler.DragWithinBounds - On press, TapHandler takes the - \l {QPointerEvent::setExclusiveGrabber}{exclusive grab}; after that, - the \l eventPoint can be dragged within the bounds of the \c parent - item, while the \l timeHeld property keeps counting, and the - \l longPressed() signal will be emitted regardless of drag distance. - However, like \c WithinBounds, if the point leaves the bounds, - the tap gesture is \l {PointerHandler::}{canceled()}, \l active() - becomes \c false, and \l timeHeld stops counting. This is suitable - for implementing press-drag-release components, such as menus, in - which a single TapHandler detects press, \c timeHeld drives an - "opening" animation, and then the user can drag to a menu item and - release, while never leaving the bounds of the parent scene containing - the menu. This value was added in Qt 6.3. + \table + \header + \li Constant + \li Description + \row + \li \c TapHandler.DragThreshold + \image pointerHandlers/tapHandlerOverlappingButtons.webp + Grab on press: \e passive + \li (the default value) The \l eventPoint must not move significantly. + If the mouse, finger or stylus moves past the system-wide drag + threshold (QStyleHints::startDragDistance), the tap gesture is + canceled, even if the device or finger is still pressed. This policy + can be useful whenever TapHandler needs to cooperate with other + input handlers (for example \l DragHandler) or event-handling Items + (for example \l {Qt Quick Controls}), because in this case TapHandler + will not take the exclusive grab, but merely a + \l {QPointerEvent::addPassiveGrabber()}{passive grab}. + That is, \c DragThreshold is especially useful to \e augment + existing behavior: it reacts to tap/click/long-press even when + another item or handler is already reacting, perhaps even in a + different layer of the UI. The following snippet shows one + TapHandler as used in one component; but if we stack up two + instances of the component, you will see the handlers in both of them + react simultaneously when a press occurs over both of them, because + the passive grab does not stop event propagation: + \quotefromfile pointerHandlers/tapHandlerOverlappingButtons.qml + \skipto Item + \printuntil component Button + \skipto TapHandler + \printuntil } + \skipuntil Text { + \skipuntil } + \printuntil Button + \printuntil Button + \printuntil } + + \row + \li \c TapHandler.WithinBounds + \image pointerHandlers/tapHandlerButtonWithinBounds.webp + Grab on press: \e exclusive + \li If the \l eventPoint leaves the bounds of the \c parent Item, the tap + gesture is canceled. The TapHandler will take the + \l {QPointerEvent::setExclusiveGrabber}{exclusive grab} on + press, but will release the grab as soon as the boundary constraint + is no longer satisfied. + \snippet pointerHandlers/tapHandlerButtonWithinBounds.qml 1 + + \row + \li \c TapHandler.ReleaseWithinBounds + \image pointerHandlers/tapHandlerButtonReleaseWithinBounds.webp + Grab on press: \e exclusive + \li At the time of release (the mouse button is released or the finger + is lifted), if the \l eventPoint is outside the bounds of the + \c parent Item, a tap gesture is not recognized. This corresponds to + typical behavior for button widgets: you can cancel a click by + dragging outside the button, and you can also change your mind by + dragging back inside the button before release. Note that it's + necessary for TapHandler to take the + \l {QPointerEvent::setExclusiveGrabber}{exclusive grab} on press + and retain it until release in order to detect this gesture. + \snippet pointerHandlers/tapHandlerButtonReleaseWithinBounds.qml 1 + + \row + \li \c TapHandler.DragWithinBounds + \image pointerHandlers/dragReleaseMenu.webp + Grab on press: \e exclusive + \li On press, TapHandler takes the + \l {QPointerEvent::setExclusiveGrabber}{exclusive grab}; after that, + the \l eventPoint can be dragged within the bounds of the \c parent + item, while the \l timeHeld property keeps counting, and the + \l longPressed() signal will be emitted regardless of drag distance. + However, like \c WithinBounds, if the point leaves the bounds, + the tap gesture is \l {PointerHandler::}{canceled()}, \l active() + becomes \c false, and \l timeHeld stops counting. This is suitable + for implementing press-drag-release components, such as menus, in + which a single TapHandler detects press, \c timeHeld drives an + "opening" animation, and then the user can drag to a menu item and + release, while never leaving the bounds of the parent scene containing + the menu. This value was added in Qt 6.3. + \snippet pointerHandlers/dragReleaseMenu.qml 1 + \endtable + + \note If you find that TapHandler is reacting in cases that conflict with + some other behavior, the first thing you should try is to think about which + \c gesturePolicy is appropriate. If you cannot fix it by changing \c gesturePolicy, + some cases are better served by adjusting \l {PointerHandler::}{grabPermissions}, + either in this handler, or in another handler that should \e prevent TapHandler + from reacting. */ void QQuickTapHandler::setGesturePolicy(QQuickTapHandler::GesturePolicy gesturePolicy) { |
