aboutsummaryrefslogtreecommitdiffstats
path: root/src/quickcontrols/material/impl/qquickmaterialplaceholdertext.cpp
blob: 2447193d6aa91d4d44bff4a3e0ca93863c35f0b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qquickmaterialplaceholdertext_p.h"

#include <QtCore/qpropertyanimation.h>
#include <QtCore/qparallelanimationgroup.h>
#include <QtGui/qpainter.h>
#include <QtGui/qpainterpath.h>
#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

static const qreal floatingScale = 0.8;
Q_GLOBAL_STATIC(QEasingCurve, animationEasingCurve, QEasingCurve::OutSine);

/*
    This class makes it easier to animate the various placeholder text changes
    for each type of text container (filled, outlined).

    By doing animations in C++, we avoid having a bunch of states, transitions,
    and animations (which are all QObjects) declared in QML, even if that text
    control never gets focus and hence never needs them.
*/

QQuickMaterialPlaceholderText::QQuickMaterialPlaceholderText(QQuickItem *parent)
    : QQuickPlaceholderText(parent)
{
    connect(this, &QQuickMaterialPlaceholderText::effectiveHorizontalAlignmentChanged,
            this, &QQuickMaterialPlaceholderText::adjustTransformOrigin);
}

bool QQuickMaterialPlaceholderText::isFilled() const
{
    return m_filled;
}

void QQuickMaterialPlaceholderText::setFilled(bool filled)
{
    if (filled == m_filled)
        return;

    m_filled = filled;
    update();
    void filledChanged();
}

bool QQuickMaterialPlaceholderText::controlHasActiveFocus() const
{
    return m_controlHasActiveFocus;
}

void QQuickMaterialPlaceholderText::setControlHasActiveFocus(bool controlHasActiveFocus)
{
    if (m_controlHasActiveFocus == controlHasActiveFocus)
        return;

    m_controlHasActiveFocus = controlHasActiveFocus;
    if (m_controlHasActiveFocus)
        controlGotActiveFocus();
    else
        controlLostActiveFocus();
    emit controlHasActiveFocusChanged();
}

bool QQuickMaterialPlaceholderText::controlHasText() const
{
    return m_controlHasText;
}

void QQuickMaterialPlaceholderText::setControlHasText(bool controlHasText)
{
    if (m_controlHasText == controlHasText)
        return;

    m_controlHasText = controlHasText;
    maybeSetFocusAnimationProgress();
    emit controlHasTextChanged();
}

/*
    Placeholder text of outlined text fields should float when:
    - There is placeholder text, and
      - The control has active focus, or
      - The control has text
*/
bool QQuickMaterialPlaceholderText::shouldFloat() const
{
    const bool controlHasActiveFocusOrText = m_controlHasActiveFocus || m_controlHasText;
    return m_filled
        ? controlHasActiveFocusOrText
        : !text().isEmpty() && controlHasActiveFocusOrText;
}

bool QQuickMaterialPlaceholderText::shouldAnimate() const
{
    return m_filled
        ? !m_controlHasText
        : !m_controlHasText && !text().isEmpty();
}

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());
    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.
        // 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.
    return (m_controlHeight - height()) / 2.0;
}

qreal QQuickMaterialPlaceholderText::floatingTargetY() const
{
    // For filled text fields, the placeholder text sits just above
    // the text when floating.
    if (m_filled)
        return m_verticalPadding;

    // Outlined text fields have the placeaholder vertically centered
    // along the outline at the top.
    return (-m_largestHeight / 2.0) + controlTopInset(textControl());
}

/*!
    \internal

    The height of the text at its largest size that we set.
*/
int QQuickMaterialPlaceholderText::largestHeight() const
{
    return m_largestHeight;
}

qreal QQuickMaterialPlaceholderText::controlImplicitBackgroundHeight() const
{
    return m_controlImplicitBackgroundHeight;
}

void QQuickMaterialPlaceholderText::setControlImplicitBackgroundHeight(qreal controlImplicitBackgroundHeight)
{
    if (qFuzzyCompare(m_controlImplicitBackgroundHeight, controlImplicitBackgroundHeight))
        return;

    m_controlImplicitBackgroundHeight = controlImplicitBackgroundHeight;
    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 control, 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;
}

void QQuickMaterialPlaceholderText::setVerticalPadding(qreal verticalPadding)
{
    if (qFuzzyCompare(m_verticalPadding, verticalPadding))
        return;

    m_verticalPadding = verticalPadding;
    emit verticalPaddingChanged();
}

void QQuickMaterialPlaceholderText::adjustTransformOrigin()
{
    switch (effectiveHAlign()) {
    case QQuickText::AlignLeft:
        Q_FALLTHROUGH();
    case QQuickText::AlignJustify:
        setTransformOrigin(QQuickItem::Left);
        break;
    case QQuickText::AlignRight:
        setTransformOrigin(QQuickItem::Right);
        break;
    case QQuickText::AlignHCenter:
        setTransformOrigin(QQuickItem::Center);
        break;
    }
}

void QQuickMaterialPlaceholderText::controlGotActiveFocus()
{
    if (m_focusOutAnimation) {
        // Focus changes can happen before the animations finish.
        // In that case, stop the animation, which will eventually delete it.
        // Until it's deleted, we clear the pointer so that our asserts don't fail
        // for the wrong reason.
        m_focusOutAnimation->stop();
        m_focusOutAnimation.clear();
    }

    Q_ASSERT(!m_focusInAnimation);
    if (shouldAnimate()) {
        m_focusInAnimation = new QParallelAnimationGroup(this);

        QPropertyAnimation *yAnimation = new QPropertyAnimation(this, "y", this);
        yAnimation->setDuration(300);
        yAnimation->setStartValue(y());
        yAnimation->setEndValue(floatingTargetY());
        yAnimation->setEasingCurve(*animationEasingCurve);
        m_focusInAnimation->addAnimation(yAnimation);

        auto *scaleAnimation = new QPropertyAnimation(this, "scale", this);
        scaleAnimation->setDuration(300);
        scaleAnimation->setStartValue(1);
        scaleAnimation->setEndValue(floatingScale);
        yAnimation->setEasingCurve(*animationEasingCurve);
        m_focusInAnimation->addAnimation(scaleAnimation);

        m_focusInAnimation->start(QAbstractAnimation::DeleteWhenStopped);
    } else {
        updateY();
    }
}

void QQuickMaterialPlaceholderText::controlLostActiveFocus()
{
    if (m_focusInAnimation) {
        m_focusInAnimation->stop();
        m_focusInAnimation.clear();
    }

    Q_ASSERT(!m_focusOutAnimation);
    if (shouldAnimate()) {
        m_focusOutAnimation = new QParallelAnimationGroup(this);

        auto *yAnimation = new QPropertyAnimation(this, "y", this);
        yAnimation->setDuration(300);
        yAnimation->setStartValue(y());
        yAnimation->setEndValue(normalTargetY());
        yAnimation->setEasingCurve(*animationEasingCurve);
        m_focusOutAnimation->addAnimation(yAnimation);

        auto *scaleAnimation = new QPropertyAnimation(this, "scale", this);
        scaleAnimation->setDuration(300);
        scaleAnimation->setStartValue(floatingScale);
        scaleAnimation->setEndValue(1);
        yAnimation->setEasingCurve(*animationEasingCurve);
        m_focusOutAnimation->addAnimation(scaleAnimation);

        m_focusOutAnimation->start(QAbstractAnimation::DeleteWhenStopped);
    } else {
        updateY();
    }
}

void QQuickMaterialPlaceholderText::maybeSetFocusAnimationProgress()
{
    updateY();
    setScale(shouldFloat() ? floatingScale : 1.0);
}

void QQuickMaterialPlaceholderText::componentComplete()
{
    QQuickPlaceholderText::componentComplete();

    adjustTransformOrigin();

    m_largestHeight = implicitHeight();
    if (m_largestHeight > 0) {
        emit largestHeightChanged();
    } else {
        qmlWarning(this) << "Expected implicitHeight of placeholder text" << text()
            << "to be greater than 0 by component completion!";
    }

    maybeSetFocusAnimationProgress();
}

QT_END_NAMESPACE