[gtk/wip/chergert/builder-expose-object-fix: 2/4] gtkbuilder: check for existing object before extending template




commit 5bc612835af6b1e653c202f0f1dc0f1c713a6a32
Author: Christian Hergert <chergert redhat com>
Date:   Sun Jul 31 21:15:08 2022 -0700

    gtkbuilder: check for existing object before extending template
    
    If gtk_builder_expose_object() is called twice with the same name, it will
    result in a g_critical(). This improves that situation by checking for the
    object before exposing additional times.
    
    This turns out to be handy in situations where templates are expanded
    multiple times, such as application-side implementations of UI merging.

 gtk/gtkbuilder.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
---
diff --git a/gtk/gtkbuilder.c b/gtk/gtkbuilder.c
index ab28ccc5d9..063bf7b03c 100644
--- a/gtk/gtkbuilder.c
+++ b/gtk/gtkbuilder.c
@@ -1367,6 +1367,7 @@ gtk_builder_extend_with_template (GtkBuilder   *builder,
                                   GError      **error)
 {
   GtkBuilderPrivate *priv = gtk_builder_get_instance_private (builder);
+  const char *name;
   GError *tmp_error;
   char *filename;
 
@@ -1384,8 +1385,15 @@ gtk_builder_extend_with_template (GtkBuilder   *builder,
   priv->resource_prefix = NULL;
   priv->template_type = template_type;
 
-  filename = g_strconcat ("<", g_type_name (template_type), " template>", NULL);
-  gtk_builder_expose_object (builder, g_type_name (template_type), object);
+  /* We specifically allow this function to be called multiple times with
+   * the same @template_type as that is used in applications like Builder
+   * to implement UI merging.
+   */
+  name = g_type_name (template_type);
+  if (gtk_builder_get_object (builder, name) != object)
+    gtk_builder_expose_object (builder, name, object);
+
+  filename = g_strconcat ("<", name, " template>", NULL);
   _gtk_builder_parser_parse_buffer (builder, filename,
                                     buffer, length,
                                     NULL,


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