diff options
author | António Fernandes <antoniof@gnome.org> | 2022-04-06 01:22:53 +0100 |
---|---|---|
committer | António Fernandes <antoniof@gnome.org> | 2023-01-10 01:11:33 +0000 |
commit | d5ce96240ac0fc55ec64d3fcf756c84502f4e441 (patch) | |
tree | 3fc0ea463a1e3d6ed4ed05ace82021c29ad6098c | |
parent | 510a4202d25b82323a1307918b382aab603190eb (diff) | |
download | nautilus-d5ce96240ac0fc55ec64d3fcf756c84502f4e441.tar.gz |
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.
-rw-r--r-- | src/nautilus-grid-view.c | 11 | ||||
-rw-r--r-- | src/nautilus-list-base-private.h | 3 | ||||
-rw-r--r-- | src/nautilus-list-base.c | 13 | ||||
-rw-r--r-- | src/nautilus-list-view.c | 8 | ||||
-rw-r--r-- | 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 b1c32f59f..016f741ba 100644 --- a/src/nautilus-grid-view.c +++ b/src/nautilus-grid-view.c @@ -444,7 +444,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); @@ -476,9 +477,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 5f839ec9a..87e262d85 100644 --- a/src/nautilus-list-base-private.h +++ b/src/nautilus-list-base-private.h @@ -29,4 +29,7 @@ void set_directory_sort_metadata (NautilusFile *f 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 8ae48479a..f0652615a 100644 --- a/src/nautilus-list-base.c +++ b/src/nautilus-list-base.c @@ -125,7 +125,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 char * @@ -886,12 +889,14 @@ void setup_cell_common (GtkListItem *listitem, NautilusViewCell *cell) { + GtkExpression *expression; GtkEventController *controller; GtkDropTarget *drop_target; - 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 f88f525a0..4a7f331f7 100644 --- a/src/nautilus-list-view.c +++ b/src/nautilus-list-view.c @@ -829,9 +829,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 */ - model = nautilus_list_base_get_model (NAUTILUS_LIST_BASE (self)); - nautilus_view_model_set_sorter (model, nautilus_view_model_get_sorter (model)); + nautilus_view_model_sort (nautilus_list_base_get_model (NAUTILUS_LIST_BASE (self))); } static guint @@ -923,7 +921,7 @@ bind_name_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); nautilus_view_item_set_item_ui (item, gtk_list_item_get_child (listitem)); @@ -949,7 +947,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 3642d39f8..ba75d1649 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 @@ -338,7 +336,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 |