[gnome-builder] libide/tweaks: introduce hidden-when property



commit b22d6ef5549e32efc3cf911e3189bb5652fd2be9
Author: Christian Hergert <chergert redhat com>
Date:   Tue Aug 23 13:13:26 2022 -0700

    libide/tweaks: introduce hidden-when property
    
    The goal here is for us to be able to have a single IdeTweaksAddin that
    can serve both the application and project form just with a hidden-when
    property to denote the differences.

 src/libide/tweaks/ide-tweaks-combo.c        | 14 +++--
 src/libide/tweaks/ide-tweaks-item-private.h |  8 ++-
 src/libide/tweaks/ide-tweaks-item.c         | 89 ++++++++++++++++++++++-------
 src/libide/tweaks/ide-tweaks-item.h         |  5 ++
 src/libide/tweaks/ide-tweaks-window.c       |  4 +-
 5 files changed, 91 insertions(+), 29 deletions(-)
---
diff --git a/src/libide/tweaks/ide-tweaks-combo.c b/src/libide/tweaks/ide-tweaks-combo.c
index 42ae66fd2..c3f211ab8 100644
--- a/src/libide/tweaks/ide-tweaks-combo.c
+++ b/src/libide/tweaks/ide-tweaks-combo.c
@@ -28,6 +28,7 @@
 #include "ide-tweaks-choice.h"
 #include "ide-tweaks-combo.h"
 #include "ide-tweaks-combo-row.h"
+#include "ide-tweaks-item-private.h"
 
 struct _IdeTweaksCombo
 {
@@ -76,13 +77,16 @@ ide_tweaks_combo_create_for_item (IdeTweaksWidget *instance,
        child != NULL;
        child = ide_tweaks_item_get_next_sibling (child))
     {
-      GVariant *target = ide_tweaks_choice_get_action_target (IDE_TWEAKS_CHOICE (child));
+      if (!_ide_tweaks_item_is_hidden (child, root))
+        {
+          GVariant *target = ide_tweaks_choice_get_action_target (IDE_TWEAKS_CHOICE (child));
 
-      if (g_variant_equal (value, target))
-        selected = i;
+          if (g_variant_equal (value, target))
+            selected = i;
 
-      g_list_store_append (store, child);
-      i++;
+          g_list_store_append (store, child);
+          i++;
+        }
     }
 
   row = g_object_new (IDE_TYPE_TWEAKS_COMBO_ROW,
diff --git a/src/libide/tweaks/ide-tweaks-item-private.h b/src/libide/tweaks/ide-tweaks-item-private.h
index 4b34f67eb..2ba4ec13d 100644
--- a/src/libide/tweaks/ide-tweaks-item-private.h
+++ b/src/libide/tweaks/ide-tweaks-item-private.h
@@ -24,8 +24,10 @@
 
 G_BEGIN_DECLS
 
-void _ide_tweaks_item_printf (IdeTweaksItem *self,
-                              GString       *string,
-                              guint          level);
+void     _ide_tweaks_item_printf    (IdeTweaksItem *self,
+                                     GString       *string,
+                                     guint          level);
+gboolean _ide_tweaks_item_is_hidden (IdeTweaksItem *self,
+                                     IdeTweaksItem *root);
 
 G_END_DECLS
diff --git a/src/libide/tweaks/ide-tweaks-item.c b/src/libide/tweaks/ide-tweaks-item.c
index 9f7a9ca14..9f7a0f8e7 100644
--- a/src/libide/tweaks/ide-tweaks-item.c
+++ b/src/libide/tweaks/ide-tweaks-item.c
@@ -24,6 +24,7 @@
 
 #include <gtk/gtk.h>
 
+#include "ide-tweaks.h"
 #include "ide-tweaks-item.h"
 #include "ide-tweaks-item-private.h"
 
@@ -35,11 +36,13 @@ typedef struct
   GQueue          children;
   char           *id;
   char          **keywords;
+  char           *hidden_when;
   guint           id_sequence;
 } IdeTweaksItemPrivate;
 
 enum {
   PROP_0,
+  PROP_HIDDEN_WHEN,
   PROP_ID,
   PROP_KEYWORDS,
   N_PROPS
@@ -198,14 +201,12 @@ ide_tweaks_item_get_property (GObject    *object,
 
   switch (prop_id)
     {
-    case PROP_ID:
-      g_value_set_string (value, gtk_buildable_get_buildable_id (GTK_BUILDABLE (self)));
-      break;
-
     case PROP_KEYWORDS:
       g_value_set_boxed (value, ide_tweaks_item_get_keywords (self));
       break;
 
+    IDE_GET_PROPERTY_STRING (ide_tweaks_item, hidden_when, HIDDEN_WHEN);
+    IDE_GET_PROPERTY_STRING (ide_tweaks_item, id, ID);
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -225,6 +226,7 @@ ide_tweaks_item_set_property (GObject      *object,
       ide_tweaks_item_set_keywords (self, g_value_get_boxed (value));
       break;
 
+    IDE_SET_PROPERTY_STRING (ide_tweaks_item, hidden_when, HIDDEN_WHEN);
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -242,10 +244,8 @@ ide_tweaks_item_class_init (IdeTweaksItemClass *klass)
   klass->copy = ide_tweaks_item_real_copy;
   klass->match = ide_tweaks_item_real_match;
 
-  properties[PROP_ID] =
-    g_param_spec_string ("id", NULL, NULL,
-                         NULL,
-                         (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
+  IDE_DEFINE_STRING_PROPERTY ("hidden-when", NULL, G_PARAM_READWRITE, HIDDEN_WHEN);
+  IDE_DEFINE_STRING_PROPERTY ("id", NULL, G_PARAM_READABLE, ID);
 
   properties [PROP_KEYWORDS] =
     g_param_spec_boxed ("keywords", NULL, NULL,
@@ -273,15 +273,28 @@ ide_tweaks_item_accepts (IdeTweaksItem *self,
   return FALSE;
 }
 
-const char *
-ide_tweaks_item_get_id (IdeTweaksItem *self)
-{
-  IdeTweaksItemPrivate *priv = ide_tweaks_item_get_instance_private (self);
+IDE_DEFINE_STRING_GETTER_PRIVATE (ide_tweaks_item, IdeTweaksItem, IDE_TYPE_TWEAKS_ITEM, id)
+IDE_DEFINE_STRING_GETTER_PRIVATE (ide_tweaks_item, IdeTweaksItem, IDE_TYPE_TWEAKS_ITEM, hidden_when)
 
-  g_return_val_if_fail (IDE_IS_TWEAKS_ITEM (self), NULL);
-
-  return priv->id;
-}
+/**
+ * ide_tweaks_item_set_hidden_when:
+ * @self: an #IdeTweaksItem
+ * @hidden_when: (nullable): the value for when the item is hidden
+ *
+ * Sets the "hidden-when" property.
+ *
+ * Use this to hide #IdeTweaksItem in situations where they should
+ * not be visible. Generally this is used to hide items when the
+ * #IdeTweaksWindow is in project or application mode.
+ *
+ * Currently supported values include:
+ *
+ *  - "application" to hide when in application-mode
+ *  - "project" to hide when in project-mode
+ *
+ * Items that are hidden will not be visited by ide_tweaks_item_visit_children().
+ */
+IDE_DEFINE_STRING_SETTER_PRIVATE (ide_tweaks_item, IdeTweaksItem, IDE_TYPE_TWEAKS_ITEM, hidden_when, 
HIDDEN_WHEN)
 
 const char * const *
 ide_tweaks_item_get_keywords (IdeTweaksItem *self)
@@ -769,14 +782,23 @@ ide_tweaks_item_visit_children (IdeTweaksItem        *self,
                                 IdeTweaksItemVisitor  visitor,
                                 gpointer              visitor_data)
 {
+  IdeTweaksItem *root = NULL;
+  IdeTweaksItem *child;
+
   g_return_val_if_fail (IDE_IS_TWEAKS_ITEM (self), FALSE);
   g_return_val_if_fail (visitor != NULL, FALSE);
 
-  for (IdeTweaksItem *child = ide_tweaks_item_get_first_child (self);
-       child != NULL;
-       child = ide_tweaks_item_get_next_sibling (child))
+  if ((child = ide_tweaks_item_get_first_child (self)))
+    root = ide_tweaks_item_get_root (IDE_TWEAKS_ITEM (self));
+
+  for (; child; child = ide_tweaks_item_get_next_sibling (child))
     {
-      IdeTweaksItemVisitResult res = visitor (child, visitor_data);
+      IdeTweaksItemVisitResult res;
+
+      if (_ide_tweaks_item_is_hidden (child, root))
+        continue;
+
+      res = visitor (child, visitor_data);
 
       if (res == IDE_TWEAKS_ITEM_VISIT_STOP)
         return TRUE;
@@ -821,3 +843,30 @@ ide_tweaks_item_match (IdeTweaksItem  *self,
 
   return IDE_TWEAKS_ITEM_GET_CLASS (self)->match (self, spec);
 }
+
+gboolean
+_ide_tweaks_item_is_hidden (IdeTweaksItem *self,
+                            IdeTweaksItem *root)
+{
+  IdeTweaksItemPrivate *priv = ide_tweaks_item_get_instance_private (self);
+  const char *project_id = NULL;
+
+  g_return_val_if_fail (IDE_IS_TWEAKS_ITEM (self), FALSE);
+  g_return_val_if_fail (!root || IDE_IS_TWEAKS_ITEM (root), FALSE);
+
+  if (priv->hidden_when == NULL)
+    return FALSE;
+
+  if (root == NULL)
+    root = ide_tweaks_item_get_root (IDE_TWEAKS_ITEM (self));
+
+  if (IDE_IS_TWEAKS (root))
+    project_id = ide_tweaks_get_project_id (IDE_TWEAKS (root));
+
+  if (priv->hidden_when[0] == 'a') /* application */
+    return ide_str_empty0 (project_id);
+  else if (priv->hidden_when[0] == 'p') /* project */
+    return !ide_str_empty0 (project_id);
+  else
+    return FALSE;
+}
diff --git a/src/libide/tweaks/ide-tweaks-item.h b/src/libide/tweaks/ide-tweaks-item.h
index 906187002..2bdec67ee 100644
--- a/src/libide/tweaks/ide-tweaks-item.h
+++ b/src/libide/tweaks/ide-tweaks-item.h
@@ -70,6 +70,11 @@ const char         *ide_tweaks_item_get_id               (IdeTweaksItem        *
 IDE_AVAILABLE_IN_ALL
 IdeTweaksItem      *ide_tweaks_item_copy                 (IdeTweaksItem        *self);
 IDE_AVAILABLE_IN_ALL
+const char         *ide_tweaks_item_get_hidden_when      (IdeTweaksItem        *self);
+IDE_AVAILABLE_IN_ALL
+void                ide_tweaks_item_set_hidden_when      (IdeTweaksItem        *self,
+                                                          const char           *hidden_when);
+IDE_AVAILABLE_IN_ALL
 const char * const *ide_tweaks_item_get_keywords         (IdeTweaksItem        *self);
 IDE_AVAILABLE_IN_ALL
 void                ide_tweaks_item_set_keywords         (IdeTweaksItem        *self,
diff --git a/src/libide/tweaks/ide-tweaks-window.c b/src/libide/tweaks/ide-tweaks-window.c
index c5d1046a2..3181fd247 100644
--- a/src/libide/tweaks/ide-tweaks-window.c
+++ b/src/libide/tweaks/ide-tweaks-window.c
@@ -25,6 +25,7 @@
 #include <libpeas/peas.h>
 
 #include "ide-tweaks-addin.h"
+#include "ide-tweaks-item-private.h"
 #include "ide-tweaks-panel-private.h"
 #include "ide-tweaks-panel-list-private.h"
 #include "ide-tweaks-settings.h"
@@ -295,7 +296,8 @@ ide_tweaks_window_rebuild (IdeTweaksWindow *self)
        child != NULL;
        child = ide_tweaks_item_get_next_sibling (child))
     {
-      if (IDE_IS_TWEAKS_SETTINGS (child))
+      if (IDE_IS_TWEAKS_SETTINGS (child) &&
+          !_ide_tweaks_item_is_hidden (child, IDE_TWEAKS_ITEM (self->tweaks)))
         {
           IdeTweaksSettings *settings = IDE_TWEAKS_SETTINGS (child);
           const char *schema_id = ide_tweaks_settings_get_schema_id (settings);


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