aboutsummaryrefslogtreecommitdiffstats
path: root/src/imports/flowview/SwipeInteraction.qml
blob: c24ece72812786a5fee434543009f8dcaed3b5f3 (plain)
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
// Copyright (C) 2026 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

import QtQuick 2.12
import TransitionItem 1.0
import QtQuick.Controls 2.3
import QtQuick.Timeline 1.0

Item {
    id: root
    property var transitionView
    property var list
    parent: transitionView
    anchors.fill: parent
    z: -1

    property real threshold: 35
    property bool horizontal: true
    property Item __activeItem
    property bool __reactToTransition: false

    Timer {
        id: blocker
        running: false
        interval: 200
        repeat: false
        onTriggered: handler.enabled = true
    }

    Connections {
        target: transitionView
        function onTransitionFinished() {
            if (!root.__reactToTransition)
                return
            /* This is a tricky one. We have to also update the list.
               The connection could be also done the other way around */
            list.currentItem = root.__activeItem
            root.__reactToTransition = false
        }
    }

    DragHandler {
        id: handler
        xAxis.enabled: root.horizontal
        yAxis.enabled: !root.horizontal
        target: null
        enabled: true

        function distance() {
            if (root.horizontal)
                return (centroid.position.x - centroid.pressPosition.x) / transitionView.width
            return -(centroid.position.y - centroid.pressPosition.y) / transitionView.height
        }

        onActiveChanged: {
            if (handler.active) {
                if (distance() < 0)
                    transitionView.nextItem = list.nextItem
                else
                    transitionView.nextItem = list.prevItem

                transitionView.scheduleTransition()
                if (transitionView.currentTransition)
                    transitionView.currentTransition.effect.enable()
            } else {
                var p = Math.abs(distance()) * 100
                if (transitionView.currentTransition && transitionView.currentTransition.effect.progress > root.threshold) {
                    root.__activeItem = transitionView.nextItem
                    root.__reactToTransition = true
                    transitionView.currentTransition.__start()
                } else { /* Drag was released, but threshold was not passed */
                    if (transitionView.currentTransition)
                        transitionView.currentTransition.effect.abort()
                }
                /* Block for 100ms */
                handler.enabled = false
                blocker.start()
            }
        }

        onCentroidChanged: {
            if (!handler.enabled)
                return

            var p = Math.abs(distance()) * 100

            if (transitionView.currentTransition)
                transitionView.currentTransition.effect.progress = p * 2
        }
    }
}