-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathSwipeMixin.qml
69 lines (60 loc) · 1.94 KB
/
SwipeMixin.qml
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
/// mixin provides touch swipe events
BaseMixin {
signal touchMove; ///< @private
signal touchStart; ///< @private
signal touchEnd; ///< @private
signal verticalSwiped; ///< emitted on vertical swipe
signal horizontalSwiped; ///< emitted on horizontal swipe
constructor: { this._bindTouch(this.enabled) }
onEnabledChanged: { this._bindTouch(value) }
///@private
function _bindTouch(value) {
if (value && !this._touchBinder) {
this._touchBinder = new _globals.core.EventBinder(this.parent.element)
this._touchBinder.on('touchstart', function(event) { this.touchStart(event) }.bind(this))
this._touchBinder.on('touchend', function(event) { this.touchEnd(event) }.bind(this))
this._touchBinder.on('touchmove', function(event) { this.touchMove(event) }.bind(this))
}
if (this._touchBinder)
this._touchBinder.enable(value)
}
///@private
onTouchStart(event): {
var box = this.parent.toScreen()
var e = event.touches[0]
var x = e.pageX - box[0]
var y = e.pageY - box[1]
this._startX = x
this._startY = y
this._orientation = null;
this._startTarget = event.target;
}
///@private
onTouchMove(event): {
var box = this.parent.toScreen()
var e = event.touches[0]
var x = e.pageX - box[0]
var y = e.pageY - box[1]
var dx = x - this._startX
var dy = y - this._startY
var adx = Math.abs(dx)
var ady = Math.abs(dy)
var motion = adx > 5 || ady > 5
if (!motion)
return
if (!this._orientation)
this._orientation = adx > ady ? 'horizontal' : 'vertical'
// for delegated events, the target may change over time
// this ensures we notify the right target and simulates the mouseleave behavior
while (event.target && event.target !== this._startTarget)
event.target = event.target.parentNode;
if (event.target !== this._startTarget) {
event.target = this._startTarget;
return;
}
if (this._orientation == 'horizontal')
this.horizontalSwiped(event)
else
this.verticalSwiped(event)
}
}