Skip to content

Commit

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

Log:
  • Loading branch information
Whale107 committed Aug 6, 2024
1 parent b8497a4 commit e83621a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
56 changes: 45 additions & 11 deletions src/widgets/dsearchedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ DWIDGET_BEGIN_NAMESPACE
DCORE_USE_NAMESPACE
DGUI_USE_NAMESPACE

constexpr int ANI_DURATION = 200;
constexpr int HIDE_CURSOR_MARGIN = -4;

#ifdef ENABLE_AI
class VoiceDevice : public QIODevice
{
Expand Down Expand Up @@ -162,7 +165,7 @@ class VoiceDevice : public QIODevice
\class Dtk::Widget::DSearchEdit
\inmodule dtkwidget
@brief The DSearchEdit class provides input box widget specifically used for searching
@details The DSearchEdit class inherits from the DLineEdit class.
Compared to regular input box widgets, DSearchEdit provides search button instructions,thus users can use it more naturally.
As shown in the figure:
Expand Down Expand Up @@ -246,7 +249,7 @@ bool DSearchEdit::isVoiceInput() const
D_DC(DSearchEdit);
#ifdef ENABLE_AI
return d->voiceInput && d->voiceInput->state() == QAudio::ActiveState;
#endif //
#endif //
#else
return false;
#endif
Expand All @@ -270,7 +273,11 @@ DSearchEditPrivate::DSearchEditPrivate(DSearchEdit *q)
, action(nullptr)
, iconWidget(nullptr)
, label(nullptr)
, animation(new QPropertyAnimation)
{
animation->setPropertyName("pos");
animation->setEasingCurve(QEasingCurve::OutCubic);
animation->setDuration(ANI_DURATION);
}

DSearchEditPrivate::~DSearchEditPrivate()
Expand Down Expand Up @@ -377,15 +384,28 @@ 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 {
action->setVisible(false);
iconWidget->setVisible(true);
lineEdit->setPlaceholderText(QString());
}
if (animation->state() == QPropertyAnimation::Running)
return;

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

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

q->connect(animation, &QPropertyAnimation::finished, q, [q, this, textMargins]() {
q->lineEdit()->setTextMargins(textMargins);
if (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
Expand All @@ -394,6 +414,20 @@ void DSearchEditPrivate::_q_toEditMode(bool focus)
_q_onVoiceActionTrigger(false);
}
#endif

if (!q->lineEdit()->text().isEmpty())
return;

if (focus) {
animation->setDirection(QPropertyAnimation::Direction::Forward);
} else {
action->setVisible(false);
animation->setDirection(QPropertyAnimation::Direction::Backward);
}

iconWidget->setVisible(true);
q->lineEdit()->setTextMargins(marginsInAnimation);
animation->start();
}

void DSearchEditPrivate::_q_onVoiceActionTrigger(bool checked)

Check warning on line 433 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 *animation;

#ifdef ENABLE_AI
QAction *voiceAction = nullptr;
Expand Down

0 comments on commit e83621a

Please sign in to comment.