SearchField.qml 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. import QtQuick
  5. import QtQuick.Templates as T
  6. import QtQuick.Controls.Imagine
  7. import QtQuick.Controls.Imagine.impl
  8. T.SearchField {
  9. id: control
  10. // Note: the width of the indicators are calculated into the padding
  11. implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
  12. contentItem.implicitWidth + leftPadding + rightPadding)
  13. implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
  14. implicitContentHeight + topPadding + bottomPadding,
  15. searchIndicator.implicitIndicatorHeight, clearIndicator.implicitIndicatorHeight)
  16. topPadding: background ? background.topPadding : 0
  17. leftPadding: (background ? background.leftPadding : 0) + (control.mirrored ? __clearIndicatorWidth : __searchIndicatorWidth)
  18. rightPadding: (background ? background.rightPadding : 0) + (control.mirrored ? __searchIndicatorWidth : __clearIndicatorWidth)
  19. bottomPadding: background ? background.bottomPadding : 0
  20. topInset: background ? -background.topInset || 0 : 0
  21. leftInset: background ? -background.leftInset || 0 : 0
  22. rightInset: background ? -background.rightInset || 0 : 0
  23. bottomInset: background ? -background.bottomInset || 0 : 0
  24. readonly property real __clearIndicatorWidth: !clearIndicator.indicator || !clearIndicator.indicator.visible
  25. ? 0 : clearIndicator.indicator.width
  26. readonly property real __searchIndicatorWidth: !searchIndicator.indicator || !searchIndicator.indicator.visible
  27. ? 0 : searchIndicator.indicator.width
  28. delegate: ItemDelegate {
  29. width: ListView.view.width
  30. text: model[control.textRole]
  31. palette.text: control.palette.text
  32. palette.highlightedText: control.palette.highlightedText
  33. font.weight: control.currentIndex === index ? Font.DemiBold : Font.Normal
  34. highlighted: control.highlightedIndex === index
  35. hoverEnabled: control.hoverEnabled
  36. required property var model
  37. required property int index
  38. }
  39. searchIndicator.indicator: NinePatchImage {
  40. x: control.mirrored ? control.width - width : 0
  41. y: Math.round((control.height - height) / 2)
  42. height: control.height
  43. source: Imagine.url + "searchfield-indicator"
  44. NinePatchImageSelector on source {
  45. states: [
  46. {"search": true},
  47. {"disabled": !control.searchIndicator.indicator.enabled},
  48. {"editable": !control.editable},
  49. {"pressed": control.searchIndicator.pressed},
  50. {"focused": control.visualFocus},
  51. {"mirrored": control.mirrored},
  52. {"hovered": control.searchIndicator.hovered}
  53. ]
  54. }
  55. }
  56. clearIndicator.indicator: NinePatchImage {
  57. x: control.mirrored ? 0 : control.width - width
  58. y: Math.round((control.height - height) / 2)
  59. height: control.height
  60. visible: control.text.length > 0
  61. source: Imagine.url + "searchfield-indicator"
  62. NinePatchImageSelector on source {
  63. states: [
  64. {"clear": true},
  65. {"disabled": !control.clearIndicator.indicator.enabled},
  66. {"editable": !control.editable},
  67. {"pressed": control.clearIndicator.pressed},
  68. {"focused": control.visualFocus},
  69. {"mirrored": control.mirrored},
  70. {"hovered": control.clearIndicator.hovered}
  71. ]
  72. }
  73. }
  74. contentItem: T.TextField {
  75. implicitHeight: contentHeight + topPadding + bottomPadding
  76. z: 2
  77. text: control.text
  78. color: control.flat ? control.palette.windowText : control.palette.text
  79. selectionColor: control.palette.highlight
  80. selectedTextColor: control.palette.highlightedText
  81. verticalAlignment: Text.AlignVCenter
  82. ContextMenu.menu: TextEditingContextMenu {
  83. editor: parent
  84. }
  85. }
  86. background: NinePatchImage {
  87. source: Imagine.url + "searchfield-background"
  88. NinePatchImageSelector on source {
  89. states: [
  90. {"disabled": !control.enabled},
  91. {"editable": !control.editable},
  92. {"focused": control.activeFocus},
  93. {"mirrored": control.mirrored},
  94. {"hovered": control.enabled && control.hovered}
  95. ]
  96. }
  97. }
  98. popup: T.Popup {
  99. y: control.height
  100. width: control.width
  101. height: Math.min(contentItem.implicitHeight, control.Window.height - control.y - control.height - control.padding)
  102. topMargin: background.topInset
  103. bottomMargin: background.bottomInset
  104. topPadding: background.topPadding
  105. leftPadding: background.leftPadding
  106. rightPadding: background.rightPadding
  107. bottomPadding: background.bottomPadding
  108. topInset: background ? -background.topInset || 0 : 0
  109. leftInset: background ? -background.leftInset || 0 : 0
  110. rightInset: background ? -background.rightInset || 0 : 0
  111. bottomInset: background ? -background.bottomInset || 0 : 0
  112. palette.text: control.palette.text
  113. palette.highlight: control.palette.highlight
  114. palette.highlightedText: control.palette.highlightedText
  115. palette.windowText: control.palette.windowText
  116. palette.buttonText: control.palette.buttonText
  117. contentItem: ListView {
  118. clip: true
  119. implicitHeight: contentHeight
  120. model: control.delegateModel
  121. currentIndex: control.highlightedIndex
  122. highlightMoveDuration: 0
  123. T.ScrollIndicator.vertical: ScrollIndicator { }
  124. }
  125. background: NinePatchImage {
  126. source: Imagine.url + "searchfield-popup"
  127. NinePatchImageSelector on source {
  128. states: [
  129. {"disabled": !control.enabled},
  130. {"pressed": control.pressed},
  131. {"editable": control.editable},
  132. {"focused": control.visualFocus || (control.editable && control.activeFocus)},
  133. {"mirrored": control.mirrored},
  134. {"hovered": control.enabled && control.hovered},
  135. {"flat": control.flat}
  136. ]
  137. }
  138. }
  139. }
  140. }