diff options
| author | Felix Ernst <[email protected]> | 2024-03-13 15:39:00 +0000 |
|---|---|---|
| committer | Felix Ernst <[email protected]> | 2024-03-13 15:39:00 +0000 |
| commit | f588a7d48b7cef5351d8b0f030a6ec5e76229976 (patch) | |
| tree | 086c0743d3dd63e5ca11cd84e55b527f58cbe5ea /src/animatedheightwidget.cpp | |
| parent | 73ea5fd8450cdd097a47ede9d740cbbcf9f9df10 (diff) | |
Animate most of the bars
When a bar is toggled visible this usually happens because the
user might want to use its functionality now. However, if bars
appear without animation or at a location the user is not
currently looking at, they might not notice that they have appeared
at all.
An animation makes it more likely that the user notices the change
and can then use the newly made visible component.
Another reason for animations for showing or hiding of components
is that it can be disorienting for users when panels or bars
suddenly appear or disappear without animation. There is no visible
movement then, so the user might not know what happened if they
didn't concentrate or blink at that moment. The newly appearing or
disappearing component might also displace other components which
can make it difficult to find what one was just looking at.
These bars animate now after this change:
- Search panel
- Filter bar
- Status bar
This is implemented by extracting the animation code from
SelectionMode::TopBar into a new abstract base class
AnimatedHeightWidget. This class is now also used in
SelectionMode::BottomBar and the animating code there was removed.
These bars are left in Dolphin that stay without animation:
- Menu bar (Would probably need to be implemented in KXmlGui)
- Tool bar (Would probably need to be implemented in KXmlGui)
- Tab bar (Needs a different appraoch because it already inherits
QTabBar and therefore can not inherit AnimatedHeightWidget)
Diffstat (limited to 'src/animatedheightwidget.cpp')
| -rw-r--r-- | src/animatedheightwidget.cpp | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/animatedheightwidget.cpp b/src/animatedheightwidget.cpp new file mode 100644 index 000000000..cee1a4922 --- /dev/null +++ b/src/animatedheightwidget.cpp @@ -0,0 +1,90 @@ +/* + This file is part of the KDE project + SPDX-FileCopyrightText: 2024 Felix Ernst <[email protected]> + + SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL +*/ + +#include "animatedheightwidget.h" + +#include <QGridLayout> +#include <QPropertyAnimation> +#include <QScrollArea> +#include <QStyle> + +AnimatedHeightWidget::AnimatedHeightWidget(QWidget *parent) + : QWidget{parent} +{ + // Showing of this widget is normally animated. We hide it for now and make it small. + hide(); + setMaximumHeight(0); + + auto fillParentLayout = new QGridLayout(this); + fillParentLayout->setContentsMargins(0, 0, 0, 0); + + // Put the contents into a QScrollArea. This prevents increasing the view width + // in case there is not enough available width for the contents. + m_contentsContainerParent = new QScrollArea(this); + fillParentLayout->addWidget(m_contentsContainerParent); + m_contentsContainerParent->setFrameShape(QFrame::NoFrame); + m_contentsContainerParent->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + m_contentsContainerParent->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + m_contentsContainerParent->setWidgetResizable(true); + + setMinimumWidth(0); +} + +QSize AnimatedHeightWidget::sizeHint() const +{ + return QSize{1, preferredHeight()}; + // 1 as width because this widget should never be the reason the DolphinViewContainer is made wider. +} + +void AnimatedHeightWidget::setVisible(bool visible, Animated animated) +{ + setEnabled(visible); + if (m_heightAnimation) { + m_heightAnimation->stop(); // deletes because of QAbstractAnimation::DeleteWhenStopped. + } + + if (animated == WithAnimation + && (style()->styleHint(QStyle::SH_Widget_Animation_Duration, nullptr, this) < 1 || GlobalConfig::animationDurationFactor() <= 0.0)) { + animated = WithoutAnimation; + } + + if (animated == WithoutAnimation) { + setMaximumHeight(visible ? preferredHeight() : 0); + setVisible(visible); + return; + } + + m_heightAnimation = new QPropertyAnimation(this, "maximumHeight"); + m_heightAnimation->setDuration(2 * style()->styleHint(QStyle::SH_Widget_Animation_Duration, nullptr, this) * GlobalConfig::animationDurationFactor()); + + m_heightAnimation->setStartValue(height()); + m_heightAnimation->setEasingCurve(QEasingCurve::OutCubic); + if (visible) { + show(); + m_heightAnimation->setEndValue(preferredHeight()); + } else { + m_heightAnimation->setEndValue(0); + connect(m_heightAnimation, &QAbstractAnimation::finished, this, &QWidget::hide); + } + + m_heightAnimation->start(QAbstractAnimation::DeleteWhenStopped); +} + +QWidget *AnimatedHeightWidget::prepareContentsContainer(QWidget *contentsContainer) +{ + Q_ASSERT_X(!m_contentsContainerParent->widget(), + "AnimatedHeightWidget::prepareContentsContainer", + "Another contentsContainer has already been prepared. There can only be one."); + contentsContainer->setParent(m_contentsContainerParent); + m_contentsContainerParent->setWidget(contentsContainer); + return contentsContainer; +} + +bool AnimatedHeightWidget::isAnimationRunning() const +{ + return m_heightAnimation && m_heightAnimation->state() == QAbstractAnimation::Running; +} |
