File: DragHandle.qml

package info (click to toggle)
lomiri 0.5.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,088 kB
  • sloc: cpp: 39,498; python: 2,559; javascript: 1,426; ansic: 1,012; sh: 289; xml: 252; makefile: 69
file content (227 lines) | stat: -rw-r--r-- 7,202 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
 * Copyright (C) 2013, 2016 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import QtQuick 2.15
import Lomiri.Components 1.3
import Lomiri.Gestures 0.1

/*
 Put a DragHandle inside a Showable to enable the user to drag it from that handle.
 Main use case is to drag fullscreen Showables into the screen or off the screen.

 This example shows a DragHandle placed on the right corner of a Showable, used
 to slide it away, off the screen.

  Showable {
    x: 0
    y: 0
    width: ... // screen width
    height: ... // screen height
    shown: true
    ...
    DragHandle {
        anchors.right: parent.right
        anchors.top: parent.top
        anchors.bottom: parent.bottom
        width: units.gu(2)

        direction: SwipeArea::Leftwards
    }
  }

 */
SwipeArea {
    id: dragArea

    property bool stretch: false

    property alias autoCompleteDragThreshold: dragEvaluator.dragThreshold

    // How far you can drag
    property real maxTotalDragDistance: {
        if (stretch) {
            0; // not enough context information to set a sensible default
        } else {
            Direction.isHorizontal(direction) ? parent.width : parent.height;
        }
    }

    property real hintDisplacement: 0

    immediateRecognition: hintDisplacement > 0

    property var overrideStartValue: undefined
    SmoothedAnimation {
        id: hintingAnimation
        target: hintingAnimation
        objectName: "hintingAnimation"
        property: "targetValue"
        duration: 150
        velocity: -1

        to: d.incrementTargetProp ? d.startValue + hintDisplacement
                                  : d.startValue - hintDisplacement
        property real targetValue
        onTargetValueChanged: {
            if (!running) {
                return;
            }

            if (d.incrementTargetProp) {
                if (parent[d.targetProp] < targetValue) {
                    parent[d.targetProp] = targetValue;
                }
            } else {
                if (parent[d.targetProp] > targetValue) {
                    parent[d.targetProp] = targetValue;
                }
            }
        }
    }

    // Private stuff
    QtObject {
        id: d

        // Whether movement along the designated direction will increment the value of the target property
        readonly property bool incrementTargetProp: (Direction.isPositive(direction) && !dragArea.stretch)
                                                 || (dragArea.stretch && !d.dragParent.shown)

        property real startValue
        property real minValue: {
            if (direction == Direction.Horizontal) {
                return startValue - maxTotalDragDistance;
            } else if (incrementTargetProp) {
                return startValue;
            } else {
                return startValue - maxTotalDragDistance;
            }
        }

        property real maxValue: incrementTargetProp ? startValue + maxTotalDragDistance
                                                    : startValue;

        property var dragParent: dragArea.parent

        // The property of DragHandle's parent that will be modified
        property string targetProp: {
            if (stretch) {
                Direction.isHorizontal(direction) ? "width" : "height";
            } else {
                Direction.isHorizontal(direction) ? "x" : "y";
            }
        }

        function limitMovement(distance) {
            var targetValue = MathUtils.clamp(d.startValue + distance, minValue, maxValue);
            var diff = targetValue - d.startValue;

            if (hintDisplacement == 0) {
                return diff;
            }

            // we should not go behind hintingAnimation's current value
            if (d.incrementTargetProp) {
                if (d.startValue + diff < hintingAnimation.targetValue) {
                    diff = hintingAnimation.targetValue - d.startValue;
                }
            } else {
                if (d.startValue + diff > hintingAnimation.targetValue) {
                    diff = hintingAnimation.targetValue - d.startValue;
                }
            }

            return diff;
        }

        function onFinishedRecognizedGesture() {
            if (dragEvaluator.shouldAutoComplete()) {
                completeDrag();
            } else {
                rollbackDrag();
            }
        }

        function completeDrag() {
            if (dragParent.shown) {
                dragParent.hide();
            } else {
                dragParent.show();
            }
        }

        function rollbackDrag() {
            if (dragParent.shown) {
                dragParent.show();
            } else {
                dragParent.hide();
            }
        }
    }

    property alias edgeDragEvaluator: dragEvaluator

    EdgeDragEvaluator {
        objectName: "edgeDragEvaluator"
        id: dragEvaluator
        // Effectively convert distance into the drag position projected onto the gesture direction axis
        trackedPosition: Direction.isPositive(dragArea.direction) ? distance : -distance
        maxDragDistance: maxTotalDragDistance
        direction: dragArea.direction
    }

    onDistanceChanged: {
        if (dragging) {
            if (!Direction.isPositive(direction))
                distance = -distance;

            if (dragArea.stretch &&
                   ((!Direction.isPositive(direction) && !d.dragParent.shown)
                     ||
                    (Direction.isPositive(direction) && d.dragParent.shown))
               )
            {
                // This happens when you have a stretching showable being shown from the right or
                // top edge (and consequently being hidden when dragged towards the right/top edge)
                // In those situations, dimension expansion/retraction happens in the opposite
                // sign of the axis direction
                distance = -distance;
            }

            var toAdd = d.limitMovement(distance);
            parent[d.targetProp] = d.startValue + toAdd;
        }
    }

    onDraggingChanged: {
        if (dragging) {
            dragEvaluator.reset();
            if (overrideStartValue !== undefined) {
                d.startValue = overrideStartValue;
            } else {
                d.startValue = parent[d.targetProp];
            }

            if (hintDisplacement > 0) {
                hintingAnimation.targetValue = d.startValue;
                hintingAnimation.start();
            }
        } else {
            hintingAnimation.stop();
            d.onFinishedRecognizedGesture();
        }
    }
}