From f588a7d48b7cef5351d8b0f030a6ec5e76229976 Mon Sep 17 00:00:00 2001 From: Felix Ernst Date: Wed, 13 Mar 2024 15:39:00 +0000 Subject: 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) --- src/selectionmode/bottombar.cpp | 66 ++++++++--------------------------------- src/selectionmode/bottombar.h | 19 ++++-------- src/selectionmode/topbar.cpp | 50 ++----------------------------- src/selectionmode/topbar.h | 27 +++++------------ 4 files changed, 30 insertions(+), 132 deletions(-) (limited to 'src/selectionmode') diff --git a/src/selectionmode/bottombar.cpp b/src/selectionmode/bottombar.cpp index b40fc95db..ee63246ca 100644 --- a/src/selectionmode/bottombar.cpp +++ b/src/selectionmode/bottombar.cpp @@ -12,36 +12,16 @@ #include #include -#include #include #include using namespace SelectionMode; BottomBar::BottomBar(KActionCollection *actionCollection, QWidget *parent) - : QWidget{parent} + : AnimatedHeightWidget{parent} { - // Showing of this widget is normally animated. We hide it for now and make it small. - hide(); - setMaximumHeight(0); - - setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed); - setMinimumWidth(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 that not enough width for the contents is available. (this trick is also used in dolphinsearchbox.cpp.) - m_scrollArea = new QScrollArea(this); - fillParentLayout->addWidget(m_scrollArea); - m_scrollArea->setFrameShape(QFrame::NoFrame); - m_scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - m_scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - m_scrollArea->setWidgetResizable(true); - - m_contentsContainer = new BottomBarContentsContainer(actionCollection, m_scrollArea); - m_scrollArea->setWidget(m_contentsContainer); + m_contentsContainer = new BottomBarContentsContainer(actionCollection, nullptr); + prepareContentsContainer(m_contentsContainer); m_contentsContainer->installEventFilter(this); // Adjusts the height of this bar to the height of the contentsContainer connect(m_contentsContainer, &BottomBarContentsContainer::error, this, &BottomBar::error); connect(m_contentsContainer, &BottomBarContentsContainer::barVisibilityChangeRequested, this, [this](bool visible) { @@ -52,6 +32,7 @@ BottomBar::BottomBar(KActionCollection *actionCollection, QWidget *parent) }); connect(m_contentsContainer, &BottomBarContentsContainer::selectionModeLeavingRequested, this, &BottomBar::selectionModeLeavingRequested); + setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed); BackgroundColorHelper::instance()->controlBackgroundColor(this); } @@ -63,7 +44,6 @@ void BottomBar::setVisible(bool visible, Animated animated) void BottomBar::setVisibleInternal(bool visible, Animated animated) { - Q_ASSERT_X(animated == WithAnimation, "SelectionModeBottomBar::setVisible", "This wasn't implemented."); if (!visible && contents() == PasteContents) { return; // The bar with PasteContents should not be hidden or users might not know how to paste what they just copied. // Set contents to anything else to circumvent this prevention mechanism. @@ -72,32 +52,7 @@ void BottomBar::setVisibleInternal(bool visible, Animated animated) return; // There is nothing on the bar that we want to show. We keep it invisible and only show it when the selection or the contents change. } - setEnabled(visible); - if (m_heightAnimation) { - m_heightAnimation->stop(); // deletes because of QAbstractAnimation::DeleteWhenStopped. - } - 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(sizeHint().height()); - connect(m_heightAnimation, &QAbstractAnimation::finished, this, [this]() { - setMaximumHeight(sizeHint().height()); - }); - } else { - m_heightAnimation->setEndValue(0); - connect(m_heightAnimation, &QAbstractAnimation::finished, this, &QWidget::hide); - } - - m_heightAnimation->start(QAbstractAnimation::DeleteWhenStopped); -} - -QSize BottomBar::sizeHint() const -{ - return QSize{1, m_contentsContainer->sizeHint().height()}; - // 1 as width because this widget should never be the reason the DolphinViewContainer is made wider. + AnimatedHeightWidget::setVisible(visible, animated); } void BottomBar::slotSelectionChanged(const KFileItemList &selection, const QUrl &baseUrl) @@ -139,7 +94,7 @@ bool BottomBar::eventFilter(QObject *watched, QEvent *event) case QEvent::ChildRemoved: QTimer::singleShot(0, this, [this]() { // The necessary height might have changed because of the added/removed child so we change the height manually. - if (isVisibleTo(parentWidget()) && isEnabled() && (!m_heightAnimation || m_heightAnimation->state() != QAbstractAnimation::Running)) { + if (isVisibleTo(parentWidget()) && isEnabled() && !isAnimationRunning()) { setMaximumHeight(sizeHint().height()); } }); @@ -153,12 +108,17 @@ void BottomBar::resizeEvent(QResizeEvent *resizeEvent) { if (resizeEvent->oldSize().width() == resizeEvent->size().width()) { // The width() didn't change so our custom override isn't needed. - return QWidget::resizeEvent(resizeEvent); + return AnimatedHeightWidget::resizeEvent(resizeEvent); } m_contentsContainer->adaptToNewBarWidth(width()); - return QWidget::resizeEvent(resizeEvent); + return AnimatedHeightWidget::resizeEvent(resizeEvent); +} + +int BottomBar::preferredHeight() const +{ + return m_contentsContainer->sizeHint().height(); } #include "moc_bottombar.cpp" diff --git a/src/selectionmode/bottombar.h b/src/selectionmode/bottombar.h index fd6eaebd9..73515b0bd 100644 --- a/src/selectionmode/bottombar.h +++ b/src/selectionmode/bottombar.h @@ -8,18 +8,16 @@ #ifndef BOTTOMBAR_H #define BOTTOMBAR_H +#include "animatedheightwidget.h" #include "global.h" #include #include -#include -#include class KActionCollection; class KFileItemList; class QPushButton; class QResizeEvent; -class QScrollArea; class QUrl; namespace SelectionMode @@ -34,7 +32,7 @@ class BottomBarContentsContainer; * * The visible contents of the bar are managed in BottomBarContentsContainer. This class serves as a wrapper around it. */ -class BottomBar : public QWidget +class BottomBar : public AnimatedHeightWidget { Q_OBJECT @@ -73,6 +71,7 @@ public: * @param visible Whether this bar is supposed to be visible long term * @param animated Whether this should be animated. The animation is skipped if the users' settings are configured that way. * + * @see AnimatedHeightWidget::setVisible() * @see QWidget::setVisible() */ void setVisible(bool visible, Animated animated); @@ -83,9 +82,6 @@ public: void resetContents(Contents contents); Contents contents() const; - /** @returns a width of 1 to make sure that this bar never causes side panels to shrink. */ - QSize sizeHint() const override; - public Q_SLOTS: /** Adapts the contents based on the selection in the related view. */ void slotSelectionChanged(const KFileItemList &selection, const QUrl &baseUrl); @@ -109,8 +105,6 @@ protected: void resizeEvent(QResizeEvent *resizeEvent) override; private: - using QWidget::setVisible; // Makes sure that the setVisible() declaration above doesn't hide the one from QWidget so we can still use it privately. - /** * Identical to SelectionModeBottomBar::setVisible() but doesn't change m_allowedToBeVisible. * @see SelectionModeBottomBar::setVisible() @@ -118,9 +112,10 @@ private: */ void setVisibleInternal(bool visible, Animated animated); + /** @see AnimatedHeightWidget::preferredHeight() */ + int preferredHeight() const override; + private: - /** The only direct child widget of this bar. */ - QScrollArea *m_scrollArea; /** The only direct grandchild of this bar. */ BottomBarContentsContainer *m_contentsContainer; @@ -128,8 +123,6 @@ private: * This is necessary because this bar might have been setVisible(true) but there is no reason to show the bar currently so it was kept hidden. * @see SelectionModeBottomBar::setVisible() */ bool m_allowedToBeVisible = false; - /** @see SelectionModeBottomBar::setVisible() */ - QPointer m_heightAnimation; }; } diff --git a/src/selectionmode/topbar.cpp b/src/selectionmode/topbar.cpp index abe9f74a5..5d77a4c00 100644 --- a/src/selectionmode/topbar.cpp +++ b/src/selectionmode/topbar.cpp @@ -16,18 +16,13 @@ #include #include #include -#include #include using namespace SelectionMode; TopBar::TopBar(QWidget *parent) - : QWidget{parent} + : AnimatedHeightWidget{parent} { - // Showing of this widget is normally animated. We hide it for now and make it small. - hide(); - setMaximumHeight(0); - setToolTip(KToolTipHelper::whatsThisHintOnly()); setWhatsThis(xi18nc("@info:whatsthis", "Selection ModeSelect files or folders to manage or manipulate them." @@ -36,25 +31,10 @@ TopBar::TopBar(QWidget *parent) "Selection rectangles (created by dragging from an empty area) invert the selection status of items within." "The available action buttons at the bottom change depending on the current selection.")); - 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 that not enough width for the contents is available. (this trick is also used in bottombar.cpp.) - auto scrollArea = new QScrollArea(this); - fillParentLayout->addWidget(scrollArea); - scrollArea->setFrameShape(QFrame::NoFrame); - scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - scrollArea->setWidgetResizable(true); - - auto contentsContainer = new QWidget(scrollArea); - scrollArea->setWidget(contentsContainer); + QWidget *contentsContainer = prepareContentsContainer(); BackgroundColorHelper::instance()->controlBackgroundColor(this); - setMinimumWidth(0); - m_fullLabelString = i18nc("@info label above the view explaining the state", "Selection Mode: Click on files or folders to select or deselect them."); m_shortLabelString = i18nc("@info label above the view explaining the state", "Selection Mode"); m_label = new QLabel(contentsContainer); @@ -69,7 +49,6 @@ TopBar::TopBar(QWidget *parent) QHBoxLayout *layout = new QHBoxLayout(contentsContainer); auto contentsMargins = layout->contentsMargins(); m_preferredHeight = contentsMargins.top() + m_label->sizeHint().height() + contentsMargins.bottom(); - scrollArea->setMaximumHeight(m_preferredHeight); m_closeButton->setFixedHeight(m_preferredHeight); layout->setContentsMargins(0, 0, 0, 0); @@ -79,33 +58,10 @@ TopBar::TopBar(QWidget *parent) layout->addWidget(m_closeButton); } -void TopBar::setVisible(bool visible, Animated animated) -{ - Q_ASSERT_X(animated == WithAnimation, "SelectionModeTopBar::setVisible", "This wasn't implemented."); - - if (m_heightAnimation) { - m_heightAnimation->stop(); // deletes because of QAbstractAnimation::DeleteWhenStopped. - } - 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(m_preferredHeight); - } else { - m_heightAnimation->setEndValue(0); - connect(m_heightAnimation, &QAbstractAnimation::finished, this, &QWidget::hide); - } - - m_heightAnimation->start(QAbstractAnimation::DeleteWhenStopped); -} - void TopBar::resizeEvent(QResizeEvent *resizeEvent) { updateLabelString(); - return QWidget::resizeEvent(resizeEvent); + return AnimatedHeightWidget::resizeEvent(resizeEvent); } void TopBar::updateLabelString() diff --git a/src/selectionmode/topbar.h b/src/selectionmode/topbar.h index 028fc4985..1f9cfdb18 100644 --- a/src/selectionmode/topbar.h +++ b/src/selectionmode/topbar.h @@ -8,17 +8,11 @@ #ifndef SELECTIONMODETOPBAR_H #define SELECTIONMODETOPBAR_H -#include "global.h" +#include "animatedheightwidget.h" -#include -#include -#include - -class QHideEvent; class QLabel; class QPushButton; class QResizeEvent; -class QShowEvent; namespace SelectionMode { @@ -26,20 +20,13 @@ namespace SelectionMode /** * @brief A bar appearing at the top of the view when in selection mode to make users aware of the selection mode state of the application. */ -class TopBar : public QWidget +class TopBar : public AnimatedHeightWidget { Q_OBJECT public: TopBar(QWidget *parent); - /** - * Plays a show or hide animation while changing visibility. - * Therefore, if this method is used to hide this widget, the actual hiding will be postponed until the animation finished. - * @see QWidget::setVisible() - */ - void setVisible(bool visible, Animated animated); - Q_SIGNALS: void selectionModeLeavingRequested(); @@ -48,11 +35,15 @@ protected: void resizeEvent(QResizeEvent *resizeEvent) override; private: - using QWidget::setVisible; // Makes sure that the setVisible() declaration above doesn't hide the one from QWidget so we can still use it privately. - /** Decides whether the m_fullLabelString or m_shortLabelString should be used based on available width. */ void updateLabelString(); + /** @see AnimatedHeightWidget::preferredHeight() */ + inline int preferredHeight() const override + { + return m_preferredHeight; + }; + private: QLabel *m_label; QPushButton *m_closeButton; @@ -63,8 +54,6 @@ private: QString m_shortLabelString; int m_preferredHeight; - - QPointer m_heightAnimation; }; } -- cgit v1.3