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/>.
19import Lomiri.Gestures 0.1
26 property bool enableDrag: true
27 property Component dragComponent
28 property var dragComponentProperties: undefined
30 readonly property bool recognisedPress: status == TouchGestureArea.Recognized &&
31 touchPoints.length >= minimumTouchPoints &&
32 touchPoints.length <= maximumTouchPoints
33 readonly property bool recognisedDrag: priv.wasRecognisedPress && dragging
35 signal pressed(int x, int y)
43 if (priv.dragObject) root.cancelled();
44 priv.wasRecognisedDrag = false;
45 priv.wasRecognisedPress = false;
49 onRecognisedPressChanged: {
50 if (recognisedPress) {
51 // get the app at the center of the gesture
54 for (var i = 0; i < touchPoints.length; i++) {
55 centerX += touchPoints[i].x;
56 centerY += touchPoints[i].y;
58 centerX = centerX/touchPoints.length;
59 centerY = centerY/touchPoints.length;
61 pressed(centerX, centerY);
62 priv.wasRecognisedPress = true;
67 if (status != TouchGestureArea.Recognized) {
68 if (status == TouchGestureArea.Rejected) {
70 } else if (status == TouchGestureArea.WaitingForTouch) {
71 if (priv.wasRecognisedPress) {
72 if (!priv.wasRecognisedDrag) {
79 priv.wasRecognisedDrag = false;
80 priv.wasRecognisedPress = false;
84 onRecognisedDragChanged: {
85 if (enableDrag && recognisedDrag) {
86 priv.wasRecognisedDrag = true;
93 property var dragObject: null
95 property bool wasRecognisedPress: false
96 property bool wasRecognisedDrag: false
100 if (priv.dragObject) {
101 var obj = priv.dragObject;
102 priv.dragObject = null;
113 if (dragComponentProperties) {
114 priv.dragObject = dragComponent.createObject(root, dragComponentProperties);
116 priv.dragObject = dragComponent.createObject(root);
118 priv.dragObject.Drag.start();
122 if (priv.dragObject) {
123 var obj = priv.dragObject;
124 priv.dragObject = null;
132 target: priv.dragObject
133 when: priv.dragObject && priv.wasRecognisedDrag
136 if (!priv.dragObject) return 0;
138 for (var i = 0; i < root.touchPoints.length; i++) {
139 sum += root.touchPoints[i].x;
141 return sum/root.touchPoints.length - priv.dragObject.width/2;
143 restoreMode: Binding.RestoreBinding
147 target: priv.dragObject
148 when: priv.dragObject && priv.wasRecognisedDrag
151 if (!priv.dragObject) return 0;
153 for (var i = 0; i < root.touchPoints.length; i++) {
154 sum += root.touchPoints[i].y;
156 return sum/root.touchPoints.length - priv.dragObject.height/2;
158 restoreMode: Binding.RestoreBinding