Kirigami2

SearchField.qml
1 /*
2  * SPDX-FileCopyrightText: 2019 Carl-Lucien Schwan <[email protected]>
3  * SPDX-FileCopyrightText: 2022 Felipe Kinoshita <[email protected]>
4  *
5  * SPDX-License-Identifier: LGPL-2.0-or-later
6  */
7 
8 import QtQuick 2.15
9 import org.kde.kirigami 2.20 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  */
27 Kirigami.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 (activeFocus || root.text.length > 0 ? 0 : (searchIcon.width + Kirigami.Units.smallSpacing)) + Kirigami.Units.smallSpacing * 2
65  }
66  rightPadding: if (effectiveHorizontalAlignment === TextInput.AlignRight) {
67  return (activeFocus || root.text.length > 0 ? 0 : (searchIcon.width + Kirigami.Units.smallSpacing)) + Kirigami.Units.smallSpacing * 2
68  } else {
69  return _rightActionsRow.width + Kirigami.Units.smallSpacing
70  }
71 
72  Kirigami.Icon {
73  id: searchIcon
74  opacity: root.activeFocus || text.length > 0 ? 0 : 1
75  LayoutMirroring.enabled: root.effectiveHorizontalAlignment === TextInput.AlignRight
76  anchors.left: root.left
77  anchors.leftMargin: Kirigami.Units.smallSpacing * 2
78  anchors.verticalCenter: root.verticalCenter
79  anchors.verticalCenterOffset: Math.round((root.topPadding - root.bottomPadding) / 2)
80  implicitHeight: Kirigami.Units.iconSizes.sizeForLabels
81  implicitWidth: Kirigami.Units.iconSizes.sizeForLabels
82  color: root.placeholderTextColor
83 
84  source: "search"
85 
86  Behavior on opacity {
87  NumberAnimation {
88  duration: Kirigami.Units.longDuration
89  easing.type: Easing.InOutQuad
90  }
91  }
92  }
93 
94  placeholderText: qsTr("Search…")
95 
96  Accessible.name: qsTr("Search")
97  Accessible.searchEdit: true
98 
99  focusSequence: StandardKey.Find
100  inputMethodHints: Qt.ImhNoPredictiveText
101  rightActions: [
102  Kirigami.Action {
103  //ltr confusingly refers to the direction of the arrow in the icon, not the text direction which it should be used in
104  icon.name: root.effectiveHorizontalAlignment === TextInput.AlignRight ? "edit-clear-locationbar-ltr" : "edit-clear-locationbar-rtl"
105  visible: root.text.length > 0
106  text: qsTr("Clear search")
107  onTriggered: {
108  root.clear();
109  // Since we are always sending the accepted signal here (whether or not the user has requested
110  // that the accepted signal be delayed), stop the delay timer that gets started by the text changing
111  // above, so that we don't end up sending two of those in rapid succession.
112  fireSearchDelay.stop();
113  root.accepted();
114  }
115  }
116  ]
117 
118  Timer {
119  id: fireSearchDelay
120  interval: root.delaySearch ? Kirigami.Units.humanMoment : Kirigami.Units.shortDuration
121  running: false; repeat: false;
122  onTriggered: {
123  if (root.acceptableInput) {
124  root.accepted();
125  }
126  }
127  }
128  onAccepted: {
129  fireSearchDelay.running = false
130  }
131  onTextChanged: {
132  if (root.autoAccept) {
133  fireSearchDelay.restart();
134  } else {
135  fireSearchDelay.stop();
136  }
137  }
138 }
QAction * repeat(const QObject *recvr, const char *slot, QObject *parent)
An item that represents an abstract Action.
Definition: Action.qml:14
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Feb 7 2023 04:14:24 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.