TextArea.qml 4.1 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.TextArea {
  10. id: control
  11. implicitWidth: Math.max(contentWidth + leftPadding + rightPadding,
  12. implicitBackgroundWidth + leftInset + rightInset,
  13. placeholder.implicitWidth + leftPadding + rightPadding)
  14. implicitHeight: Math.max(contentHeight + topPadding + bottomPadding,
  15. implicitBackgroundHeight + topInset + bottomInset)
  16. // If we're clipped, or we're in a Flickable that's clipped, set our topInset
  17. // to half the height of the placeholder text to avoid it being clipped.
  18. topInset: clip || (parent?.parent as Flickable && parent?.parent.clip) ? placeholder.largestHeight / 2 : 0
  19. leftPadding: Material.textFieldHorizontalPadding
  20. rightPadding: Material.textFieldHorizontalPadding
  21. // Need to account for the placeholder text when it's sitting on top.
  22. topPadding: Material.containerStyle === Material.Filled && placeholderText.length > 0 && (activeFocus || length > 0)
  23. ? Material.textFieldVerticalPadding + placeholder.largestHeight
  24. // When the condition above is not met, the text should always sit in the middle
  25. // of a default-height TextArea, which is just near the top for a higher-than-default one.
  26. // Account for any topInset as well, otherwise the text will be too close to the background.
  27. : ((implicitBackgroundHeight - placeholder.largestHeight) / 2) + topInset
  28. bottomPadding: Material.textFieldVerticalPadding
  29. color: enabled ? Material.foreground : Material.hintTextColor
  30. selectionColor: Material.accentColor
  31. selectedTextColor: Material.primaryHighlightedTextColor
  32. placeholderTextColor: enabled && activeFocus ? Material.accentColor : Material.hintTextColor
  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. // When the TextArea is in a Flickable, the background is reparented to it
  44. // so that decorations don't move with the content. We need to do the same.
  45. // Also allow the background to be set to null; in that case we're just not visible.
  46. parent: control.background?.parent ?? null
  47. filled: control.Material.containerStyle === Material.Filled
  48. verticalPadding: control.Material.textFieldVerticalPadding
  49. controlHasActiveFocus: control.activeFocus
  50. controlHasText: control.length > 0
  51. controlImplicitBackgroundHeight: control.implicitBackgroundHeight
  52. controlHeight: control.height
  53. leftPadding: control.leftPadding
  54. floatingLeftPadding: control.Material.textFieldHorizontalPadding
  55. }
  56. background: MaterialTextContainer {
  57. implicitWidth: 120
  58. implicitHeight: control.Material.textFieldHeight
  59. filled: control.Material.containerStyle === Material.Filled
  60. fillColor: control.Material.textFieldFilledContainerColor
  61. outlineColor: (enabled && control.hovered) ? control.Material.primaryTextColor : control.Material.hintTextColor
  62. focusedOutlineColor: control.Material.accentColor
  63. // When the control's size is set larger than its implicit size, use whatever size is smaller
  64. // so that the gap isn't too big.
  65. placeholderTextWidth: Math.min(placeholder.width, placeholder.implicitWidth) * placeholder.scale
  66. placeholderTextHAlign: control.effectiveHorizontalAlignment
  67. controlHasActiveFocus: control.activeFocus
  68. controlHasText: control.length > 0
  69. placeholderHasText: placeholder.text.length > 0
  70. horizontalPadding: control.Material.textFieldHorizontalPadding
  71. }
  72. }