[gnome-software] Repurpose the 'important' property of a GsCategory into an integer for sorting



commit 7148a94505deb722fdf20f45f186e997d216588e
Author: Richard Hughes <richard hughsie com>
Date:   Fri Jul 22 11:36:32 2016 +0100

    Repurpose the 'important' property of a GsCategory into an integer for sorting
    
    This lets us remove the hacky promotion and demotion of important categories,
    and also allows us to 'curate' the order of the categories manually.
    
    Resolves: https://bugzilla.gnome.org/show_bug.cgi?id=769071

 src/gs-category-tile.c                     |   50 ++++++++++++++++++++-------
 src/gs-category-tile.h                     |    3 ++
 src/gs-category.c                          |   26 +++++++-------
 src/gs-category.h                          |    6 ++--
 src/gs-plugin-loader.c                     |    7 +---
 src/gs-shell-overview.c                    |   49 +++++----------------------
 src/plugins/gs-desktop-common.c            |   22 ++++++------
 src/plugins/gs-desktop-common.h            |    2 +-
 src/plugins/gs-plugin-desktop-categories.c |    2 +-
 9 files changed, 80 insertions(+), 87 deletions(-)
---
diff --git a/src/gs-category-tile.c b/src/gs-category-tile.c
index fffe2ec..332b485 100644
--- a/src/gs-category-tile.c
+++ b/src/gs-category-tile.c
@@ -34,6 +34,7 @@ struct _GsCategoryTile
        GsCategory      *cat;
        GtkWidget       *label;
        GtkWidget       *image;
+       gboolean         colorful;
 };
 
 G_DEFINE_TYPE (GsCategoryTile, gs_category_tile, GTK_TYPE_BUTTON)
@@ -46,33 +47,56 @@ gs_category_tile_get_category (GsCategoryTile *tile)
        return tile->cat;
 }
 
-void
-gs_category_tile_set_category (GsCategoryTile *tile, GsCategory *cat)
+static void
+gs_category_tile_refresh (GsCategoryTile *tile)
 {
        GPtrArray *key_colors;
 
-       g_return_if_fail (GS_IS_CATEGORY_TILE (tile));
-       g_return_if_fail (GS_IS_CATEGORY (cat));
-
-       g_clear_object (&tile->cat);
-       tile->cat = g_object_ref (cat);
-
-       gtk_label_set_label (GTK_LABEL (tile->label), gs_category_get_name (cat));
+       /* set labels */
+       gtk_label_set_label (GTK_LABEL (tile->label),
+                            gs_category_get_name (tile->cat));
        gtk_image_set_from_icon_name (GTK_IMAGE (tile->image),
-                                     gs_category_get_icon (cat),
+                                     gs_category_get_icon (tile->cat),
                                      GTK_ICON_SIZE_MENU);
 
-       /* set custom CSS for important tiles */
-       key_colors = gs_category_get_key_colors (cat);
-       if (gs_category_get_important (cat) && key_colors->len > 0) {
+       /* set custom CSS for colorful tiles */
+       key_colors = gs_category_get_key_colors (tile->cat);
+       if (tile->colorful && key_colors->len > 0) {
                GdkRGBA *tmp = g_ptr_array_index (key_colors, 0);
                g_autofree gchar *css = NULL;
                g_autofree gchar *color = gdk_rgba_to_string (tmp);;
                css = g_strdup_printf ("border-bottom: 3px solid %s", color);
                gs_utils_widget_set_css_simple (GTK_WIDGET (tile), css);
+       } else {
+               gs_utils_widget_set_css_simple (GTK_WIDGET (tile), NULL);
        }
 }
 
+void
+gs_category_tile_set_category (GsCategoryTile *tile, GsCategory *cat)
+{
+       g_return_if_fail (GS_IS_CATEGORY_TILE (tile));
+       g_return_if_fail (GS_IS_CATEGORY (cat));
+
+       g_set_object (&tile->cat, cat);
+       gs_category_tile_refresh (tile);
+}
+
+gboolean
+gs_category_tile_get_colorful (GsCategoryTile *tile)
+{
+       g_return_val_if_fail (GS_IS_CATEGORY_TILE (tile), FALSE);
+       return tile->colorful;
+}
+
+void
+gs_category_tile_set_colorful (GsCategoryTile *tile, gboolean colorful)
+{
+       g_return_if_fail (GS_IS_CATEGORY_TILE (tile));
+       tile->colorful = colorful;
+       gs_category_tile_refresh (tile);
+}
+
 static void
 gs_category_tile_destroy (GtkWidget *widget)
 {
diff --git a/src/gs-category-tile.h b/src/gs-category-tile.h
index 7f33758..0167237 100644
--- a/src/gs-category-tile.h
+++ b/src/gs-category-tile.h
@@ -36,6 +36,9 @@ GtkWidget     *gs_category_tile_new                   (GsCategory *cat);
 GsCategory      *gs_category_tile_get_category         (GsCategoryTile *tile);
 void            gs_category_tile_set_category          (GsCategoryTile *tile,
                                                         GsCategory     *cat);
+gboolean        gs_category_tile_get_colorful          (GsCategoryTile *tile);
+void            gs_category_tile_set_colorful          (GsCategoryTile *tile,
+                                                        gboolean        colorful);
 
 G_END_DECLS
 
diff --git a/src/gs-category.c b/src/gs-category.c
index df90f1e..d6816d1 100644
--- a/src/gs-category.c
+++ b/src/gs-category.c
@@ -41,7 +41,7 @@ struct _GsCategory
        gchar           *id;
        gchar           *name;
        gchar           *icon;
-       gboolean         important;
+       gint             score;
        GPtrArray       *key_colors;
        GPtrArray       *desktop_groups;
        GsCategory      *parent;
@@ -85,8 +85,7 @@ gs_category_to_string (GsCategory *category)
                g_string_append_printf (str, "  parent: %s\n",
                                        gs_category_get_id (category->parent));
        }
-       g_string_append_printf (str, "  important: %s\n",
-                               category->important ? "yes" : "no");
+       g_string_append_printf (str, "  score: %i\n", category->score);
        if (category->children->len == 0) {
                g_string_append_printf (str, "  children: %i\n",
                                        category->children->len);
@@ -249,34 +248,35 @@ gs_category_set_icon (GsCategory *category, const gchar *icon)
 }
 
 /**
- * gs_category_get_important:
+ * gs_category_get_score:
  * @category: a #GsCategory
  *
- * Gets if the category is important.
+ * Gets if the category score.
  * Important categories may be shown before other categories, or tagged in a
  * different way, for example with color or in a different section.
  *
  * Returns: the string, or %NULL
  **/
-gboolean
-gs_category_get_important (GsCategory *category)
+gint
+gs_category_get_score (GsCategory *category)
 {
        g_return_val_if_fail (GS_IS_CATEGORY (category), FALSE);
-       return category->important;
+       return category->score;
 }
 
 /**
- * gs_category_set_important:
+ * gs_category_set_score:
  * @category: a #GsCategory
- * @important: a category important, or %NULL
+ * @score: a category score, or %NULL
  *
- * Sets if the category is important.
+ * Sets the category score, where larger numbers get sorted before lower
+ * numbers.
  **/
 void
-gs_category_set_important (GsCategory *category, gboolean important)
+gs_category_set_score (GsCategory *category, gint score)
 {
        g_return_if_fail (GS_IS_CATEGORY (category));
-       category->important = important;
+       category->score = score;
 }
 
 /**
diff --git a/src/gs-category.h b/src/gs-category.h
index 1dd38bb..b49607b 100644
--- a/src/gs-category.h
+++ b/src/gs-category.h
@@ -42,9 +42,9 @@ void           gs_category_set_name           (GsCategory     *category,
 const gchar    *gs_category_get_icon           (GsCategory     *category);
 void            gs_category_set_icon           (GsCategory     *category,
                                                 const gchar    *icon);
-gboolean        gs_category_get_important      (GsCategory     *category);
-void            gs_category_set_important      (GsCategory     *category,
-                                                gboolean        important);
+gint            gs_category_get_score          (GsCategory     *category);
+void            gs_category_set_score          (GsCategory     *category,
+                                                gint            score);
 GPtrArray      *gs_category_get_key_colors     (GsCategory     *category);
 void            gs_category_add_key_color      (GsCategory     *category,
                                                 const GdkRGBA  *key_color);
diff --git a/src/gs-plugin-loader.c b/src/gs-plugin-loader.c
index 25c7cdd..b44284d 100644
--- a/src/gs-plugin-loader.c
+++ b/src/gs-plugin-loader.c
@@ -2245,13 +2245,10 @@ gs_plugin_loader_category_sort_cb (gconstpointer a, gconstpointer b)
 {
        GsCategory *cata = GS_CATEGORY (*(GsCategory **) a);
        GsCategory *catb = GS_CATEGORY (*(GsCategory **) b);
-
-       /* addons always go last */
-       if (g_strcmp0 (gs_category_get_id (cata), "addons") == 0)
+       if (gs_category_get_score (cata) < gs_category_get_score (catb))
                return 1;
-       if (g_strcmp0 (gs_category_get_id (catb), "addons") == 0)
+       if (gs_category_get_score (cata) > gs_category_get_score (catb))
                return -1;
-
        return g_strcmp0 (gs_category_get_name (cata),
                          gs_category_get_name (catb));
 }
diff --git a/src/gs-shell-overview.c b/src/gs-shell-overview.c
index 7134c00..4b8980b 100644
--- a/src/gs-shell-overview.c
+++ b/src/gs-shell-overview.c
@@ -353,13 +353,11 @@ gs_shell_overview_get_categories_cb (GObject *source_object,
        GsShellOverviewPrivate *priv = gs_shell_overview_get_instance_private (self);
        GsPluginLoader *plugin_loader = GS_PLUGIN_LOADER (source_object);
        guint i;
-       guint important_cnt = 0;
-       guint important_aim;
        GsCategory *cat;
        GtkFlowBox *flowbox;
        GtkWidget *tile;
-       gboolean has_category = FALSE;
-       gboolean use_expander = FALSE;
+       const guint MAX_CATS_PER_SECTION = 6;
+       guint added_cnt = 0;
        g_autoptr(GError) error = NULL;
        g_autoptr(GPtrArray) list = NULL;
 
@@ -372,35 +370,6 @@ gs_shell_overview_get_categories_cb (GObject *source_object,
        gs_container_remove_all (GTK_CONTAINER (priv->flowbox_categories));
        gs_container_remove_all (GTK_CONTAINER (priv->flowbox_categories2));
 
-       /* ensure there are no more than two rows of curated categories */
-       flowbox = GTK_FLOW_BOX (priv->flowbox_categories);
-       important_aim = 2 * gtk_flow_box_get_max_children_per_line (flowbox);
-       for (i = 0; i < list->len; i++) {
-               cat = GS_CATEGORY (g_ptr_array_index (list, i));
-               if (gs_category_get_size (cat) == 0)
-                       continue;
-               if (gs_category_get_important (cat)) {
-                       if (++important_cnt > important_aim) {
-                               g_debug ("overriding %s as unimportant",
-                                        gs_category_get_id (cat));
-                               gs_category_set_important (cat, FALSE);
-                       }
-               }
-       }
-
-       /* ensure we show a full first flowbox if we don't have enough */
-       for (i = 0; i < list->len && important_cnt < important_aim; i++) {
-               cat = GS_CATEGORY (g_ptr_array_index (list, i));
-               if (gs_category_get_size (cat) == 0)
-                       continue;
-               if (!gs_category_get_important (cat)) {
-                       important_cnt++;
-                       g_debug ("overriding %s as important",
-                                gs_category_get_id (cat));
-                       gs_category_set_important (cat, TRUE);
-               }
-       }
-
        /* add categories to the correct flowboxes, the second being hidden */
        for (i = 0; i < list->len; i++) {
                cat = GS_CATEGORY (g_ptr_array_index (list, i));
@@ -409,15 +378,15 @@ gs_shell_overview_get_categories_cb (GObject *source_object,
                tile = gs_category_tile_new (cat);
                g_signal_connect (tile, "clicked",
                                  G_CALLBACK (category_tile_clicked), self);
-               if (gs_category_get_important (cat)) {
+               if (added_cnt < MAX_CATS_PER_SECTION) {
                        flowbox = GTK_FLOW_BOX (priv->flowbox_categories);
+                       gs_category_tile_set_colorful (GS_CATEGORY_TILE (tile), TRUE);
                } else {
                        flowbox = GTK_FLOW_BOX (priv->flowbox_categories2);
-                       use_expander = TRUE;
                }
                gtk_flow_box_insert (flowbox, tile, -1);
                gtk_widget_set_can_focus (gtk_widget_get_parent (tile), FALSE);
-               has_category = TRUE;
+               added_cnt++;
 
                /* we save these for the 'More...' buttons */
                g_hash_table_insert (priv->category_hash,
@@ -426,12 +395,12 @@ gs_shell_overview_get_categories_cb (GObject *source_object,
        }
 
        /* show the expander if we have too many children */
-       gtk_widget_set_visible (priv->categories_expander, use_expander);
+       gtk_widget_set_visible (priv->categories_expander,
+                               added_cnt > MAX_CATS_PER_SECTION);
 out:
-       if (has_category) {
+       if (added_cnt > 0)
                priv->empty = FALSE;
-       }
-       gtk_widget_set_visible (priv->category_heading, has_category);
+       gtk_widget_set_visible (priv->category_heading, added_cnt > 0);
 
        priv->loading_categories = FALSE;
        priv->refresh_count--;
diff --git a/src/plugins/gs-desktop-common.c b/src/plugins/gs-desktop-common.c
index c388a10..1730ec5 100644
--- a/src/plugins/gs-desktop-common.c
+++ b/src/plugins/gs-desktop-common.c
@@ -321,37 +321,37 @@ static const GsDesktopMap map_reference[] = {
 static const GsDesktopData msdata[] = {
        /* TRANSLATORS: this is the menu spec main category for Audio & Video */
        { "audio-video",        map_audiovisual,        N_("Audio & Video"),
-                               "folder-music-symbolic", "#215d9c", TRUE },
+                               "folder-music-symbolic", "#215d9c", 100 },
        /* TRANSLATORS: this is the menu spec main category for Development */
        { "developer-tools",    map_developertools,     N_("Developer Tools"),
-                               "applications-engineering-symbolic", "#297bcc" },
+                               "applications-engineering-symbolic", "#297bcc", 40 },
        /* TRANSLATORS: this is the menu spec main category for Education */
        { "education",          map_education,          N_("Education"),
-                               "system-help-symbolic", "#29cc5d" },
+                               "system-help-symbolic", "#29cc5d", 30 },
        /* TRANSLATORS: this is the menu spec main category for Game */
        { "games",              map_games,              N_("Games"),
-                               "applications-games-symbolic", "#c4a000", TRUE },
+                               "applications-games-symbolic", "#c4a000", 70 },
        /* TRANSLATORS: this is the menu spec main category for Graphics */
        { "graphics",           map_graphics,           N_("Graphics & Photography"),
-                               "applications-graphics-symbolic", "#75507b", TRUE },
+                               "applications-graphics-symbolic", "#75507b", 60 },
        /* TRANSLATORS: this is the menu spec main category for Office */
        { "productivity",       map_productivity,       N_("Productivity"),
-                               "text-editor-symbolic", "#cc0000", TRUE },
+                               "text-editor-symbolic", "#cc0000", 80 },
        /* TRANSLATORS: this is the menu spec main category for Add-ons */
        { "addons",             map_addons,             N_("Add-ons"),
-                               "application-x-addon-symbolic", "#4e9a06", TRUE },
+                               "application-x-addon-symbolic", "#4e9a06", 50 },
        /* TRANSLATORS: this is the menu spec main category for Science */
        { "science",            map_science,            N_("Science"),
-                               "applications-science-symbolic", "#9c29ca" },
+                               "applications-science-symbolic", "#9c29ca", 20 },
        /* TRANSLATORS: this is the menu spec main category for Communication */
        { "communication",      map_communication,      N_("Communication & News"),
-                               "user-available-symbolic", "#729fcf", TRUE },
+                               "user-available-symbolic", "#729fcf", 90 },
        /* TRANSLATORS: this is the menu spec main category for Reference */
        { "reference",          map_reference,          N_("Reference"),
-                               "view-dual-symbolic", "#ac5500" },
+                               "view-dual-symbolic", "#ac5500", 0 },
        /* TRANSLATORS: this is the menu spec main category for Utilities */
        { "utilities",          map_utilities,          N_("Utilities"),
-                               "applications-utilities-symbolic", "#2944cc" },
+                               "applications-utilities-symbolic", "#2944cc", 10 },
        { NULL }
 };
 
diff --git a/src/plugins/gs-desktop-common.h b/src/plugins/gs-desktop-common.h
index 40fd254..db78c4c 100644
--- a/src/plugins/gs-desktop-common.h
+++ b/src/plugins/gs-desktop-common.h
@@ -38,7 +38,7 @@ typedef struct {
        const gchar     *name;
        const gchar     *icon;
        const gchar     *key_colors;
-       gboolean         important;
+       gint             score;
 } GsDesktopData;
 
 const GsDesktopData    *gs_desktop_get_data            (void);
diff --git a/src/plugins/gs-plugin-desktop-categories.c b/src/plugins/gs-plugin-desktop-categories.c
index 9ce6ec4..440cd5d 100644
--- a/src/plugins/gs-plugin-desktop-categories.c
+++ b/src/plugins/gs-plugin-desktop-categories.c
@@ -58,7 +58,7 @@ gs_plugin_add_categories (GsPlugin *plugin,
                category = gs_category_new (msdata[i].id);
                gs_category_set_icon (category, msdata[i].icon);
                gs_category_set_name (category, gettext (msdata[i].name));
-               gs_category_set_important (category, msdata[i].important);
+               gs_category_set_score (category, msdata[i].score);
                if (gdk_rgba_parse (&key_color, msdata[i].key_colors))
                        gs_category_add_key_color (category, &key_color);
                g_ptr_array_add (list, category);


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