Debian Codegraph @

rc/qml/Components/SwipeArea.qml

raw
1 /*
2 * Copyright (C) 2012-2015 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 version 3 as
6 * published by the Free Software Foundation.
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 * Authors:
17 * Jim Nelson <[email protected]>
18 * Charles Lindsay <[email protected]>
19 */
20
21 import QtQuick 2.9
22 import Lomiri.Components 1.3
23
24 // Because some QML components' natural swiping motion requires custom
25 // event handling, this emulates it and allows for programmatic control
26 // when a swipe has occurred.
27 //
28 // TODO: Currently no support for vertical swiping
29 MouseArea {
30 /*!
31 */
32 signal startSwipe(bool leftToRight, int start)
33 /*!
34 */
35 signal swiping(bool leftToRight, int start, int distance)
36 /*!
37 */
38 signal swiped(bool leftToRight)
39 /*!
40 */
41 signal tapped(int x, int y, bool rightButton)
42 /*!
43 */
44 signal longPressed(int x, int y)
45
46 /*!
47 */
48 property int requiredHorizMovement: units.gu(1)
49
50 // internal
51 /*!
52 */
53 property int startX: -1
54 /*!
55 */
56 property bool leftToRight: true
57 /*!
58 */
59 property bool swipeStarted: false
60 /*!
61 */
62 property bool longPress: false
63
64 // internal
65 /*!
66 */
67 function reset() {
68 startX = -1;
69 leftToRight = true;
70 swipeStarted = false;
71 longPress = false;
72 }
73
74 preventStealing: swipeStarted
75 acceptedButtons: Qt.LeftButton | Qt.RightButton
76
77 onEnabledChanged: reset()
78 onVisibleChanged: reset()
79
80 onPositionChanged: {
81 if (longPress)
82 return;
83
84 // look for initial swipe
85 if (startX == -1) {
86 startX = mouse.x;
87
88 return;
89 }
90
91 var diff = 0;
92 if (mouse.x < startX) {
93 // once started, don't signal anything on other side of bounding point
94 if (swipeStarted && leftToRight)
95 return;
96
97 diff = startX - mouse.x;
98 leftToRight = false;
99 } else if (mouse.x > startX) {
100 // once started, don't signal anything on other side of bounding point
101 if (swipeStarted && !leftToRight)
102 return;
103
104 diff = mouse.x - startX;
105 leftToRight = true;
106 } else {
107 // If mouse.x == startX, there's no horizontal swiping going on. This
108 // lets e.g. the GridView steal vertical mouse movement for its flicking.
109 return;
110 }
111
112 if (swipeStarted) {
113 swiping(leftToRight, startX, diff);
114
115 return;
116 }
117
118 if (diff < requiredHorizMovement)
119 return;
120
121 swipeStarted = true;
122
123 startSwipe(leftToRight, startX);
124 swiping(leftToRight, startX, diff);
125 }
126
127 onPressAndHold: {
128 if (swipeStarted)
129 return;
130
131 longPress = true;
132 longPressed(mouse.x, mouse.y);
133 }
134
135 onReleased: {
136 if (swipeStarted)
137 swiped(leftToRight);
138 else if (!longPress)
139 tapped(mouse.x, mouse.y, (mouse.button == Qt.RightButton));
140
141 reset();
142 }
143 }