]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Avoid flickering of space info on startup
authorFelix Ernst <felixernst@zohomail.eu>
Sun, 10 Nov 2024 15:47:19 +0000 (16:47 +0100)
committerFelix Ernst <felixernst@kde.org>
Tue, 12 Nov 2024 10:54:25 +0000 (10:54 +0000)
Prior to this commit, when Dolphin was opening in a directory for
which the free space information cannot be retrieved, the free
space info in the status bar would still briefly be visible before
hiding for good.

This commit avoids this flickering by keeping the space info hidden
until space info has been successfully retrieved. There is no use
showing an empty/wrong space info before that anyway.

I assume the error in the previous code was that it assumed that
one could prevent a widget from being shown by overriding
QWidget::showEvent(). This does not work because this method is
only called to notify QWidgets of their state change.

This commit was primarily written because the brief showing of an
empty space info was messing with automatic tests.

src/statusbar/dolphinstatusbar.cpp
src/statusbar/statusbarspaceinfo.cpp
src/statusbar/statusbarspaceinfo.h

index 99affde6fa2821d17d161ad042389a1c572327c0..6da4b4ddf7c38f23d8bcb4a48b68d5f11759c91f 100644 (file)
@@ -296,7 +296,7 @@ void DolphinStatusBar::contextMenuEvent(QContextMenuEvent *event)
     } else if (action == showSpaceInfoAction) {
         const bool visible = showSpaceInfoAction->isChecked();
         GeneralSettings::setShowSpaceInfo(visible);
-        m_spaceInfo->setVisible(visible);
+        m_spaceInfo->setShown(visible);
     }
     updateContentsMargins();
 }
@@ -350,7 +350,6 @@ void DolphinStatusBar::setExtensionsVisible(bool visible)
     }
 
     m_spaceInfo->setShown(showSpaceInfo);
-    m_spaceInfo->setVisible(showSpaceInfo);
     m_zoomSlider->setVisible(showZoomSlider);
     m_zoomLabel->setVisible(showZoomSlider);
     updateContentsMargins();
index a482bde9cd6785dbfbd166200fb27e728b40a6c2..c25d028d68897a95fa79d5981273ee327aeadf82 100644 (file)
@@ -32,7 +32,11 @@ StatusBarSpaceInfo::StatusBarSpaceInfo(QWidget *parent)
     : QWidget(parent)
     , m_observer(nullptr)
     , m_installFilelightWidgetAction{nullptr}
+    , m_hasSpaceInfo{false}
+    , m_shown{false}
 {
+    hide(); // Only become visible when we have space info to show. @see StatusBarSpaceInfo::setShown().
+
     m_capacityBar = new KCapacityBar(KCapacityBar::DrawTextInline, this);
     m_textInfoButton = new QToolButton(this);
     m_textInfoButton->setAutoRaise(true);
@@ -57,7 +61,17 @@ void StatusBarSpaceInfo::setShown(bool shown)
     m_shown = shown;
     if (!m_shown) {
         hide();
-        m_ready = false;
+        return;
+    }
+
+    // We only show() this widget in slotValueChanged() when it m_hasSpaceInfo.
+    if (m_observer.isNull()) {
+        m_observer.reset(new SpaceInfoObserver(m_url, this));
+        connect(m_observer.data(), &SpaceInfoObserver::valuesChanged, this, &StatusBarSpaceInfo::slotValuesChanged);
+    }
+
+    if (m_hasSpaceInfo) {
+        slotValuesChanged();
     }
 }
 
@@ -65,7 +79,7 @@ void StatusBarSpaceInfo::setUrl(const QUrl &url)
 {
     if (m_url != url) {
         m_url = url;
-        m_ready = false;
+        m_hasSpaceInfo = false;
         if (m_observer) {
             m_observer.reset(new SpaceInfoObserver(m_url, this));
             connect(m_observer.data(), &SpaceInfoObserver::valuesChanged, this, &StatusBarSpaceInfo::slotValuesChanged);
@@ -87,23 +101,18 @@ void StatusBarSpaceInfo::update()
 
 void StatusBarSpaceInfo::showEvent(QShowEvent *event)
 {
-    if (m_shown) {
-        if (m_ready) {
-            QWidget::showEvent(event);
-        }
-
-        if (m_observer.isNull()) {
-            m_observer.reset(new SpaceInfoObserver(m_url, this));
-            connect(m_observer.data(), &SpaceInfoObserver::valuesChanged, this, &StatusBarSpaceInfo::slotValuesChanged);
-        }
+    if (m_shown && m_observer.isNull()) {
+        m_observer.reset(new SpaceInfoObserver(m_url, this));
+        connect(m_observer.data(), &SpaceInfoObserver::valuesChanged, this, &StatusBarSpaceInfo::slotValuesChanged);
     }
+    QWidget::showEvent(event);
 }
 
 void StatusBarSpaceInfo::hideEvent(QHideEvent *event)
 {
-    if (m_ready) {
+    if (m_hasSpaceInfo) {
         m_observer.reset();
-        m_ready = false;
+        m_hasSpaceInfo = false;
     }
     QWidget::hideEvent(event);
 }
@@ -225,7 +234,7 @@ void StatusBarSpaceInfo::slotValuesChanged()
         return;
     }
 
-    m_ready = true;
+    m_hasSpaceInfo = true;
 
     const quint64 available = m_observer->available();
     const quint64 used = size - available;
index 237192c9cdb06dca6bb3ad06d74047025bafd42e..7a1012cf01310e9fb5fc0b38ad4e1c89a4346687 100644 (file)
@@ -35,9 +35,11 @@ public:
     ~StatusBarSpaceInfo() override;
 
     /**
-     * Use this to set the widget visibility as it can hide itself
+     * Works similar to QWidget::setVisible() except that this will postpone showing the widget until space info has been retrieved. Hiding happens instantly.
+     *
+     * @param shown Whether this widget is supposed to be visible long-term
      */
-    void setShown(bool);
+    void setShown(bool shown);
     void setUrl(const QUrl &url);
     QUrl url() const;
 
@@ -80,6 +82,11 @@ private:
      */
     void initialiseInstallFilelightWidgetAction();
 
+    // The following three methods are only for private use.
+    using QWidget::hide; // Use StatusBarSpaceInfo::setShown() instead.
+    using QWidget::setVisible; // Use StatusBarSpaceInfo::setShown() instead.
+    using QWidget::show; // Use StatusBarSpaceInfo::setShown() instead.
+
 private:
     QScopedPointer<SpaceInfoObserver> m_observer;
     KCapacityBar *m_capacityBar;
@@ -88,7 +95,9 @@ private:
     /** An action containing a UI to install Filelight. */
     QWidgetAction *m_installFilelightWidgetAction;
     QUrl m_url;
-    bool m_ready;
+    /** Whether m_observer has already retrieved space information for the current url. */
+    bool m_hasSpaceInfo;
+    /** Whether this widget is supposed to be visible long-term. @see StatusBarSpaceInfo::setShown(). */
     bool m_shown;
 };