summary refs log tree commit diff
path: root/resources/qml/ToggleButton.qml
blob: f48fcb682caf92d81c9e1c02a4a301edf15355b8 (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
// SPDX-FileCopyrightText: Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later

import QtQuick 2.5
import QtQuick 2.12
import QtQuick.Controls 2.12
import im.nheko 1.0

Switch {
    id: toggleButton

    implicitWidth: indicatorItem.width
    state: checked ? "on" : "off"

    indicator: Item {
        id: indicatorItem

        implicitHeight: 24
        implicitWidth: 48
        y: parent.height / 2 - height / 2

        Rectangle {
            id: track

            color: Qt.rgba(border.color.r, border.color.g, border.color.b, 0.6)
            height: parent.height * 0.6
            radius: height / 2
            width: parent.width - height
            x: radius
            y: parent.height / 2 - height / 2
        }
        Rectangle {
            id: handle

            border.color: "#767676"
            color: palette.button
            height: width
            radius: width / 2
            width: parent.height * 0.9
            y: parent.height / 2 - height / 2
        }
    }
    states: [
        State {
            name: "off"

            PropertyChanges {
                track.border.color: "#767676"
            }
            PropertyChanges {
                handle.x: 0
            }
        },
        State {
            name: "on"

            PropertyChanges {
                track.border.color: palette.highlight
            }
            PropertyChanges {
                handle.x: indicatorItem.width - handle.width
            }
        }
    ]
    transitions: [
        Transition {
            reversible: true
            to: "off"

            ParallelAnimation {
                NumberAnimation {
                    duration: 200
                    easing.type: Easing.InOutQuad
                    property: "x"
                    target: handle
                }
                ColorAnimation {
                    duration: 200
                    properties: "color,border.color"
                    target: track
                }
            }
        }
    ]
}