[nautilus/wip/corey/fix-sort: 1/9] files-view: Drop .compare_files() vfunc




commit 1476fd1b686a54eb5fc273cc660276b1452afa22
Author: António Fernandes <antoniof gnome org>
Date:   Thu Oct 20 17:51:29 2022 +0100

    files-view: Drop .compare_files() vfunc
    
    It gets called at a time when the new files haven't made it into the
    view mode yet. So, we create a pair of dummy items for the sorter to
    work on, and then throw them away.
    
    Creating and destroying object instances at every step in the sorting
    algorithm wastes a lot of cycles while loading large folders, with
    visible performance impact.
    
    So, instead of sorting before the view items are created, do it when
    they are created, right before they are added to the model's internal
    list store.

 src/nautilus-files-view.c | 32 --------------------------------
 src/nautilus-files-view.h |  8 --------
 src/nautilus-list-base.c  | 25 -------------------------
 src/nautilus-view-model.c |  4 ++++
 4 files changed, 4 insertions(+), 65 deletions(-)
---
diff --git a/src/nautilus-files-view.c b/src/nautilus-files-view.c
index f19fab712..09961ad80 100644
--- a/src/nautilus-files-view.c
+++ b/src/nautilus-files-view.c
@@ -4085,36 +4085,10 @@ ready_to_load (NautilusFile *file)
                                          NAUTILUS_FILE_ATTRIBUTES_FOR_ICON);
 }
 
-static int
-compare_files_cover (gconstpointer a,
-                     gconstpointer b,
-                     gpointer      callback_data)
-{
-    const FileAndDirectory *fad1, *fad2;
-    NautilusFilesView *view;
-
-    view = callback_data;
-    fad1 = a;
-    fad2 = b;
-
-    if (fad1->directory < fad2->directory)
-    {
-        return -1;
-    }
-    else if (fad1->directory > fad2->directory)
-    {
-        return 1;
-    }
-    else
-    {
-        return NAUTILUS_FILES_VIEW_CLASS (G_OBJECT_GET_CLASS (view))->compare_files (view, fad1->file, 
fad2->file);
-    }
-}
 static void
 sort_files (NautilusFilesView  *view,
             GList             **list)
 {
-    *list = g_list_sort_with_data (*list, compare_files_cover, view);
 }
 
 /* Go through all the new added and changed files.
@@ -4203,20 +4177,14 @@ process_new_files (NautilusFilesView *view)
         }
     }
 
-    /* If any files were added to old_added_files, then resort it. */
     if (old_added_files != priv->old_added_files)
     {
         priv->old_added_files = old_added_files;
-        sort_files (view, &priv->old_added_files);
     }
 
-    /* Resort old_changed_files too, since file attributes
-     * relevant to sorting could have changed.
-     */
     if (old_changed_files != priv->old_changed_files)
     {
         priv->old_changed_files = old_changed_files;
-        sort_files (view, &priv->old_changed_files);
     }
 }
 
diff --git a/src/nautilus-files-view.h b/src/nautilus-files-view.h
index e1882b7ba..74375a7cc 100644
--- a/src/nautilus-files-view.h
+++ b/src/nautilus-files-view.h
@@ -183,14 +183,6 @@ struct _NautilusFilesViewClass {
 
         void    (* update_actions_state)     (NautilusFilesView *view);
 
-        /* sort_files is a function pointer that subclasses can override
-         * to provide a sorting order to determine which files should be
-         * presented when only a partial list is provided.
-         */
-        int     (* compare_files)            (NautilusFilesView *view,
-                                              NautilusFile      *a,
-                                              NautilusFile      *b);
-
         /* is_empty is a function pointer that subclasses must
          * override to report whether the view contains any items.
          */
diff --git a/src/nautilus-list-base.c b/src/nautilus-list-base.c
index c1f85e6d1..04a02cb2a 100644
--- a/src/nautilus-list-base.c
+++ b/src/nautilus-list-base.c
@@ -1299,30 +1299,6 @@ real_reveal_selection (NautilusFilesView *files_view)
     nautilus_list_base_scroll_to_item (self, get_first_selected_item (self));
 }
 
-static int
-real_compare_files (NautilusFilesView *files_view,
-                    NautilusFile      *file1,
-                    NautilusFile      *file2)
-{
-    NautilusListBase *self = NAUTILUS_LIST_BASE (files_view);
-    NautilusListBasePrivate *priv = nautilus_list_base_get_instance_private (self);
-    GtkSorter *sorter;
-    g_autoptr (NautilusViewItem) item1 = NULL;
-    g_autoptr (NautilusViewItem) item2 = NULL;
-
-    sorter = nautilus_view_model_get_sorter (priv->model);
-    if (sorter == NULL)
-    {
-        return 0;
-    }
-
-    /* Generate fake model items for sorter use only. */
-    item1 = nautilus_view_item_new (file1, NAUTILUS_GRID_ICON_SIZE_SMALL);
-    item2 = nautilus_view_item_new (file2, NAUTILUS_GRID_ICON_SIZE_SMALL);
-
-    return gtk_sorter_compare (sorter, item1, item2);
-}
-
 static void
 on_clipboard_contents_received (GObject      *source_object,
                                 GAsyncResult *res,
@@ -1737,7 +1713,6 @@ nautilus_list_base_class_init (NautilusListBaseClass *klass)
     files_view_class->select_all = real_select_all;
     files_view_class->set_selection = real_set_selection;
     files_view_class->invert_selection = real_invert_selection;
-    files_view_class->compare_files = real_compare_files;
     files_view_class->end_file_changes = real_end_file_changes;
     files_view_class->end_loading = real_end_loading;
     files_view_class->get_first_visible_file = real_get_first_visible_file;
diff --git a/src/nautilus-view-model.c b/src/nautilus-view-model.c
index 0ce806713..ba59329f2 100644
--- a/src/nautilus-view-model.c
+++ b/src/nautilus-view-model.c
@@ -391,6 +391,10 @@ nautilus_view_model_add_items (NautilusViewModel *self,
     GList *l;
     int i = 0;
 
+    /* Sort items before adding them to the internal model. This ensures that
+     * the first sorted item is become the initial focus and scroll anchor. */
+    g_queue_sort (items, compare_data_func, self);
+
     array = g_malloc_n (g_queue_get_length (items),
                         sizeof (NautilusViewItem *));
 


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