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




commit f653836be11c66b4e30ff04725d88bad6016d172
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-files-model-view.c |  5 ++++-
 src/nautilus-files-model-view.h |  3 +++
 src/nautilus-grid-view.c        | 11 ++++++++---
 src/nautilus-list-view.c        | 18 ++++++++----------
 src/nautilus-view-model.c       |  8 +++-----
 5 files changed, 26 insertions(+), 19 deletions(-)
---
diff --git a/src/nautilus-files-model-view.c b/src/nautilus-files-model-view.c
index 82f2cefea..f0abf961b 100644
--- a/src/nautilus-files-model-view.c
+++ b/src/nautilus-files-model-view.c
@@ -72,7 +72,10 @@ static inline NautilusViewItemModel *
 get_view_item (GListModel *model,
                guint       position)
 {
-    return NAUTILUS_VIEW_ITEM_MODEL (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_MODEL (gtk_tree_list_row_get_item (GTK_TREE_LIST_ROW (row)));
 }
 
 static const SortConstants *
diff --git a/src/nautilus-files-model-view.h b/src/nautilus-files-model-view.h
index b7e50c962..1de12ba2a 100644
--- a/src/nautilus-files-model-view.h
+++ b/src/nautilus-files-model-view.h
@@ -38,4 +38,7 @@ void                          set_directory_sort_metadata       (NautilusFile *f
                                                                  gboolean      reversed);
 const NautilusFileSortType    get_sorts_type_from_metadata_text (const char   *metadata_name);
 
+#define listitem_get_view_item(li) \
+(NAUTILUS_VIEW_ITEM_MODEL (gtk_tree_list_row_get_item (GTK_TREE_LIST_ROW (gtk_list_item_get_item (li)))))
+
 G_END_DECLS
diff --git a/src/nautilus-grid-view.c b/src/nautilus-grid-view.c
index 15d18f1c7..ffaec95ac 100644
--- a/src/nautilus-grid-view.c
+++ b/src/nautilus-grid-view.c
@@ -553,7 +553,8 @@ bind_item_ui (GtkSignalListItemFactory *factory,
     NautilusViewItemModel *item_model;
 
     item_ui = gtk_list_item_get_child (listitem);
-    item_model = NAUTILUS_VIEW_ITEM_MODEL (gtk_list_item_get_item (listitem));
+    item_model = listitem_get_view_item (listitem);
+    g_return_if_fail (item_model != NULL);
 
     nautilus_grid_view_item_ui_set_model (NAUTILUS_GRID_VIEW_ITEM_UI (item_ui),
                                           item_model);
@@ -585,10 +586,14 @@ unbind_item_ui (GtkSignalListItemFactory *factory,
     NautilusViewItemModel *item_model;
 
     item_ui = NAUTILUS_GRID_VIEW_ITEM_UI (gtk_list_item_get_child (listitem));
-    item_model = NAUTILUS_VIEW_ITEM_MODEL (gtk_list_item_get_item (listitem));
+    item_model = listitem_get_view_item (listitem);
 
     nautilus_grid_view_item_ui_set_model (item_ui, NULL);
-    nautilus_view_item_model_set_item_ui (item_model, NULL);
+    /* item may be NULL when row has just been destroyed. */
+    if (item_model != NULL)
+    {
+        nautilus_view_item_model_set_item_ui (item_model, NULL);
+    }
 }
 
 static void
diff --git a/src/nautilus-list-view.c b/src/nautilus-list-view.c
index fb7d27a69..eb8c7b18f 100644
--- a/src/nautilus-list-view.c
+++ b/src/nautilus-list-view.c
@@ -960,7 +960,7 @@ bind_item_ui (GtkSignalListItemFactory *factory,
     GtkWidget *row_widget;
 
     item_ui = NAUTILUS_LIST_VIEW_ITEM_UI (gtk_list_item_get_child (listitem));
-    item_model = NAUTILUS_VIEW_ITEM_MODEL (gtk_list_item_get_item (listitem));
+    item_model = listitem_get_view_item (listitem);
     g_return_if_fail (item_model != NULL);
 
     nautilus_list_view_item_ui_set_model (item_ui, item_model);
@@ -1005,7 +1005,7 @@ unbind_item_ui (GtkSignalListItemFactory *factory,
     NautilusViewItemModel *item_model;
 
     item_ui = NAUTILUS_LIST_VIEW_ITEM_UI (gtk_list_item_get_child (listitem));
-    item_model = NAUTILUS_VIEW_ITEM_MODEL (gtk_list_item_get_item (listitem));
+    item_model = listitem_get_view_item (listitem);
 
     nautilus_list_view_item_ui_set_model (item_ui, NULL);
 
@@ -1057,7 +1057,7 @@ on_star_click_released (GtkGestureClick *gesture,
     g_autofree gchar *uri = NULL;
     GtkWidget *star;
 
-    item = NAUTILUS_VIEW_ITEM_MODEL (gtk_list_item_get_item (listitem));
+    item = listitem_get_view_item (listitem);
     g_return_if_fail (item != NULL);
     file = nautilus_view_item_model_get_file (item);
     uri = nautilus_file_get_uri (file);
@@ -1106,7 +1106,7 @@ on_starred_changed (NautilusTagManager *tag_manager,
 {
     NautilusViewItemModel *item;
 
-    item = NAUTILUS_VIEW_ITEM_MODEL (gtk_list_item_get_item (listitem));
+    item = listitem_get_view_item (listitem);
     if (item != NULL &&
         g_list_find (changed_files, nautilus_view_item_model_get_file (item)))
     {
@@ -1122,7 +1122,7 @@ bind_star (GtkSignalListItemFactory *factory,
     NautilusViewItemModel *item;
     GtkImage *star;
 
-    item = NAUTILUS_VIEW_ITEM_MODEL (gtk_list_item_get_item (listitem));
+    item = listitem_get_view_item (listitem);
     g_return_if_fail (item != NULL);
     star = GTK_IMAGE (gtk_list_item_get_child (listitem));
 
@@ -1179,7 +1179,7 @@ bind_label (GtkSignalListItemFactory *factory,
     NautilusViewItemModel *item;
     GtkLabel *label;
 
-    item = NAUTILUS_VIEW_ITEM_MODEL (gtk_list_item_get_item (listitem));
+    item = listitem_get_view_item (listitem);
     g_return_if_fail (item != NULL);
     label = GTK_LABEL (gtk_list_item_get_child (listitem));
 
@@ -1195,7 +1195,7 @@ unbind_label (GtkSignalListItemFactory *factory,
     NautilusViewItemModel *item;
     GtkLabel *label;
 
-    item = NAUTILUS_VIEW_ITEM_MODEL (gtk_list_item_get_item (listitem));
+    item = listitem_get_view_item (listitem);
 
     /* item may be NULL when row has just been destroyed. */
     if (item != NULL)
@@ -1438,9 +1438,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_files_model_view_get_model (NAUTILUS_FILES_MODEL_VIEW (self)),
-                                    gtk_column_view_get_sorter (self->view_ui));
+    nautilus_view_model_sort (nautilus_files_model_view_get_model (NAUTILUS_FILES_MODEL_VIEW (self)));
 }
 
 static void
diff --git a/src/nautilus-view-model.c b/src/nautilus-view-model.c
index 22d62a90b..3cd43cb6e 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_MODEL;
+    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]