Debian Codegraph @

src/GUI/qml/SearchButton.qml

raw
1 // SPDX-FileCopyrightText: 2018 James Turner
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 import QtQuick 2.2
5 import FlightGear 1.0
6
7 FocusScope
8 {
9 id: root
10
11 width: Style.strutSize * 3
12 height: frame.height
13
14 property string placeholder: qsTr("Search")
15 property bool active: false
16
17 signal search(string term)
18
19 property bool autoSubmit: true
20 property alias autoSubmitTimeout: searchTimer.interval
21
22 onActiveChanged: {
23 if (!active) {
24 clear();
25 }
26 }
27
28 function clear()
29 {
30 buttonText.text = ""
31 root.focus = false
32 searchTimer.stop();
33 root.search("");
34 }
35
36 readonly property bool canClear: (buttonText.text.length > 0)
37
38 Rectangle
39 {
40 id: frame
41 radius: Style.roundRadius
42
43 width: root.width
44 height: Math.max(searchIcon.height, buttonText.height) + (Style.roundRadius)
45 border.width: 1
46 border.color: (mouse.containsMouse | active) ? Style.themeColor: Style.minorFrameColor
47 clip: true
48 color: Style.backgroundColor
49
50 TextInput {
51 id: buttonText
52 anchors.left: parent.left
53 anchors.right: searchIcon.left
54 anchors.margins: Style.margin
55 anchors.verticalCenter: parent.verticalCenter
56
57 color: Style.baseTextColor
58 focus: true
59 font.pixelSize: Style.baseFontPixelSize
60
61 onTextChanged: {
62 if (root.autoSubmit) {
63 searchTimer.restart();
64 }
65 }
66
67 onEditingFinished: {
68 if (text == "") {
69 clear();
70 } else {
71 root.search(text);
72 root.focus = false;
73 }
74 }
75
76 text: ""
77
78 // placeholder text, hides itself whenever parent has non-empty text
79 StyledText {
80 anchors.fill: parent
81 visible: parent.text == ""
82 text: root.placeholder
83
84 // weird rule here - we want to make the placeholder text very
85 // subtle when the textdit is focused, so the user knows it will
86 // be overwritten
87 color: buttonText.activeFocus ? Style.disabledTextColor : Style.baseTextColor
88 }
89 }
90
91 Image {
92 id: searchIcon
93
94 // give hover feedback when showing the clear icon
95 source: clearButtonMouse.containsMouse ? "image://colored-icon/clear?destructive"
96 : root.canClear ? "image://colored-icon/clear?text" : "image://colored-icon/search?text"
97
98 anchors.right: parent.right
99 anchors.rightMargin: Style.margin
100 anchors.verticalCenter: parent.verticalCenter
101 }
102
103 MouseArea {
104 id: mouse
105 anchors.left: parent.left
106 height: parent.height
107 anchors.right: clearButtonMouse.left
108 hoverEnabled: true
109
110 onClicked: {
111 if (!buttonText.activeFocus) {
112 root.forceActiveFocus();
113 buttonText.selectAll();
114 }
115 }
116
117 onDoubleClicked: {
118 buttonText.selectAll();
119 }
120 }
121
122
123 MouseArea {
124 id: clearButtonMouse
125 anchors.right: parent.right
126 height: parent.height
127 width: searchIcon.width
128 hoverEnabled: root.canClear
129 visible: root.canClear
130 onClicked: clear();
131 }
132
133 Timer {
134 id: searchTimer
135 interval: 800
136 onTriggered: {
137 if (buttonText.text.length > 2) {
138 root.search(buttonText.text)
139 } else if (buttonText.text.length == 0) {
140 root.search(""); // ensure we update with no search
141 }
142 }
143 }
144 }
145 } // of FocusScope