SearchField.qml 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright (C) 2025 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
  3. // Qt-Security score:significant reason:default
  4. pragma ComponentBehavior: Bound
  5. import QtQuick
  6. import QtQuick.Controls.impl
  7. import QtQuick.Templates as T
  8. import QtQuick.Controls.Basic.impl
  9. T.SearchField {
  10. id: control
  11. implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
  12. implicitContentWidth + leftPadding + rightPadding)
  13. implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
  14. implicitContentHeight + topPadding + bottomPadding,
  15. searchIndicator.implicitIndicatorHeight + topPadding + bottomPadding,
  16. clearIndicator.implicitIndicatorHeight + topPadding + bottomPadding)
  17. leftPadding: padding + (control.mirrored || !searchIndicator.indicator || !searchIndicator.indicator.visible ? 0 : searchIndicator.indicator.width + spacing)
  18. rightPadding: padding + (control.mirrored || !clearIndicator.indicator || !clearIndicator.indicator.visible ? 0 : clearIndicator.indicator.width + spacing)
  19. delegate: ItemDelegate {
  20. width: ListView.view.width
  21. text: model[control.textRole]
  22. palette.text: control.palette.text
  23. palette.highlightedText: control.palette.highlightedText
  24. font.weight: control.currentIndex === index ? Font.DemiBold : Font.Normal
  25. highlighted: control.highlightedIndex === index
  26. hoverEnabled: control.hoverEnabled
  27. required property var model
  28. required property int index
  29. }
  30. searchIndicator.indicator: Rectangle {
  31. implicitWidth: 28
  32. implicitHeight: 28
  33. height: control.height - (background.border.width * 2)
  34. x: !control.mirrored ? 3 : control.width - width - 3
  35. y: background.border.width
  36. color: control.palette.button
  37. ColorImage {
  38. x: (parent.width - width) / 2
  39. y: (parent.height - height) / 2
  40. color: control.palette.dark
  41. defaultColor: "#353637"
  42. source: "qrc:/qt-project.org/imports/QtQuick/Controls/Basic/images/search-magnifier.png"
  43. opacity: enabled ? 1 : 0.3
  44. }
  45. }
  46. clearIndicator.indicator: Rectangle {
  47. implicitWidth: 28
  48. implicitHeight: 28
  49. height: control.height - (background.border.width * 2)
  50. x: control.mirrored ? 3 : control.width - width - 3
  51. y: background.border.width
  52. visible: control.text.length > 0
  53. color: control.palette.button
  54. ColorImage {
  55. x: (parent.width - width) / 2
  56. y: (parent.height - height) / 2
  57. color: control.palette.dark
  58. defaultColor: "#353637"
  59. source: "qrc:/qt-project.org/imports/QtQuick/Controls/Basic/images/close_circle.png"
  60. opacity: enabled ? 1 : 0.3
  61. }
  62. }
  63. contentItem: T.TextField {
  64. implicitHeight: contentHeight + topPadding + bottomPadding
  65. leftPadding: control.searchIndicator.indicator && !control.mirrored ? 6 : 0
  66. rightPadding: control.clearIndicator.indicator && !control.mirrored ? 6 : 0
  67. topPadding: 6 - control.padding
  68. bottomPadding: 6 - control.padding
  69. text: control.text
  70. color: control.palette.text
  71. selectionColor: control.palette.highlight
  72. selectedTextColor: control.palette.highlightedText
  73. verticalAlignment: TextInput.AlignVCenter
  74. ContextMenu.menu: TextEditingContextMenu {
  75. editor: parent
  76. }
  77. }
  78. background: Rectangle {
  79. implicitWidth: 200
  80. implicitHeight: 40
  81. color: control.palette.button
  82. border.width: (control.activeFocus || control.contentItem.activeFocus) ? 2 : 1
  83. border.color: (control.activeFocus || control.contentItem.activeFocus) ? control.palette.highlight : control.palette.mid
  84. }
  85. popup: T.Popup {
  86. y: control.height
  87. width: control.width
  88. height: Math.min(contentItem.implicitHeight, control.Window.height - control.y - control.height - control.padding)
  89. topMargin: 6
  90. bottomMargin: 6
  91. palette: control.palette
  92. contentItem: ListView {
  93. clip: true
  94. implicitHeight: contentHeight
  95. model: control.delegateModel
  96. currentIndex: control.highlightedIndex
  97. highlightMoveDuration: 0
  98. Rectangle {
  99. z: 10
  100. width: parent.width
  101. height: parent.height
  102. color: "transparent"
  103. border.color: control.palette.mid
  104. }
  105. T.ScrollIndicator.vertical: ScrollIndicator { }
  106. }
  107. background: Rectangle {
  108. color: control.palette.window
  109. }
  110. }
  111. }