summaryrefslogtreecommitdiff
path: root/components/controls/StyledBusyIndicator.qml
blob: e54aafb8ad1a6c3bbee711f1a3b05b7d74257807 (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
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
import qs.config
import QtQuick
import QtQuick.Controls

BusyIndicator {
    id: root

    property real implicitSize: Appearance.font.size.normal * 3
    property real strokeWidth: Appearance.padding.small
    property real internalStrokeWidth: strokeWidth
    property string animState

    padding: 0
    implicitWidth: implicitSize
    implicitHeight: implicitSize

    onRunningChanged: {
        if (running) {
            updater.completeEndProgress = 0;
            animState = "running";
        } else {
            if (animState == "running")
                animState = "completing";
        }
    }

    states: State {
        name: "stopped"
        when: !root.running

        PropertyChanges {
            root.opacity: 0
            root.internalStrokeWidth: root.strokeWidth / 3
        }
    }

    transitions: Transition {
        NumberAnimation {
            properties: "opacity,internalStrokeWidth"
            duration: updater.completeEndDuration
            easing.type: Easing.BezierSpline
            easing.bezierCurve: Appearance.anim.curves.standard
        }
    }

    background: null

    contentItem: CircularProgress {
        anchors.fill: parent
        strokeWidth: root.internalStrokeWidth
        padding: root.padding
        startAngle: updater.startFraction * 360
        value: updater.endFraction - updater.startFraction
    }

    Updater {
        id: updater
    }

    NumberAnimation {
        running: root.animState !== "stopped"
        loops: Animation.Infinite
        target: updater
        property: "progress"
        from: 0
        to: 1
        duration: updater.duration
    }

    NumberAnimation {
        running: root.animState === "completing"
        target: updater
        property: "completeEndProgress"
        from: 0
        to: 1
        duration: updater.completeEndDuration
        onFinished: {
            if (root.animState === "completing")
                root.animState = "stopped";
        }
    }

    component Updater: QtObject {
        readonly property int duration: 5400 * Appearance.anim.durations.scale
        readonly property int expandDuration: 667 * Appearance.anim.durations.scale
        readonly property int collapseDuration: 667 * Appearance.anim.durations.scale
        readonly property int completeEndDuration: 333 * Appearance.anim.durations.scale
        readonly property int tailDegOffset: -20
        readonly property int extraDegPerCycle: 250
        readonly property int constantRotDeg: 1520
        readonly property list<int> expandDelay: [0, 1350, 2700, 4050].map(d => d * Appearance.anim.durations.scale)
        readonly property list<int> collapseDelay: [667, 2017, 3367, 4717].map(d => d * Appearance.anim.durations.scale)

        property real progress: 0
        property real startFraction: 0
        property real endFraction: 0
        property real rotation: 0
        property real completeEndProgress: 0

        onProgressChanged: update(progress)

        function update(p: real): void {
            const playtime = p * duration;
            let startDeg = constantRotDeg * p + tailDegOffset;
            let endDeg = constantRotDeg * p;

            for (let i = 0; i < 4; i++) {
                const expandFraction = getFractionInRange(playtime, expandDelay[i], expandDuration);
                endDeg += fastOutSlowIn(expandFraction) * extraDegPerCycle;

                const collapseFraction = getFractionInRange(playtime, collapseDelay[i], collapseDuration);
                startDeg += fastOutSlowIn(collapseFraction) * extraDegPerCycle;
            }

            // Gap closing
            startDeg += (endDeg - startDeg) * completeEndProgress;

            startFraction = startDeg / 360;
            endFraction = endDeg / 360;
        }

        function getFractionInRange(currentTime: real, delay: int, duration: int): real {
            if (currentTime < delay)
                return 0;
            if (currentTime > delay + duration)
                return 1;
            return (currentTime - delay) / duration;
        }

        function lerp(a: real, b: real, t: real): real {
            return a + (b - a) * t;
        }

        function cubic(a: real, b: real, c: real, d: real, t: real): real {
            return ((1 - t) ** 3) * a + 3 * ((1 - t) ** 2) * t * b + 3 * (1 - t) * (t ** 2) * c + (t ** 3) * d;
        }

        function cubicBezier(p1x: real, p1y: real, p2x: real, p2y: real, t: real): real {
            return cubic(0, p1y, p2y, 1, t);
        }

        function fastOutSlowIn(t: real): real {
            return cubicBezier(0.4, 0.0, 0.2, 1.0, t);
        }
    }
}