Kirigami2

SearchField.qml
1/*
2 * SPDX-FileCopyrightText: 2019 Carl-Lucien Schwan <carl@carlschwan.eu>
3 * SPDX-FileCopyrightText: 2022 Felipe Kinoshita <kinofhek@gmail.com>
4 *
5 * SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
8import QtQuick
9import org.kde.kirigami as Kirigami
10
11/**
12 * @brief This is a standard TextField following the KDE HIG, which, by default,
13 * uses Ctrl+F as the focus keyboard shortcut and "Search…" as a placeholder text.
14 *
15 * Example usage for the search field component:
16 * @code
17 * import org.kde.kirigami 2.20 as Kirigami
18 *
19 * Kirigami.SearchField {
20 * id: searchField
21 * onAccepted: console.log("Search text is " + searchField.text)
22 * }
23 * @endcode
24 *
25 * @inherit org::kde::kirigami::ActionTextField
26 */
27Kirigami.ActionTextField {
28 id: root
29 /**
30 * @brief This property sets whether the accepted signal is fired automatically
31 * when the text is changed.
32 *
33 * Setting this to false will require that the user presses return or enter
34 * (the same way a QtQuick.Controls.TextInput works).
35 *
36 * default: ``true``
37 *
38 * @since 5.81
39 * @since org.kde.kirigami 2.16
40 */
41 property bool autoAccept: true
42
43 /**
44 * @brief This property sets whether to delay automatic acceptance of the search input.
45 *
46 * Set this to true if your search is expensive (such as for online
47 * operations or in exceptionally slow data sets) and want to delay it
48 * for 2.5 seconds.
49 *
50 * @note If you must have immediate feedback (filter-style), use the
51 * text property directly instead of accepted()
52 *
53 * default: ``false``
54 *
55 * @since 5.81
56 * @since org.kde.kirigami 2.16
57 */
58 property bool delaySearch: false
59
60 // padding to accommodate search icon nicely
61 leftPadding: if (effectiveHorizontalAlignment === TextInput.AlignRight) {
62 return _rightActionsRow.width + Kirigami.Units.smallSpacing
63 } else {
64 return searchIcon.width + Kirigami.Units.smallSpacing * 3
65 }
66 rightPadding: if (effectiveHorizontalAlignment === TextInput.AlignRight) {
67 return searchIcon.width + Kirigami.Units.smallSpacing * 3
68 } else {
69 return _rightActionsRow.width + Kirigami.Units.smallSpacing
70 }
71
72 Kirigami.Icon {
73 id: searchIcon
74 LayoutMirroring.enabled: root.effectiveHorizontalAlignment === TextInput.AlignRight
75 anchors.left: root.left
76 anchors.leftMargin: Kirigami.Units.smallSpacing * 2
77 anchors.verticalCenter: root.verticalCenter
78 anchors.verticalCenterOffset: Math.round((root.topPadding - root.bottomPadding) / 2)
79 implicitHeight: Kirigami.Units.iconSizes.sizeForLabels
80 implicitWidth: Kirigami.Units.iconSizes.sizeForLabels
81 color: root.placeholderTextColor
82
83 source: "search"
84 }
85
86 placeholderText: qsTr("Search…")
87
88 Accessible.name: qsTr("Search")
89 Accessible.searchEdit: true
90
91 focusSequence: StandardKey.Find
92 inputMethodHints: Qt.ImhNoPredictiveText
93 EnterKey.type: Qt.EnterKeySearch
94 rightActions: [
95 Kirigami.Action {
96 //ltr confusingly refers to the direction of the arrow in the icon, not the text direction which it should be used in
97 icon.name: root.effectiveHorizontalAlignment === TextInput.AlignRight ? "edit-clear-locationbar-ltr" : "edit-clear-locationbar-rtl"
98 visible: root.text.length > 0
99 text: qsTr("Clear search")
100 onTriggered: {
101 root.clear();
102 // Since we are always sending the accepted signal here (whether or not the user has requested
103 // that the accepted signal be delayed), stop the delay timer that gets started by the text changing
104 // above, so that we don't end up sending two of those in rapid succession.
105 fireSearchDelay.stop();
106 root.accepted();
107 }
108 }
109 ]
110
111 Timer {
112 id: fireSearchDelay
113 interval: root.delaySearch ? Kirigami.Units.humanMoment : Kirigami.Units.shortDuration
114 running: false
115 repeat: false
116 onTriggered: {
117 if (root.acceptableInput) {
118 root.accepted();
119 }
120 }
121 }
122 onAccepted: {
123 fireSearchDelay.running = false
124 }
125 onTextChanged: {
126 if (root.autoAccept) {
127 fireSearchDelay.restart();
128 } else {
129 fireSearchDelay.stop();
130 }
131 }
132}
An item that represents an abstract Action.
Definition Action.qml:15
QAction * repeat(const QObject *recvr, const char *slot, QObject *parent)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 3 2024 11:49:34 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.