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 2.15
0009 import org.kde.kirigami 2.20 as Kirigami
0010 
0011 /**
0012  * @brief This is a standard QtQuick.Controls.TextField following the KDE HIG,
0013  * which, by default, uses Ctrl+F as the focus keyboard shortcut
0014  * and "Search…" as a placeholder text.
0015  *
0016  * Example usage:
0017  * @code{.qml}
0018  * import org.kde.kirigami 2.20 as Kirigami
0019  *
0020  * Kirigami.SearchField {
0021  *     id: searchField
0022  *     onAccepted: console.log("Search text is " + searchField.text)
0023  * }
0024  * @endcode
0025  * @inherit 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 @c false will require that the user presses return or enter
0034      * (similarly to QtQuick.TextInput).
0035      *
0036      * default: ``true``
0037      *
0038      * @since KDE Frameworks 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 @c 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 KDE Frameworks 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 (activeFocus || root.text.length > 0 ? 0 : (searchIcon.width + Kirigami.Units.smallSpacing)) + Kirigami.Units.smallSpacing * 2
0065     }
0066     rightPadding: if (effectiveHorizontalAlignment === TextInput.AlignRight) {
0067         return (activeFocus || root.text.length > 0 ? 0 : (searchIcon.width + Kirigami.Units.smallSpacing)) + Kirigami.Units.smallSpacing * 2
0068     } else {
0069         return _rightActionsRow.width + Kirigami.Units.smallSpacing
0070     }
0071 
0072     Kirigami.Icon {
0073         id: searchIcon
0074         opacity: root.activeFocus || text.length > 0 ? 0 : 1
0075         LayoutMirroring.enabled: root.effectiveHorizontalAlignment === TextInput.AlignRight
0076         anchors.left: root.left
0077         anchors.leftMargin: Kirigami.Units.smallSpacing * 2
0078         anchors.verticalCenter: root.verticalCenter
0079         anchors.verticalCenterOffset: Math.round((root.topPadding - root.bottomPadding) / 2)
0080         implicitHeight: Kirigami.Units.iconSizes.sizeForLabels
0081         implicitWidth: Kirigami.Units.iconSizes.sizeForLabels
0082         color: root.placeholderTextColor
0083 
0084         source: "search"
0085 
0086         Behavior on opacity {
0087             NumberAnimation {
0088                 duration: Kirigami.Units.longDuration
0089                 easing.type: Easing.InOutQuad
0090             }
0091         }
0092     }
0093 
0094     placeholderText: qsTr("Search…")
0095 
0096     Accessible.name: qsTr("Search")
0097     Accessible.searchEdit: true
0098 
0099     focusSequence: StandardKey.Find
0100     inputMethodHints: Qt.ImhNoPredictiveText
0101     EnterKey.type: Qt.EnterKeySearch
0102     rightActions: [
0103         Kirigami.Action {
0104             //ltr confusingly refers to the direction of the arrow in the icon, not the text direction which it should be used in
0105             icon.name: root.effectiveHorizontalAlignment === TextInput.AlignRight ? "edit-clear-locationbar-ltr" : "edit-clear-locationbar-rtl"
0106             visible: root.text.length > 0
0107             text: qsTr("Clear search")
0108             onTriggered: {
0109                 root.clear();
0110                 // Since we are always sending the accepted signal here (whether or not the user has requested
0111                 // that the accepted signal be delayed), stop the delay timer that gets started by the text changing
0112                 // above, so that we don't end up sending two of those in rapid succession.
0113                 fireSearchDelay.stop();
0114                 root.accepted();
0115             }
0116         }
0117     ]
0118 
0119     Timer {
0120         id: fireSearchDelay
0121         interval: root.delaySearch ? Kirigami.Units.humanMoment : Kirigami.Units.shortDuration
0122         running: false; repeat: false;
0123         onTriggered: {
0124             if (root.acceptableInput) {
0125                 root.accepted();
0126             }
0127         }
0128     }
0129     onAccepted: {
0130         fireSearchDelay.running = false
0131     }
0132     onTextChanged: {
0133         if (root.autoAccept) {
0134             fireSearchDelay.restart();
0135         } else {
0136             fireSearchDelay.stop();
0137         }
0138     }
0139 }