[gnome-builder] libide/tweaks: add external widget creation support



commit 4a3f6a1c1facd5f55a68f79eeb87599ab0adf69a
Author: Christian Hergert <chergert redhat com>
Date:   Wed Aug 10 14:41:19 2022 -0700

    libide/tweaks: add external widget creation support
    
    This requires a GType, but we could add a closure form/signal too if we
    want based on how we end up registering these preferences going forward.

 src/libide/tweaks/ide-tweaks-external.c | 140 ++++++++++++++++++++++++++++++++
 src/libide/tweaks/ide-tweaks-external.h |  40 +++++++++
 src/libide/tweaks/ide-tweaks-init.c     |   1 +
 src/libide/tweaks/libide-tweaks.h       |   1 +
 src/libide/tweaks/meson.build           |   4 +-
 5 files changed, 185 insertions(+), 1 deletion(-)
---
diff --git a/src/libide/tweaks/ide-tweaks-external.c b/src/libide/tweaks/ide-tweaks-external.c
new file mode 100644
index 000000000..62ae8cdcf
--- /dev/null
+++ b/src/libide/tweaks/ide-tweaks-external.c
@@ -0,0 +1,140 @@
+/* ide-tweaks-external.c
+ *
+ * Copyright 2022 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "ide-tweaks-external"
+
+#include "config.h"
+
+#include <adwaita.h>
+
+#include "ide-tweaks-external.h"
+
+struct _IdeTweaksExternal
+{
+  IdeTweaksWidget parent_instance;
+  GType widget_type;
+};
+
+enum {
+  PROP_0,
+  PROP_WIDGET_TYPE,
+  N_PROPS
+};
+
+G_DEFINE_FINAL_TYPE (IdeTweaksExternal, ide_tweaks_external, IDE_TYPE_TWEAKS_WIDGET)
+
+static GParamSpec *properties[N_PROPS];
+
+static GtkWidget *
+ide_tweaks_external_create (IdeTweaksWidget *widget)
+{
+  IdeTweaksExternal *self = (IdeTweaksExternal *)widget;
+
+  g_assert (IDE_IS_TWEAKS_EXTERNAL (self));
+
+  if (self->widget_type == G_TYPE_INVALID)
+    return NULL;
+
+  return g_object_new (self->widget_type, NULL);
+}
+
+static void
+ide_tweaks_external_get_property (GObject    *object,
+                                  guint       prop_id,
+                                  GValue     *value,
+                                  GParamSpec *pspec)
+{
+  IdeTweaksExternal *self = IDE_TWEAKS_EXTERNAL (object);
+
+  switch (prop_id)
+    {
+    case PROP_WIDGET_TYPE:
+      g_value_set_gtype (value, ide_tweaks_external_get_widget_type (self));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+ide_tweaks_external_set_property (GObject      *object,
+                                  guint         prop_id,
+                                  const GValue *value,
+                                  GParamSpec   *pspec)
+{
+  IdeTweaksExternal *self = IDE_TWEAKS_EXTERNAL (object);
+
+  switch (prop_id)
+    {
+    case PROP_WIDGET_TYPE:
+      ide_tweaks_external_set_widget_type (self, g_value_get_gtype (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+ide_tweaks_external_class_init (IdeTweaksExternalClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  IdeTweaksWidgetClass *widget_class = IDE_TWEAKS_WIDGET_CLASS (klass);
+
+  object_class->get_property = ide_tweaks_external_get_property;
+  object_class->set_property = ide_tweaks_external_set_property;
+
+  widget_class->create = ide_tweaks_external_create;
+
+  properties[PROP_WIDGET_TYPE] =
+    g_param_spec_gtype ("widget-type", NULL, NULL,
+                        GTK_TYPE_WIDGET,
+                        (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_properties (object_class, N_PROPS, properties);
+}
+
+static void
+ide_tweaks_external_init (IdeTweaksExternal *self)
+{
+}
+
+GType
+ide_tweaks_external_get_widget_type (IdeTweaksExternal *self)
+{
+  g_return_val_if_fail (IDE_IS_TWEAKS_EXTERNAL (self), G_TYPE_INVALID);
+
+  return self->widget_type;
+}
+
+void
+ide_tweaks_external_set_widget_type (IdeTweaksExternal *self,
+                                     GType              widget_type)
+{
+  g_return_if_fail (IDE_IS_TWEAKS_EXTERNAL (self));
+  g_return_if_fail (!widget_type || g_type_is_a (widget_type, GTK_TYPE_WIDGET));
+
+  if (self->widget_type != widget_type)
+    {
+      self->widget_type = widget_type;
+      g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_WIDGET_TYPE]);
+    }
+}
diff --git a/src/libide/tweaks/ide-tweaks-external.h b/src/libide/tweaks/ide-tweaks-external.h
new file mode 100644
index 000000000..ee3f0e6b8
--- /dev/null
+++ b/src/libide/tweaks/ide-tweaks-external.h
@@ -0,0 +1,40 @@
+/* ide-tweaks-external.h
+ *
+ * Copyright 2022 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include "ide-tweaks-widget.h"
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_TWEAKS_EXTERNAL (ide_tweaks_external_get_type())
+
+IDE_AVAILABLE_IN_ALL
+G_DECLARE_FINAL_TYPE (IdeTweaksExternal, ide_tweaks_external, IDE, TWEAKS_EXTERNAL, IdeTweaksWidget)
+
+IDE_AVAILABLE_IN_ALL
+IdeTweaksExternal *ide_tweaks_external_new             (void);
+IDE_AVAILABLE_IN_ALL
+GType              ide_tweaks_external_get_widget_type (IdeTweaksExternal *self);
+IDE_AVAILABLE_IN_ALL
+void               ide_tweaks_external_set_widget_type (IdeTweaksExternal *self,
+                                                        GType              widget_type);
+
+G_END_DECLS
diff --git a/src/libide/tweaks/ide-tweaks-init.c b/src/libide/tweaks/ide-tweaks-init.c
index e5bee1bb2..23134effc 100644
--- a/src/libide/tweaks/ide-tweaks-init.c
+++ b/src/libide/tweaks/ide-tweaks-init.c
@@ -33,6 +33,7 @@ _ide_tweaks_init (void)
   g_resources_register (ide_tweaks_get_resource ());
 
   g_type_ensure (IDE_TYPE_TWEAKS);
+  g_type_ensure (IDE_TYPE_TWEAKS_EXTERNAL);
   g_type_ensure (IDE_TYPE_TWEAKS_FACTORY);
   g_type_ensure (IDE_TYPE_TWEAKS_GROUP);
   g_type_ensure (IDE_TYPE_TWEAKS_ITEM);
diff --git a/src/libide/tweaks/libide-tweaks.h b/src/libide/tweaks/libide-tweaks.h
index 1e50a9032..cd1b6cd9f 100644
--- a/src/libide/tweaks/libide-tweaks.h
+++ b/src/libide/tweaks/libide-tweaks.h
@@ -22,6 +22,7 @@
 
 #define IDE_TWEAKS_INSIDE
 # include "ide-tweaks.h"
+# include "ide-tweaks-external.h"
 # include "ide-tweaks-factory.h"
 # include "ide-tweaks-group.h"
 # include "ide-tweaks-item.h"
diff --git a/src/libide/tweaks/meson.build b/src/libide/tweaks/meson.build
index 34cffcaf9..d65e778f5 100644
--- a/src/libide/tweaks/meson.build
+++ b/src/libide/tweaks/meson.build
@@ -9,6 +9,7 @@ libide_include_directories += include_directories('.')
 libide_tweaks_public_headers = [
   'libide-tweaks.h',
   'ide-tweaks.h',
+  'ide-tweaks-external.h',
   'ide-tweaks-factory.h',
   'ide-tweaks-group.h',
   'ide-tweaks-item.h',
@@ -30,9 +31,10 @@ install_headers(libide_tweaks_public_headers, subdir: libide_tweaks_header_subdi
 
 libide_tweaks_public_sources = [
   'ide-tweaks.c',
-  'ide-tweaks-item.c',
+  'ide-tweaks-external.c',
   'ide-tweaks-factory.c',
   'ide-tweaks-group.c',
+  'ide-tweaks-item.c',
   'ide-tweaks-page.c',
   'ide-tweaks-radio.c',
   'ide-tweaks-section.c',


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