summary refs log tree commit diff
path: root/resources/qml/ui/Ripple.qml
blob: b240f42157b0baf9b79f50b74a58e961176e3e66 (plain) (blame)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later

import QtGraphicalEffects 1.0
import QtQuick 2.10
import QtQuick.Controls 2.3

Item {
    id: ripple

    property alias clip: backgroundLayer.clip
    property real radius: 0
    property color color: "#22000000"
    property real maxRadius: Math.max(width, height)
    property real radiusAnimationRate: 0.05
    property real radiusTailAnimationRate: 0.5
    property real opacityAnimationDuration: 300
    readonly property real diameter: radius * 2
    property real centerX
    property real centerY
    property var rippleTarget: parent

    function start() {
        console.log("Starting ripple animation");
        ripple.state = "ACTIVE";
    }

    function stop() {
        console.log("Stopping ripple animation");
        ripple.state = "NORMAL";
    }

    anchors.fill: parent
    state: "NORMAL"
    states: [
        State {
            name: "NORMAL"
        },
        State {
            name: "ACTIVE"
        }
    ]
    transitions: [
        Transition {
            from: "NORMAL"
            to: "ACTIVE"

            SequentialAnimation {
                ScriptAction {
                    script: {
                        ripple.opacity = 1;
                        ripple.visible = true;
                    }
                }

                NumberAnimation {
                    id: radius_animation

                    target: ripple
                    properties: "radius"
                    from: 0
                    to: ripple.maxRadius
                    duration: ripple.maxRadius / ripple.radiusAnimationRate

                    easing {
                        type: Easing.OutQuad
                    }

                }

            }

        },
        Transition {
            from: "ACTIVE"
            to: "NORMAL"

            SequentialAnimation {
                ParallelAnimation {
                    NumberAnimation {
                        id: radius_tail_animation

                        target: ripple
                        properties: "radius"
                        to: ripple.maxRadius
                        duration: ripple.maxRadius / ripple.radiusTailAnimationRate

                        easing {
                            type: Easing.Linear
                        }

                    }

                    NumberAnimation {
                        id: opacity_animation

                        target: ripple
                        properties: "opacity"
                        to: 0
                        duration: ripple.opacityAnimationDuration

                        easing {
                            type: Easing.InQuad
                        }

                    }

                }

                ScriptAction {
                    script: {
                        ripple.visible = false;
                    }
                }

            }

        }
    ]

    Connections {
        // Button
        // Default to center

        function onPressed(mouse) {
            // MouseArea
            if (mouse) {
                ripple.centerX = mouse.x;
                ripple.centerY = mouse.y;
            } else if (rippleTarget.pressX) {
                ripple.centerX = rippleTarget.pressX;
                ripple.centerY = rippleTarget.pressY;
            } else {
                ripple.centerX = width / 2;
                ripple.centerY = height / 2;
            }
            ripple.start();
        }

        function onReleased() {
            ripple.stop();
        }

        function onExited() {
            ripple.stop();
        }

        function onCanceled() {
            ripple.stop();
        }

        function onClicked() {
            ripple.stop();
        }

        target: rippleTarget
        ignoreUnknownSignals: true
    }

    Rectangle {
        id: backgroundLayer

        anchors.fill: parent
        color: "transparent"
        clip: true

        Rectangle {
            id: circle

            x: ripple.centerX - ripple.radius
            y: ripple.centerY - ripple.radius
            height: ripple.diameter
            width: ripple.diameter
            radius: ripple.radius
            color: ripple.color
        }

    }

}