aboutsummaryrefslogtreecommitdiffstats
path: root/src/quickcontrols/fluentwinui3/RangeSlider.qml
blob: 764707a6ba44dfbe6a7beb27c9e702161598f7e4 (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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// 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.Controls.FluentWinUI3.impl as Impl
import QtQuick.Templates as T

T.RangeSlider {
    id: control

    implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
                            first.implicitHandleWidth + leftPadding + rightPadding,
                            second.implicitHandleWidth + leftPadding + rightPadding)
    implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
                             first.implicitHandleHeight + topPadding + bottomPadding,
                             second.implicitHandleHeight + topPadding + bottomPadding)

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

    readonly property string __controlState: [
        !control.enabled && "disabled",
        control.enabled && control.hovered && !(first.pressed || second.pressed) && "hovered",
    ].filter(Boolean).join("_") || "normal"
    readonly property var __config: Config.controls.rangeslider[__controlState] || {}

    readonly property real __steps: Math.abs(to - from) / stepSize
    readonly property bool __isDiscrete: stepSize >= Number.EPSILON
        && Math.abs(Math.round(__steps) - __steps) < Number.EPSILON

    readonly property bool __isHighContrast: Application.styleHints.accessibility.contrastPreference === Qt.HighContrast

    property string __firstHandleState: [
        !control.enabled && "disabled",
        first.hovered && !first.pressed && "hovered",
        first.pressed && "handle_pressed",
    ].filter(Boolean).join("_") || "normal"
    readonly property var __firstHandleConfig: Config.controls.rangeslider[__firstHandleState] || {}

    property string __secondHandleState: [
        !control.enabled && "disabled",
        second.hovered && !second.pressed && "hovered",
        second.pressed && "handle_pressed",
    ].filter(Boolean).join("_") || "normal"
    readonly property var __secondHandleConfig: Config.controls.rangeslider[__secondHandleState] || {}

    readonly property Item __focusFrameControl: control
    readonly property Item __focusFrameTarget: control

    first.handle: ItemGroup {
        x: Math.round(control.leftPadding + (control.horizontal
            ? control.first.visualPosition * (control.availableWidth - width)
            : (control.availableWidth - width) / 2))
        y: Math.round(control.topPadding + (control.horizontal
            ? (control.availableHeight - height) / 2
            : control.first.visualPosition * (control.availableHeight - height)))

        Impl.StyleImage {
            visible: !control.__isHighContrast
            imageConfig: control.__firstHandleConfig.first_handle

            readonly property Item __focusFrameTarget: control
        }

        Rectangle {
            visible: control.__isHighContrast
            implicitWidth: control.__secondHandleConfig.first_handle.width
            implicitHeight: control.__secondHandleConfig.first_handle.height
            color: control.palette.buttonText
            radius: width / 2
        }

        property Rectangle indicator: Rectangle {
            property real diameter: !control.enabled ? 10
                                                     : control.first.pressed ? 8
                                                     : control.__isHighContrast && !control.hovered ? 0
                                                     : control.first.hovered ? 14 : 10
            parent: control.first.handle
            width: diameter
            height: diameter
            radius: diameter * 0.5
            x: (control.__secondHandleConfig.first_handle.width - width) / 2
            y: (control.__secondHandleConfig.first_handle.height - height) / 2
            color: control.enabled ? (control.first.hovered ? Qt.rgba(control.palette.accent.r, control.palette.accent.g, control.palette.accent.b, 0.9020)
                                   : control.first.pressed ? Qt.rgba(control.palette.accent.r, control.palette.accent.g, control.palette.accent.b, 0.8)
                                   : control.palette.accent)
                                   : control.palette.accent
            Behavior on diameter {
                // From WindowsUI 3 Animation Values
                NumberAnimation {
                    duration: 167
                    easing.type: Easing.OutCubic
                }
            }
        }
    }

    second.handle: ItemGroup {
        x: Math.round(control.leftPadding + (control.horizontal
            ? control.second.visualPosition * (control.availableWidth - width)
            : (control.availableWidth - width) / 2))
        y: Math.round(control.topPadding + (control.horizontal
            ? (control.availableHeight - height) / 2
            : control.second.visualPosition * (control.availableHeight - height)))

        Impl.StyleImage {
            visible: !control.__isHighContrast
            imageConfig: control.__secondHandleConfig.second_handle

            readonly property Item __focusFrameTarget: control
        }

        Rectangle {
            visible: control.__isHighContrast
            implicitWidth: control.__secondHandleConfig.second_handle.width
            implicitHeight: control.__secondHandleConfig.second_handle.height
            color: control.palette.buttonText
            radius: width / 2
        }

        property Rectangle indicator: Rectangle {
            property real diameter: !control.enabled ? 10
                                                     : control.second.pressed ? 8
                                                     : control.__isHighContrast && !control.hovered ? 0
                                                     : control.second.hovered ? 14 : 10
            parent: control.second.handle
            width: diameter
            height: diameter
            radius: diameter * 0.5
            x: (control.__secondHandleConfig.second_handle.width - width) / 2
            y: (control.__secondHandleConfig.second_handle.height - height) / 2
            color: control.enabled ? (control.second.hovered ? Qt.rgba(control.palette.accent.r, control.palette.accent.g, control.palette.accent.b, 0.9020)
                                   : control.second.pressed ? Qt.rgba(control.palette.accent.r, control.palette.accent.g, control.palette.accent.b, 0.8)
                                   : control.palette.accent)
                                   : control.palette.accent
            Behavior on diameter {
                // From WindowsUI 3 Animation Values
                NumberAnimation{
                    duration: 167
                    easing.type: Easing.OutCubic
                }
            }
        }
    }

    background: Item {
        implicitWidth: control.horizontal
            ? (_background.implicitWidth || _background.groove.implicitWidth)
            : (_background.implicitHeight || _background.groove.implicitHeight)
        implicitHeight: control.horizontal
            ? (_background.implicitHeight || _background.groove.implicitHeight)
            : (_background.implicitWidth || _background.groove.implicitWidth)

        property Item _background: Impl.StyleImage {
            visible: !control.__isHighContrast
            parent: control.background
            width: parent.width
            height: parent.width
            imageConfig: control.__config.background

            property Item groove: Impl.StyleImage {
                parent: control.background._background
                x: control.leftPadding - control.leftInset + (control.horizontal
                    ? control.__firstHandleConfig.first_handle.width / 2
                    : (control.availableWidth - width) / 2)
                y: control.topPadding - control.rightInset + (control.horizontal
                    ? ((control.availableHeight - height) / 2)
                    : control.__firstHandleConfig.first_handle.height / 2)

                width: control.horizontal
                    ? control.availableWidth
                        - (control.__firstHandleConfig.first_handle.width / 2) - (control.__secondHandleConfig.second_handle.width / 2)
                    : implicitWidth
                height: control.horizontal
                    ? implicitHeight
                    : control.availableHeight
                        - (control.__firstHandleConfig.first_handle.width / 2) - (control.__secondHandleConfig.second_handle.width / 2)
                imageConfig: control.__config.groove
                horizontal: control.horizontal

                property Rectangle track: Rectangle {
                    parent: control.background._background.groove
                    x: control.horizontal ? parent.width * control.first.position : 0
                    y: control.horizontal ? 0 : parent.height - (parent.height * control.second.position)
                    implicitWidth: control.horizontal ? control.__config.track.width : control.__config.track.height
                    implicitHeight: control.horizontal ? control.__config.track.height : control.__config.track.width
                    width: control.horizontal
                        ? parent.width * (control.second.position - control.first.position)
                        : parent.width
                    height: control.horizontal
                        ? parent.height
                        : parent.height * (control.second.position - control.first.position)
                    radius: control.__config.track.height * 0.5
                    color: control.palette.accent
                }
            }

            property Repeater ticksTop: Repeater {
                parent: control.__isHighContrast ? control.background._highContrastBackground : control.background._background.groove
                model: control.__isDiscrete ? Math.floor(control.__steps) + 1 : 0
                delegate: Rectangle {
                    width: control.horizontal ? 1 : 4
                    height: control.horizontal ? 4 : 1
                    x: control.horizontal
                        ? 6 + index * (parent.width - 2 * 6 - width) / (control.background._background.ticksTop.model - 1)
                        : -4 - width
                    y: control.horizontal
                        ? -4 - height
                        : 6 + index * (parent.height - 2 * 6 - height) / (control.background._background.ticksTop.model - 1)
                    color: Application.styleHints.colorScheme === Qt.Light ? "#9C000000" : "#9AFFFFFF"

                    required property int index
                }
            }

            property Repeater ticksBottom: Repeater {
                parent: control.__isHighContrast ? control.background._highContrastBackground : control.background._background.groove
                model: control.__isDiscrete ? Math.floor(control.__steps) + 1 : 0
                delegate: Rectangle {
                    width: control.horizontal ? 1 : 4
                    height: control.horizontal ? 4 : 1
                    x: control.horizontal
                        ? 6 + index * (parent.width - 2 * 6 - width) / (control.background._background.ticksBottom.model - 1)
                        : parent.width + 4
                    y: control.horizontal
                        ? parent.height + 4
                        : 6 + index * (parent.height - 2 * 6 - height) / (control.background._background.ticksBottom.model - 1)
                    color: Application.styleHints.colorScheme === Qt.Light ? "#9C000000" : "#9AFFFFFF"

                    required property int index
                }
            }
        }

        property Item _highContrastBackground: Rectangle {
            parent: control.background
            visible: control.__isHighContrast
            implicitWidth: control.horizontal ? 200 : 4
            implicitHeight: control.horizontal ? 4 : 200
            x: control.leftPadding - control.leftInset + (control.horizontal
                ? control.__firstHandleConfig.first_handle.width / 2
                : (control.availableWidth - width) / 2)
            y: control.topPadding - control.topInset + (control.horizontal
                ? ((control.availableHeight - height) / 2)
                : control.__firstHandleConfig.first_handle.height / 2)
            width: control.horizontal
                ? control.availableWidth - control.__firstHandleConfig.first_handle.width
                : implicitWidth
            height: control.horizontal
                ? implicitHeight
                : control.availableHeight - control.__firstHandleConfig.first_handle.width
            radius: 2
            color: control.palette.buttonText
            scale: control.horizontal && control.mirrored ? -1 : 1

            Rectangle {
                x: control.horizontal ? parent.width * control.first.position : 0
                y: control.horizontal ? 0 : parent.height - (parent.height * control.second.position)
                implicitWidth: control.horizontal ? parent.width * (control.second.position - control.first.position) : parent.width
                implicitHeight: control.horizontal ? parent.height : parent.height * (control.second.position - control.first.position)
                radius: 2
                color: control.palette.highlight
            }
        }
    }
}