[gnome-software] Truncate the search results if there is a large number of applications



commit 7eb08783ab4f3354164d77efd2faa4e1bd38b1af
Author: Richard Hughes <richard hughsie com>
Date:   Wed Apr 19 20:30:37 2017 +0100

    Truncate the search results if there is a large number of applications
    
    Searching for 'gno' returns over 900 desktop apps, extensions and runtimes and
    it's basically impossible to show that number for search-as-you-type. Limit the
    number of search results by sorting the initial result set before refining the
    list.
    
    If the list is truncated tell the user how many results are missing in the form
    of an insensitive label at the bottom of the search results.

 lib/gs-plugin-loader.c |   19 +++++++++++++++++++
 src/gs-search-page.c   |   39 +++++++++++++++++++++++++++++++++++----
 2 files changed, 54 insertions(+), 4 deletions(-)
---
diff --git a/lib/gs-plugin-loader.c b/lib/gs-plugin-loader.c
index 1ad4263..1f759a1 100644
--- a/lib/gs-plugin-loader.c
+++ b/lib/gs-plugin-loader.c
@@ -2045,6 +2045,16 @@ gs_plugin_loader_convert_unavailable (GsAppList *list, const gchar *search)
        }
 }
 
+static gint
+gs_plugin_loader_app_sort_match_value_cb (GsApp *app1, GsApp *app2, gpointer user_data)
+{
+       if (gs_app_get_match_value (app1) > gs_app_get_match_value (app2))
+               return -1;
+       if (gs_app_get_match_value (app1) < gs_app_get_match_value (app2))
+               return 1;
+       return 0;
+}
+
 static void
 gs_plugin_loader_search_thread_cb (GTask *task,
                                   gpointer object,
@@ -2075,6 +2085,15 @@ gs_plugin_loader_search_thread_cb (GTask *task,
                }
        }
 
+       /* too many results */
+       if (job->max_results > 0 &&
+           gs_app_list_length (job->list) > job->max_results) {
+               gs_app_list_sort (job->list, gs_plugin_loader_app_sort_match_value_cb, NULL);
+               g_debug ("truncating results to %u from %u",
+                        job->max_results, gs_app_list_length (job->list));
+               gs_app_list_truncate (job->list, job->max_results);
+       }
+
        /* run refine() on each one */
        if (!gs_plugin_loader_run_refine (job, job->list, cancellable, &error)) {
                g_task_return_error (task, error);
diff --git a/src/gs-search-page.c b/src/gs-search-page.c
index 241b82a..469b904 100644
--- a/src/gs-search-page.c
+++ b/src/gs-search-page.c
@@ -22,6 +22,7 @@
 #include "config.h"
 
 #include <string.h>
+#include <glib/gi18n.h>
 
 #include "gs-search-page.h"
 #include "gs-shell.h"
@@ -152,6 +153,27 @@ gs_search_page_get_search_cb (GObject *source_object,
                gtk_widget_show (app_row);
        }
 
+       /* too many results */
+       if (gs_app_list_has_flag (list, GS_APP_LIST_FLAG_IS_TRUNCATED)) {
+               GtkStyleContext *context;
+               GtkWidget *w = gtk_label_new (NULL);
+               g_autofree gchar *str = NULL;
+
+               /* TRANSLATORS: this is when there are too many search results
+                * to show in in the search page */
+               str = g_strdup_printf (_("%u more matches"),
+                                      gs_app_list_get_size_peak (list) - gs_app_list_length (list));
+               gtk_label_set_label (GTK_LABEL (w), str);
+               gtk_widget_set_margin_bottom (w, 20);
+               gtk_widget_set_margin_top (w, 20);
+               gtk_widget_set_margin_start (w, 20);
+               gtk_widget_set_margin_end (w, 20);
+               context = gtk_widget_get_style_context (w);
+               gtk_style_context_add_class (context, GTK_STYLE_CLASS_DIM_LABEL);
+               gtk_container_add (GTK_CONTAINER (self->list_box_search), w);
+               gtk_widget_show (w);
+       }
+
        if (self->appid_to_show != NULL) {
                g_autoptr (GsApp) a = NULL;
                a = gs_app_new (self->appid_to_show);
@@ -339,12 +361,21 @@ gs_search_page_sort_func (GtkListBoxRow *a,
                           GtkListBoxRow *b,
                           gpointer user_data)
 {
-       GsApp *a1 = gs_app_row_get_app (GS_APP_ROW (a));
-       GsApp *a2 = gs_app_row_get_app (GS_APP_ROW (b));
-       g_autofree gchar *key1 = gs_search_page_get_app_sort_key (a1);
-       g_autofree gchar *key2 = gs_search_page_get_app_sort_key (a2);
+       GsAppRow *ar;
+       GsAppRow *br;
+       g_autofree gchar *key1 = NULL;
+       g_autofree gchar *key2 = NULL;
+
+       if (!GS_IS_APP_ROW (a))
+               return 1;
+       if (!GS_IS_APP_ROW (b))
+               return -1;
 
        /* compare the keys according to the algorithm above */
+       ar = GS_APP_ROW (a);
+       br = GS_APP_ROW (b);
+       key1 = gs_search_page_get_app_sort_key (gs_app_row_get_app (GS_APP_ROW (ar)));
+       key2 = gs_search_page_get_app_sort_key (gs_app_row_get_app (GS_APP_ROW (br)));
        return g_strcmp0 (key2, key1);
 }
 


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