[nautilus/wip/antoniof/new-list-view-continuation: 23/27] view-model: Expose row objects to the view




commit 8c323a8e8234b476402989f67937553136602b55
Author: António Fernandes <antoniof gnome org>
Date:   Wed Apr 6 01:22:53 2022 +0100

    view-model: Expose row objects to the view
    
    This is going to allow us to reuse the model for a GtkColumnView-based
    list view with expanding trees support.
    
    As documented, GtkTreeViewRow:item may be NULL when the item it held
    is destroyed. So, we must take care to handle the NULL case when
    expected and also warn and return when not expected.

 src/nautilus-grid-view.c         | 11 ++++++++---
 src/nautilus-list-base-private.h |  3 +++
 src/nautilus-list-base.c         | 13 +++++++++----
 src/nautilus-list-view.c         |  8 +++-----
 src/nautilus-view-model.c        |  8 +++-----
 5 files changed, 26 insertions(+), 17 deletions(-)
---
diff --git a/src/nautilus-grid-view.c b/src/nautilus-grid-view.c
index 90f878d52..6c44bcc90 100644
--- a/src/nautilus-grid-view.c
+++ b/src/nautilus-grid-view.c
@@ -328,7 +328,8 @@ bind_cell (GtkSignalListItemFactory *factory,
     NautilusViewItem *item;
 
     cell = gtk_list_item_get_child (listitem);
-    item = NAUTILUS_VIEW_ITEM (gtk_list_item_get_item (listitem));
+    item = listitem_get_view_item (listitem);
+    g_return_if_fail (item != NULL);
 
     nautilus_view_item_set_item_ui (item, cell);
 
@@ -356,9 +357,13 @@ unbind_cell (GtkSignalListItemFactory *factory,
 {
     NautilusViewItem *item;
 
-    item = NAUTILUS_VIEW_ITEM (gtk_list_item_get_item (listitem));
+    item = listitem_get_view_item (listitem);
 
-    nautilus_view_item_set_item_ui (item, NULL);
+    /* item may be NULL when row has just been destroyed. */
+    if (item != NULL)
+    {
+        nautilus_view_item_set_item_ui (item, NULL);
+    }
 }
 
 static void
diff --git a/src/nautilus-list-base-private.h b/src/nautilus-list-base-private.h
index 0d5bbc7b5..3175ac373 100644
--- a/src/nautilus-list-base-private.h
+++ b/src/nautilus-list-base-private.h
@@ -30,4 +30,7 @@ const NautilusFileSortType    get_sorts_type_from_metadata_text (const char   *m
 void                          setup_cell_common                 (GtkListItem      *listitem,
                                                                  NautilusViewCell *cell);
 
+#define listitem_get_view_item(li) \
+(NAUTILUS_VIEW_ITEM (gtk_tree_list_row_get_item (GTK_TREE_LIST_ROW (gtk_list_item_get_item (li)))))
+
 G_END_DECLS
diff --git a/src/nautilus-list-base.c b/src/nautilus-list-base.c
index 135aa35f6..1b1d21f75 100644
--- a/src/nautilus-list-base.c
+++ b/src/nautilus-list-base.c
@@ -97,7 +97,10 @@ static inline NautilusViewItem *
 get_view_item (GListModel *model,
                guint       position)
 {
-    return NAUTILUS_VIEW_ITEM (g_list_model_get_item (model, position));
+    g_autoptr (GtkTreeListRow) row = g_list_model_get_item (model, position);
+
+    g_return_val_if_fail (GTK_IS_TREE_LIST_ROW (row), NULL);
+    return NAUTILUS_VIEW_ITEM (gtk_tree_list_row_get_item (GTK_TREE_LIST_ROW (row)));
 }
 
 static const SortConstants *
@@ -458,11 +461,13 @@ void
 setup_cell_common (GtkListItem      *listitem,
                    NautilusViewCell *cell)
 {
+    GtkExpression *expression;
     GtkEventController *controller;
 
-    g_object_bind_property (listitem, "item",
-                            cell, "item",
-                            G_BINDING_SYNC_CREATE);
+    expression = gtk_property_expression_new (GTK_TYPE_LIST_ITEM, NULL, "item");
+    expression = gtk_property_expression_new (GTK_TYPE_TREE_LIST_ROW, expression, "item");
+    gtk_expression_bind (expression, cell, "item", listitem);
+
     gtk_list_item_set_child (listitem, GTK_WIDGET (cell));
 
     controller = GTK_EVENT_CONTROLLER (gtk_gesture_click_new ());
diff --git a/src/nautilus-list-view.c b/src/nautilus-list-view.c
index c0c5a286e..06ab85316 100644
--- a/src/nautilus-list-view.c
+++ b/src/nautilus-list-view.c
@@ -800,9 +800,7 @@ real_sort_directories_first_changed (NautilusFilesView *files_view)
 
     self->directories_first = nautilus_files_view_should_sort_directories_first (NAUTILUS_FILES_VIEW (self));
 
-    /* Reset the sorter to trigger ressorting */
-    nautilus_view_model_set_sorter (nautilus_list_base_get_model (NAUTILUS_LIST_BASE (self)),
-                                    gtk_column_view_get_sorter (self->view_ui));
+    nautilus_view_model_sort (nautilus_list_base_get_model (NAUTILUS_LIST_BASE (self)));
 }
 
 static void
@@ -908,7 +906,7 @@ bind_name_cell (GtkSignalListItemFactory *factory,
 {
     NautilusViewItem *item;
 
-    item = NAUTILUS_VIEW_ITEM (gtk_list_item_get_item (listitem));
+    item = listitem_get_view_item (listitem);
 
     nautilus_view_item_set_item_ui (item, gtk_list_item_get_child (listitem));
 }
@@ -920,7 +918,7 @@ unbind_name_cell (GtkSignalListItemFactory *factory,
 {
     NautilusViewItem *item;
 
-    item = NAUTILUS_VIEW_ITEM (gtk_list_item_get_item (listitem));
+    item = listitem_get_view_item (listitem);
     g_return_if_fail (NAUTILUS_IS_VIEW_ITEM (item));
 
     nautilus_view_item_set_item_ui (item, NULL);
diff --git a/src/nautilus-view-model.c b/src/nautilus-view-model.c
index a9a20e603..194ec958f 100644
--- a/src/nautilus-view-model.c
+++ b/src/nautilus-view-model.c
@@ -49,7 +49,7 @@ get_child_row (NautilusViewModel *self,
 static GType
 nautilus_view_model_get_item_type (GListModel *list)
 {
-    return NAUTILUS_TYPE_VIEW_ITEM;
+    return GTK_TYPE_TREE_LIST_ROW;
 }
 
 static guint
@@ -70,15 +70,13 @@ nautilus_view_model_get_item (GListModel *list,
                               guint       position)
 {
     NautilusViewModel *self = NAUTILUS_VIEW_MODEL (list);
-    g_autoptr (GtkTreeListRow) row = NULL;
 
     if (self->sort_model == NULL)
     {
         return NULL;
     }
 
-    row = g_list_model_get_item (G_LIST_MODEL (self->sort_model), position);
-    return gtk_tree_list_row_get_item (row);
+    return g_list_model_get_item (G_LIST_MODEL (self->sort_model), position);
 }
 
 static void
@@ -329,7 +327,7 @@ nautilus_view_model_get_sorter (NautilusViewModel *self)
 
     row_sorter = GTK_TREE_LIST_ROW_SORTER (gtk_sort_list_model_get_sorter (self->sort_model));
 
-    return gtk_tree_list_row_sorter_get_sorter (row_sorter);
+    return row_sorter != NULL ? gtk_tree_list_row_sorter_get_sorter (row_sorter) : NULL;
 }
 
 void


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