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/>.
18import Lomiri.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;
112 if (dragComponentProperties) {
113 priv.dragObject = dragComponent.createObject(root, dragComponentProperties);
115 priv.dragObject = dragComponent.createObject(root);
117 priv.dragObject.Drag.start();
121 if (priv.dragObject) {
122 var obj = priv.dragObject;
123 priv.dragObject = null;
131 target: priv.dragObject
132 when: priv.dragObject && priv.wasRecognisedDrag
135 if (!priv.dragObject) return 0;
137 for (var i = 0; i < root.touchPoints.length; i++) {
138 sum += root.touchPoints[i].x;
140 return sum/root.touchPoints.length - priv.dragObject.width/2;
145 target: priv.dragObject
146 when: priv.dragObject && priv.wasRecognisedDrag
149 if (!priv.dragObject) return 0;
151 for (var i = 0; i < root.touchPoints.length; i++) {
152 sum += root.touchPoints[i].y;
154 return sum/root.touchPoints.length - priv.dragObject.height/2;