Warning, /frameworks/kirigami/src/controls/SearchField.qml is written in an unsupported language. File is not indexed.

0001 /*
0002  *  SPDX-FileCopyrightText: 2019 Carl-Lucien Schwan <carl@carlschwan.eu>
0003  *  SPDX-FileCopyrightText: 2022 Felipe Kinoshita <kinofhek@gmail.com>
0004  *
0005  *  SPDX-License-Identifier: LGPL-2.0-or-later
0006  */
0007 
0008 import QtQuick
0009 import org.kde.kirigami as Kirigami
0010 
0011 /**
0012  * @brief This is a standard TextField following the KDE HIG, which, by default,
0013  * uses Ctrl+F as the focus keyboard shortcut and "Search…" as a placeholder text.
0014  *
0015  * Example usage for the search field component:
0016  * @code
0017  * import org.kde.kirigami 2.20 as Kirigami
0018  *
0019  * Kirigami.SearchField {
0020  *     id: searchField
0021  *     onAccepted: console.log("Search text is " + searchField.text)
0022  * }
0023  * @endcode
0024  *
0025  * @inherit org::kde::kirigami::ActionTextField
0026  */
0027 Kirigami.ActionTextField {
0028     id: root
0029     /**
0030      * @brief This property sets whether the accepted signal is fired automatically
0031      * when the text is changed.
0032      *
0033      * Setting this to false will require that the user presses return or enter
0034      * (the same way a QtQuick.Controls.TextInput works).
0035      *
0036      * default: ``true``
0037      *
0038      * @since 5.81
0039      * @since org.kde.kirigami 2.16
0040      */
0041     property bool autoAccept: true
0042 
0043     /**
0044      * @brief This property sets whether to delay automatic acceptance of the search input.
0045      *
0046      * Set this to true if your search is expensive (such as for online
0047      * operations or in exceptionally slow data sets) and want to delay it
0048      * for 2.5 seconds.
0049      *
0050      * @note If you must have immediate feedback (filter-style), use the
0051      * text property directly instead of accepted()
0052      *
0053      * default: ``false``
0054      *
0055      * @since 5.81
0056      * @since org.kde.kirigami 2.16
0057      */
0058     property bool delaySearch: false
0059 
0060     // padding to accommodate search icon nicely
0061     leftPadding: if (effectiveHorizontalAlignment === TextInput.AlignRight) {
0062         return _rightActionsRow.width + Kirigami.Units.smallSpacing
0063     } else {
0064         return searchIcon.width + Kirigami.Units.smallSpacing * 3
0065     }
0066     rightPadding: if (effectiveHorizontalAlignment === TextInput.AlignRight) {
0067         return searchIcon.width + Kirigami.Units.smallSpacing * 3
0068     } else {
0069         return _rightActionsRow.width + Kirigami.Units.smallSpacing
0070     }
0071 
0072     Kirigami.Icon {
0073         id: searchIcon
0074         LayoutMirroring.enabled: root.effectiveHorizontalAlignment === TextInput.AlignRight
0075         anchors.left: root.left
0076         anchors.leftMargin: Kirigami.Units.smallSpacing * 2
0077         anchors.verticalCenter: root.verticalCenter
0078         anchors.verticalCenterOffset: Math.round((root.topPadding - root.bottomPadding) / 2)
0079         implicitHeight: Kirigami.Units.iconSizes.sizeForLabels
0080         implicitWidth: Kirigami.Units.iconSizes.sizeForLabels
0081         color: root.placeholderTextColor
0082 
0083         source: "search"
0084     }
0085 
0086     placeholderText: qsTr("Search…")
0087 
0088     Accessible.name: qsTr("Search")
0089     Accessible.searchEdit: true
0090 
0091     focusSequence: StandardKey.Find
0092     inputMethodHints: Qt.ImhNoPredictiveText
0093     EnterKey.type: Qt.EnterKeySearch
0094     rightActions: [
0095         Kirigami.Action {
0096             //ltr confusingly refers to the direction of the arrow in the icon, not the text direction which it should be used in
0097             icon.name: root.effectiveHorizontalAlignment === TextInput.AlignRight ? "edit-clear-locationbar-ltr" : "edit-clear-locationbar-rtl"
0098             visible: root.text.length > 0
0099             text: qsTr("Clear search")
0100             onTriggered: {
0101                 root.clear();
0102                 // Since we are always sending the accepted signal here (whether or not the user has requested
0103                 // that the accepted signal be delayed), stop the delay timer that gets started by the text changing
0104                 // above, so that we don't end up sending two of those in rapid succession.
0105                 fireSearchDelay.stop();
0106                 root.accepted();
0107             }
0108         }
0109     ]
0110 
0111     Timer {
0112         id: fireSearchDelay
0113         interval: root.delaySearch ? Kirigami.Units.humanMoment : Kirigami.Units.shortDuration
0114         running: false
0115         repeat: false
0116         onTriggered: {
0117             if (root.acceptableInput) {
0118                 root.accepted();
0119             }
0120         }
0121     }
0122     onAccepted: {
0123         fireSearchDelay.running = false
0124     }
0125     onTextChanged: {
0126         if (root.autoAccept) {
0127             fireSearchDelay.restart();
0128         } else {
0129             fireSearchDelay.stop();
0130         }
0131     }
0132 }