[gnome-builder] libide/tweaks: use section title for similar check



commit 511b988ce1b9eafad8a7f45eeeed627fde84055c
Author: Christian Hergert <chergert redhat com>
Date:   Tue Aug 16 14:51:54 2022 -0700

    libide/tweaks: use section title for similar check
    
    We can use this to add sections to sub-pages via a factory.

 src/libide/gui/tweaks.ui                  |  6 +++
 src/libide/tweaks/ide-tweaks-panel-list.c | 29 ++++++++++-
 src/libide/tweaks/ide-tweaks-section.c    | 87 +++++++++++++++++++++++++++++++
 src/libide/tweaks/ide-tweaks-section.h    |  7 ++-
 4 files changed, 126 insertions(+), 3 deletions(-)
---
diff --git a/src/libide/gui/tweaks.ui b/src/libide/gui/tweaks.ui
index 7f5b51439..01cd996ab 100644
--- a/src/libide/gui/tweaks.ui
+++ b/src/libide/gui/tweaks.ui
@@ -3,6 +3,7 @@
   <template class="IdeTweaks">
     <child>
       <object class="IdeTweaksSection" id="visual_section">
+        <property name="title" translatable="yes">Visual</property>
         <child>
           <object class="IdeTweaksPage" id="appearance_page">
             <property name="icon-name">preferences-desktop-appearance-symbolic</property>
@@ -29,22 +30,27 @@
     </child>
     <child>
       <object class="IdeTweaksSection" id="code_section">
+        <property name="title" translatable="yes">Code</property>
       </object>
     </child>
     <child>
       <object class="IdeTweaksSection" id="projects_section">
+        <property name="title" translatable="yes">Projects</property>
       </object>
     </child>
     <child>
       <object class="IdeTweaksSection" id="foundry_section">
+        <property name="title" translatable="yes">Foundry</property>
       </object>
     </child>
     <child>
       <object class="IdeTweaksSection" id="external_section">
+        <property name="title" translatable="yes">External</property>
       </object>
     </child>
     <child>
       <object class="IdeTweaksSection" id="plugins_section">
+        <property name="title" translatable="yes">Plugins</property>
         <child>
           <object class="IdeTweaksPage" id="plugins_page">
             <property name="icon-name">builder-plugin-symbolic</property>
diff --git a/src/libide/tweaks/ide-tweaks-panel-list.c b/src/libide/tweaks/ide-tweaks-panel-list.c
index 929fda48e..e53ee6706 100644
--- a/src/libide/tweaks/ide-tweaks-panel-list.c
+++ b/src/libide/tweaks/ide-tweaks-panel-list.c
@@ -116,6 +116,31 @@ ide_tweaks_panel_list_set_item (IdeTweaksPanelList *self,
     }
 }
 
+static gboolean
+section_equal (IdeTweaksItem *section_a,
+               IdeTweaksItem *section_b)
+{
+  const char *title_a;
+  const char *title_b;
+
+  if (section_a == section_b)
+    return TRUE;
+
+  if (section_a == NULL || section_b == NULL)
+    return FALSE;
+
+  title_a = ide_tweaks_section_get_title (IDE_TWEAKS_SECTION (section_a));
+  title_b = ide_tweaks_section_get_title (IDE_TWEAKS_SECTION (section_b));
+
+  if (title_a == NULL && title_b != NULL)
+    return FALSE;
+
+  if (title_a != NULL && title_b == NULL)
+    return FALSE;
+
+  return g_strcmp0 (title_a, title_b) == 0;
+}
+
 static void
 ide_tweaks_panel_list_header_func (IdeTweaksPanelListRow *row,
                                    IdeTweaksPanelListRow *before,
@@ -134,8 +159,8 @@ ide_tweaks_panel_list_header_func (IdeTweaksPanelListRow *row,
 
   if (IDE_IS_TWEAKS_PAGE (before_item) &&
       IDE_IS_TWEAKS_PAGE (row_item) &&
-      ide_tweaks_page_get_section (IDE_TWEAKS_PAGE (before_item)) !=
-      ide_tweaks_page_get_section (IDE_TWEAKS_PAGE (row_item)))
+      !section_equal (ide_tweaks_page_get_section (IDE_TWEAKS_PAGE (before_item)),
+                      ide_tweaks_page_get_section (IDE_TWEAKS_PAGE (row_item))))
     header = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
 
   gtk_list_box_row_set_header (GTK_LIST_BOX_ROW (row), header);
diff --git a/src/libide/tweaks/ide-tweaks-section.c b/src/libide/tweaks/ide-tweaks-section.c
index e8a30efe2..e05ad8473 100644
--- a/src/libide/tweaks/ide-tweaks-section.c
+++ b/src/libide/tweaks/ide-tweaks-section.c
@@ -28,10 +28,19 @@
 struct _IdeTweaksSection
 {
   IdeTweaksItem parent_instance;
+  char *title;
 };
 
 G_DEFINE_FINAL_TYPE (IdeTweaksSection, ide_tweaks_section, IDE_TYPE_TWEAKS_ITEM)
 
+enum {
+  PROP_0,
+  PROP_TITLE,
+  N_PROPS
+};
+
+static GParamSpec *properties[N_PROPS];
+
 static gboolean
 ide_tweaks_section_accepts (IdeTweaksItem *item,
                             IdeTweaksItem *child)
@@ -42,15 +51,93 @@ ide_tweaks_section_accepts (IdeTweaksItem *item,
   return IDE_IS_TWEAKS_PAGE (child);
 }
 
+static void
+ide_tweaks_section_dispose (GObject *object)
+{
+  IdeTweaksSection *self = (IdeTweaksSection *)object;
+
+  g_clear_pointer (&self->title, g_free);
+
+  G_OBJECT_CLASS (ide_tweaks_section_parent_class)->dispose (object);
+}
+
+static void
+ide_tweaks_section_get_property (GObject    *object,
+                                 guint       prop_id,
+                                 GValue     *value,
+                                 GParamSpec *pspec)
+{
+  IdeTweaksSection *self = IDE_TWEAKS_SECTION (object);
+
+  switch (prop_id)
+    {
+    case PROP_TITLE:
+      g_value_set_string (value, ide_tweaks_section_get_title (self));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+ide_tweaks_section_set_property (GObject      *object,
+                                 guint         prop_id,
+                                 const GValue *value,
+                                 GParamSpec   *pspec)
+{
+  IdeTweaksSection *self = IDE_TWEAKS_SECTION (object);
+
+  switch (prop_id)
+    {
+    case PROP_TITLE:
+      ide_tweaks_section_set_title (self, g_value_get_string (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
 static void
 ide_tweaks_section_class_init (IdeTweaksSectionClass *klass)
 {
   IdeTweaksItemClass *item_class = IDE_TWEAKS_ITEM_CLASS (klass);
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
   item_class->accepts = ide_tweaks_section_accepts;
+
+  object_class->dispose = ide_tweaks_section_dispose;
+  object_class->get_property = ide_tweaks_section_get_property;
+  object_class->set_property = ide_tweaks_section_set_property;
+
+  properties[PROP_TITLE] =
+    g_param_spec_string ("title", NULL, NULL,
+                         NULL,
+                         (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_section_init (IdeTweaksSection *self)
 {
 }
+
+const char *
+ide_tweaks_section_get_title (IdeTweaksSection *self)
+{
+  g_return_val_if_fail (IDE_IS_TWEAKS_SECTION (self), NULL);
+
+  return self->title;
+}
+
+void
+ide_tweaks_section_set_title (IdeTweaksSection *self,
+                              const char       *title)
+{
+  g_return_if_fail (IDE_IS_TWEAKS_SECTION (self));
+
+  if (ide_set_string (&self->title, title))
+    g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_TITLE]);
+}
diff --git a/src/libide/tweaks/ide-tweaks-section.h b/src/libide/tweaks/ide-tweaks-section.h
index d999aff69..2d908b30e 100644
--- a/src/libide/tweaks/ide-tweaks-section.h
+++ b/src/libide/tweaks/ide-tweaks-section.h
@@ -34,6 +34,11 @@ IDE_AVAILABLE_IN_ALL
 G_DECLARE_FINAL_TYPE (IdeTweaksSection, ide_tweaks_section, IDE, TWEAKS_SECTION, IdeTweaksItem)
 
 IDE_AVAILABLE_IN_ALL
-IdeTweaksSection *ide_tweaks_section_new (void);
+IdeTweaksSection *ide_tweaks_section_new       (void);
+IDE_AVAILABLE_IN_ALL
+const char       *ide_tweaks_section_get_title (IdeTweaksSection *self);
+IDE_AVAILABLE_IN_ALL
+void              ide_tweaks_section_set_title (IdeTweaksSection *self,
+                                                const char       *title);
 
 G_END_DECLS


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