Plasma-framework

SearchField.qml
1// SPDX-FileCopyrightText: 2019 Carl-Lucien Schwan <carl@carlschwan.eu>
2// SPDX-FileCopyrightText: 2022 Felipe Kinoshita <kinofhek@gmail.com>
3// SPDX-License-Identifier: LGPL-2.0-or-later
4
5import QtQuick
6import QtQuick.Controls as QQC2
7
8import org.kde.plasma.components as PlasmaComponents3
9import org.kde.plasma.extras as PlasmaExtras
10import org.kde.ksvg as KSvg
11import org.kde.kirigami as Kirigami
12
13/**
14 * This is a standard textfield following KDE HIG. Using Ctrl+F as focus
15 * sequence and "Search…" as placeholder text.
16 *
17 * Example usage for the search field component:
18 * @code
19 * import org.kde.plasma.extras as PlasmaExtras
20 *
21 * PlasmaExtras.SearchField {
22 * id: searchField
23 * onAccepted: console.log("Search text is " + searchField.text)
24 * }
25 * @endcode
26 *
27 * @inherit org::plasmas::extras::ActionTextField
28 * @since 5.93
29 */
30PlasmaExtras.ActionTextField {
31 id: root
32
33 property int _iconWidth: searchIcon.width + searchIcon.anchors.leftMargin
34
35 // padding to accommodate search icon nicely
36 leftPadding: if (root.effectiveHorizontalAlignment === TextInput.AlignRight) {
37 return (_rightActionsRow.visible ? _rightActionsRow.width : 0) + (__hasBackgroundAndMargins ? background.margins.left : 0);
38 } else {
39 return _iconWidth + (_leftActionsRow.visible ? _leftActionsRow.width : 0) + (__hasBackgroundAndMargins ? background.margins.left : 0);
40 }
41 rightPadding: if (root.effectiveHorizontalAlignment === TextInput.AlignRight) {
42 return _iconWidth + (_leftActionsRow.visible ? _leftActionsRow.width : 0) + (__hasBackgroundAndMargins ? background.margins.right : 0);
43 } else {
44 return (_rightActionsRow.visible ? _rightActionsRow.width : 0) + (__hasBackgroundAndMargins ? background.margins.right : 0);
45 }
46
47 Kirigami.Icon {
48 id: searchIcon
49 LayoutMirroring.enabled: root.effectiveHorizontalAlignment === TextInput.AlignRight
50 anchors.left: root.left
51 anchors.leftMargin: Kirigami.Units.smallSpacing * 2
52 anchors.verticalCenter: root.verticalCenter
53 anchors.verticalCenterOffset: Math.round((root.topPadding - root.bottomPadding) / 2)
54 implicitHeight: Kirigami.Units.iconSizes.sizeForLabels
55 implicitWidth: Kirigami.Units.iconSizes.sizeForLabels
56
57 source: "search"
58 }
59
60 placeholderText: i18nd("libplasma6", "Search…")
61
62 Accessible.name: i18nd("libplasma6", "Search")
63 Accessible.searchEdit: true
64
65 focusSequence: StandardKey.Find
66 inputMethodHints: Qt.ImhNoPredictiveText
67
68 rightActions: [
70 //ltr confusingly refers to the direction of the arrow in the icon, not the text direction which it should be used in
71 icon.name: root.effectiveHorizontalAlignment === TextInput.AlignRight ? "edit-clear-locationbar-ltr" : "edit-clear-locationbar-rtl"
72 visible: root.text.length > 0
73 text: i18nd("libplasma6", "Clear search")
74 onTriggered: {
75 root.clear();
76 root.accepted();
77 }
78 }
79 ]
80}
QString i18nd(const char *domain, const char *text, const TYPE &arg...)
void clear()
QTextStream & right(QTextStream &stream)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 10 2024 11:48:21 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.