[gtk/wip/otte/listview] listview: Try to keep the list items in order when scrolling
- From: Benjamin Otte <otte src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/wip/otte/listview] listview: Try to keep the list items in order when scrolling
- Date: Fri, 28 Sep 2018 02:19:37 +0000 (UTC)
commit c5749f485ade4fb4c33f1960eabbc3c988dc39e6
Author: Benjamin Otte <otte redhat com>
Date: Fri Sep 28 03:33:16 2018 +0200
listview: Try to keep the list items in order when scrolling
Instead of just destroying all items and then recreating them (or even
hide()ing and then show()ing them again (or even even repositioning
them in the widget tree)), just try to reust them in the order they are.
This works surprisingly well when scrolling and most/all widgets
just moved.
gtk/gtklistitemmanager.c | 29 +++++++++++++++++++++++++++++
gtk/gtklistitemmanagerprivate.h | 4 ++++
gtk/gtklistview.c | 34 +++++++++++++++++++++++++---------
3 files changed, 58 insertions(+), 9 deletions(-)
---
diff --git a/gtk/gtklistitemmanager.c b/gtk/gtklistitemmanager.c
index 63ed592ccd..3d7eec97e8 100644
--- a/gtk/gtklistitemmanager.c
+++ b/gtk/gtklistitemmanager.c
@@ -21,6 +21,8 @@
#include "gtklistitemmanagerprivate.h"
+#include "gtkwidgetprivate.h"
+
struct _GtkListItemManager
{
GObject parent_instance;
@@ -318,6 +320,33 @@ gtk_list_item_manager_try_reacquire_list_item (GtkListItemManager *self,
return GTK_WIDGET (result);
}
+/**
+ * gtk_list_item_manager_move_list_item:
+ * @self: a #GtkListItemManager
+ * @list_item: an acquired #GtkListItem that should be moved to represent
+ * a different row
+ * @position: the new position of that list item
+ * @prev_sibling: the new previous sibling
+ *
+ * Moves the widget to represent a new position in the listmodel without
+ * releasing the item.
+ *
+ * This is most useful when scrolling.
+ **/
+void
+gtk_list_item_manager_move_list_item (GtkListItemManager *self,
+ GtkWidget *list_item,
+ guint position,
+ GtkWidget *prev_sibling)
+{
+ gpointer item;
+
+ item = g_list_model_get_item (self->model, position);
+ gtk_list_item_factory_bind (self->factory, GTK_LIST_ITEM (list_item), position, item);
+ gtk_widget_insert_after (list_item, _gtk_widget_get_parent (list_item), prev_sibling);
+ g_object_unref (item);
+}
+
/**
* gtk_list_item_manager_update_list_item:
* @self: a #GtkListItemManager
diff --git a/gtk/gtklistitemmanagerprivate.h b/gtk/gtklistitemmanagerprivate.h
index 0f1d1bddd1..d34e346a21 100644
--- a/gtk/gtklistitemmanagerprivate.h
+++ b/gtk/gtklistitemmanagerprivate.h
@@ -69,6 +69,10 @@ GtkWidget * gtk_list_item_manager_try_reacquire_list_item
void gtk_list_item_manager_update_list_item (GtkListItemManager *self,
GtkWidget *item,
guint position);
+void gtk_list_item_manager_move_list_item (GtkListItemManager *self,
+ GtkWidget *list_item,
+ guint position,
+ GtkWidget *prev_sibling);
void gtk_list_item_manager_release_list_item (GtkListItemManager *self,
GtkListItemManagerChange *change,
GtkWidget *widget);
diff --git a/gtk/gtklistview.c b/gtk/gtklistview.c
index ea95e4e8b0..141dcff86f 100644
--- a/gtk/gtklistview.c
+++ b/gtk/gtklistview.c
@@ -327,7 +327,8 @@ gtk_list_view_merge_list_rows (GtkListView *self,
}
static void
-gtk_list_view_release_rows (GtkListView *self)
+gtk_list_view_release_rows (GtkListView *self,
+ GQueue *released)
{
ListRow *row, *prev, *next;
guint i;
@@ -338,7 +339,7 @@ gtk_list_view_release_rows (GtkListView *self)
{
if (row->widget)
{
- gtk_list_item_manager_release_list_item (self->item_manager, NULL, row->widget);
+ g_queue_push_tail (released, row->widget);
row->widget = NULL;
i++;
prev = gtk_css_rb_tree_get_previous (self->rows, row);
@@ -370,7 +371,7 @@ gtk_list_view_release_rows (GtkListView *self)
if (row->widget)
{
- gtk_list_item_manager_release_list_item (self->item_manager, NULL, row->widget);
+ g_queue_push_tail (released, row->widget);
row->widget = NULL;
prev = gtk_css_rb_tree_get_previous (self->rows, row);
if (prev && gtk_list_view_merge_list_rows (self, prev, row))
@@ -381,7 +382,7 @@ gtk_list_view_release_rows (GtkListView *self)
{
if (next->widget)
{
- gtk_list_item_manager_release_list_item (self->item_manager, NULL, next->widget);
+ g_queue_push_tail (released, next->widget);
next->widget = NULL;
}
gtk_list_view_merge_list_rows (self, row, next);
@@ -395,9 +396,10 @@ gtk_list_view_ensure_rows (GtkListView *self,
{
ListRow *row, *new_row;
guint i, offset;
- GtkWidget *insert_after;
+ GtkWidget *widget, *insert_after;
+ GQueue released = G_QUEUE_INIT;
- gtk_list_view_release_rows (self);
+ gtk_list_view_release_rows (self, &released);
row = gtk_list_view_get_row (self, self->anchor_start, &offset);
if (offset > 0)
@@ -435,9 +437,20 @@ gtk_list_view_ensure_rows (GtkListView *self,
}
if (new_row->widget == NULL)
{
- new_row->widget = gtk_list_item_manager_acquire_list_item (self->item_manager,
- i,
- insert_after);
+ new_row->widget = g_queue_pop_head (&released);
+ if (new_row->widget)
+ {
+ gtk_list_item_manager_move_list_item (self->item_manager,
+ new_row->widget,
+ i,
+ insert_after);
+ }
+ else
+ {
+ new_row->widget = gtk_list_item_manager_acquire_list_item (self->item_manager,
+ i,
+ insert_after);
+ }
}
}
else
@@ -447,6 +460,9 @@ gtk_list_view_ensure_rows (GtkListView *self,
}
insert_after = new_row->widget;
}
+
+ while ((widget = g_queue_pop_head (&released)))
+ gtk_list_item_manager_release_list_item (self->item_manager, NULL, widget);
}
static void
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]