[gnome-builder/wip/gtk4-port] libide/projects: switch template when language changes



commit 58a845bc278bc23c0eb9bc5aea1b1a6534b2bb8e
Author: Christian Hergert <chergert redhat com>
Date:   Fri May 27 14:11:56 2022 -0700

    libide/projects: switch template when language changes
    
    If the language changes, we might need to change the current template so
    that it is a valid selection. This does it automatically to whatever
    the first template in the (filtered) list is.

 src/libide/projects/ide-template-input.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
---
diff --git a/src/libide/projects/ide-template-input.c b/src/libide/projects/ide-template-input.c
index 382e91fd8..4fa8a107e 100644
--- a/src/libide/projects/ide-template-input.c
+++ b/src/libide/projects/ide-template-input.c
@@ -579,6 +579,34 @@ ide_template_input_set_directory (IdeTemplateInput *self,
   g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_DIRECTORY]);
 }
 
+static void
+auto_select_template (IdeTemplateInput *self)
+{
+  g_autofree char *first_id = NULL;
+  GListModel *model;
+  guint n_items;
+
+  g_assert (IDE_IS_TEMPLATE_INPUT (self));
+
+  model = G_LIST_MODEL (self->filtered_templates);
+  n_items = g_list_model_get_n_items (model);
+
+  for (guint i = 0; i < n_items; i++)
+    {
+      g_autoptr(IdeProjectTemplate) template = g_list_model_get_item (model, i);
+      g_autofree char *id = ide_project_template_get_id (template);
+
+      if (ide_str_equal0 (id, self->template))
+        return;
+
+      if (first_id == NULL)
+        first_id = g_steal_pointer (&id);
+    }
+
+  if (first_id != NULL)
+    ide_template_input_set_template (self, first_id);
+}
+
 void
 ide_template_input_set_language (IdeTemplateInput *self,
                                  const char       *language)
@@ -589,10 +617,13 @@ ide_template_input_set_language (IdeTemplateInput *self,
     {
       g_free (self->language);
       self->language = g_strdup (language);
+
       gtk_custom_filter_set_filter_func (self->template_filter,
                                          template_filter_func,
                                          g_strdup (language),
                                          g_free);
+      auto_select_template (self);
+
       g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_LANGUAGE]);
     }
 }


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