[Xfce4-commits] [xfce/xfdesktop] 01/01: Optimized loading wallpapers at start-up; optimized sorting icons in icon view.

noreply at xfce.org noreply at xfce.org
Mon May 11 12:19:58 CEST 2015


This is an automated email from the git hooks/post-receive script.

eric pushed a commit to branch master
in repository xfce/xfdesktop.

commit 5650454c6e16389994b4628834c2aeba68551125
Author: Igor Kushnir <igorkuo at meta.ua>
Date:   Mon May 11 11:25:26 2015 +0300

    Optimized loading wallpapers at start-up; optimized sorting icons in icon view.
    
    Signed-off-by: Eric Koegel <eric.koegel at gmail.com>
---
 src/xfce-backdrop.c       |   44 +++++++++++++++++++++++++++++++++++++++++---
 src/xfdesktop-icon-view.c |   43 ++++++++++++++++---------------------------
 2 files changed, 57 insertions(+), 30 deletions(-)

diff --git a/src/xfce-backdrop.c b/src/xfce-backdrop.c
index af514be..10da9da 100644
--- a/src/xfce-backdrop.c
+++ b/src/xfce-backdrop.c
@@ -26,6 +26,10 @@
 #include <config.h>
 #endif
 
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+
 #ifdef HAVE_STRING_H
 #include <string.h>
 #endif
@@ -336,6 +340,36 @@ cb_xfce_backdrop__image_files_changed(GFileMonitor     *monitor,
     }
 }
 
+/* Equivalent to, but faster than
+ * g_list_sort(list, (GCompareFunc)compare_by_collate_key) */
+static GList *
+sort_image_list(GList *list, guint list_size)
+{
+    gchar **array;
+    guint i;
+    GList *l;
+
+    g_assert(g_list_length(list) == list_size);
+    /* Create an array of the same size as list */
+    array = g_malloc(list_size * sizeof(array[0]));
+
+    /* Copy list contents to the array */
+    for(l = list, i = 0; l; l = l->next, ++i)
+        array[i] = l->data;
+
+    /* Sort the array */
+    qsort(array, list_size, sizeof(array[0]),
+          (GCompareFunc)compare_by_collate_key);
+
+    /* Copy sorted array back to the list */
+    for(l = list, i = 0; l; l = l->next, ++i)
+        l->data = array[i];
+
+    g_free(array);
+
+    return list;
+}
+
 /* Returns a GList of all the image files in the parent directory of filename */
 static GList *
 list_image_files_in_dir(const gchar *filename)
@@ -344,6 +378,7 @@ list_image_files_in_dir(const gchar *filename)
     gboolean needs_slash = TRUE;
     const gchar *file;
     GList *files = NULL;
+    guint file_count = 0;
     gchar *dir_name;
 
     dir_name = g_path_get_dirname(filename);
@@ -360,15 +395,18 @@ list_image_files_in_dir(const gchar *filename)
     while((file = g_dir_read_name(dir))) {
         gchar *current_file = g_strdup_printf(needs_slash ? "%s/%s" : "%s%s",
                                               dir_name, file);
-        if(xfdesktop_image_file_is_valid(current_file))
-            files = g_list_insert_sorted(files, current_file, (GCompareFunc)compare_by_collate_key);
-        else
+        if(xfdesktop_image_file_is_valid(current_file)) {
+            files = g_list_prepend(files, current_file);
+            ++file_count;
+        } else
             g_free(current_file);
     }
 
     g_dir_close(dir);
     g_free(dir_name);
 
+    if(file_count > 1)
+        files = sort_image_list(files, file_count);
     return files;
 }
 
diff --git a/src/xfdesktop-icon-view.c b/src/xfdesktop-icon-view.c
index 8bc180d..3c44e10 100644
--- a/src/xfdesktop-icon-view.c
+++ b/src/xfdesktop-icon-view.c
@@ -1852,10 +1852,8 @@ xfdesktop_icon_view_sort_icons(XfdesktopIconView *icon_view)
 {
 #ifdef ENABLE_FILE_ICONS
     GList *l = NULL;
-    GList *special_icons = NULL;
-    GList *volume_icons = NULL;
-    GList *folder_icons = NULL;
-    GList *regular_icons = NULL;
+    gint i;
+    GList *icons[4] = { NULL, NULL, NULL, NULL };
     gint16 row = -1; /* start at -1 because we'll increment it */
     gint16 col = 0;
 
@@ -1868,41 +1866,32 @@ xfdesktop_icon_view_sort_icons(XfdesktopIconView *icon_view)
         if(xfdesktop_icon_get_position(l->data, &old_row, &old_col))
             xfdesktop_grid_set_position_free(icon_view, old_row, old_col);
 
-        /* Add it to the correct list */
+        /* Choose the correct list index */
         if(XFDESKTOP_IS_SPECIAL_FILE_ICON(l->data)) {
-            special_icons = g_list_insert_sorted(special_icons,
-                                                 l->data,
-                                                 (GCompareFunc)xfdesktop_icon_view_compare_icons);
+            i = 0;
         } else if(XFDESKTOP_IS_VOLUME_ICON(l->data)) {
-            volume_icons = g_list_insert_sorted(volume_icons,
-                                                l->data,
-                                                (GCompareFunc)xfdesktop_icon_view_compare_icons);
+            i = 1;
         } else if(XFDESKTOP_IS_FILE_ICON(l->data) &&
                   g_file_query_file_type(xfdesktop_file_icon_peek_file(l->data),
                                          G_FILE_QUERY_INFO_NONE,
                                          NULL) == G_FILE_TYPE_DIRECTORY)
         {
-            folder_icons = g_list_insert_sorted(folder_icons,
-                                                l->data,
-                                                (GCompareFunc)xfdesktop_icon_view_compare_icons);
+            i = 2;
         } else {
-            regular_icons = g_list_insert_sorted(regular_icons,
-                                                 l->data,
-                                                 (GCompareFunc)xfdesktop_icon_view_compare_icons);
+            i = 3;
         }
+
+        /* Add the icon to the correct list */
+        icons[i] = g_list_prepend(icons[i], l->data);
     }
 
     /* Append the icons: special, folder, then regular */
-    xfdesktop_icon_view_append_icons(icon_view, special_icons, &row, &col);
-    xfdesktop_icon_view_append_icons(icon_view, volume_icons, &row, &col);
-    xfdesktop_icon_view_append_icons(icon_view, folder_icons, &row, &col);
-    xfdesktop_icon_view_append_icons(icon_view, regular_icons, &row, &col);
-
-
-    g_list_free(special_icons);
-    g_list_free(volume_icons);
-    g_list_free(folder_icons);
-    g_list_free(regular_icons);
+    for(i = 0; i < sizeof(icons) / sizeof(icons[0]); ++i) {
+        l = g_list_sort(icons[i],
+                        (GCompareFunc)xfdesktop_icon_view_compare_icons);
+        xfdesktop_icon_view_append_icons(icon_view, l, &row, &col);
+        g_list_free(l);
+    }
 #endif
 }
 

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Xfce4-commits mailing list