aboutsummaryrefslogtreecommitdiffstats
path: root/src/quickcontrols/fluentwinui3/SearchField.qml
blob: adbec5846d4c42a1b5a0aef2b7043ec0f845a950 (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
// Copyright (C) 2025 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

pragma ComponentBehavior: Bound

import QtQuick
import QtQuick.Templates as T
import QtQuick.Controls.impl
import QtQuick.Controls.FluentWinUI3.impl as Impl

T.SearchField {
    id: control

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

    spacing: __config.contentItem.spacing / 2 || 0

    topPadding: __config.topPadding || 0
    bottomPadding: __config.bottomPadding || 0

    readonly property real __clearIndicator: (!clearIndicator.indicator || !clearIndicator.indicator.visible ? 0 : clearIndicator.indicator.width + control.spacing)
    readonly property real __searchIndicator: (!searchIndicator.indicator || !searchIndicator.indicator.visible ? 0 : searchIndicator.indicator.width + control.spacing)
    leftPadding: __config.leftPadding + (control.mirrored ? __clearIndicator + __searchIndicator : 0)
    rightPadding: __config.rightPadding + (control.mirrored ? 0 : __clearIndicator + __searchIndicator)

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

    readonly property string __currentState: [
        !control.enabled && "disabled",
        (control.searchIndicator.pressed && control.clearIndicator.pressed) && "hovered",
        control.popup.visible && "open",
        (control.searchIndicator.pressed && control.clearIndicator.pressed) && "pressed"
    ].filter(Boolean).join("_") || "normal"
    readonly property var __config: (control.popup.visible
                                    ? Config.controls.editablecombobox[__currentState]
                                    : Config.controls.combobox[__currentState]) || {}

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

    delegate: ItemDelegate {
        width: ListView.view.width
        text: model[control.textRole]
        palette.text: control.palette.text
        palette.highlightedText: control.palette.highlightedText
        font.weight: control.currentIndex === index ? Font.DemiBold : Font.Normal
        highlighted: control.highlightedIndex === index
        hoverEnabled: control.hoverEnabled

        required property var model
        required property int index
    }

    searchIndicator.indicator: Impl.StyleImage {
        // use SpinBox indicator assets as they share the same style
        property string state: [
            (control.searchIndicator.hovered || control.searchIndicator.pressed) && "up",
            (control.searchIndicator.indicator.enabled && control.searchIndicator.hovered && !control.searchIndicator.pressed) && "hovered",
            (control.searchIndicator.indicator.enabled && control.searchIndicator.pressed) && "pressed",
            (!control.searchIndicator.indicator.enabled) && "disabled"
        ].filter(Boolean).join("_") || "normal"
        readonly property var indicatorConfig: Config.controls.spinbox[state] || {}
        imageConfig: indicatorConfig.indicator_up_background

        x: !control.mirrored ? control.width - width - control.spacing : control.spacing
        y: control.topPadding + (control.availableHeight - height) / 2

        implicitWidth: 32
        implicitHeight: 24

        ColorImage {
            x: (parent.width - width) / 2
            y: (parent.height - height) / 2
            width: 13
            height: 13

            source: Qt.resolvedUrl("icons/search-magnifier")
            color: control.palette.placeholderText
            opacity: control.searchIndicator.pressed ? 0.7 : 1
        }
    }

    clearIndicator.indicator: Impl.StyleImage {
        // use SpinBox indicator assets as they share the same style
        property string state: [
            (control.clearIndicator.hovered || control.clearIndicator.pressed) && "down",
            (control.clearIndicator.indicator.enabled && control.clearIndicator.hovered && !control.clearIndicator.pressed) && "hovered",
            (control.clearIndicator.indicator.enabled && control.clearIndicator.pressed) && "pressed",
            (!control.clearIndicator.indicator.enabled) && "disabled"
        ].filter(Boolean).join("_") || "normal"
        readonly property var indicatorConfig: Config.controls.spinbox[state] || {}
        imageConfig: indicatorConfig.indicator_down_background

        x: (!searchIndicator.indicator || !searchIndicator.indicator.visible)
           ? (!control.mirrored ? control.width - width - control.spacing : control.spacing)
           : (!control.mirrored ? control.width - width - (control.spacing * 2) - searchIndicator.indicator.width : searchIndicator.indicator.width + (control.spacing * 2))
        y: control.topPadding + (control.availableHeight - height) / 2
        implicitWidth: 32
        implicitHeight: 24
        visible: control.text.length > 0

        ColorImage {
            x: (parent.width - width) / 2
            y: (parent.height - height) / 2
            width: 13
            height: 13

            source: Qt.resolvedUrl("icons/close_big")
            color: control.palette.placeholderText
            opacity: control.clearIndicator.pressed ? 0.7 : 1
        }
    }

    contentItem: T.TextField {
        leftPadding: control.__config.label_contentItem.leftPadding
        rightPadding: control.__config.label_contentItem.rightPadding
        topPadding: control.__config.label_contentItem.topPadding
        bottomPadding: control.__config.label_contentItem.bottomPadding

        implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
                                 contentHeight + topPadding + bottomPadding)

        text: control.text

        color: control.palette.text
        selectionColor: control.palette.highlight
        selectedTextColor: control.palette.highlightedText
        horizontalAlignment: control.__config.label_text.textHAlignment
        verticalAlignment: control.__config.label_text.textVAlignment

        readonly property Item __focusFrameControl: control
    }

    background: ItemGroup {
        Impl.StyleImage {
            visible: !control.__isHighContrast
            imageConfig: control.__config.background
            Item {
                visible: control.popup.visible || control.activeFocus
                width: parent.width
                height: 2
                y: parent.height - height
                Impl.FocusStroke {
                    width: parent.width
                    height: parent.height
                    radius: control.popup.visible ? 0 : control.__config.background.bottomOffset
                    color: control.palette.accent
                }
            }
        }
        Rectangle {
            visible: control.__isHighContrast
            implicitWidth: control.__config.background.width
            implicitHeight: control.__config.background.height
            color: control.palette.window
            border.color: control.hovered ? control.palette.accent : control.palette.text
            radius: 4
        }
    }

    popup: T.Popup {
        y: control.height
        width: control.width
        height: control.suggestionCount > 0 ? Math.min(contentItem.implicitHeight + topPadding + bottomPadding, control.Window.height - topMargin - bottomMargin) : 0
        topMargin: 8
        bottomMargin: 8
        palette: control.palette

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

        contentItem: ListView {
            clip: true
            implicitHeight: contentHeight
            model: control.delegateModel
            currentIndex: control.highlightedIndex
            highlightMoveDuration: 0
        }

        enter: Transition {
            NumberAnimation { property: "height"; from: control.popup.height / 3; to: control.popup.height; easing.type: Easing.OutCubic; duration: 250 }
        }

        background: ItemGroup {
            Impl.StyleImage {
                visible: !control.__isHighContrast
                imageConfig: control.__config.popup_background.filePath ? control.__config.popup_background : Config.controls.popup["normal"].background // fallback to regular popup
            }
            Rectangle {
                visible: control.__isHighContrast
                implicitWidth: Config.controls.popup["normal"].background.width
                implicitHeight: Config.controls.popup["normal"].background.height
                color: control.palette.window
                border.color: control.palette.text
                radius: 4
            }
        }
    }
}