SearchField.qml 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.Window
  7. import QtQuick.Controls.impl
  8. import QtQuick.Templates as T
  9. import QtQuick.Controls.Material
  10. import QtQuick.Controls.Material.impl
  11. T.SearchField {
  12. id: control
  13. implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
  14. implicitContentWidth + leftPadding + rightPadding)
  15. implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
  16. implicitContentHeight + topPadding + bottomPadding,
  17. searchIndicator.implicitIndicatorHeight + topPadding + bottomPadding,
  18. clearIndicator.implicitIndicatorHeight + topPadding + bottomPadding)
  19. leftPadding: padding + (control.mirrored || !searchIndicator.indicator || !searchIndicator.indicator.visible ? 0 : searchIndicator.indicator.width + spacing)
  20. rightPadding: padding + (control.mirrored || !clearIndicator.indicator || !clearIndicator.indicator.visible ? 0 : clearIndicator.indicator.width + spacing)
  21. delegate: MenuItem {
  22. width: ListView.view.width
  23. text: model[control.textRole]
  24. font.weight: control.currentIndex === index ? Font.DemiBold : Font.Normal
  25. highlighted: control.highlightedIndex === index
  26. hoverEnabled: control.hoverEnabled
  27. Material.foreground: control.currentIndex === index ? ListView.view.contentItem.Material.accent : ListView.view.contentItem.Material.foreground
  28. required property var model
  29. required property int index
  30. }
  31. searchIndicator.indicator: Item {
  32. x: !control.mirrored ? 10 : control.width - width - 10
  33. y: control.topPadding + (control.availableHeight - height) / 2
  34. height: control.height
  35. width: height / 2
  36. ColorImage {
  37. x: (parent.width - width) / 2
  38. y: (parent.height - height) / 2
  39. source: "qrc:/qt-project.org/imports/QtQuick/Controls/Material/images/search-magnifier.png"
  40. color: control.enabled ? control.Material.foreground : control.Material.hintTextColor
  41. }
  42. }
  43. clearIndicator.indicator: Item {
  44. x: control.mirrored ? 10 : control.width - width - 10
  45. y: control.topPadding + (control.availableHeight - height) / 2
  46. height: control.height
  47. width: height / 2
  48. visible: control.text.length > 0
  49. ColorImage {
  50. x: (parent.width - width) / 2
  51. y: (parent.height - height) / 2
  52. source: "qrc:/qt-project.org/imports/QtQuick/Controls/Material/images/close_circle.png"
  53. color: control.enabled ? control.Material.foreground : control.Material.hintTextColor
  54. }
  55. }
  56. contentItem: T.TextField {
  57. leftPadding: Material.textFieldHorizontalPadding
  58. rightPadding: Material.textFieldHorizontalPadding
  59. topPadding: Material.textFieldVerticalPadding
  60. bottomPadding: Material.textFieldVerticalPadding
  61. text: control.text
  62. color: control.enabled ? control.Material.foreground : control.Material.hintTextColor
  63. selectionColor: control.Material.accentColor
  64. selectedTextColor: control.Material.primaryHighlightedTextColor
  65. verticalAlignment: Text.AlignVCenter
  66. cursorDelegate: CursorDelegate { }
  67. }
  68. background: MaterialTextContainer {
  69. implicitWidth: 160
  70. implicitHeight: control.Material.textFieldHeight
  71. outlineColor: (enabled && control.hovered) ? control.Material.primaryTextColor : control.Material.hintTextColor
  72. focusedOutlineColor: control.Material.accentColor
  73. controlHasActiveFocus: control.activeFocus
  74. controlHasText: true
  75. horizontalPadding: control.Material.textFieldHorizontalPadding
  76. }
  77. popup: T.Popup {
  78. y: control.height
  79. width: control.width
  80. height: contentItem.implicitHeight > 0 ? Math.min(contentItem.implicitHeight + verticalPadding * 2, control.Window.height - control.y - control.height - control.padding) : 0
  81. topMargin: 10
  82. bottomMargin: 10
  83. verticalPadding: 10
  84. contentItem: ListView {
  85. clip: true
  86. implicitHeight: contentHeight
  87. model: control.delegateModel
  88. currentIndex: control.highlightedIndex
  89. highlightMoveDuration: 0
  90. T.ScrollIndicator.vertical: ScrollIndicator { }
  91. }
  92. background: Rectangle {
  93. radius: 5
  94. color: control.Material.dialogColor
  95. layer.enabled: control.enabled > 0
  96. layer.effect: RoundedElevationEffect {
  97. elevation: 4
  98. roundedScale: Material.ExtraSmallScale
  99. }
  100. }
  101. }
  102. }