diff options
author | Mitch Curtis <[email protected]> | 2023-04-20 15:06:21 +0800 |
---|---|---|
committer | Mitch Curtis <[email protected]> | 2023-05-09 13:23:11 +0800 |
commit | 5da4d5e808ed8d300b8886ba1d19c0ab2c632ea7 (patch) | |
tree | 999245080cf54e5f4092a3cb12f059b19dbebdc2 /src/quickcontrols/material/impl/qquickmaterialplaceholdertext.cpp | |
parent | 045d2259f83a20d1970ba3788361078b73b6caa5 (diff) |
Material: fix clipped floating placeholder text
The changes to placeholder text in
20e3d1b522d1b79239e9ac4a6af47ce3648512bd and
5704741a4073ac131782c3dd73cac5cda6800a28 result in floating placeholder
text (i.e. the text shown at the top of the control when it has active
focus) being clipped when e.g. in a ScrollView.
This is probably only an issue for TextArea in practice, since you
wouldn't usually put a TextField in a ScrollView, but you can still run
into it if you clip it.
We don't want to unconditionally set topInset by default, because, as
mentioned above, these controls are not always clipped. In most cases
they are used on their own, and this issue won't affect them.
Unconditionally setting topInset would ruin the layout of existing UIs.
So, we set topInset only if the control itself clips (or its Flickable
parent in the case of TextArea).
[ChangeLog][Controls][Material] The outlined TextArea now sets topInset
by default if it or its Flickable parent clips. This avoids the
floating placeholder being clipped in those cases. The outlined
TextField sets topInset by default only if the TextField itself clips.
Fixes: QTBUG-113321
Pick-to: 6.5
Change-Id: I8555e4fc0c7a9800f76b54a84d94f4d04691bc23
Reviewed-by: Richard Moe Gustavsen <[email protected]>
Diffstat (limited to 'src/quickcontrols/material/impl/qquickmaterialplaceholdertext.cpp')
-rw-r--r-- | src/quickcontrols/material/impl/qquickmaterialplaceholdertext.cpp | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/src/quickcontrols/material/impl/qquickmaterialplaceholdertext.cpp b/src/quickcontrols/material/impl/qquickmaterialplaceholdertext.cpp index 7ec3386457..8533802a5a 100644 --- a/src/quickcontrols/material/impl/qquickmaterialplaceholdertext.cpp +++ b/src/quickcontrols/material/impl/qquickmaterialplaceholdertext.cpp @@ -10,6 +10,7 @@ #include <QtQml/qqmlinfo.h> #include <QtQuickTemplates2/private/qquicktheme_p.h> #include <QtQuickTemplates2/private/qquicktextarea_p.h> +#include <QtQuickTemplates2/private/qquicktextfield_p.h> QT_BEGIN_NAMESPACE @@ -106,6 +107,17 @@ void QQuickMaterialPlaceholderText::updateY() setY(shouldFloat() ? floatingTargetY() : normalTargetY()); } +qreal controlTopInset(QQuickItem *textControl) +{ + if (const auto textArea = qobject_cast<QQuickTextArea *>(textControl)) + return textArea->topInset(); + + if (const auto textField = qobject_cast<QQuickTextField *>(textControl)) + return textField->topInset(); + + return 0; +} + qreal QQuickMaterialPlaceholderText::normalTargetY() const { auto *textArea = qobject_cast<QQuickTextArea *>(textControl()); @@ -115,7 +127,10 @@ qreal QQuickMaterialPlaceholderText::normalTargetY() const // (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; + // We should also account for any topInset the user might have specified, + // which is useful to ensure that the text doesn't get clipped. + return ((m_controlImplicitBackgroundHeight - m_largestHeight) / 2.0) + + controlTopInset(textControl()); } // When the placeholder text shouldn't float, it should sit in the middle of the TextField. @@ -131,7 +146,7 @@ qreal QQuickMaterialPlaceholderText::floatingTargetY() const // Outlined text fields have the placeaholder vertically centered // along the outline at the top. - return -m_largestHeight / 2; + return (-m_largestHeight / 2) + controlTopInset(textControl()); } /*! |