[gtk/prop-list: 16/21] inspector: Make the resource list sortable



commit 9645142e8ce9bcd6f49c1e29bce9eb2228affc0e
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Dec 8 16:02:14 2019 -0500

    inspector: Make the resource list sortable
    
    This is using a GtkTreeListRowSorter to keep expanded
    state of the tree while changing the sorting.

 gtk/inspector/resource-list.c | 66 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 65 insertions(+), 1 deletion(-)
---
diff --git a/gtk/inspector/resource-list.c b/gtk/inspector/resource-list.c
index 9be6f7387f..80a8185486 100644
--- a/gtk/inspector/resource-list.c
+++ b/gtk/inspector/resource-list.c
@@ -599,9 +599,30 @@ stop_search (GtkWidget                *entry,
   gtk_search_bar_set_search_mode (GTK_SEARCH_BAR (rl->priv->search_bar), FALSE);
 }
 
+static char *
+holder_name (gpointer item)
+{
+  return g_strdup (resource_holder_get_name (RESOURCE_HOLDER (item)));
+}
+
+static int
+holder_count (gpointer item)
+{
+  return resource_holder_get_count (RESOURCE_HOLDER (item));
+}
+
+static gsize
+holder_size (gpointer item)
+{
+  return resource_holder_get_size (RESOURCE_HOLDER (item));
+}
+
 static void
 gtk_inspector_resource_list_init (GtkInspectorResourceList *rl)
 {
+  GtkSorter *sorter;
+  GtkExpression *expression;
+
   rl->priv = gtk_inspector_resource_list_get_instance_private (rl);
 
   gtk_widget_init_template (GTK_WIDGET (rl));
@@ -610,6 +631,42 @@ gtk_inspector_resource_list_init (GtkInspectorResourceList *rl)
 
   gtk_search_bar_connect_entry (GTK_SEARCH_BAR (rl->priv->search_bar),
                                 GTK_EDITABLE (rl->priv->search_entry));
+
+  expression = gtk_cclosure_expression_new (G_TYPE_STRING, NULL,
+                                            0, NULL,
+                                            (GCallback)holder_name,
+                                            NULL, NULL);
+
+  sorter = gtk_string_sorter_new ();
+  gtk_string_sorter_set_expression (GTK_STRING_SORTER (sorter), expression);
+  gtk_column_view_column_set_sorter (rl->priv->path, sorter);
+  g_object_unref (sorter);
+
+  gtk_expression_unref (expression);
+
+  expression = gtk_cclosure_expression_new (G_TYPE_INT, NULL,
+                                            0, NULL,
+                                            (GCallback)holder_count,
+                                            NULL, NULL);
+
+  sorter = gtk_numeric_sorter_new ();
+  gtk_numeric_sorter_set_expression (GTK_NUMERIC_SORTER (sorter), expression);
+  gtk_column_view_column_set_sorter (rl->priv->count, sorter);
+  g_object_unref (sorter);
+
+  gtk_expression_unref (expression);
+
+  expression = gtk_cclosure_expression_new (G_TYPE_UINT64, NULL,
+                                            0, NULL,
+                                            (GCallback)holder_size,
+                                            NULL, NULL);
+
+  sorter = gtk_numeric_sorter_new ();
+  gtk_numeric_sorter_set_expression (GTK_NUMERIC_SORTER (sorter), expression);
+  gtk_column_view_column_set_sorter (rl->priv->size, sorter);
+  g_object_unref (sorter);
+
+  gtk_expression_unref (expression);
 }
 
 static GListModel *
@@ -628,6 +685,8 @@ constructed (GObject *object)
 {
   GtkInspectorResourceList *rl = GTK_INSPECTOR_RESOURCE_LIST (object);
   GListModel *root_model;
+  GListModel *sort_model;
+  GtkSorter *sorter;
  
   g_signal_connect (rl->priv->open_details_button, "clicked",
                     G_CALLBACK (open_details), rl);
@@ -641,8 +700,13 @@ constructed (GObject *object)
                                                   create_model_for_object,
                                                   NULL,
                                                   NULL);
-  rl->priv->selection = gtk_single_selection_new (G_LIST_MODEL (rl->priv->tree_model));
+
+  sorter = gtk_tree_list_row_sorter_new (gtk_column_view_get_sorter (GTK_COLUMN_VIEW (rl->priv->list)));
+  sort_model = G_LIST_MODEL (gtk_sort_list_model_new (G_LIST_MODEL (rl->priv->tree_model), sorter));
+  rl->priv->selection = gtk_single_selection_new (sort_model);
   g_object_unref (root_model);
+  g_object_unref (sort_model);
+  g_object_unref (sorter);
 
   gtk_column_view_set_model (GTK_COLUMN_VIEW (rl->priv->list), G_LIST_MODEL (rl->priv->selection));
 


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