[gnome-software: 3/18] gs-app: Sort icon array by size




commit 321e75439fceff1df83cb41ed3919e4cfe2fde9e
Author: Philip Withnall <pwithnall endlessos org>
Date:   Mon Mar 1 23:51:07 2021 +0000

    gs-app: Sort icon array by size
    
    This will be useful for speeding searches through it up later.
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>
    
    Helps: #1147

 lib/gs-app.c | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)
---
diff --git a/lib/gs-app.c b/lib/gs-app.c
index 664f1f298..1aec14ff5 100644
--- a/lib/gs-app.c
+++ b/lib/gs-app.c
@@ -61,7 +61,7 @@ typedef struct
        gchar                   *name;
        gchar                   *renamed_from;
        GsAppQuality             name_quality;
-       GPtrArray               *icons;  /* (nullable) (owned) (element-type AsIcon) */
+       GPtrArray               *icons;  /* (nullable) (owned) (element-type AsIcon), sorted by pixel size, 
smallest first */
        GPtrArray               *sources;
        GPtrArray               *source_ids;
        gchar                   *project_group;
@@ -1917,6 +1917,25 @@ gs_app_get_icons (GsApp *app)
        return priv->icons;
 }
 
+static gint
+icon_sort_width_cb (gconstpointer a,
+                    gconstpointer b)
+{
+       AsIcon *icon_a = *((AsIcon **) a);
+       AsIcon *icon_b = *((AsIcon **) b);
+       guint width_a = as_icon_get_width (icon_a);
+       guint width_b = as_icon_get_width (icon_b);
+
+       if (width_a == 0 && width_b == 0)
+               return 0;
+       else if (width_a == 0)
+               return 1;
+       else if (width_b == 0)
+               return -1;
+       else
+               return width_a - width_b;
+}
+
 /**
  * gs_app_add_icon:
  * @app: a #GsApp
@@ -1944,6 +1963,9 @@ gs_app_add_icon (GsApp *app, AsIcon *icon)
                priv->icons = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
 
        g_ptr_array_add (priv->icons, g_object_ref (icon));
+
+       /* Ensure the array is sorted by increasing width. */
+       g_ptr_array_sort (priv->icons, icon_sort_width_cb);
 }
 
 /**


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