Kirigami2

SearchDialog.qml
1// SPDX-FileCopyrightText: 2023 Tobias Fella <tobias.fella@kde.org>
2// SPDX-FileCopyrightText: 2024 Carl Schwan <carl@carlschwan.eu>
3// SPDX-License-Identifier: LGPL-2.0-or-later
4
5pragma ComponentBehavior: Bound
6
7import QtQuick
8import QtQuick.Controls as QQC2
9import QtQuick.Layouts
10
11import org.kde.kirigami as Kirigami
12
13/**
14 * A dialog to let's you do a global search accross your applications
15 * documents, chat rooms and more.
16 *
17 * Example usage for a chat app where we want to quickly search for a room.
18 *
19 * @code{.qml}
20 * import QtQuick
21 * import org.kde.kitemmodels as KItemModels
22 * import org.kde.kirigami as Kirigami
23 *
24 * Kirigami.SearchDialog {
25 * id: root
26 *
27 * onTextChanged: {
28 * sortModel.filterText = text;
29 * }
30 * onAccepted: listView.currentItem.clicked()
31 *
32 * emptyText: i18nc("Placeholder message", "No room found.")
33 *
34 * model: KItemModels.KSortFilterProxyModel {
35 * id: sortModel
36 *
37 * sourceModel: RoomModel { }
38 * }
39 *
40 * delegate: RoomDelegate {
41 * onClicked: root.close()
42 * }
43 *
44 * Shortcut {
45 * sequence: "Ctrl+K"
46 * onActivated: root.open()
47 * }
48 * }
49 * @endcode{}
50 *
51 * @image html searchdialog.html
52 *
53 * @note This component is unsuitable on mobile. Instead on mobile prefer to
54 * use a seperate page for the search.
55 *
56 * @since Kirigami 6.3
57 */
58QQC2.Dialog {
59 id: root
60
61 /**
62 * This property holds an alias to the model of the internal ListView.
63 */
64 property alias model: listView.model
65
66 /**
67 * This property holds an alias to the delegate component of the internal ListView.
68 */
69 property alias delegate: listView.delegate
71 /**
72 * This property holds an alias to the currentItem component of the internal ListView.
73 */
74 property alias currentItem: listView.currentItem
75
76 /**
77 * This property holds an alias to the section component of the internal ListView.
78 */
79 property alias section: listView.section
80
81 /**
82 * This property holds an alias to the content of the search field.
83 */
84 property alias text: searchField.text
85
86 /**
87 * This property holds an alias to the left actions of the seach field.
88 */
89 property alias searchFieldLeftActions: searchField.leftActions
91 /**
92 * This property holds an alias to the right actions of the seach field.
93 */
94 property alias searchFieldRightActions: searchField.rightActions
96 /**
97 * The placeholder text shown in the (empty) search field.
98 */
99 property alias searchFieldPlaceholderText: searchField.placeholderText
101 /**
102 * This property holds the number of search results displayed in the internal ListView.
103 */
104 property alias count: listView.count
105
106 /**
107 * This property holds an alias to the placeholder message text displayed
108 * when the internal list view is empty.
109 */
110 property alias emptyText: placeholder.text
111
112 /**
113 * This property holds an alias to the placeholder message icon displayed
114 * when the internal list view is empty.
115 */
116 property alias emptyIcon: placeholder.icon
117
118 /**
119 * @brief Helpful action when the list is empty
120 *
121 * This property holds an alias to the helpful action of the placeholder message
122 * when the internal list view is empty.
123 *
124 * @since 6.10
125 */
126 property alias emptyHelpfulAction: placeholder.helpfulAction
127
128 width: Math.min(Kirigami.Units.gridUnit * 35, parent.width)
129 height: Math.min(Kirigami.Units.gridUnit * 20, parent.height)
130
131 padding: 0
132
133 anchors.centerIn: parent
134
135 modal: true
136
137 onOpened: {
138 searchField.forceActiveFocus();
139 searchField.text = "";
140 listView.currentIndex = 0;
141 }
142
143 contentItem: ColumnLayout {
144 spacing: 0
145
146 Kirigami.SearchField {
147 id: searchField
148
149 Layout.fillWidth: true
150
151 background: null
152
153 Layout.margins: Kirigami.Units.smallSpacing
154
155 Keys.onDownPressed: {
156 const listViewHadFocus = listView.activeFocus;
157 listView.forceActiveFocus();
158 if (listView.currentIndex < listView.count - 1) {
159 // don't move to the next entry when we just changed focus from the search field to the list view
160 if (listViewHadFocus) {
161 listView.currentIndex++;
162 }
163 } else {
164 listView.currentIndex = 0;
165 }
166 }
167 Keys.onUpPressed: {
168 listView.forceActiveFocus();
169 if (listView.currentIndex === 0) {
170 listView.currentIndex = listView.count - 1;
171 } else {
172 listView.currentIndex--;
173 }
174 }
175 Keys.onPressed: (event) => {
176 switch (event.key) {
177 case Qt.Key_PageDown:
178 listView.forceActiveFocus();
179 listView.currentIndex = Math.min(listView.count - 1, listView.currentIndex + Math.floor(listView.height / listView.currentItem.height));
180 event.accepted = true;
181 break;
182 case Qt.Key_PageUp:
183 listView.forceActiveFocus();
184 listView.currentIndex = Math.max(0, listView.currentIndex - Math.floor(listView.height / listView.currentItem.height));
185 event.accepted = true;
186 break;
187 }
188 }
189
190 focusSequence: ""
191 autoAccept: false
192
193 onAccepted: root.accepted()
194 }
195
196 Kirigami.Separator {
197 Layout.fillWidth: true
198 }
199
200 QQC2.ScrollView {
201 Layout.fillWidth: true
202 Layout.fillHeight: true
203 Keys.forwardTo: searchField
204
205 ListView {
206 id: listView
207
208 currentIndex: 0
209 clip: true
210 highlightMoveDuration: 200
211 Keys.forwardTo: searchField
212 keyNavigationEnabled: true
213
214 Kirigami.PlaceholderMessage {
215 id: placeholder
216 anchors.centerIn: parent
217 width: parent.width - Kirigami.Units.gridUnit * 4
218 icon.name: 'system-search-symbolic'
219 visible: listView.count === 0 && text.length > 0
220 }
221 }
222 }
223 }
224}
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:51:21 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.