[nautilus/wip/antoniof/flow-box-preparation: 17/29] view-model: Implement GListModel directly




commit 063e5f82d072df9d8807e2bea9430dbdbc21eca8
Author: António Fernandes <antoniof gnome org>
Date:   Tue Aug 17 19:42:42 2021 +0100

    view-model: Implement GListModel directly
    
    It costs us very little because we already hold an internal model, so
    it's easy to wrap it.
    
    This makes it a starting point for some day using GTK4's list widgets,
    which take a GListModel.

 src/nautilus-view-icon-controller.c |  8 ++--
 src/nautilus-view-icon-ui.c         |  8 +---
 src/nautilus-view-model.c           | 80 ++++++++++++++++++++++++++++++-------
 src/nautilus-view-model.h           |  1 -
 4 files changed, 71 insertions(+), 26 deletions(-)
---
diff --git a/src/nautilus-view-icon-controller.c b/src/nautilus-view-icon-controller.c
index 5e528e032..f4d9ee14a 100644
--- a/src/nautilus-view-icon-controller.c
+++ b/src/nautilus-view-icon-controller.c
@@ -300,7 +300,7 @@ real_is_empty (NautilusFilesView *files_view)
 {
     NautilusViewIconController *self = NAUTILUS_VIEW_ICON_CONTROLLER (files_view);
 
-    return g_list_model_get_n_items (G_LIST_MODEL (nautilus_view_model_get_g_model (self->model))) == 0;
+    return g_list_model_get_n_items (G_LIST_MODEL (self->model)) == 0;
 }
 
 static void
@@ -318,12 +318,12 @@ real_remove_file (NautilusFilesView *files_view,
     NautilusViewItemModel *current_item_model;
     guint i = 0;
 
-    while ((current_item_model = NAUTILUS_VIEW_ITEM_MODEL (g_list_model_get_item (G_LIST_MODEL 
(nautilus_view_model_get_g_model (self->model)), i))))
+    while ((current_item_model = NAUTILUS_VIEW_ITEM_MODEL (g_list_model_get_item (G_LIST_MODEL 
(self->model), i))))
     {
         current_file = nautilus_view_item_model_get_file (current_item_model);
         if (current_file == file)
         {
-            g_list_store_remove (nautilus_view_model_get_g_model (self->model), i);
+            nautilus_view_model_remove_item (self->model, current_item_model);
             break;
         }
         i++;
@@ -562,7 +562,7 @@ set_icon_size (NautilusViewIconController *self,
     NautilusViewItemModel *current_item_model;
     guint i = 0;
 
-    while ((current_item_model = NAUTILUS_VIEW_ITEM_MODEL (g_list_model_get_item (G_LIST_MODEL 
(nautilus_view_model_get_g_model (self->model)), i))))
+    while ((current_item_model = NAUTILUS_VIEW_ITEM_MODEL (g_list_model_get_item (G_LIST_MODEL 
(self->model), i))))
     {
         nautilus_view_item_model_set_icon_size (current_item_model,
                                                 get_icon_size_for_zoom_level (self->zoom_level));
diff --git a/src/nautilus-view-icon-ui.c b/src/nautilus-view-icon-ui.c
index 04f7bd4e7..15656d321 100644
--- a/src/nautilus-view-icon-ui.c
+++ b/src/nautilus-view-icon-ui.c
@@ -104,12 +104,10 @@ nautilus_view_icon_ui_set_selection (NautilusViewIconUi *self,
 {
     NautilusViewItemModel *item_model;
     NautilusViewModel *model;
-    GListStore *gmodel;
     gint i = 0;
 
     model = nautilus_view_icon_controller_get_model (self->controller);
-    gmodel = nautilus_view_model_get_g_model (model);
-    while ((item_model = NAUTILUS_VIEW_ITEM_MODEL (g_list_model_get_item (G_LIST_MODEL (gmodel), i))))
+    while ((item_model = NAUTILUS_VIEW_ITEM_MODEL (g_list_model_get_item (G_LIST_MODEL (model), i))))
     {
         GtkWidget *item_ui;
 
@@ -203,7 +201,6 @@ constructed (GObject *object)
 {
     NautilusViewIconUi *self = NAUTILUS_VIEW_ICON_UI (object);
     NautilusViewModel *model;
-    GListStore *gmodel;
 
     G_OBJECT_CLASS (nautilus_view_icon_ui_parent_class)->constructed (object);
 
@@ -219,9 +216,8 @@ constructed (GObject *object)
     gtk_widget_set_margin_end (GTK_WIDGET (self), 10);
 
     model = nautilus_view_icon_controller_get_model (self->controller);
-    gmodel = nautilus_view_model_get_g_model (model);
     gtk_flow_box_bind_model (GTK_FLOW_BOX (self),
-                             G_LIST_MODEL (gmodel),
+                             G_LIST_MODEL (model),
                              create_widget_func, self, NULL);
 
     g_signal_connect (self, "child-activated", (GCallback) on_child_activated, self);
diff --git a/src/nautilus-view-model.c b/src/nautilus-view-model.c
index 12db63a30..e2c5caeca 100644
--- a/src/nautilus-view-model.c
+++ b/src/nautilus-view-model.c
@@ -11,16 +11,75 @@ struct _NautilusViewModel
     NautilusViewModelSortData *sort_data;
 };
 
-G_DEFINE_TYPE (NautilusViewModel, nautilus_view_model, G_TYPE_OBJECT)
+static GType
+nautilus_view_model_get_item_type (GListModel *list)
+{
+    return NAUTILUS_TYPE_VIEW_ITEM_MODEL;
+}
+
+static guint
+nautilus_view_model_get_n_items (GListModel *list)
+{
+    NautilusViewModel *self = NAUTILUS_VIEW_MODEL (list);
+
+    if (self->internal_model == NULL)
+    {
+        return 0;
+    }
+
+    return g_list_model_get_n_items (G_LIST_MODEL (self->internal_model));
+}
+
+static gpointer
+nautilus_view_model_get_item (GListModel *list,
+                              guint       position)
+{
+    NautilusViewModel *self = NAUTILUS_VIEW_MODEL (list);
+
+    if (self->internal_model == NULL)
+    {
+        return NULL;
+    }
+
+    return g_list_model_get_item (G_LIST_MODEL (self->internal_model), position);
+}
+
+static void
+nautilus_view_model_list_model_init (GListModelInterface *iface)
+{
+    iface->get_item_type = nautilus_view_model_get_item_type;
+    iface->get_n_items = nautilus_view_model_get_n_items;
+    iface->get_item = nautilus_view_model_get_item;
+}
+
+G_DEFINE_TYPE_WITH_CODE (NautilusViewModel, nautilus_view_model, G_TYPE_OBJECT,
+                         G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL,
+                                                nautilus_view_model_list_model_init))
 
 enum
 {
     PROP_0,
     PROP_SORT_TYPE,
-    PROP_G_MODEL,
     N_PROPS
 };
 
+static void
+dispose (GObject *object)
+{
+    NautilusViewModel *self = NAUTILUS_VIEW_MODEL (object);
+
+    if (self->internal_model != NULL)
+    {
+        g_signal_handlers_disconnect_by_func (self->internal_model,
+                                              g_list_model_items_changed,
+                                              self);
+        g_object_unref (self->internal_model);
+        self->internal_model = NULL;
+    }
+
+    G_OBJECT_CLASS (nautilus_view_model_parent_class)->dispose (object);
+}
+
 static void
 finalize (GObject *object)
 {
@@ -33,7 +92,6 @@ finalize (GObject *object)
     {
         g_free (self->sort_data);
     }
-    g_object_unref (self->internal_model);
 }
 
 static void
@@ -52,12 +110,6 @@ get_property (GObject    *object,
         }
         break;
 
-        case PROP_G_MODEL:
-        {
-            g_value_set_object (value, self->internal_model);
-        }
-        break;
-
         default:
         {
             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -97,6 +149,9 @@ constructed (GObject *object)
 
     self->internal_model = g_list_store_new (NAUTILUS_TYPE_VIEW_ITEM_MODEL);
     self->map_files_to_model = g_hash_table_new (NULL, NULL);
+
+    g_signal_connect_swapped (self->internal_model, "items-changed",
+                              G_CALLBACK (g_list_model_items_changed), self);
 }
 
 static void
@@ -104,6 +159,7 @@ nautilus_view_model_class_init (NautilusViewModelClass *klass)
 {
     GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
+    object_class->dispose = dispose;
     object_class->finalize = finalize;
     object_class->get_property = get_property;
     object_class->set_property = set_property;
@@ -162,12 +218,6 @@ nautilus_view_model_get_sort_type (NautilusViewModel *self)
     return self->sort_data;
 }
 
-GListStore *
-nautilus_view_model_get_g_model (NautilusViewModel *self)
-{
-    return self->internal_model;
-}
-
 GQueue *
 nautilus_view_model_get_items_from_files (NautilusViewModel *self,
                                           GQueue            *files)
diff --git a/src/nautilus-view-model.h b/src/nautilus-view-model.h
index 4c16e82db..11c5a0545 100644
--- a/src/nautilus-view-model.h
+++ b/src/nautilus-view-model.h
@@ -22,7 +22,6 @@ NautilusViewModel * nautilus_view_model_new (void);
 void nautilus_view_model_set_sort_type (NautilusViewModel         *self,
                                         NautilusViewModelSortData *sort_data);
 NautilusViewModelSortData * nautilus_view_model_get_sort_type (NautilusViewModel *self);
-GListStore * nautilus_view_model_get_g_model (NautilusViewModel *self);
 NautilusViewItemModel * nautilus_view_model_get_item_from_file (NautilusViewModel *self,
                                                                 NautilusFile      *file);
 GQueue * nautilus_view_model_get_items_from_files (NautilusViewModel *self,


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