aboutsummaryrefslogtreecommitdiffstats
path: root/src/quickcontrols/fluentwinui3/impl/CheckIndicator.qml
blob: bc1233c6e159b26ca4e37540f7d4bf764f35630e (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
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
// Qt-Security score:significant reason:default

import QtQuick
import QtQuick.Controls.impl
import QtQuick.Templates as T
import QtQuick.Shapes

ColorImage {
    id: indicator

    required property T.AbstractButton control
    required property url filePath

    readonly property color __color: {
        if (Application.styleHints.accessibility.contrastPreference === Qt.HighContrast)
            return control.palette.button
        if (control.enabled && control.checkState !== Qt.Unchecked)
            return control.palette.accent
        return defaultColor
    }

    readonly property color __indicatorColor: {
        if (Application.styleHints.accessibility.contrastPreference === Qt.HighContrast) {
            if (control.checkState === Qt.Checked)
                return control.down ? control.palette.buttonText : control.hovered ? control.palette.button : control.palette.highlightedText
            if (control.checkState === Qt.PartiallyChecked)
                return control.hovered && !control.down ? control.palette.highlight : control.palette.highlightedText
            return "transparent"
        } else if (control.down) {
            return Application.styleHints.colorScheme === Qt.Light ? Qt.rgba(1, 1, 1, 0.7) : Qt.rgba(0, 0, 0, 0.5)
        } else if (Application.styleHints.colorScheme === Qt.Dark && !control.enabled)
            return Qt.rgba(1, 1, 1, 0.5302)
        else if (Application.styleHints.colorScheme === Qt.Dark)
            return "black"
        else
            return "white"
    }

    source: filePath
    color: __color

    Rectangle {
        anchors.fill: parent
        radius: 4
        color: {
            if (Application.styleHints.accessibility.contrastPreference === Qt.HighContrast) {
                if (control.checkState === Qt.Unchecked)
                    return control.down ? control.palette.highlight : control.hovered ? control.palette.highlightedText : control.palette.button
                if (control.checkState === Qt.PartiallyChecked)
                    return control.hovered && !control.down ? control.palette.highlightedText : control.palette.highlight
                return control.down ? control.palette.button : control.hovered ? control.palette.buttonText : control.palette.highlight
            }
            return "transparent"
        }
        border.color: {
            if (Application.styleHints.accessibility.contrastPreference === Qt.HighContrast) {
                if (control.checkState === Qt.Unchecked)
                    return control.hovered ? control.palette.highlight : control.palette.buttonText
                if (control.checkState === Qt.PartiallyChecked)
                    return control.palette.highlight
            }
            return "transparent"
        }

        // TODO: Add animation for checkmark indicator
        Shape {
            x: (parent.width - width) / 2
            y: (parent.height - height) / 2
            width: 12
            height: 12
            visible: control.checked

            antialiasing: true
            preferredRendererType: Shape.CurveRenderer

            ShapePath {
                strokeWidth: 1
                strokeColor: indicator.__indicatorColor
                fillColor: "transparent"
                capStyle: ShapePath.RoundCap
                joinStyle: ShapePath.RoundJoin

                startX: 1
                startY: 6
                PathLine { x: 5; y: 10 }
                PathLine { x: 11; y: 3 }
            }
        }

        Rectangle {
            visible: control.checkState === Qt.PartiallyChecked
            x: (parent.width - width) / 2
            y: (parent.height - height) / 2
            width: 8
            height: 1
            radius: height * 0.5
            color: indicator.__indicatorColor
        }
    }
}