ComboBox.qml 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright (C) 2024 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.Templates as T
  7. import QtQuick.Controls.impl
  8. import QtQuick.Controls.FluentWinUI3.impl as Impl
  9. T.ComboBox {
  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. implicitIndicatorHeight + topPadding + bottomPadding)
  16. spacing: __config.contentItem.spacing || 0
  17. topPadding: __config.topPadding || 0
  18. bottomPadding: __config.bottomPadding || 0
  19. leftPadding: (__config.leftPadding + (!control.mirrored || !indicator || !indicator.visible ? 0 : indicator.width + spacing)) || 0
  20. rightPadding: (__config.rightPadding + (control.mirrored || !indicator || !indicator.visible ? 0 : indicator.width + spacing)) || 0
  21. topInset: -__config.topInset || 0
  22. bottomInset: -__config.bottomInset || 0
  23. leftInset: -__config.leftInset || 0
  24. rightInset: -__config.rightInset || 0
  25. readonly property string __currentState: [
  26. !control.enabled && "disabled",
  27. control.enabled && !control.pressed && control.hovered && "hovered",
  28. control.down && control.popup.visible && "open",
  29. control.pressed && "pressed"
  30. ].filter(Boolean).join("_") || "normal"
  31. readonly property var __config: (control.editable && control.down && control.popup.visible // editable combobox differs from normal one only in opened state
  32. ? Config.controls.editablecombobox[__currentState]
  33. : Config.controls.combobox[__currentState]) || {}
  34. readonly property Item __focusFrameTarget: control.editable ? null : control
  35. readonly property bool __isHighContrast: Application.styleHints.accessibility.contrastPreference === Qt.HighContrast
  36. delegate: ItemDelegate {
  37. required property var model
  38. required property int index
  39. width: ListView.view.width
  40. text: model[control.textRole]
  41. palette.highlightedText: control.palette.highlightedText
  42. highlighted: control.highlightedIndex === index
  43. hoverEnabled: control.hoverEnabled
  44. }
  45. indicator: Image {
  46. x: control.mirrored ? control.__config.leftPadding : control.width - width - control.__config.rightPadding
  47. y: (control.topPadding + (control.availableHeight - height) / 2) + (control.pressed ? 1 : 0)
  48. source: Qt.resolvedUrl(control.__config.indicator.filePath)
  49. Behavior on y {
  50. NumberAnimation{ easing.type: Easing.OutCubic; duration: 167 }
  51. }
  52. }
  53. contentItem: T.TextField {
  54. text: control.editable ? control.editText : control.displayText
  55. topPadding: control.__config.label_contentItem.topPadding || 0
  56. leftPadding: control.__config.label_contentItem.leftPadding || 0
  57. rightPadding: control.__config.label_contentItem.rightPadding || 0
  58. bottomPadding: control.__config.label_contentItem.bottomPadding || 0
  59. implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
  60. contentHeight + topPadding + bottomPadding)
  61. enabled: control.editable
  62. autoScroll: control.editable
  63. readOnly: control.down
  64. inputMethodHints: control.inputMethodHints
  65. validator: control.validator
  66. selectByMouse: control.selectTextByMouse
  67. readonly property color __pressedText: Application.styleHints.colorScheme == Qt.Light
  68. ? Qt.rgba(control.palette.text.r, control.palette.text.g, control.palette.text.b, 0.62)
  69. : Qt.rgba(control.palette.text.r, control.palette.text.g, control.palette.text.b, 0.7725)
  70. color: control.down ? __pressedText : control.palette.text
  71. selectionColor: control.palette.highlight
  72. selectedTextColor: control.palette.highlightedText
  73. horizontalAlignment: control.__config.label_text.textHAlignment
  74. verticalAlignment: control.__config.label_text.textVAlignment
  75. readonly property Item __focusFrameControl: control
  76. }
  77. background: ItemGroup {
  78. Impl.StyleImage {
  79. visible: !control.__isHighContrast
  80. imageConfig: control.__config.background
  81. Item {
  82. visible: control.editable && ((control.down && control.popup.visible) || control.activeFocus)
  83. width: parent.width
  84. height: 2
  85. y: parent.height - height
  86. Impl.FocusStroke {
  87. width: parent.width
  88. height: parent.height
  89. radius: control.down && control.popup.visible ? 0 : control.__config.background.bottomOffset
  90. color: control.palette.accent
  91. }
  92. }
  93. }
  94. Rectangle {
  95. visible: control.__isHighContrast
  96. implicitWidth: control.__config.background.width
  97. implicitHeight: control.__config.background.height
  98. color: control.palette.window
  99. border.color: control.hovered ? control.palette.accent : control.palette.text
  100. radius: 4
  101. }
  102. }
  103. popup: T.Popup {
  104. topPadding: control.__config.popup_contentItem.topPadding || 0
  105. leftPadding: control.__config.popup_contentItem.leftPadding || 0
  106. rightPadding: control.__config.popup_contentItem.rightPadding || 0
  107. bottomPadding: control.__config.popup_contentItem.bottomPadding || 0
  108. contentItem: ListView {
  109. clip: true
  110. implicitHeight: contentHeight
  111. highlightMoveDuration: 0
  112. model: control.delegateModel
  113. currentIndex: control.highlightedIndex
  114. }
  115. y: control.editable ? control.height
  116. : -0.25 * Math.max(implicitBackgroundHeight + topInset + bottomInset,
  117. contentHeight + topPadding + bottomPadding)
  118. readonly property real __targetHeight: Math.min(contentItem.implicitHeight + topPadding + bottomPadding, control.Window.height - topMargin - bottomMargin)
  119. property real __heightScale: 1
  120. height: __heightScale * __targetHeight
  121. width: control.width
  122. topMargin: 8
  123. bottomMargin: 8
  124. palette: control.palette
  125. enter: Transition {
  126. NumberAnimation { property: "__heightScale"; from: 0.33; to: 1; easing.type: Easing.OutCubic; duration: 250 }
  127. }
  128. background: ItemGroup {
  129. Impl.StyleImage {
  130. visible: !control.__isHighContrast
  131. imageConfig: control.__config.popup_background.filePath ? control.__config.popup_background : Config.controls.popup["normal"].background // fallback to regular popup
  132. }
  133. Rectangle {
  134. visible: control.__isHighContrast
  135. implicitWidth: Config.controls.popup["normal"].background.width
  136. implicitHeight: Config.controls.popup["normal"].background.height
  137. color: control.palette.window
  138. border.color: control.palette.text
  139. radius: 4
  140. }
  141. }
  142. }
  143. }