[nautilus/wip/antoniof/new-list-view-continuation: 25/27] list-view-item-ui: Show spinner while loading subfolders




commit f79a7a9b658a97e4aad24b625bcce2680f17f925
Author: António Fernandes <antoniof gnome org>
Date:   Thu Apr 7 00:17:45 2022 +0100

    list-view-item-ui: Show spinner while loading subfolders
    
    The spinner is shown only after 1 second without the directory having
    finished loading, to avoid visual noise when directories loads quickly.

 src/nautilus-list-view.c               | 22 ++++++++++++++
 src/nautilus-name-cell.c               | 53 ++++++++++++++++++++++++++++++++++
 src/nautilus-view-item.c               | 29 +++++++++++++++++++
 src/nautilus-view-item.h               |  2 ++
 src/resources/ui/nautilus-name-cell.ui |  8 +++++
 5 files changed, 114 insertions(+)
---
diff --git a/src/nautilus-list-view.c b/src/nautilus-list-view.c
index b19da3fd4..dc8524b39 100644
--- a/src/nautilus-list-view.c
+++ b/src/nautilus-list-view.c
@@ -921,6 +921,18 @@ unload_file_timeout (gpointer data)
     return G_SOURCE_REMOVE;
 }
 
+static void
+on_subdirectory_done_loading (NautilusDirectory *directory,
+                              GtkTreeListRow    *row)
+{
+    NautilusViewItem *item;
+
+    g_signal_handlers_disconnect_by_func (directory, on_subdirectory_done_loading, row);
+
+    item = NAUTILUS_VIEW_ITEM (gtk_tree_list_row_get_item (row));
+    nautilus_view_item_set_loading (item, FALSE);
+}
+
 static void
 on_row_expanded_changed (GObject    *gobject,
                          GParamSpec *pspec,
@@ -947,9 +959,19 @@ on_row_expanded_changed (GObject    *gobject,
         {
             nautilus_files_view_add_subdirectory (NAUTILUS_FILES_VIEW (self), directory);
         }
+        if (!nautilus_directory_are_all_files_seen (directory))
+        {
+            nautilus_view_item_set_loading (item, TRUE);
+            g_signal_connect_object (directory,
+                                     "done-loading",
+                                     G_CALLBACK (on_subdirectory_done_loading),
+                                     row,
+                                     0);
+        }
     }
     else
     {
+        nautilus_view_item_set_loading (item, FALSE);
         g_timeout_add_seconds (COLLAPSE_TO_UNLOAD_DELAY,
                                unload_file_timeout,
                                unload_delay_data_new (self, item, directory));
diff --git a/src/nautilus-name-cell.c b/src/nautilus-name-cell.c
index 7a2866de5..19bb36a31 100644
--- a/src/nautilus-name-cell.c
+++ b/src/nautilus-name-cell.c
@@ -7,6 +7,8 @@
 #include "nautilus-name-cell.h"
 #include "nautilus-file-utilities.h"
 
+#define LOADING_TIMEOUT_SECONDS 1
+
 struct _NautilusNameCell
 {
     NautilusViewCell parent_instance;
@@ -18,12 +20,14 @@ struct _NautilusNameCell
 
     GtkWidget *expander;
     GtkWidget *fixed_height_box;
+    GtkWidget *spinner;
     GtkWidget *icon;
     GtkWidget *label;
     GtkWidget *snippet;
     GtkWidget *path;
 
     gboolean show_snippet;
+    guint loading_timeout_id;
 };
 
 G_DEFINE_TYPE (NautilusNameCell, nautilus_name_cell, NAUTILUS_TYPE_VIEW_CELL)
@@ -219,6 +223,52 @@ on_item_is_cut_changed (NautilusNameCell *self)
     }
 }
 
+static gboolean
+on_loading_timeout (gpointer user_data)
+{
+    NautilusNameCell *self = NAUTILUS_NAME_CELL (user_data);
+    gboolean is_loading;
+
+    g_object_get (nautilus_view_cell_get_item (NAUTILUS_VIEW_CELL (self)),
+                  "is-loading", &is_loading,
+                  NULL);
+    if (is_loading)
+    {
+        gtk_widget_show (self->spinner);
+        gtk_spinner_start (GTK_SPINNER (self->spinner));
+    }
+
+    return G_SOURCE_REMOVE;
+}
+
+static void
+on_item_is_loading_changed (GObject    *object,
+                            GParamSpec *pspec,
+                            gpointer    user_data)
+{
+    NautilusNameCell *self = NAUTILUS_NAME_CELL (user_data);
+    gboolean is_loading;
+
+    if (object != G_OBJECT (nautilus_view_cell_get_item (NAUTILUS_VIEW_CELL (self))))
+    {
+        return;
+    }
+
+    g_clear_handle_id (&self->loading_timeout_id, g_source_remove);
+    g_object_get (object, "is-loading", &is_loading, NULL);
+    if (is_loading)
+    {
+        self->loading_timeout_id = g_timeout_add_seconds (LOADING_TIMEOUT_SECONDS,
+                                                          G_SOURCE_FUNC (on_loading_timeout),
+                                                          self);
+    }
+    else
+    {
+        gtk_widget_hide (self->spinner);
+        gtk_spinner_stop (GTK_SPINNER (self->spinner));
+    }
+}
+
 static void
 nautilus_name_cell_init (NautilusNameCell *self)
 {
@@ -230,6 +280,8 @@ nautilus_name_cell_init (NautilusNameCell *self)
                                     (GCallback) on_item_size_changed, self);
     g_signal_group_connect_swapped (self->item_signal_group, "notify::is-cut",
                                     (GCallback) on_item_is_cut_changed, self);
+    g_signal_group_connect_swapped (self->item_signal_group, "notify::is-loading",
+                                    (GCallback) on_item_is_loading_changed, self);
     g_signal_group_connect_swapped (self->item_signal_group, "file-changed",
                                     (GCallback) on_file_changed, self);
     g_signal_connect_object (self->item_signal_group, "bind",
@@ -263,6 +315,7 @@ nautilus_name_cell_class_init (NautilusNameCellClass *klass)
 
     gtk_widget_class_bind_template_child (widget_class, NautilusNameCell, expander);
     gtk_widget_class_bind_template_child (widget_class, NautilusNameCell, fixed_height_box);
+    gtk_widget_class_bind_template_child (widget_class, NautilusNameCell, spinner);
     gtk_widget_class_bind_template_child (widget_class, NautilusNameCell, icon);
     gtk_widget_class_bind_template_child (widget_class, NautilusNameCell, label);
     gtk_widget_class_bind_template_child (widget_class, NautilusNameCell, snippet);
diff --git a/src/nautilus-view-item.c b/src/nautilus-view-item.c
index 68a59f769..168528b51 100644
--- a/src/nautilus-view-item.c
+++ b/src/nautilus-view-item.c
@@ -11,6 +11,7 @@ struct _NautilusViewItem
     GObject parent_instance;
     guint icon_size;
     gboolean is_cut;
+    gboolean is_loading;
     NautilusFile *file;
     GtkWidget *item_ui;
 };
@@ -23,6 +24,7 @@ enum
     PROP_FILE,
     PROP_ICON_SIZE,
     PROP_IS_CUT,
+    PROP_IS_LOADING,
     PROP_ITEM_UI,
     N_PROPS
 };
@@ -83,6 +85,12 @@ nautilus_view_item_get_property (GObject    *object,
         }
         break;
 
+        case PROP_IS_LOADING:
+        {
+            g_value_set_boolean (value, self->is_loading);
+        }
+        break;
+
         case PROP_ITEM_UI:
         {
             g_value_set_object (value, self->item_ui);
@@ -124,6 +132,12 @@ nautilus_view_item_set_property (GObject      *object,
         }
         break;
 
+        case PROP_IS_LOADING:
+        {
+            self->is_loading = g_value_get_boolean (value);
+        }
+        break;
+
         case PROP_ITEM_UI:
         {
             g_set_object (&self->item_ui, g_value_get_object (value));
@@ -167,6 +181,12 @@ nautilus_view_item_class_init (NautilusViewItemClass *klass)
                                                            "", "",
                                                            FALSE,
                                                            G_PARAM_READWRITE));
+    g_object_class_install_property (object_class,
+                                     PROP_IS_LOADING,
+                                     g_param_spec_boolean ("is-loading",
+                                                           "", "",
+                                                           FALSE,
+                                                           G_PARAM_READWRITE));
     g_object_class_install_property (object_class,
                                      PROP_FILE,
                                      g_param_spec_object ("file",
@@ -228,6 +248,15 @@ nautilus_view_item_set_cut (NautilusViewItem *self,
     g_object_set (self, "is-cut", is_cut, NULL);
 }
 
+void
+nautilus_view_item_set_loading (NautilusViewItem *self,
+                                gboolean          loading)
+{
+    g_return_if_fail (NAUTILUS_IS_VIEW_ITEM (self));
+
+    g_object_set (self, "is-loading", loading, NULL);
+}
+
 NautilusFile *
 nautilus_view_item_get_file (NautilusViewItem *self)
 {
diff --git a/src/nautilus-view-item.h b/src/nautilus-view-item.h
index 1deecabf0..91096b293 100644
--- a/src/nautilus-view-item.h
+++ b/src/nautilus-view-item.h
@@ -26,6 +26,8 @@ void               nautilus_view_item_set_icon_size (NautilusViewItem *self,
 guint              nautilus_view_item_get_icon_size (NautilusViewItem *self);
 void               nautilus_view_item_set_cut       (NautilusViewItem *self,
                                                      gboolean          is_cut);
+void               nautilus_view_item_set_loading   (NautilusViewItem *self,
+                                                     gboolean          is_loading);
 
 NautilusFile *     nautilus_view_item_get_file      (NautilusViewItem *self);
 
diff --git a/src/resources/ui/nautilus-name-cell.ui b/src/resources/ui/nautilus-name-cell.ui
index 4d2b736f0..37536716d 100644
--- a/src/resources/ui/nautilus-name-cell.ui
+++ b/src/resources/ui/nautilus-name-cell.ui
@@ -11,6 +11,14 @@
             <property name="orientation">horizontal</property>
             <property name="halign">fill</property>
             <property name="valign">center</property>
+            <child>
+              <object class="GtkSpinner" id="spinner">
+                <property name="visible">False</property>
+                <property name="spinning">False</property>
+                <property name="halign">center</property>
+                <property name="valign">center</property>
+              </object>
+            </child>
             <child>
               <object class="GtkBox" id="fixed_height_box">
                 <property name="orientation">vertical</property>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]