18 import Ubuntu.Components 0.1
23 property int orientation: Qt.Vertical
24 property bool dragging
25 property real dragVelocity: 0
26 property real dragValue: (orientation == Qt.Vertical ? (mouseY - __pressedPosition.y)
27 : (mouseX - __pressedPosition.x))
28 property real lateralPosition: orientation == Qt.Horizontal ? MathUtils.clamp(mouseY, 0, height) : MathUtils.clamp(mouseX, 0, width)
29 property point __pressedPosition: Qt.point(0, 0)
30 property var __dragEvents: []
31 property bool clickValidated:
true
32 property bool zeroVelocityCounts:
false
36 property var __dateTime:
new function() {
37 this.getCurrentTimeMs =
function() {
return new Date().getTime()}
45 if (dragValue != 0 && pressed) {
59 function updateSpeed() {
61 for (var i=0; i<__dragEvents.length; i++) {
62 totalSpeed += __dragEvents[i][3]
65 if (zeroVelocityCounts || Math.abs(totalSpeed) > 0.001) {
66 dragVelocity = totalSpeed / __dragEvents.length * 1000
70 function cullOldDragEvents(currentTime) {
72 for (var numberOfCulledEvents=0; numberOfCulledEvents<__dragEvents.length-2; numberOfCulledEvents++) {
74 if (currentTime - __dragEvents[numberOfCulledEvents][0] <= 50)
break
77 __dragEvents.splice(0, numberOfCulledEvents)
80 function getEventSpeed(currentTime, event) {
81 if (__dragEvents.length != 0) {
82 var lastDrag = __dragEvents[__dragEvents.length-1]
83 var duration = Math.max(1, currentTime - lastDrag[0])
84 if (orientation == Qt.Vertical) {
85 return (event.y - lastDrag[2]) / duration
87 return (event.x - lastDrag[1]) / duration
94 function pushDragEvent(event) {
95 var currentTime = __dateTime.getCurrentTimeMs()
96 __dragEvents.push([currentTime, event.x, event.y, getEventSpeed(currentTime, event)])
97 cullOldDragEvents(currentTime)
105 if (!draggingArea.containsMouse)
106 clickValidated =
false
110 __pressedPosition = Qt.point(mouse.x, mouse.y)
113 clickValidated = true
118 __pressedPosition = Qt.point(mouse.x, mouse.y)