Skip to content

Commit

Permalink
chore: 新增搜索框图标移动动画
Browse files Browse the repository at this point in the history
新增搜索框图标移动动画

Log:
  • Loading branch information
Whale107 committed Aug 2, 2024
1 parent b8497a4 commit 22b6241
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
44 changes: 37 additions & 7 deletions src/widgets/dsearchedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,11 @@ DSearchEditPrivate::DSearchEditPrivate(DSearchEdit *q)
, action(nullptr)
, iconWidget(nullptr)
, label(nullptr)
, m_animation(new QPropertyAnimation)
{
m_animation->setPropertyName("pos");
m_animation->setEasingCurve(QEasingCurve::OutCubic);
m_animation->setDuration(200);
}

DSearchEditPrivate::~DSearchEditPrivate()
Expand Down Expand Up @@ -377,23 +381,49 @@ void DSearchEditPrivate::_q_toEditMode(bool focus)
{
D_Q(DSearchEdit);

if (focus || !q->lineEdit()->text().isEmpty()) {
action->setVisible(true);
iconWidget->setVisible(false);
lineEdit->setPlaceholderText(placeholderText);
} else {
if (m_animation->state() == QPropertyAnimation::Running)
return;

auto textMargins = q->lineEdit()->textMargins();
QMargins marginsInAnimation(-4, 0, 0, 0);

m_animation->setTargetObject(iconWidget);
m_animation->setStartValue(QPoint(q->lineEdit()->geometry().center().x() - iconWidget->width() / 2, iconWidget->pos().y()));
m_animation->setEndValue(QPoint(0, iconWidget->pos().y()));

if (focus) {
m_animation->setDirection(QPropertyAnimation::Direction::Forward);
} else if (q->lineEdit()->text().isEmpty()) {
action->setVisible(false);
iconWidget->setVisible(true);
lineEdit->setPlaceholderText(QString());
m_animation->setDirection(QPropertyAnimation::Direction::Backward);
}

q->connect(m_animation, &QPropertyAnimation::finished, q, [q, this, textMargins]() {
q->lineEdit()->setTextMargins(textMargins);
if (m_animation->direction() == QPropertyAnimation::Direction::Forward) {
action->setVisible(true);
iconWidget->setVisible(false);
lineEdit->setPlaceholderText(placeholderText);
} else {
iconWidget->setVisible(true);
lineEdit->setPlaceholderText(QString());
iconWidget->move(QPoint(q->lineEdit()->geometry().center().x() - iconWidget->width() / 2, iconWidget->pos().y()));
}
});

#ifdef ENABLE_AI
//Focus disappears, clear voice check
if (voiceAction) {
voiceAction->setChecked(false);
_q_onVoiceActionTrigger(false);
}
#endif
if (!q->lineEdit()->text().isEmpty())
return;

iconWidget->setVisible(q->lineEdit()->text().isEmpty());
q->lineEdit()->setTextMargins(marginsInAnimation);
m_animation->start();
}

void DSearchEditPrivate::_q_onVoiceActionTrigger(bool checked)

Check warning on line 429 in src/widgets/dsearchedit.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function '_q_onVoiceActionTrigger' is never used.
Expand Down
2 changes: 2 additions & 0 deletions src/widgets/private/dsearchedit_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <DSearchEdit>

#include <QLabel>
#include <QPropertyAnimation>

QT_BEGIN_NAMESPACE
class QAudioInput;
Expand Down Expand Up @@ -37,6 +38,7 @@ class DSearchEditPrivate : DLineEditPrivate

QWidget *iconWidget;
QLabel *label;
QPropertyAnimation *m_animation;

#ifdef ENABLE_AI
QAction *voiceAction = nullptr;
Expand Down

0 comments on commit 22b6241

Please sign in to comment.