summaryrefslogtreecommitdiffstats
path: root/templates/Switch.qml
blob: 766643598174f080508aa5dc6e1176f4333e0102 (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
import QtQuick
import QtQuick.Controls.impl
import QtQuick.Templates as T

T.Switch {
    id: control

    implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
                            implicitContentWidth + leftPadding + rightPadding,
                            implicitIndicatorWidth)
    implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
                             implicitContentHeight + topPadding + bottomPadding,
                             implicitIndicatorHeight + topPadding + bottomPadding)

    spacing: __config.spacing || 0

    topPadding: __config.topPadding || 0
    leftPadding: __config.leftPadding || 0
    rightPadding: __config.rightPadding || 0
    bottomPadding: __config.bottomPadding || 0

    topInset: -__config.topInset || 0
    bottomInset: -__config.bottomInset || 0
    leftInset: -__config.leftInset || 0
    rightInset: -__config.rightInset || 0

    readonly property string __currentState: [
        control.checked && "checked",
        !control.enabled && "disabled",
        control.enabled && !control.down && control.hovered && "hovered",
        control.down && "pressed"
    ].filter(Boolean).join("_") || "normal"
    readonly property var __config: Config.controls.switch_[__currentState] || {}
    readonly property bool __mirroredIndicator: control.mirrored !== (__config.mirrored || false)

    indicator: Item {
        x: control.__mirroredIndicator ? control.width - width - control.rightPadding : control.leftPadding
        y: control.topPadding + (control.availableHeight - height) / 2
        // If handleBackground is not generated, use the size of the handle
        implicitWidth: handleBackground.width > 0 ? handleBackground.width : handleBackground.handle.width * 2
        implicitHeight: handleBackground.height > 0 ? handleBackground.height : handleBackground.handle.height

        property Item handleBackground: StyleImage {
            parent: control.indicator
            imageConfig: control.__config.handle_background

            property Item handle: StyleImage {
                parent: control.indicator.handleBackground
                x: control.__config.handle_contentItem.leftPadding
                    + (control.visualPosition * (parent.width - width
                        - control.__config.handle_contentItem.leftPadding
                        - control.__config.handle_contentItem.rightPadding))
                y: control.__config.handle_contentItem.topPadding

                imageConfig: control.__config.handle

                Behavior on x {
                    enabled: !control.down
                    SmoothedAnimation {
                        velocity: 200
                    }
                }
            }
        }
    }

    contentItem: Text {
        leftPadding: control.indicator && !control.__mirroredIndicator ? control.indicator.width + control.spacing : 0
        rightPadding: control.indicator && control.__mirroredIndicator ? control.indicator.width + control.spacing : 0
        text: control.text
        font: control.font
        color: control.palette.windowText
        elide: Text.ElideRight
        horizontalAlignment: control.__config.label.textHAlignment
        verticalAlignment: control.__config.label.textVAlignment
    }

    background: StyleImage {
        imageConfig: control.__config.background
    }
}