2
* Copyright (C) 2013 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.Components 0.1
19
import "Math.js" as MathLocal
24
property Showable target
25
property var hintingAnimation: hintingAnimation
26
property string boundProperty: orientation == Qt.Vertical ? "y" : "x"
27
property int orientation: Qt.Vertical
28
property int direction: Qt.LeftToRight
29
property real openedValue: orientation == Qt.Vertical ? y : x
30
property real closedValue: orientation == Qt.Vertical ? y + (direction == Qt.LeftToRight ? -height : height) : x + (direction == Qt.LeftToRight ? -width : width)
31
property real hintDisplacement: 0
32
property real handleSize: units.gu(2)
33
property real dragVelocity: draggingArea.dragVelocity != 0 ? Math.abs(draggingArea.dragVelocity) : -1
34
property real dragVelocityThreshold: units.gu(5)
35
property bool dragging: false
36
property bool pressed: draggingArea.pressed
37
property int lateralPosition: draggingArea.lateralPosition
38
property real dragPosition
39
property bool openOnPress: true
41
signal openPressed(int mouseX, int mouseY)
42
signal openReleased(int mouseX, int mouseY)
49
if (orientation == Qt.Vertical) {
50
value = draggingArea.dragValue + draggingArea.y
51
if (direction == Qt.RightToLeft) {
52
value += draggingArea.height - height
55
value = draggingArea.dragValue + draggingArea.x
56
if (direction == Qt.RightToLeft) {
57
value += draggingArea.width - width
61
if (direction == Qt.LeftToRight) {
66
} else if (dragging) {
67
if (direction == Qt.LeftToRight) {
68
value += hintDisplacement
70
value -= hintDisplacement
76
property var draggingArea: leftDraggingArea.enabled ? leftDraggingArea : rightDraggingArea
78
property real __hintValue: closedValue + (direction == Qt.LeftToRight ? hintDisplacement : -hintDisplacement)
80
function dragToValue(dragPosition) {
81
return dragPosition + closedValue
84
property bool __opened: target.shown
85
enabled: target.available
87
// Can be replaced with a fake implementation during tests
88
// property var __getCurrentTimeMs: function () { return new Date().getTime() }
89
property var __dateTime: new function() {
90
this.getCurrentTimeMs = function() {return new Date().getTime()}
93
Component.onCompleted: target[boundProperty] = __opened ? openedValue : closedValue
94
onOpenedValueChanged: if (__opened && !dragging) target[boundProperty] = openedValue
95
onClosedValueChanged: if (!__opened && !dragging) target[boundProperty] = closedValue
97
function __computeValue(dragPosition) {
98
return MathLocal.clamp(dragToValue(dragPosition), __hintValue, openedValue)
102
hintingAnimation.stop()
107
hintingAnimation.stop()
112
target.showAnimation.stop()
113
target.hideAnimation.stop()
114
hintingAnimation.restart()
117
function __settle() {
118
hintingAnimation.stop()
119
if (__opened) target.show()
123
function __startDragging() {
124
hintingAnimation.stop()
128
function __endDragging(dragVelocity) {
130
if (revealer.direction == Qt.RightToLeft) {
131
dragVelocity = -dragVelocity
133
if (Math.abs(dragVelocity) >= dragVelocityThreshold) {
134
if (dragVelocity > 0) __open()
144
target: revealer.target
145
property: revealer.boundProperty
146
value: __computeValue(dragPosition)
153
target: revealer.target
154
property: revealer.boundProperty
156
to: revealer.__hintValue
162
property bool isOpeningArea: revealer.direction == Qt.LeftToRight
164
height: orientation == Qt.Vertical ? handleSize : parent.height
165
width: orientation == Qt.Horizontal ? handleSize : parent.width
166
orientation: revealer.orientation
167
enabled: isOpeningArea ? !revealer.__opened : revealer.__opened
169
__dateTime: revealer.__dateTime
173
if (revealer.openOnPress) {
174
revealer.openPressed(mouseX, mouseY)
178
revealer.closePressed()
182
if (isOpeningArea && revealer.openOnPress) {
183
revealer.openReleased(mouseX, mouseY)
187
onDragStart: __startDragging()
188
onDragEnd: __endDragging(dragVelocity)
190
if (clickValidated) {
192
if (revealer.openOnPress) revealer.openClicked()
194
revealer.closeClicked()
201
id: rightDraggingArea
203
property bool isOpeningArea: revealer.direction == Qt.RightToLeft
205
x: orientation == Qt.Vertical ? 0 : parent.width - width
206
y: orientation == Qt.Vertical ? parent.height - height : 0
207
height: orientation == Qt.Vertical ? handleSize : parent.height
208
width: orientation == Qt.Horizontal ? handleSize : parent.width
209
orientation: revealer.orientation
210
enabled: isOpeningArea ? !revealer.__opened : revealer.__opened
212
__dateTime: revealer.__dateTime
216
if (revealer.openOnPress) {
217
revealer.openPressed(mouseX, mouseY)
221
revealer.closePressed()
225
if (isOpeningArea && revealer.openOnPress) {
226
revealer.openReleased(mouseX, mouseY)
230
onDragStart: __startDragging()
231
onDragEnd: __endDragging(dragVelocity)
233
if (clickValidated) {
235
if (revealer.openOnPress) revealer.openClicked()
237
revealer.closeClicked()