TextArea.qml 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. 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. // When the TextArea is in a Flickable, the background is reparented to it
  47. // so that decorations don't move with the content. We need to do the same.
  48. // Also allow the background to be set to null; in that case we're just not visible.
  49. parent: control.background?.parent ?? null
  50. filled: control.Material.containerStyle === Material.Filled
  51. verticalPadding: control.Material.textFieldVerticalPadding
  52. controlHasActiveFocus: control.activeFocus
  53. controlHasText: control.length > 0
  54. controlImplicitBackgroundHeight: control.implicitBackgroundHeight
  55. controlHeight: control.height
  56. leftPadding: control.leftPadding
  57. floatingLeftPadding: control.Material.textFieldHorizontalPadding
  58. }
  59. background: MaterialTextContainer {
  60. implicitWidth: 120
  61. implicitHeight: control.Material.textFieldHeight
  62. filled: control.Material.containerStyle === Material.Filled
  63. fillColor: control.Material.textFieldFilledContainerColor
  64. outlineColor: (enabled && control.hovered) ? control.Material.primaryTextColor : control.Material.hintTextColor
  65. focusedOutlineColor: control.Material.accentColor
  66. // When the control's size is set larger than its implicit size, use whatever size is smaller
  67. // so that the gap isn't too big.
  68. placeholderTextWidth: Math.min(placeholder.width, placeholder.implicitWidth) * placeholder.scale
  69. placeholderTextHAlign: control.effectiveHorizontalAlignment
  70. controlHasActiveFocus: control.activeFocus
  71. controlHasText: control.length > 0
  72. placeholderHasText: placeholder.text.length > 0
  73. horizontalPadding: control.Material.textFieldHorizontalPadding
  74. }
  75. }