2 * Copyright (C) 2016 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.Gestures 0.1
25 property bool enableDrag: true
26 property Component dragComponent
27 property var dragComponentProperties: undefined
29 readonly property bool recognisedPress: status == TouchGestureArea.Recognized &&
30 touchPoints.length >= minimumTouchPoints &&
31 touchPoints.length <= maximumTouchPoints
32 readonly property bool recognisedDrag: priv.wasRecognisedPress && dragging
34 signal pressed(int x, int y)
42 if (priv.dragObject) root.cancelled();
43 priv.wasRecognisedDrag = false;
44 priv.wasRecognisedPress = false;
48 onRecognisedPressChanged: {
49 if (recognisedPress) {
50 // get the app at the center of the gesture
53 for (var i = 0; i < touchPoints.length; i++) {
54 centerX += touchPoints[i].x;
55 centerY += touchPoints[i].y;
57 centerX = centerX/touchPoints.length;
58 centerY = centerY/touchPoints.length;
60 pressed(centerX, centerY);
61 priv.wasRecognisedPress = true;
66 if (status != TouchGestureArea.Recognized) {
67 if (status == TouchGestureArea.Rejected) {
69 } else if (status == TouchGestureArea.WaitingForTouch) {
70 if (priv.wasRecognisedPress) {
71 if (!priv.wasRecognisedDrag) {
78 priv.wasRecognisedDrag = false;
79 priv.wasRecognisedPress = false;
83 onRecognisedDragChanged: {
84 if (enableDrag && recognisedDrag) {
85 priv.wasRecognisedDrag = true;
92 property var dragObject: null
94 property bool wasRecognisedPress: false
95 property bool wasRecognisedDrag: false
99 if (priv.dragObject) {
100 var obj = priv.dragObject;
101 priv.dragObject = null;
109 if (dragComponentProperties) {
110 priv.dragObject = dragComponent.createObject(root, dragComponentProperties);
112 priv.dragObject = dragComponent.createObject(root);
114 priv.dragObject.Drag.start();
118 if (priv.dragObject) {
119 var obj = priv.dragObject;
120 priv.dragObject = null;
128 target: priv.dragObject
129 when: priv.dragObject && priv.wasRecognisedDrag
132 if (!priv.dragObject) return 0;
134 for (var i = 0; i < root.touchPoints.length; i++) {
135 sum += root.touchPoints[i].x;
137 return sum/root.touchPoints.length - priv.dragObject.width/2;
142 target: priv.dragObject
143 when: priv.dragObject && priv.wasRecognisedDrag
146 if (!priv.dragObject) return 0;
148 for (var i = 0; i < root.touchPoints.length; i++) {
149 sum += root.touchPoints[i].y;
151 return sum/root.touchPoints.length - priv.dragObject.height/2;