[gnome-builder] libide/tweaks: cleanup visitor and make it more generic



commit 5dc38fac7b62f469d230ef7fc99e2e20a5f918ff
Author: Christian Hergert <chergert redhat com>
Date:   Thu Aug 4 15:28:50 2022 -0700

    libide/tweaks: cleanup visitor and make it more generic
    
    We want to be able to use this outside of just GListModel abstractions.

 src/libide/tweaks/ide-tweaks-custom.h        | 35 -------------
 src/libide/tweaks/ide-tweaks-group.c         |  4 +-
 src/libide/tweaks/ide-tweaks-init.c          |  2 +-
 src/libide/tweaks/ide-tweaks-item.c          | 40 +++++++++++++++
 src/libide/tweaks/ide-tweaks-item.h          | 72 ++++++++++++++++++---------
 src/libide/tweaks/ide-tweaks-model-private.h | 11 -----
 src/libide/tweaks/ide-tweaks-model.c         | 74 +++++++++++-----------------
 src/libide/tweaks/ide-tweaks-panel-list.c    |  4 +-
 src/libide/tweaks/ide-tweaks-panel.c         |  4 +-
 src/libide/tweaks/libide-tweaks.h            |  2 +-
 src/libide/tweaks/meson.build                |  4 +-
 11 files changed, 127 insertions(+), 125 deletions(-)
---
diff --git a/src/libide/tweaks/ide-tweaks-group.c b/src/libide/tweaks/ide-tweaks-group.c
index cae65d852..b549c70e4 100644
--- a/src/libide/tweaks/ide-tweaks-group.c
+++ b/src/libide/tweaks/ide-tweaks-group.c
@@ -22,8 +22,8 @@
 
 #include "config.h"
 
-#include "ide-tweaks-custom.h"
 #include "ide-tweaks-group.h"
+#include "ide-tweaks-widget.h"
 
 struct _IdeTweaksGroup
 {
@@ -48,7 +48,7 @@ ide_tweaks_group_accepts (IdeTweaksItem *item,
   g_assert (IDE_IS_TWEAKS_GROUP (item));
   g_assert (IDE_IS_TWEAKS_ITEM (child));
 
-  return IDE_IS_TWEAKS_CUSTOM (child);
+  return IDE_IS_TWEAKS_WIDGET (child);
 }
 
 static void
diff --git a/src/libide/tweaks/ide-tweaks-init.c b/src/libide/tweaks/ide-tweaks-init.c
index b5a925140..65fd41d3d 100644
--- a/src/libide/tweaks/ide-tweaks-init.c
+++ b/src/libide/tweaks/ide-tweaks-init.c
@@ -33,12 +33,12 @@ _ide_tweaks_init (void)
   g_resources_register (ide_tweaks_get_resource ());
 
   g_type_ensure (IDE_TYPE_TWEAKS);
-  g_type_ensure (IDE_TYPE_TWEAKS_CUSTOM);
   g_type_ensure (IDE_TYPE_TWEAKS_FACTORY);
   g_type_ensure (IDE_TYPE_TWEAKS_GROUP);
   g_type_ensure (IDE_TYPE_TWEAKS_ITEM);
   g_type_ensure (IDE_TYPE_TWEAKS_PAGE);
   g_type_ensure (IDE_TYPE_TWEAKS_SECTION);
   g_type_ensure (IDE_TYPE_TWEAKS_VARIABLE);
+  g_type_ensure (IDE_TYPE_TWEAKS_WIDGET);
   g_type_ensure (IDE_TYPE_TWEAKS_WINDOW);
 }
diff --git a/src/libide/tweaks/ide-tweaks-item.c b/src/libide/tweaks/ide-tweaks-item.c
index 97d45edd6..92a618045 100644
--- a/src/libide/tweaks/ide-tweaks-item.c
+++ b/src/libide/tweaks/ide-tweaks-item.c
@@ -762,3 +762,43 @@ ide_tweaks_item_get_ancestor (IdeTweaksItem *self,
 
   return NULL;
 }
+
+/**
+ * ide_tweaks_item_visit_children:
+ * @self: an #IdeTweaksItem
+ * @visitor: (scope call): an #IdeTweaksItemVistor to callback
+ * @visitor_data: closure data for @visitor
+ *
+ * Calls @visitor for every matching item.
+ *
+ * Based on the result of @visitor, items may be recursed into.
+ *
+ * It is an error to modify @self or any descendant from @visitor.
+ *
+ * Returns: %TRUE if %IDE_TWEAKS_ITEM_VISIT_STOP was returned; otherwise
+ *   %FALSE is returned.
+ */
+gboolean
+ide_tweaks_item_visit_children (IdeTweaksItem        *self,
+                                IdeTweaksItemVisitor  visitor,
+                                gpointer              visitor_data)
+{
+  g_return_val_if_fail (IDE_IS_TWEAKS_ITEM (self), FALSE);
+  g_return_val_if_fail (visitor != NULL, FALSE);
+
+  for (IdeTweaksItem *child = ide_tweaks_item_get_first_child (self);
+       child != NULL;
+       child = ide_tweaks_item_get_next_sibling (child))
+    {
+      IdeTweaksItemVisitResult res = visitor (child, visitor_data);
+
+      if (res == IDE_TWEAKS_ITEM_VISIT_STOP)
+        return TRUE;
+
+      if (res == IDE_TWEAKS_ITEM_VISIT_RECURSE &&
+          ide_tweaks_item_visit_children (child, visitor, visitor_data))
+        return TRUE;
+    }
+
+  return FALSE;
+}
diff --git a/src/libide/tweaks/ide-tweaks-item.h b/src/libide/tweaks/ide-tweaks-item.h
index bc832713e..8a0665573 100644
--- a/src/libide/tweaks/ide-tweaks-item.h
+++ b/src/libide/tweaks/ide-tweaks-item.h
@@ -28,11 +28,31 @@
 
 G_BEGIN_DECLS
 
+typedef enum
+{
+  IDE_TWEAKS_ITEM_VISIT_STOP = 0,
+  IDE_TWEAKS_ITEM_VISIT_CONTINUE,
+  IDE_TWEAKS_ITEM_VISIT_RECURSE,
+  IDE_TWEAKS_ITEM_VISIT_ACCEPT_AND_CONTINUE,
+} IdeTweaksItemVisitResult;
+
 #define IDE_TYPE_TWEAKS_ITEM (ide_tweaks_item_get_type())
 
 IDE_AVAILABLE_IN_ALL
 G_DECLARE_DERIVABLE_TYPE (IdeTweaksItem, ide_tweaks_item, IDE, TWEAKS_ITEM, GObject)
 
+/**
+ * IdeTweaksItemVisitor:
+ * @item: an #IdeTweaksItem being visited
+ * @user_data: user data provided
+ *
+ * Called for every matching item while visiting the item graph.
+ *
+ * Returns: an #IdeTweaksItemVisitResult
+ */
+typedef IdeTweaksItemVisitResult (*IdeTweaksItemVisitor) (IdeTweaksItem *item,
+                                                          gpointer       user_data);
+
 struct _IdeTweaksItemClass
 {
   GObjectClass parent_class;
@@ -43,44 +63,48 @@ struct _IdeTweaksItemClass
 };
 
 IDE_AVAILABLE_IN_ALL
-const char *        ide_tweaks_item_get_id               (IdeTweaksItem      *self);
+const char         *ide_tweaks_item_get_id               (IdeTweaksItem        *self);
+IDE_AVAILABLE_IN_ALL
+IdeTweaksItem      *ide_tweaks_item_copy                 (IdeTweaksItem        *self);
 IDE_AVAILABLE_IN_ALL
-IdeTweaksItem      *ide_tweaks_item_copy                 (IdeTweaksItem      *self);
+const char * const *ide_tweaks_item_get_keywords         (IdeTweaksItem        *self);
 IDE_AVAILABLE_IN_ALL
-const char * const *ide_tweaks_item_get_keywords         (IdeTweaksItem      *self);
+void                ide_tweaks_item_set_keywords         (IdeTweaksItem        *self,
+                                                          const char * const   *keywords);
 IDE_AVAILABLE_IN_ALL
-void                ide_tweaks_item_set_keywords         (IdeTweaksItem      *self,
-                                                          const char * const *keywords);
+const char         *ide_tweaks_item_get_sort_key         (IdeTweaksItem        *self);
 IDE_AVAILABLE_IN_ALL
-const char         *ide_tweaks_item_get_sort_key         (IdeTweaksItem      *self);
+void                ide_tweaks_item_set_sort_key         (IdeTweaksItem        *self,
+                                                          const char           *sort_key);
 IDE_AVAILABLE_IN_ALL
-void                ide_tweaks_item_set_sort_key         (IdeTweaksItem      *self,
-                                                          const char         *sort_key);
+gboolean            ide_tweaks_item_is_ancestor          (IdeTweaksItem        *self,
+                                                          IdeTweaksItem        *ancestor);
 IDE_AVAILABLE_IN_ALL
-gboolean            ide_tweaks_item_is_ancestor          (IdeTweaksItem      *self,
-                                                          IdeTweaksItem      *ancestor);
+IdeTweaksItem      *ide_tweaks_item_get_parent           (IdeTweaksItem        *self);
 IDE_AVAILABLE_IN_ALL
-IdeTweaksItem      *ide_tweaks_item_get_parent           (IdeTweaksItem      *self);
+IdeTweaksItem      *ide_tweaks_item_get_last_child       (IdeTweaksItem        *self);
 IDE_AVAILABLE_IN_ALL
-IdeTweaksItem      *ide_tweaks_item_get_last_child       (IdeTweaksItem      *self);
+IdeTweaksItem      *ide_tweaks_item_get_first_child      (IdeTweaksItem        *self);
 IDE_AVAILABLE_IN_ALL
-IdeTweaksItem      *ide_tweaks_item_get_first_child      (IdeTweaksItem      *self);
+IdeTweaksItem      *ide_tweaks_item_get_previous_sibling (IdeTweaksItem        *self);
 IDE_AVAILABLE_IN_ALL
-IdeTweaksItem      *ide_tweaks_item_get_previous_sibling (IdeTweaksItem      *self);
+IdeTweaksItem      *ide_tweaks_item_get_next_sibling     (IdeTweaksItem        *self);
 IDE_AVAILABLE_IN_ALL
-IdeTweaksItem      *ide_tweaks_item_get_next_sibling     (IdeTweaksItem      *self);
+void                ide_tweaks_item_insert_after         (IdeTweaksItem        *self,
+                                                          IdeTweaksItem        *parent,
+                                                          IdeTweaksItem        *previous_sibling);
 IDE_AVAILABLE_IN_ALL
-void                ide_tweaks_item_insert_after         (IdeTweaksItem      *self,
-                                                          IdeTweaksItem      *parent,
-                                                          IdeTweaksItem      *previous_sibling);
+void                ide_tweaks_item_insert_before        (IdeTweaksItem        *self,
+                                                          IdeTweaksItem        *parent,
+                                                          IdeTweaksItem        *next_sibling);
 IDE_AVAILABLE_IN_ALL
-void                ide_tweaks_item_insert_before        (IdeTweaksItem      *self,
-                                                          IdeTweaksItem      *parent,
-                                                          IdeTweaksItem      *next_sibling);
+gpointer            ide_tweaks_item_get_ancestor         (IdeTweaksItem        *self,
+                                                          GType                 ancestor_type);
 IDE_AVAILABLE_IN_ALL
-gpointer            ide_tweaks_item_get_ancestor         (IdeTweaksItem      *self,
-                                                          GType               ancestor_type);
+void                ide_tweaks_item_unparent             (IdeTweaksItem        *self);
 IDE_AVAILABLE_IN_ALL
-void                ide_tweaks_item_unparent             (IdeTweaksItem      *self);
+gboolean            ide_tweaks_item_visit_children       (IdeTweaksItem        *self,
+                                                          IdeTweaksItemVisitor  visitor,
+                                                          gpointer              visitor_data);
 
 G_END_DECLS
diff --git a/src/libide/tweaks/ide-tweaks-model-private.h b/src/libide/tweaks/ide-tweaks-model-private.h
index 6db7c9487..c59c60139 100644
--- a/src/libide/tweaks/ide-tweaks-model-private.h
+++ b/src/libide/tweaks/ide-tweaks-model-private.h
@@ -27,17 +27,6 @@ G_BEGIN_DECLS
 
 #define IDE_TYPE_TWEAKS_MODEL (ide_tweaks_model_get_type())
 
-typedef enum
-{
-  IDE_TWEAKS_ITEM_VISIT_STOP = 0,
-  IDE_TWEAKS_ITEM_VISIT_SKIP,
-  IDE_TWEAKS_ITEM_VISIT_ACCEPT,
-  IDE_TWEAKS_ITEM_VISIT_RECURSE,
-} IdeTweaksItemVisitResult;
-
-typedef IdeTweaksItemVisitResult (*IdeTweaksItemVisitor) (IdeTweaksItem *item,
-                                                          gpointer       user_data);
-
 G_DECLARE_FINAL_TYPE (IdeTweaksModel, ide_tweaks_model, IDE, TWEAKS_MODEL, GObject)
 
 IdeTweaksModel *ide_tweaks_model_new      (IdeTweaksItem        *item,
diff --git a/src/libide/tweaks/ide-tweaks-model.c b/src/libide/tweaks/ide-tweaks-model.c
index 1e0a7bc15..298a8392a 100644
--- a/src/libide/tweaks/ide-tweaks-model.c
+++ b/src/libide/tweaks/ide-tweaks-model.c
@@ -36,9 +36,6 @@ struct _IdeTweaksModel
   GDestroyNotify        visitor_data_destroy;
 };
 
-static gboolean ide_tweaks_model_populate (IdeTweaksModel *self,
-                                           IdeTweaksItem  *item);
-
 static GType
 list_model_get_item_type (GListModel *model)
 {
@@ -82,40 +79,50 @@ enum {
 
 static GParamSpec *properties [N_PROPS];
 
-static gboolean
-ide_tweaks_model_visit (IdeTweaksModel *self,
-                        IdeTweaksItem  *item)
+static IdeTweaksItemVisitResult
+ide_tweaks_model_populate_cb (IdeTweaksItem *item,
+                              gpointer       user_data)
 {
-  IdeTweaksItemVisitResult res;
+  IdeTweaksModel *self = user_data;
 
-  g_assert (IDE_IS_TWEAKS_MODEL (self));
-  g_assert (IDE_IS_TWEAKS_ITEM (item));
+  if (IDE_IS_TWEAKS_FACTORY (item))
+    {
+      g_autoptr(GPtrArray) factory_items = _ide_tweaks_factory_inflate (IDE_TWEAKS_FACTORY (item));
 
-  res = self->visitor (item, self->visitor_data);
+      for (guint i = 0; i < factory_items->len; i++)
+        {
+          IdeTweaksItem *factory_item = g_ptr_array_index (factory_items, i);
 
-  switch (res)
-    {
-    case IDE_TWEAKS_ITEM_VISIT_STOP:
-      return TRUE;
+          if (ide_tweaks_model_populate_cb (factory_item, self) == IDE_TWEAKS_ITEM_VISIT_STOP)
+            return IDE_TWEAKS_ITEM_VISIT_STOP;
+        }
 
-    case IDE_TWEAKS_ITEM_VISIT_SKIP:
-      return FALSE;
+      return IDE_TWEAKS_ITEM_VISIT_CONTINUE;
+    }
 
-    case IDE_TWEAKS_ITEM_VISIT_ACCEPT:
+  switch (self->visitor (item, self->visitor_data))
+    {
+    case IDE_TWEAKS_ITEM_VISIT_ACCEPT_AND_CONTINUE:
       g_ptr_array_add (self->items, g_object_ref (item));
-      return FALSE;
+      return IDE_TWEAKS_ITEM_VISIT_CONTINUE;
 
     case IDE_TWEAKS_ITEM_VISIT_RECURSE:
-      return ide_tweaks_model_populate (self, item);
+      if (ide_tweaks_item_visit_children (item, ide_tweaks_model_populate_cb, self))
+        return IDE_TWEAKS_ITEM_VISIT_STOP;
+      return IDE_TWEAKS_ITEM_VISIT_CONTINUE;
+
+    case IDE_TWEAKS_ITEM_VISIT_STOP:
+      return IDE_TWEAKS_ITEM_VISIT_STOP;
 
+    case IDE_TWEAKS_ITEM_VISIT_CONTINUE:
     default:
-      break;
+      return IDE_TWEAKS_ITEM_VISIT_CONTINUE;
     }
 
   g_assert_not_reached ();
 }
 
-static gboolean
+static void
 ide_tweaks_model_populate (IdeTweaksModel *self,
                            IdeTweaksItem  *item)
 {
@@ -123,30 +130,7 @@ ide_tweaks_model_populate (IdeTweaksModel *self,
   g_assert (self->items != NULL);
   g_assert (self->visitor != NULL);
 
-  if (IDE_IS_TWEAKS_FACTORY (item))
-    {
-      g_autoptr(GPtrArray) factory_items = _ide_tweaks_factory_inflate (IDE_TWEAKS_FACTORY (item));
-
-      for (guint i = 0; i < factory_items->len; i++)
-        {
-          IdeTweaksItem *factory_item = g_ptr_array_index (factory_items, i);
-
-          if (ide_tweaks_model_visit (self, factory_item))
-            return TRUE;
-        }
-    }
-  else
-    {
-      for (IdeTweaksItem *child = ide_tweaks_item_get_first_child (item);
-           child != NULL;
-           child = ide_tweaks_item_get_next_sibling (child))
-        {
-          if (ide_tweaks_model_visit (self, child))
-            return TRUE;
-        }
-    }
-
-  return FALSE;
+  ide_tweaks_item_visit_children (item, ide_tweaks_model_populate_cb, self);
 }
 
 IdeTweaksModel *
diff --git a/src/libide/tweaks/ide-tweaks-panel-list.c b/src/libide/tweaks/ide-tweaks-panel-list.c
index 9f6b9aaa4..929fda48e 100644
--- a/src/libide/tweaks/ide-tweaks-panel-list.c
+++ b/src/libide/tweaks/ide-tweaks-panel-list.c
@@ -74,13 +74,13 @@ panel_list_visitor (IdeTweaksItem *item,
     return IDE_TWEAKS_ITEM_VISIT_RECURSE;
 
   if (IDE_IS_TWEAKS_PAGE (item))
-    return IDE_TWEAKS_ITEM_VISIT_ACCEPT;
+    return IDE_TWEAKS_ITEM_VISIT_ACCEPT_AND_CONTINUE;
 
   if (IDE_IS_TWEAKS_FACTORY (item) &&
       _ide_tweaks_factory_is_one_of (IDE_TWEAKS_FACTORY (item), &page_type, 1))
     return IDE_TWEAKS_ITEM_VISIT_RECURSE;
 
-  return IDE_TWEAKS_ITEM_VISIT_SKIP;
+  return IDE_TWEAKS_ITEM_VISIT_CONTINUE;
 }
 
 static GtkWidget *
diff --git a/src/libide/tweaks/ide-tweaks-panel.c b/src/libide/tweaks/ide-tweaks-panel.c
index 438f45392..fae39c827 100644
--- a/src/libide/tweaks/ide-tweaks-panel.c
+++ b/src/libide/tweaks/ide-tweaks-panel.c
@@ -74,9 +74,9 @@ group_visitor_func (IdeTweaksItem *item,
     return IDE_TWEAKS_ITEM_VISIT_RECURSE;
 
   if (IDE_IS_TWEAKS_GROUP (item))
-    return IDE_TWEAKS_ITEM_VISIT_ACCEPT;
+    return IDE_TWEAKS_ITEM_VISIT_ACCEPT_AND_CONTINUE;
 
-  return IDE_TWEAKS_ITEM_VISIT_SKIP;
+  return IDE_TWEAKS_ITEM_VISIT_CONTINUE;
 }
 
 static void
diff --git a/src/libide/tweaks/libide-tweaks.h b/src/libide/tweaks/libide-tweaks.h
index 7dc81a173..982253dbd 100644
--- a/src/libide/tweaks/libide-tweaks.h
+++ b/src/libide/tweaks/libide-tweaks.h
@@ -22,12 +22,12 @@
 
 #define IDE_TWEAKS_INSIDE
 # include "ide-tweaks.h"
-# include "ide-tweaks-custom.h"
 # include "ide-tweaks-factory.h"
 # include "ide-tweaks-group.h"
 # include "ide-tweaks-item.h"
 # include "ide-tweaks-page.h"
 # include "ide-tweaks-section.h"
 # include "ide-tweaks-variable.h"
+# include "ide-tweaks-widget.h"
 # include "ide-tweaks-window.h"
 #undef IDE_TWEAKS_INSIDE
diff --git a/src/libide/tweaks/meson.build b/src/libide/tweaks/meson.build
index fd836b724..289f73c8e 100644
--- a/src/libide/tweaks/meson.build
+++ b/src/libide/tweaks/meson.build
@@ -9,13 +9,13 @@ libide_include_directories += include_directories('.')
 libide_tweaks_public_headers = [
   'libide-tweaks.h',
   'ide-tweaks.h',
-  'ide-tweaks-custom.h',
   'ide-tweaks-factory.h',
   'ide-tweaks-group.h',
   'ide-tweaks-item.h',
   'ide-tweaks-page.h',
   'ide-tweaks-section.h',
   'ide-tweaks-variable.h',
+  'ide-tweaks-widget.h',
   'ide-tweaks-window.h',
 ]
 
@@ -27,13 +27,13 @@ install_headers(libide_tweaks_public_headers, subdir: libide_tweaks_header_subdi
 
 libide_tweaks_public_sources = [
   'ide-tweaks.c',
-  'ide-tweaks-custom.c',
   'ide-tweaks-item.c',
   'ide-tweaks-factory.c',
   'ide-tweaks-group.c',
   'ide-tweaks-page.c',
   'ide-tweaks-section.c',
   'ide-tweaks-variable.c',
+  'ide-tweaks-widget.c',
   'ide-tweaks-window.c',
 ]
 


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