Unity 8
DragHandle.qml
1 /*
2  * Copyright (C) 2013,2016 Canonical, Ltd.
3  *
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.
7  *
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.
12  *
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/>.
15  */
16 
17 import QtQuick 2.4
18 import Ubuntu.Components 1.3
19 import Ubuntu.Gestures 0.1
20 
21 /*
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.
24 
25  This example shows a DragHandle placed on the right corner of a Showable, used
26  to slide it away, off the screen.
27 
28  Showable {
29  x: 0
30  y: 0
31  width: ... // screen width
32  height: ... // screen height
33  shown: true
34  ...
35  DragHandle {
36  anchors.right: parent.right
37  anchors.top: parent.top
38  anchors.bottom: parent.bottom
39  width: units.gu(2)
40 
41  direction: SwipeArea::Leftwards
42  }
43  }
44 
45  */
46 SwipeArea {
47  id: dragArea
48 
49  property bool stretch: false
50 
51  property alias autoCompleteDragThreshold: dragEvaluator.dragThreshold
52 
53  // How far you can drag
54  property real maxTotalDragDistance: {
55  if (stretch) {
56  0; // not enough context information to set a sensible default
57  } else {
58  Direction.isHorizontal(direction) ? parent.width : parent.height;
59  }
60  }
61 
62  property real hintDisplacement: 0
63 
64  immediateRecognition: hintDisplacement > 0
65 
66  property var overrideStartValue: undefined
67  SmoothedAnimation {
68  id: hintingAnimation
69  target: hintingAnimation
70  property: "targetValue"
71  duration: 150
72  velocity: -1
73 
74  to: d.incrementTargetProp ? d.startValue + hintDisplacement
75  : d.startValue - hintDisplacement
76  property real targetValue
77  onTargetValueChanged: {
78  if (!running) {
79  return;
80  }
81 
82  if (d.incrementTargetProp) {
83  if (parent[d.targetProp] < targetValue) {
84  parent[d.targetProp] = targetValue;
85  }
86  } else {
87  if (parent[d.targetProp] > targetValue) {
88  parent[d.targetProp] = targetValue;
89  }
90  }
91  }
92  }
93 
94  // Private stuff
95  QtObject {
96  id: d
97 
98  // Whether movement along the designated direction will increment the value of the target property
99  readonly property bool incrementTargetProp: (Direction.isPositive(direction) && !dragArea.stretch)
100  || (dragArea.stretch && !d.dragParent.shown)
101 
102  property real startValue
103  property real minValue: {
104  if (direction == Direction.Horizontal) {
105  return startValue - maxTotalDragDistance;
106  } else if (incrementTargetProp) {
107  return startValue;
108  } else {
109  return startValue - maxTotalDragDistance;
110  }
111  }
112 
113  property real maxValue: incrementTargetProp ? startValue + maxTotalDragDistance
114  : startValue;
115 
116  property var dragParent: dragArea.parent
117 
118  // The property of DragHandle's parent that will be modified
119  property string targetProp: {
120  if (stretch) {
121  Direction.isHorizontal(direction) ? "width" : "height";
122  } else {
123  Direction.isHorizontal(direction) ? "x" : "y";
124  }
125  }
126 
127  function limitMovement(distance) {
128  var targetValue = MathUtils.clamp(d.startValue + distance, minValue, maxValue);
129  var diff = targetValue - d.startValue;
130 
131  if (hintDisplacement == 0) {
132  return diff;
133  }
134 
135  // we should not go behind hintingAnimation's current value
136  if (d.incrementTargetProp) {
137  if (d.startValue + diff < hintingAnimation.targetValue) {
138  diff = hintingAnimation.targetValue - d.startValue;
139  }
140  } else {
141  if (d.startValue + diff > hintingAnimation.targetValue) {
142  diff = hintingAnimation.targetValue - d.startValue;
143  }
144  }
145 
146  return diff;
147  }
148 
149  function onFinishedRecognizedGesture() {
150  if (dragEvaluator.shouldAutoComplete()) {
151  completeDrag();
152  } else {
153  rollbackDrag();
154  }
155  }
156 
157  function completeDrag() {
158  if (dragParent.shown) {
159  dragParent.hide();
160  } else {
161  dragParent.show();
162  }
163  }
164 
165  function rollbackDrag() {
166  if (dragParent.shown) {
167  dragParent.show();
168  } else {
169  dragParent.hide();
170  }
171  }
172  }
173 
174  property alias edgeDragEvaluator: dragEvaluator
175 
176  EdgeDragEvaluator {
177  objectName: "edgeDragEvaluator"
178  id: dragEvaluator
179  // Effectively convert distance into the drag position projected onto the gesture direction axis
180  trackedPosition: Direction.isPositive(dragArea.direction) ? distance : -distance
181  maxDragDistance: maxTotalDragDistance
182  direction: dragArea.direction
183  }
184 
185  onDistanceChanged: {
186  if (dragging) {
187  if (!Direction.isPositive(direction))
188  distance = -distance;
189 
190  if (dragArea.stretch &&
191  ((!Direction.isPositive(direction) && !d.dragParent.shown)
192  ||
193  (Direction.isPositive(direction) && d.dragParent.shown))
194  )
195  {
196  // This happens when you have a stretching showable being shown from the right or
197  // top edge (and consequently being hidden when dragged towards the right/top edge)
198  // In those situations, dimension expansion/retraction happens in the opposite
199  // sign of the axis direction
200  distance = -distance;
201  }
202 
203  var toAdd = d.limitMovement(distance);
204  parent[d.targetProp] = d.startValue + toAdd;
205  }
206  }
207 
208  onDraggingChanged: {
209  if (dragging) {
210  dragEvaluator.reset();
211  if (overrideStartValue !== undefined) {
212  d.startValue = overrideStartValue;
213  } else {
214  d.startValue = parent[d.targetProp];
215  }
216 
217  if (hintDisplacement > 0) {
218  hintingAnimation.targetValue = d.startValue;
219  hintingAnimation.start();
220  }
221  } else {
222  hintingAnimation.stop();
223  d.onFinishedRecognizedGesture();
224  }
225  }
226 }