TextField.qml 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright (C) 2017 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.impl
  7. import QtQuick.Controls.Material
  8. import QtQuick.Controls.Material.impl
  9. T.TextField {
  10. id: control
  11. implicitWidth: implicitBackgroundWidth + leftInset + rightInset
  12. || Math.max(contentWidth, placeholder.implicitWidth) + leftPadding + rightPadding
  13. implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
  14. contentHeight + topPadding + bottomPadding)
  15. // If we're clipped, set topInset to half the height of the placeholder text to avoid it being clipped.
  16. topInset: clip ? placeholder.largestHeight / 2 : 0
  17. leftPadding: Material.textFieldHorizontalPadding
  18. rightPadding: Material.textFieldHorizontalPadding
  19. // Need to account for the placeholder text when it's sitting on top.
  20. topPadding: Material.containerStyle === Material.Filled
  21. ? placeholderText.length > 0 && (activeFocus || length > 0)
  22. ? Material.textFieldVerticalPadding + placeholder.largestHeight
  23. : Material.textFieldVerticalPadding
  24. // Account for any topInset (used to avoid floating placeholder text being clipped),
  25. // otherwise the text will be too close to the background.
  26. : Material.textFieldVerticalPadding + topInset
  27. bottomPadding: Material.textFieldVerticalPadding
  28. color: enabled ? Material.foreground : Material.hintTextColor
  29. selectionColor: Material.accentColor
  30. selectedTextColor: Material.primaryHighlightedTextColor
  31. placeholderTextColor: enabled && activeFocus ? Material.accentColor : Material.hintTextColor
  32. verticalAlignment: TextInput.AlignVCenter
  33. Material.containerStyle: Material.Outlined
  34. ContextMenu.menu: TextEditingContextMenu {
  35. editor: control
  36. }
  37. cursorDelegate: CursorDelegate { }
  38. FloatingPlaceholderText {
  39. id: placeholder
  40. width: control.width - (control.leftPadding + control.rightPadding)
  41. text: control.placeholderText
  42. font: control.font
  43. color: control.placeholderTextColor
  44. elide: Text.ElideRight
  45. renderType: control.renderType
  46. filled: control.Material.containerStyle === Material.Filled
  47. verticalPadding: control.Material.textFieldVerticalPadding
  48. controlHasActiveFocus: control.activeFocus
  49. controlHasText: control.length > 0
  50. controlImplicitBackgroundHeight: control.implicitBackgroundHeight
  51. controlHeight: control.height
  52. leftPadding: control.leftPadding
  53. floatingLeftPadding: control.Material.textFieldHorizontalPadding
  54. }
  55. background: MaterialTextContainer {
  56. implicitWidth: 120
  57. implicitHeight: control.Material.textFieldHeight
  58. filled: control.Material.containerStyle === Material.Filled
  59. fillColor: control.Material.textFieldFilledContainerColor
  60. outlineColor: (enabled && control.hovered) ? control.Material.primaryTextColor : control.Material.hintTextColor
  61. focusedOutlineColor: control.Material.accentColor
  62. // When the control's size is set larger than its implicit size, use whatever size is smaller
  63. // so that the gap isn't too big.
  64. placeholderTextWidth: Math.min(placeholder.width, placeholder.implicitWidth) * placeholder.scale
  65. placeholderTextHAlign: control.effectiveHorizontalAlignment
  66. controlHasActiveFocus: control.activeFocus
  67. controlHasText: control.length > 0
  68. placeholderHasText: placeholder.text.length > 0
  69. horizontalPadding: control.Material.textFieldHorizontalPadding
  70. }
  71. }