[gnome-builder] libide/tweaks: add optional context to IdeTweaks



commit 2623850afbef02d1ff70d4a513274a1021ccb5c6
Author: Christian Hergert <chergert redhat com>
Date:   Tue Aug 23 14:00:05 2022 -0700

    libide/tweaks: add optional context to IdeTweaks
    
    This allows for templates to do bindings to the context in some situations.

 src/libide/tweaks/ide-tweaks.c | 73 ++++++++++++++++++++++++++++++++++++++++++
 src/libide/tweaks/ide-tweaks.h |  4 +++
 2 files changed, 77 insertions(+)
---
diff --git a/src/libide/tweaks/ide-tweaks.c b/src/libide/tweaks/ide-tweaks.c
index 4619c0f49..bc4db7dac 100644
--- a/src/libide/tweaks/ide-tweaks.c
+++ b/src/libide/tweaks/ide-tweaks.c
@@ -31,6 +31,7 @@
 struct _IdeTweaks
 {
   IdeTweaksItem    parent_instance;
+  IdeContext      *context;
   GtkBuilder      *builder;
   GtkBuilderScope *scope;
   char            *project_id;
@@ -40,6 +41,7 @@ G_DEFINE_FINAL_TYPE (IdeTweaks, ide_tweaks, IDE_TYPE_TWEAKS_ITEM)
 
 enum {
   PROP_0,
+  PROP_CONTEXT,
   PROP_PROJECT_ID,
   N_PROPS
 };
@@ -75,6 +77,21 @@ ide_tweaks_accepts (IdeTweaksItem *item,
          IDE_IS_TWEAKS_SETTINGS (child);
 }
 
+static void
+ide_tweaks_constructed (GObject *object)
+{
+  IdeTweaks *self = (IdeTweaks *)object;
+
+  G_OBJECT_CLASS (ide_tweaks_parent_class)->constructed (object);
+
+  if (self->context != NULL)
+    {
+      if (self->project_id == NULL)
+        self->project_id = ide_context_dup_project_id (self->context);
+      ide_tweaks_expose_object (self, "IdeContext", G_OBJECT (self->context));
+    }
+}
+
 static void
 ide_tweaks_dispose (GObject *object)
 {
@@ -97,6 +114,10 @@ ide_tweaks_get_property (GObject    *object,
 
   switch (prop_id)
     {
+    case PROP_CONTEXT:
+      g_value_set_object (value, ide_tweaks_get_context (self));
+      break;
+
     case PROP_PROJECT_ID:
       g_value_set_string (value, ide_tweaks_get_project_id (self));
       break;
@@ -116,6 +137,10 @@ ide_tweaks_set_property (GObject      *object,
 
   switch (prop_id)
     {
+    case PROP_CONTEXT:
+      self->context = g_value_dup_object (value);
+      break;
+
     case PROP_PROJECT_ID:
       ide_tweaks_set_project_id (self, g_value_get_string (value));
       break;
@@ -131,12 +156,18 @@ ide_tweaks_class_init (IdeTweaksClass *klass)
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
   IdeTweaksItemClass *item_class = IDE_TWEAKS_ITEM_CLASS (klass);
 
+  object_class->constructed = ide_tweaks_constructed;
   object_class->dispose = ide_tweaks_dispose;
   object_class->get_property = ide_tweaks_get_property;
   object_class->set_property = ide_tweaks_set_property;
 
   item_class->accepts = ide_tweaks_accepts;
 
+  properties[PROP_CONTEXT] =
+    g_param_spec_object ("context", NULL, NULL,
+                         IDE_TYPE_CONTEXT,
+                         (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
+
   properties[PROP_PROJECT_ID] =
     g_param_spec_string ("project-id", NULL, NULL,
                          NULL,
@@ -255,3 +286,45 @@ ide_tweaks_get_object (IdeTweaks  *self,
 
   return gtk_builder_get_object (self->builder, name);
 }
+
+/**
+ * ide_tweaks_new_for_context:
+ * @context: (nullable): an #IdeContext or %NULL
+ *
+ * Creates a new #IdeTweaks for @context.
+ *
+ * If @context is %NULL, this function acts the same as ide_tweaks_new().
+ *
+ * If @context is non-%NULL, this function will expose @context as
+ * "IdeContext" to the templates as well as with the #IdeTweaks:context
+ * property to make property bindings easier with lookup.
+ *
+ * #IdeTweaks:project-id is also set when @context is non-%NULL.
+ *
+ * Returns: (transfer full): a new #IdeTweaks
+ */
+IdeTweaks *
+ide_tweaks_new_for_context (IdeContext *context)
+{
+  g_return_val_if_fail (!context || IDE_IS_CONTEXT (context), NULL);
+
+  return g_object_new (IDE_TYPE_TWEAKS,
+                       "context", context,
+                       NULL);
+}
+
+/**
+ * ide_tweaks_get_context:
+ * @self: a #IdeTweaks
+ *
+ * Gets the #IdeContext if any.
+ *
+ * Returns: (nullable) (transfer none): an #IdeContext or %NULL
+ */
+IdeContext *
+ide_tweaks_get_context (IdeTweaks *self)
+{
+  g_return_val_if_fail (IDE_IS_TWEAKS (self), NULL);
+
+  return self->context;
+}
diff --git a/src/libide/tweaks/ide-tweaks.h b/src/libide/tweaks/ide-tweaks.h
index 8dba534f3..ca0e7765b 100644
--- a/src/libide/tweaks/ide-tweaks.h
+++ b/src/libide/tweaks/ide-tweaks.h
@@ -36,6 +36,10 @@ G_DECLARE_FINAL_TYPE (IdeTweaks, ide_tweaks, IDE, TWEAKS, IdeTweaksItem)
 IDE_AVAILABLE_IN_ALL
 IdeTweaks *ide_tweaks_new             (void);
 IDE_AVAILABLE_IN_ALL
+IdeTweaks *ide_tweaks_new_for_context (IdeContext    *context);
+IDE_AVAILABLE_IN_ALL
+IdeContext *ide_tweaks_get_context    (IdeTweaks     *self);
+IDE_AVAILABLE_IN_ALL
 const char *ide_tweaks_get_project_id (IdeTweaks     *self);
 IDE_AVAILABLE_IN_ALL
 void        ide_tweaks_set_project_id (IdeTweaks     *self,


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