Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: 新增搜索框图标移动动画 #590

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/widgets/dindeterminateprogressbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ DIndeterminateProgressbar::DIndeterminateProgressbar(QWidget *parent)

d->m_leftToRight ? step += 2 : step -= 2;
d->m_sliderWidget->move(step, 0);
update();
});
d->m_timer->start();
}
Expand Down
55 changes: 46 additions & 9 deletions src/widgets/dsearchedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
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 @@ -270,7 +273,11 @@
, 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,31 @@
{
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);

if (!animation->parent())
animation->setParent(iconWidget);

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,9 +417,23 @@
_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 436 in src/widgets/dsearchedit.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function '_q_onVoiceActionTrigger' is never used.
{
#if (!defined DTK_NO_MULTIMEDIA) && (defined ENABLE_AI)
if (checked) {
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
Loading