diff options
author | Bartlomiej Moskal <[email protected]> | 2025-02-26 13:01:27 +0100 |
---|---|---|
committer | Bartlomiej Moskal <[email protected]> | 2025-02-28 09:29:02 +0100 |
commit | e70994631407b16738d2c2be8c956d8d3bbf5499 (patch) | |
tree | 202d3390afaaa953006d9c639d186981ba5b09fe | |
parent | 9d85f5722e8cf2836422b1667506e47c61b00d64 (diff) |
Material: Fix FloatingPlaceholderText X position
In cb7eb152204e206539f307a9556eea43c589f026, we stopped
using the leftPadding value for FloatingPlaceholderText.
As a result, the text is now misaligned with the placeholder.
This commit restores the FloatingPlaceholderText X position to
leftPadding. If leftPadding is not set, it defaults to
Material.textFieldHorizontalPadding.
To prevent regressions (such as QTBUG-120149), a new X position
animation has been added to FloatingPlaceholderText.
Fixes: QTBUG-133492
Pick-to: 6.9 6.8
Change-Id: I5c80dcedd7c2beec0891b524fc55388fa5456f2a
Reviewed-by: Mitch Curtis <[email protected]>
7 files changed, 108 insertions, 4 deletions
diff --git a/src/quickcontrols/material/TextArea.qml b/src/quickcontrols/material/TextArea.qml index eadaceca66..0458c3bbbd 100644 --- a/src/quickcontrols/material/TextArea.qml +++ b/src/quickcontrols/material/TextArea.qml @@ -46,8 +46,6 @@ T.TextArea { FloatingPlaceholderText { id: placeholder - // Don't set this to control.leftPadding, because we don't want it to change if the user changes leftPadding. - x: control.Material.textFieldHorizontalPadding width: control.width - (control.leftPadding + control.rightPadding) text: control.placeholderText font: control.font @@ -65,6 +63,8 @@ T.TextArea { controlHasText: control.length > 0 controlImplicitBackgroundHeight: control.implicitBackgroundHeight controlHeight: control.height + leftPadding: control.leftPadding + floatingLeftPadding: control.Material.textFieldHorizontalPadding } background: MaterialTextContainer { diff --git a/src/quickcontrols/material/TextField.qml b/src/quickcontrols/material/TextField.qml index 94b1a61a5f..57f51ca841 100644 --- a/src/quickcontrols/material/TextField.qml +++ b/src/quickcontrols/material/TextField.qml @@ -46,8 +46,6 @@ T.TextField { FloatingPlaceholderText { id: placeholder - // Don't set this to control.leftPadding, because we don't want it to change if the user changes leftPadding. - x: control.Material.textFieldHorizontalPadding width: control.width - (control.leftPadding + control.rightPadding) text: control.placeholderText font: control.font @@ -61,6 +59,8 @@ T.TextField { controlHasText: control.length > 0 controlImplicitBackgroundHeight: control.implicitBackgroundHeight controlHeight: control.height + leftPadding: control.leftPadding + floatingLeftPadding: control.Material.textFieldHorizontalPadding } background: MaterialTextContainer { diff --git a/src/quickcontrols/material/impl/qquickmaterialplaceholdertext.cpp b/src/quickcontrols/material/impl/qquickmaterialplaceholdertext.cpp index 2447193d6a..aeef892e6f 100644 --- a/src/quickcontrols/material/impl/qquickmaterialplaceholdertext.cpp +++ b/src/quickcontrols/material/impl/qquickmaterialplaceholdertext.cpp @@ -107,6 +107,11 @@ void QQuickMaterialPlaceholderText::updateY() setY(shouldFloat() ? floatingTargetY() : normalTargetY()); } +void QQuickMaterialPlaceholderText::updateX() +{ + setX(shouldFloat() ? floatingTargetX() : normalTargetX()); +} + qreal controlTopInset(QQuickItem *textControl) { if (const auto textArea = qobject_cast<QQuickTextArea *>(textControl)) @@ -149,6 +154,16 @@ qreal QQuickMaterialPlaceholderText::floatingTargetY() const return (-m_largestHeight / 2.0) + controlTopInset(textControl()); } +qreal QQuickMaterialPlaceholderText::normalTargetX() const +{ + return m_leftPadding; +} + +qreal QQuickMaterialPlaceholderText::floatingTargetX() const +{ + return m_floatingLeftPadding; +} + /*! \internal @@ -212,6 +227,24 @@ void QQuickMaterialPlaceholderText::setVerticalPadding(qreal verticalPadding) emit verticalPaddingChanged(); } +void QQuickMaterialPlaceholderText::setLeftPadding(int leftPadding) +{ + if (leftPadding == m_leftPadding) + return; + + m_leftPadding = leftPadding; + updateX(); +} + +void QQuickMaterialPlaceholderText::setFloatingLeftPadding(int floatingLeftPadding) +{ + if (floatingLeftPadding == m_floatingLeftPadding) + return; + + m_floatingLeftPadding = floatingLeftPadding; + updateX(); +} + void QQuickMaterialPlaceholderText::adjustTransformOrigin() { switch (effectiveHAlign()) { @@ -251,6 +284,13 @@ void QQuickMaterialPlaceholderText::controlGotActiveFocus() yAnimation->setEasingCurve(*animationEasingCurve); m_focusInAnimation->addAnimation(yAnimation); + QPropertyAnimation *xAnimation = new QPropertyAnimation(this, "x", this); + xAnimation->setDuration(300); + xAnimation->setStartValue(x()); + xAnimation->setEndValue(floatingTargetX()); + xAnimation->setEasingCurve(*animationEasingCurve); + m_focusInAnimation->addAnimation(xAnimation); + auto *scaleAnimation = new QPropertyAnimation(this, "scale", this); scaleAnimation->setDuration(300); scaleAnimation->setStartValue(1); @@ -261,6 +301,7 @@ void QQuickMaterialPlaceholderText::controlGotActiveFocus() m_focusInAnimation->start(QAbstractAnimation::DeleteWhenStopped); } else { updateY(); + updateX(); } } @@ -282,6 +323,13 @@ void QQuickMaterialPlaceholderText::controlLostActiveFocus() yAnimation->setEasingCurve(*animationEasingCurve); m_focusOutAnimation->addAnimation(yAnimation); + QPropertyAnimation *xAnimation = new QPropertyAnimation(this, "x", this); + xAnimation->setDuration(300); + xAnimation->setStartValue(x()); + xAnimation->setEndValue(normalTargetX()); + xAnimation->setEasingCurve(*animationEasingCurve); + m_focusOutAnimation->addAnimation(xAnimation); + auto *scaleAnimation = new QPropertyAnimation(this, "scale", this); scaleAnimation->setDuration(300); scaleAnimation->setStartValue(floatingScale); @@ -292,12 +340,14 @@ void QQuickMaterialPlaceholderText::controlLostActiveFocus() m_focusOutAnimation->start(QAbstractAnimation::DeleteWhenStopped); } else { updateY(); + updateX(); } } void QQuickMaterialPlaceholderText::maybeSetFocusAnimationProgress() { updateY(); + updateX(); setScale(shouldFloat() ? floatingScale : 1.0); } diff --git a/src/quickcontrols/material/impl/qquickmaterialplaceholdertext_p.h b/src/quickcontrols/material/impl/qquickmaterialplaceholdertext_p.h index bf851d367a..3e9cebddc1 100644 --- a/src/quickcontrols/material/impl/qquickmaterialplaceholdertext_p.h +++ b/src/quickcontrols/material/impl/qquickmaterialplaceholdertext_p.h @@ -37,6 +37,8 @@ class QQuickMaterialPlaceholderText : public QQuickPlaceholderText Q_PROPERTY(qreal controlImplicitBackgroundHeight READ controlImplicitBackgroundHeight WRITE setControlImplicitBackgroundHeight NOTIFY controlImplicitBackgroundHeightChanged FINAL) Q_PROPERTY(qreal controlHeight READ controlHeight WRITE setControlHeight FINAL) + Q_PROPERTY(int leftPadding WRITE setLeftPadding FINAL) + Q_PROPERTY(int floatingLeftPadding WRITE setFloatingLeftPadding FINAL) QML_NAMED_ELEMENT(FloatingPlaceholderText) QML_ADDED_IN_VERSION(6, 5) @@ -63,6 +65,8 @@ public: qreal verticalPadding() const; void setVerticalPadding(qreal verticalPadding); + void setLeftPadding(int leftPadding); + void setFloatingLeftPadding(int floatingLeftPadding); signals: void filledChanged(); void largestHeightChanged(); @@ -79,8 +83,11 @@ private: bool shouldAnimate() const; void updateY(); + void updateX(); qreal normalTargetY() const; qreal floatingTargetY() const; + qreal normalTargetX() const; + qreal floatingTargetX() const; void controlGotActiveFocus(); void controlLostActiveFocus(); @@ -96,6 +103,8 @@ private: qreal m_verticalPadding = 0; qreal m_controlImplicitBackgroundHeight = 0; qreal m_controlHeight = 0; + int m_leftPadding = 0; + int m_floatingLeftPadding = 0; QPointer<QParallelAnimationGroup> m_focusInAnimation; QPointer<QParallelAnimationGroup> m_focusOutAnimation; }; diff --git a/tests/auto/quickcontrols/qquickmaterialstyle/data/tst_material.qml b/tests/auto/quickcontrols/qquickmaterialstyle/data/tst_material.qml index b127b54a38..abaebb2f60 100644 --- a/tests/auto/quickcontrols/qquickmaterialstyle/data/tst_material.qml +++ b/tests/auto/quickcontrols/qquickmaterialstyle/data/tst_material.qml @@ -1082,6 +1082,11 @@ TestCase { verify(placeholderTextItem as MaterialImpl.FloatingPlaceholderText) // This is the default value returned by textFieldHorizontalPadding when using a non-dense variant. compare(placeholderTextItem.x, 16) + + // When the text is cleared, the placeholder text should appear in the previous text position. + // If left padding is set, use its value; otherwise, use the default from textFieldHorizontalPadding. + control.text = "" + compare(placeholderTextItem.x, data.leftPadding != undefined ? data.leftPadding : 16) } Component { diff --git a/tests/manual/quickcontrols/material/pages/TextAreaPage.qml b/tests/manual/quickcontrols/material/pages/TextAreaPage.qml index 9de28d1118..831f7e2094 100644 --- a/tests/manual/quickcontrols/material/pages/TextAreaPage.qml +++ b/tests/manual/quickcontrols/material/pages/TextAreaPage.qml @@ -123,6 +123,26 @@ Page { Material.containerStyle: layout.containerStyle } + TextArea { + placeholderText: "leftPadding 0" + leftPadding: 0 + + Material.containerStyle: layout.containerStyle + } + + TextArea { + placeholderText: "No leftPadding" + + Material.containerStyle: layout.containerStyle + } + + TextArea { + placeholderText: "leftPadding 50" + leftPadding: 50 + + Material.containerStyle: layout.containerStyle + } + Flickable { width: 200 height: 100 diff --git a/tests/manual/quickcontrols/material/pages/TextFieldPage.qml b/tests/manual/quickcontrols/material/pages/TextFieldPage.qml index 9aeef4b5c2..2b48eab86f 100644 --- a/tests/manual/quickcontrols/material/pages/TextFieldPage.qml +++ b/tests/manual/quickcontrols/material/pages/TextFieldPage.qml @@ -91,6 +91,26 @@ Page { Material.containerStyle: layout.containerStyle } + + TextField { + placeholderText: "leftPadding 0" + leftPadding: 0 + + Material.containerStyle: layout.containerStyle + } + + TextField { + placeholderText: "No leftPadding" + + Material.containerStyle: layout.containerStyle + } + + TextField { + placeholderText: "leftPadding 50" + leftPadding: 50 + + Material.containerStyle: layout.containerStyle + } } ColumnLayout { |