TextField.qml 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. cursorDelegate: CursorDelegate { }
  35. FloatingPlaceholderText {
  36. id: placeholder
  37. width: control.width - (control.leftPadding + control.rightPadding)
  38. text: control.placeholderText
  39. font: control.font
  40. color: control.placeholderTextColor
  41. elide: Text.ElideRight
  42. renderType: control.renderType
  43. filled: control.Material.containerStyle === Material.Filled
  44. verticalPadding: control.Material.textFieldVerticalPadding
  45. controlHasActiveFocus: control.activeFocus
  46. controlHasText: control.length > 0
  47. controlImplicitBackgroundHeight: control.implicitBackgroundHeight
  48. controlHeight: control.height
  49. leftPadding: control.leftPadding
  50. floatingLeftPadding: control.Material.textFieldHorizontalPadding
  51. }
  52. background: MaterialTextContainer {
  53. implicitWidth: 120
  54. implicitHeight: control.Material.textFieldHeight
  55. filled: control.Material.containerStyle === Material.Filled
  56. fillColor: control.Material.textFieldFilledContainerColor
  57. outlineColor: (enabled && control.hovered) ? control.Material.primaryTextColor : control.Material.hintTextColor
  58. focusedOutlineColor: control.Material.accentColor
  59. // When the control's size is set larger than its implicit size, use whatever size is smaller
  60. // so that the gap isn't too big.
  61. placeholderTextWidth: Math.min(placeholder.width, placeholder.implicitWidth) * placeholder.scale
  62. placeholderTextHAlign: control.effectiveHorizontalAlignment
  63. controlHasActiveFocus: control.activeFocus
  64. controlHasText: control.length > 0
  65. placeholderHasText: placeholder.text.length > 0
  66. horizontalPadding: control.Material.textFieldHorizontalPadding
  67. }
  68. }