diff options
| author | Mitch Curtis <mitch.curtis@qt.io> | 2023-03-07 16:06:29 +0800 |
|---|---|---|
| committer | Mitch Curtis <mitch.curtis@qt.io> | 2023-03-08 14:50:54 +0800 |
| commit | 4f5b4f81b0e73f264a19a690b8795f776778fa3c (patch) | |
| tree | 040a67918b165ea7f604823dfebffe7e2f3f4d4e /src/quickcontrols/material/impl/qquickmaterialplaceholdertext.cpp | |
| parent | ee3d11f70bf7d5d7f3b0f4f0b531a0ed2a20e721 (diff) | |
Material: fix placeholder text y position when control is too small
Ensure that it's sensibly positioned despite its size.
Fixes: QTBUG-111515
Pick-to: 6.5 6.5.0
Change-Id: I71816c461ff1d2f85e010bf871ab1b7ef2ccaf6e
Reviewed-by: Oliver Eftevaag <oliver.eftevaag@qt.io>
Diffstat (limited to 'src/quickcontrols/material/impl/qquickmaterialplaceholdertext.cpp')
| -rw-r--r-- | src/quickcontrols/material/impl/qquickmaterialplaceholdertext.cpp | 58 |
1 files changed, 46 insertions, 12 deletions
diff --git a/src/quickcontrols/material/impl/qquickmaterialplaceholdertext.cpp b/src/quickcontrols/material/impl/qquickmaterialplaceholdertext.cpp index b908a62fc0..40557f46a2 100644 --- a/src/quickcontrols/material/impl/qquickmaterialplaceholdertext.cpp +++ b/src/quickcontrols/material/impl/qquickmaterialplaceholdertext.cpp @@ -9,6 +9,7 @@ #include <QtGui/qpainterpath.h> #include <QtQml/qqmlinfo.h> #include <QtQuickTemplates2/private/qquicktheme_p.h> +#include <QtQuickTemplates2/private/qquicktextarea_p.h> QT_BEGIN_NAMESPACE @@ -100,13 +101,25 @@ bool QQuickMaterialPlaceholderText::shouldAnimate() const : !m_controlHasText && !text().isEmpty(); } +void QQuickMaterialPlaceholderText::updateY() +{ + setY(shouldFloat() ? floatingTargetY() : normalTargetY()); +} + qreal QQuickMaterialPlaceholderText::normalTargetY() const { + auto *textArea = qobject_cast<QQuickTextArea *>(parentItem()); + if (textArea && m_controlHeight >= textArea->implicitHeight()) { + // TextArea can be multiple lines in height, and we want the + // placeholder text to sit in the middle of its default-height + // (one-line) if its explicit height is greater than or equal to its + // implicit height - i.e. if it has room for it. If it doesn't have + // room, just do what TextField does. + return (m_controlImplicitBackgroundHeight - m_largestHeight) / 2.0; + } + // When the placeholder text shouldn't float, it should sit in the middle of the TextField. - // We could just use the control's height minus our height instead of the members, but - // that doesn't work for TextArea, which can be multiple lines in height and hence taller. - // In that case, we want the placeholder text to sit in the middle of its default-height (one-line). - return (m_controlImplicitBackgroundHeight - m_largestHeight) / 2.0; + return (m_controlHeight - height()) / 2.0; } qreal QQuickMaterialPlaceholderText::floatingTargetY() const @@ -142,10 +155,34 @@ void QQuickMaterialPlaceholderText::setControlImplicitBackgroundHeight(qreal con return; m_controlImplicitBackgroundHeight = controlImplicitBackgroundHeight; - setY(shouldFloat() ? floatingTargetY() : normalTargetY()); + updateY(); emit controlImplicitBackgroundHeightChanged(); } +/*! + \internal + + Exists so that we can call updateY when the control's height changes, + which is necessary for some y position calculations. + + We don't really need it for the actual calculations, since we already + have access to the parent item, from which the property comes, but + it's simpler just to use it. +*/ +qreal QQuickMaterialPlaceholderText::controlHeight() const +{ + return m_controlHeight; +} + +void QQuickMaterialPlaceholderText::setControlHeight(qreal controlHeight) +{ + if (qFuzzyCompare(m_controlHeight, controlHeight)) + return; + + m_controlHeight = controlHeight; + updateY(); +} + qreal QQuickMaterialPlaceholderText::verticalPadding() const { return m_verticalPadding; @@ -185,8 +222,7 @@ void QQuickMaterialPlaceholderText::controlGotActiveFocus() m_focusInAnimation->start(QAbstractAnimation::DeleteWhenStopped); } else { - const int newY = shouldFloat() ? floatingTargetY() : normalTargetY(); - setY(newY); + updateY(); } } @@ -212,16 +248,14 @@ void QQuickMaterialPlaceholderText::controlLostActiveFocus() m_focusOutAnimation->start(QAbstractAnimation::DeleteWhenStopped); } else { - const int newY = shouldFloat() ? floatingTargetY() : normalTargetY(); - setY(newY); + updateY(); } } void QQuickMaterialPlaceholderText::maybeSetFocusAnimationProgress() { - const bool shouldWeFloat = shouldFloat(); - setY(shouldWeFloat ? floatingTargetY() : normalTargetY()); - setScale(shouldWeFloat ? floatingScale : 1.0); + updateY(); + setScale(shouldFloat() ? floatingScale : 1.0); } void QQuickMaterialPlaceholderText::componentComplete() |
