2 * Copyright (C) 2013 Canonical, Ltd.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 import Ubuntu.Components 1.3
19 import Ubuntu.Gestures 0.1
22 Put a DragHandle inside a Showable to enable the user to drag it from that handle.
23 Main use case is to drag fullscreen Showables into the screen or off the screen.
25 This example shows a DragHandle placed on the right corner of a Showable, used
26 to slide it away, off the screen.
31 width: ... // screen width
32 height: ... // screen height
36 anchors.right: parent.right
37 anchors.top: parent.top
38 anchors.bottom: parent.bottom
41 direction: DirectionalDragArea::Leftwards
49 property bool stretch: false
51 property alias autoCompleteDragThreshold: dragEvaluator.dragThreshold
53 // How far you can drag
54 property real maxTotalDragDistance: {
56 0; // not enough context information to set a sensible default
58 Direction.isHorizontal(direction) ? parent.width : parent.height;
62 property real hintDisplacement: 0
64 immediateRecognition: hintDisplacement > 0
66 property var overrideStartValue: undefined
69 target: hintingAnimation
70 property: "targetValue"
73 to: Direction.isPositive(direction) ? d.startValue + hintDisplacement
74 : d.startValue - hintDisplacement
75 property real targetValue
76 onTargetValueChanged: {
81 if (Direction.isPositive(direction)) {
82 if (parent[d.targetProp] < targetValue) {
83 parent[d.targetProp] = targetValue;
86 if (parent[d.targetProp] > targetValue) {
87 parent[d.targetProp] = targetValue;
96 property real startValue
97 property real minValue: {
98 if (direction == Direction.Horizontal) {
99 return startValue - maxTotalDragDistance;
100 } else if (Direction.isPositive(direction)) {
103 return startValue - maxTotalDragDistance;
107 property real maxValue: Direction.isPositive(direction) ? startValue + maxTotalDragDistance
110 property var dragParent: dragArea.parent
112 // The property of DragHandle's parent that will be modified
113 property string targetProp: {
115 Direction.isHorizontal(direction) ? "width" : "height";
117 Direction.isHorizontal(direction) ? "x" : "y";
121 function limitMovement(inputStep) {
122 var targetValue = MathUtils.clamp(dragParent[targetProp] + inputStep, minValue, maxValue);
123 var step = targetValue - dragParent[targetProp];
125 if (hintDisplacement == 0) {
129 // we should not go behind hintingAnimation's current value
130 if (Direction.isPositive(direction)) {
131 if (dragParent[targetProp] + step < hintingAnimation.targetValue) {
132 step = hintingAnimation.targetValue - dragParent[targetProp];
135 if (dragParent[targetProp] + step > hintingAnimation.targetValue) {
136 step = hintingAnimation.targetValue - dragParent[targetProp];
143 function onFinishedRecognizedGesture() {
144 if (dragEvaluator.shouldAutoComplete()) {
151 function completeDrag() {
152 if (dragParent.shown) {
159 function rollbackDrag() {
160 if (dragParent.shown) {
168 property alias edgeDragEvaluator: dragEvaluator
171 objectName: "edgeDragEvaluator"
173 // Effectively convert distance into the drag position projected onto the gesture direction axis
174 trackedPosition: Direction.isPositive(dragArea.direction) ? sceneDistance : -sceneDistance
175 maxDragDistance: maxTotalDragDistance
176 direction: dragArea.direction
181 // don't go the whole distance in order to smooth out the movement
182 var step = distance * 0.3;
184 step = d.limitMovement(step);
186 parent[d.targetProp] += step;
192 dragEvaluator.reset();
193 if (overrideStartValue !== undefined) {
194 d.startValue = overrideStartValue;
196 d.startValue = parent[d.targetProp];
199 if (hintDisplacement > 0) {
200 hintingAnimation.targetValue = d.startValue;
201 hintingAnimation.start();
204 hintingAnimation.stop();
205 d.onFinishedRecognizedGesture();