[gtk/wip/otte/listview: 123/169] listitem: Add gtk_list_item_get_position()



commit bc61ad4bc4dcddf60705d2d79faf2854c4270e3c
Author: Benjamin Otte <otte redhat com>
Date:   Mon Sep 24 04:01:37 2018 +0200

    listitem: Add gtk_list_item_get_position()
    
    Also refactor the whole list item management yet again.
    
    Now, list item APIs doesn't have bind/unbind functions anymore, but only
    property setters.
    
    The item factory is the only one doing the binding.
    As before, the item manager manages when items need to be bound.

 docs/reference/gtk/gtk4-sections.txt |  1 +
 gtk/gtklistitem.c                    | 66 +++++++++++++++++++++++++++++-------
 gtk/gtklistitem.h                    |  2 ++
 gtk/gtklistitemfactory.c             | 26 ++++++++++++--
 gtk/gtklistitemfactoryprivate.h      |  4 +++
 gtk/gtklistitemmanager.c             | 23 ++++++++++++-
 gtk/gtklistitemmanagerprivate.h      |  3 ++
 gtk/gtklistitemprivate.h             |  5 +--
 gtk/gtklistview.c                    | 18 ++++++++++
 9 files changed, 130 insertions(+), 18 deletions(-)
---
diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt
index 2ddc7e07df..361751f7b0 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -498,6 +498,7 @@ gtk_single_selection_get_type
 <TITLE>GtkListItem</TITLE>
 GtkListItem
 gtk_list_item_get_item
+gtk_list_item_get_position
 <SUBSECTION Standard>
 GTK_LIST_ITEM
 GTK_LIST_ITEM_CLASS
diff --git a/gtk/gtklistitem.c b/gtk/gtklistitem.c
index 2cddbea044..8be93b6f07 100644
--- a/gtk/gtklistitem.c
+++ b/gtk/gtklistitem.c
@@ -52,12 +52,14 @@ struct _GtkListItem
   GtkBin parent_instance;
 
   GObject *item;
+  guint position;
 };
 
 enum
 {
   PROP_0,
   PROP_ITEM,
+  PROP_POSITION,
 
   N_PROPS
 };
@@ -90,6 +92,10 @@ gtk_list_item_get_property (GObject    *object,
       g_value_set_object (value, self->item);
       break;
 
+    case PROP_POSITION:
+      g_value_set_uint (value, self->position);
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
       break;
@@ -134,6 +140,18 @@ gtk_list_item_class_init (GtkListItemClass *klass)
                          G_TYPE_OBJECT,
                          G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
 
+  /**
+   * GtkListItem:position:
+   *
+   * Position in the item
+   */
+  properties[PROP_POSITION] =
+    g_param_spec_uint ("position",
+                       P_("Position"),
+                       P_("Position of the item"),
+                       0, G_MAXUINT, 0,
+                       G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
+
   g_object_class_install_properties (gobject_class, N_PROPS, properties);
 
   /* This gets overwritten by gtk_list_item_new() but better safe than sorry */
@@ -159,10 +177,10 @@ gtk_list_item_new (const char *css_name)
  * gtk_list_item_get_item:
  * @self: a #GtkListItem
  *
- * Gets the item that is currently displayed or model that @self is
+ * Gets the item that is currently displayed in model that @self is
  * currently bound to or %NULL if @self is unbound.
  *
- * Returns: (nullable) (transfer none) (type GObject): The model in use
+ * Returns: (nullable) (transfer none) (type GObject): The item displayed
  **/
 gpointer
 gtk_list_item_get_item (GtkListItem *self)
@@ -172,6 +190,23 @@ gtk_list_item_get_item (GtkListItem *self)
   return self->item;
 }
 
+/**
+ * gtk_list_item_get_position:
+ * @self: a #GtkListItem
+ *
+ * Gets the position in the model that @self currently displays.
+ * If @self is unbound, 0 is returned.
+ *
+ * Returns: The position of this item
+ **/
+guint
+gtk_list_item_get_position (GtkListItem *self)
+{
+  g_return_val_if_fail (GTK_IS_LIST_ITEM (self), 0);
+
+  return self->position;
+}
+
 void
 gtk_list_item_set_child (GtkListItem *self,
                          GtkWidget   *child)
@@ -184,28 +219,33 @@ gtk_list_item_set_child (GtkListItem *self,
 }
 
 void
-gtk_list_item_bind (GtkListItem *self,
-                    gpointer     item)
+gtk_list_item_set_item (GtkListItem *self,
+                        gpointer     item)
 {
   g_return_if_fail (GTK_IS_LIST_ITEM (self));
-  g_return_if_fail (G_IS_OBJECT (item));
-  /* Must unbind before rebinding */
-  g_return_if_fail (self->item == NULL);
+  g_return_if_fail (item == NULL || G_IS_OBJECT (item));
 
-  self->item = g_object_ref (item);
+  if (self->item == item)
+    return;
+
+  g_clear_object (&self->item);
+  if (item)
+    self->item = g_object_ref (item);
 
   g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ITEM]);
 }
 
 void
-gtk_list_item_unbind (GtkListItem *self)
+gtk_list_item_set_position (GtkListItem *self,
+                            guint        position)
 {
   g_return_if_fail (GTK_IS_LIST_ITEM (self));
-  /* Must be bound */
-  g_return_if_fail (self->item != NULL);
 
-  g_clear_object (&self->item);
+  if (self->position == position)
+    return;
 
-  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ITEM]);
+  self->position = position;
+
+  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_POSITION]);
 }
 
diff --git a/gtk/gtklistitem.h b/gtk/gtklistitem.h
index bd09ba028b..5e977f35a5 100644
--- a/gtk/gtklistitem.h
+++ b/gtk/gtklistitem.h
@@ -35,6 +35,8 @@ G_DECLARE_FINAL_TYPE (GtkListItem, gtk_list_item, GTK, LIST_ITEM, GtkBin)
 
 GDK_AVAILABLE_IN_ALL
 gpointer        gtk_list_item_get_item                          (GtkListItem            *self);
+GDK_AVAILABLE_IN_ALL
+guint           gtk_list_item_get_position                      (GtkListItem            *self);
 
 G_END_DECLS
 
diff --git a/gtk/gtklistitemfactory.c b/gtk/gtklistitemfactory.c
index 9df1165eeb..ba1f0c81cd 100644
--- a/gtk/gtklistitemfactory.c
+++ b/gtk/gtklistitemfactory.c
@@ -105,14 +105,31 @@ gtk_list_item_factory_create (GtkListItemFactory *self)
 void
 gtk_list_item_factory_bind (GtkListItemFactory *self,
                             GtkListItem        *list_item,
+                            guint               position,
                             gpointer            item)
 {
   g_return_if_fail (GTK_IS_LIST_ITEM_FACTORY (self));
   g_return_if_fail (GTK_IS_LIST_ITEM (list_item));
 
-  gtk_list_item_bind (list_item, item);
+  g_object_freeze_notify (G_OBJECT (list_item));
+
+  gtk_list_item_set_item (list_item, item);
+  gtk_list_item_set_position (list_item, position);
 
   self->bind_func (gtk_bin_get_child (GTK_BIN (list_item)), item, self->user_data);
+
+  g_object_thaw_notify (G_OBJECT (list_item));
+}
+
+void
+gtk_list_item_factory_update (GtkListItemFactory *self,
+                              GtkListItem        *list_item,
+                              guint               position)
+{
+  g_return_if_fail (GTK_IS_LIST_ITEM_FACTORY (self));
+  g_return_if_fail (GTK_IS_LIST_ITEM (list_item));
+
+  gtk_list_item_set_position (list_item, position);
 }
 
 void
@@ -122,5 +139,10 @@ gtk_list_item_factory_unbind (GtkListItemFactory *self,
   g_return_if_fail (GTK_IS_LIST_ITEM_FACTORY (self));
   g_return_if_fail (GTK_IS_LIST_ITEM (list_item));
 
-  gtk_list_item_unbind (list_item);
+  g_object_freeze_notify (G_OBJECT (list_item));
+
+  gtk_list_item_set_item (list_item, NULL);
+  gtk_list_item_set_position (list_item, 0);
+
+  g_object_thaw_notify (G_OBJECT (list_item));
 }
diff --git a/gtk/gtklistitemfactoryprivate.h b/gtk/gtklistitemfactoryprivate.h
index a6bec12faf..21bc5b5586 100644
--- a/gtk/gtklistitemfactoryprivate.h
+++ b/gtk/gtklistitemfactoryprivate.h
@@ -47,7 +47,11 @@ GtkListItem *           gtk_list_item_factory_create            (GtkListItemFact
 
 void                    gtk_list_item_factory_bind              (GtkListItemFactory     *self,
                                                                  GtkListItem            *list_item,
+                                                                 guint                   position,
                                                                  gpointer                item);
+void                    gtk_list_item_factory_update            (GtkListItemFactory     *self,
+                                                                 GtkListItem            *list_item,
+                                                                 guint                   position);
 void                    gtk_list_item_factory_unbind            (GtkListItemFactory     *self,
                                                                  GtkListItem            *list_item);
 
diff --git a/gtk/gtklistitemmanager.c b/gtk/gtklistitemmanager.c
index a88502efbb..d365137d18 100644
--- a/gtk/gtklistitemmanager.c
+++ b/gtk/gtklistitemmanager.c
@@ -236,7 +236,7 @@ gtk_list_item_manager_acquire_list_item (GtkListItemManager *self,
   result = gtk_list_item_factory_create (self->factory);
 
   item = g_list_model_get_item (self->model, position);
-  gtk_list_item_factory_bind (self->factory, result, item);
+  gtk_list_item_factory_bind (self->factory, result, position, item);
   g_object_unref (item);
   gtk_widget_insert_before (GTK_WIDGET (result), self->widget, next_sibling);
 
@@ -274,6 +274,7 @@ gtk_list_item_manager_try_reacquire_list_item (GtkListItemManager       *self,
   item = g_list_model_get_item (self->model, position);
   if (g_hash_table_steal_extended (change->items, item, NULL, (gpointer *) &result))
     {
+      gtk_list_item_factory_update (self->factory, result, position);
       gtk_widget_insert_before (GTK_WIDGET (result), self->widget, next_sibling);
       /* XXX: Should we let the listview do this? */
       gtk_widget_queue_resize (GTK_WIDGET (result));
@@ -287,6 +288,26 @@ gtk_list_item_manager_try_reacquire_list_item (GtkListItemManager       *self,
   return GTK_WIDGET (result);
 }
 
+/**
+ * gtk_list_item_manager_update_list_item:
+ * @self: a #GtkListItemManager
+ * @item: a #GtkListItem that has been acquired
+ * @position: the new position of that list item
+ *
+ * Updates the position of the given @item. This function must be called whenever
+ * the position of an item changes, like when new items are added before it.
+ **/
+void
+gtk_list_item_manager_update_list_item (GtkListItemManager *self,
+                                        GtkWidget          *item,
+                                        guint               position)
+{
+  g_return_if_fail (GTK_IS_LIST_ITEM_MANAGER (self));
+  g_return_if_fail (GTK_IS_LIST_ITEM (item));
+
+  gtk_list_item_factory_update (self->factory, GTK_LIST_ITEM (item), position);
+}
+
 /*
  * gtk_list_item_manager_release_list_item:
  * @self: a #GtkListItemManager
diff --git a/gtk/gtklistitemmanagerprivate.h b/gtk/gtklistitemmanagerprivate.h
index 2af8e8fae7..d1cdfaf819 100644
--- a/gtk/gtklistitemmanagerprivate.h
+++ b/gtk/gtklistitemmanagerprivate.h
@@ -63,6 +63,9 @@ GtkWidget *             gtk_list_item_manager_try_reacquire_list_item
                                                                  GtkListItemManagerChange *change,
                                                                  guint                   position,
                                                                  GtkWidget              *next_sibling);
+void                    gtk_list_item_manager_update_list_item  (GtkListItemManager     *self,
+                                                                 GtkWidget              *item,
+                                                                 guint                   position);
 void                    gtk_list_item_manager_release_list_item (GtkListItemManager     *self,
                                                                  GtkListItemManagerChange *change,
                                                                  GtkWidget              *widget);
diff --git a/gtk/gtklistitemprivate.h b/gtk/gtklistitemprivate.h
index 254f8ae72f..a4bcd9218a 100644
--- a/gtk/gtklistitemprivate.h
+++ b/gtk/gtklistitemprivate.h
@@ -29,9 +29,10 @@ GtkWidget *     gtk_list_item_new                               (const char
 void            gtk_list_item_set_child                         (GtkListItem            *item,
                                                                  GtkWidget              *child);
 
-void            gtk_list_item_bind                              (GtkListItem            *self,
+void            gtk_list_item_set_item                          (GtkListItem            *self,
                                                                  gpointer                item);
-void            gtk_list_item_unbind                            (GtkListItem            *self);
+void            gtk_list_item_set_position                      (GtkListItem            *self,
+                                                                 guint                   position);
 
 G_END_DECLS
 
diff --git a/gtk/gtklistview.c b/gtk/gtklistview.c
index 4619ac7fd2..8234f62219 100644
--- a/gtk/gtklistview.c
+++ b/gtk/gtklistview.c
@@ -509,6 +509,22 @@ gtk_list_view_add_rows (GtkListView              *self,
   gtk_widget_queue_resize (GTK_WIDGET (self));
 }
 
+static void
+gtk_list_view_update_rows (GtkListView *self,
+                           guint        position)
+{
+  ListRow *row;
+
+  for (row = gtk_list_view_get_row (self, position, NULL);
+       row;
+       row = gtk_rb_tree_node_get_next (row))
+    {
+      gtk_list_item_manager_update_list_item (self->item_manager, row->widget, position);
+
+      position++;
+    }
+}
+
 static void
 gtk_list_view_model_items_changed_cb (GListModel  *model,
                                       guint        position,
@@ -522,6 +538,8 @@ gtk_list_view_model_items_changed_cb (GListModel  *model,
 
   gtk_list_view_remove_rows (self, change, position, removed);
   gtk_list_view_add_rows (self, change, position, added);
+  if (removed != added)
+    gtk_list_view_update_rows (self, position + added);
 
   gtk_list_item_manager_end_change (self->item_manager, change);
 }


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