[gtk] flattenlistmodeL: Compute items-changed position properly



commit 129691f3a628c2796fb4d367c4479a2c1a973fed
Author: Benjamin Otte <otte redhat com>
Date:   Mon Jul 1 23:57:20 2019 +0000

    flattenlistmodeL: Compute items-changed position properly
    
    The code previously forgot to include the left child of the model's
    node. Which of course only happened if that child wasn't NULL, which is
    a common case.
    
    Found and test provided by Matthias Clasen.

 gtk/gtkflattenlistmodel.c        | 14 +++++++++++---
 testsuite/gtk/flattenlistmodel.c | 39 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+), 3 deletions(-)
---
diff --git a/gtk/gtkflattenlistmodel.c b/gtk/gtkflattenlistmodel.c
index e33a47a30b..3204f5b5ea 100644
--- a/gtk/gtkflattenlistmodel.c
+++ b/gtk/gtkflattenlistmodel.c
@@ -216,17 +216,25 @@ gtk_flatten_list_model_items_changed_cb (GListModel          *model,
                                          guint                added,
                                          gpointer             _node)
 {
-  FlattenNode *node = _node, *parent;
+  FlattenNode *node = _node, *parent, *left;
   GtkFlattenListModel *self = node->list;
   guint real_position;
 
   gtk_rb_tree_node_mark_dirty (node);
+  real_position = position;
 
-  for (real_position = position;
+  left = gtk_rb_tree_node_get_left (node);
+  if (left)
+    {
+      FlattenAugment *aug = gtk_rb_tree_get_augment (self->items, left);
+      real_position += aug->n_items;
+    }
+
+  for (;
        (parent = gtk_rb_tree_node_get_parent (node)) != NULL;
        node = parent)
     {
-      FlattenNode *left = gtk_rb_tree_node_get_left (parent);
+      left = gtk_rb_tree_node_get_left (parent);
       if (left != node)
         {
           if (left)
diff --git a/testsuite/gtk/flattenlistmodel.c b/testsuite/gtk/flattenlistmodel.c
index 94de2490ef..79921e5042 100644
--- a/testsuite/gtk/flattenlistmodel.c
+++ b/testsuite/gtk/flattenlistmodel.c
@@ -300,6 +300,44 @@ test_submodel_add (void)
   g_object_unref (flat);
 }
 
+static void
+test_submodel_add2 (void)
+{
+  GtkFlattenListModel *flat;
+  GListStore *model, *store[2];
+
+  model = g_list_store_new (G_TYPE_LIST_MODEL);
+  flat = new_model (model);
+  assert_model (flat, "");
+  assert_changes (flat, "");
+
+  store[0] = add_store (model, 1, 0, 0);
+  store[1] = add_store (model, 1, 0, 0);
+  store[2] = add_store (model, 1, 0, 0);
+
+  assert_model (flat, "");
+  assert_changes (flat, "");
+
+  add (store[0], 1);
+  assert_model (flat, "1");
+  assert_changes (flat, "+0");
+
+  add (store[1], 3);
+  assert_model (flat, "1 3");
+  assert_changes (flat, "+1");
+
+  add (store[0], 2);
+  assert_model (flat, "1 2 3");
+  assert_changes (flat, "+1");
+
+  add (store[1], 4);
+  assert_model (flat, "1 2 3 4");
+  assert_changes (flat, "+3");
+
+  g_object_unref (model);
+  g_object_unref (flat);
+}
+
 static void
 test_model_remove (void)
 {
@@ -365,6 +403,7 @@ main (int argc, char *argv[])
   g_test_add_func ("/flattenlistmodel/model/add", test_model_add);
 #if GLIB_CHECK_VERSION (2, 58, 0) /* g_list_store_splice() is broken before 2.58 */
   g_test_add_func ("/flattenlistmodel/submodel/add", test_submodel_add);
+  g_test_add_func ("/flattenlistmodel/submodel/add2", test_submodel_add2);
   g_test_add_func ("/flattenlistmodel/model/remove", test_model_remove);
   g_test_add_func ("/flattenlistmodel/submodel/remove", test_submodel_remove);
 #endif


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