[gnome-builder] libide/gui: add plugin page tweaks on demand



commit ef76aa6ef4069972813bfce4d3a1248ce9eaf833
Author: Christian Hergert <chergert redhat com>
Date:   Wed Aug 17 18:03:19 2022 -0700

    libide/gui: add plugin page tweaks on demand

 src/libide/gui/ide-application-actions.c |  15 ++--
 src/libide/gui/ide-application-plugins.c | 132 ++++++++++++++++++++++++-------
 src/libide/gui/ide-application-private.h |   4 +-
 3 files changed, 115 insertions(+), 36 deletions(-)
---
diff --git a/src/libide/gui/ide-application-actions.c b/src/libide/gui/ide-application-actions.c
index b95de0b76..0029cbc4d 100644
--- a/src/libide/gui/ide-application-actions.c
+++ b/src/libide/gui/ide-application-actions.c
@@ -27,6 +27,7 @@
 
 #include <ide-build-ident.h>
 
+#include <libide-plugins.h>
 #include <libide-projects.h>
 #include <libide-tweaks.h>
 
@@ -38,6 +39,8 @@
 #include "ide-primary-workspace.h"
 #include "ide-support-private.h"
 
+#include "ide-plugin-section-private.h"
+
 static void
 ide_application_actions_tweaks (GSimpleAction *action,
                                 GVariant      *parameter,
@@ -48,11 +51,11 @@ ide_application_actions_tweaks (GSimpleAction *action,
     "resource:///org/gnome/libide-gui/tweaks-plugins.ui",
   };
   IdeApplication *self = user_data;
-  g_autoptr(GListModel) plugins = NULL;
   g_autoptr(IdeTweaks) tweaks = NULL;
   IdeTweaksWindow *window;
-  GtkWindow *toplevel = NULL;
+  IdeTweaksPage *plugins_page;
   const GList *windows;
+  GtkWindow *toplevel = NULL;
 
   IDE_ENTRY;
 
@@ -77,10 +80,6 @@ ide_application_actions_tweaks (GSimpleAction *action,
 
   tweaks = ide_tweaks_new ();
 
-  /* Give access to all the known plugins */
-  plugins = _ide_application_list_plugins (self);
-  ide_tweaks_expose_object (tweaks, "Plugins", G_OBJECT (plugins));
-
   /* Load our base tweaks scaffolding */
   for (guint i = 0; i < G_N_ELEMENTS (tweaks_resources); i++)
     {
@@ -93,6 +92,10 @@ ide_application_actions_tweaks (GSimpleAction *action,
         g_critical ("Failed to load tweaks: %s", error->message);
     }
 
+  /* Expose Plugin Toggles */
+  plugins_page = IDE_TWEAKS_PAGE (ide_tweaks_get_object (tweaks, "plugins_page"));
+  _ide_application_add_plugin_tweaks (self, plugins_page);
+
   /* Now display window */
   window = g_object_new (IDE_TYPE_TWEAKS_WINDOW,
                          "tweaks", tweaks,
diff --git a/src/libide/gui/ide-application-plugins.c b/src/libide/gui/ide-application-plugins.c
index 6d6d399ba..5224e35fd 100644
--- a/src/libide/gui/ide-application-plugins.c
+++ b/src/libide/gui/ide-application-plugins.c
@@ -28,6 +28,8 @@
 #include "ide-application-addin.h"
 #include "ide-application-private.h"
 
+#include "ide-plugin-section-private.h"
+
 static void
 ide_application_changed_plugin_cb (GSettings      *settings,
                                    const gchar    *key,
@@ -501,41 +503,113 @@ _ide_application_unload_addins (IdeApplication *self)
   g_clear_object (&self->addins);
 }
 
-/**
- * ide_application_list_plugins:
- * @self: a #IdeApplication
- *
- * Gets a list of plugins.
- *
- * The result contains instances of #IdePlugin as #PeasPluginInfo is a boxed
- * type which cannot be used with #GListModel.
- *
- * Returns: (transfer full): a #GListModel of IdePlugin
- */
-GListModel *
-_ide_application_list_plugins (IdeApplication *self)
+static GtkWidget *
+create_plugin_toggle (IdeTweaksWidget *instance,
+                      IdeTweaksWidget *widget,
+                      IdePlugin       *plugin)
 {
-  GListStore *store;
-  const GList *plugins;
+  g_autofree char *schema_path = NULL;
+  g_autoptr(GSettings) settings = NULL;
+  AdwActionRow *row;
+  GtkSwitch *toggle;
+  const char *id;
+
+  g_assert (IDE_IS_TWEAKS_WIDGET (instance));
+  g_assert (IDE_IS_TWEAKS_WIDGET (widget));
+  g_assert (IDE_IS_PLUGIN (plugin));
+
+  id = ide_plugin_get_id (plugin);
+
+  toggle = g_object_new (GTK_TYPE_SWITCH,
+                         "valign", GTK_ALIGN_CENTER,
+                         NULL);
+  row = g_object_new (ADW_TYPE_ACTION_ROW,
+                      "title", ide_plugin_get_name (plugin),
+                      "subtitle", ide_plugin_get_description (plugin),
+                      "activatable-widget", toggle,
+                      NULL);
+  adw_action_row_add_suffix (row, GTK_WIDGET (toggle));
+
+  schema_path = g_strdup_printf ("/org/gnome/builder/plugins/%s/", id);
+  settings = g_settings_new_with_path ("org.gnome.builder.plugin", schema_path);
+  g_object_set_data_full (G_OBJECT (row),
+                          "SETTINGS",
+                          g_object_ref (settings),
+                          g_object_unref);
+
+  g_settings_bind (settings, "enabled", toggle, "active", G_SETTINGS_BIND_DEFAULT);
+
+  return GTK_WIDGET (row);
+}
 
-  g_return_val_if_fail (IDE_IS_APPLICATION (self), NULL);
+void
+_ide_application_add_plugin_tweaks (IdeApplication *self,
+                                    IdeTweaksPage  *page)
+{
+  g_autoptr(GHashTable) categories = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_object_unref);
+  IdeTweaksPage *category_page;
+  GListModel *sections;
+  guint n_sections;
 
-  store = g_list_store_new (IDE_TYPE_PLUGIN);
-  plugins = peas_engine_get_plugin_list (peas_engine_get_default ());
+  g_return_if_fail (IDE_IS_APPLICATION (self));
+  g_return_if_fail (IDE_IS_TWEAKS_PAGE (page));
 
-  for (const GList *iter = plugins; iter; iter = iter->next)
+  sections = _ide_plugin_section_get_all ();
+  n_sections = g_list_model_get_n_items (sections);
+
+  for (guint i = 0; i < n_sections; i++)
     {
-      const PeasPluginInfo *plugin_info = iter->data;
-      g_autoptr(IdePlugin) plugin = NULL;
+      g_autoptr(IdeTweaksSection) t_section = ide_tweaks_section_new ();
+      g_autoptr(IdePluginSection) section = g_list_model_get_item (sections, i);
+      GListModel *plugins = ide_plugin_section_get_plugins (section);
+      guint n_plugins;
 
-      if (peas_plugin_info_is_hidden (plugin_info))
-        continue;
+      ide_tweaks_section_set_title (t_section,
+                                    ide_plugin_section_get_id (section));
+      ide_tweaks_item_insert_after (IDE_TWEAKS_ITEM (t_section),
+                                    IDE_TWEAKS_ITEM (page),
+                                    NULL);
 
-      plugin = g_object_new (IDE_TYPE_PLUGIN,
-                             "info", plugin_info,
-                             NULL);
-      g_list_store_append (store, plugin);
-    }
+      n_plugins = g_list_model_get_n_items (plugins);
 
-  return G_LIST_MODEL (store);
+      for (guint j = 0; j < n_plugins; j++)
+        {
+          g_autoptr(IdePlugin) plugin = g_list_model_get_item (plugins, j);
+          const char *category_id = ide_plugin_get_category_id (plugin);
+          const char *category = ide_plugin_get_category (plugin);
+          g_autoptr(IdeTweaksSettings) settings = NULL;
+          g_autoptr(IdeTweaksWidget) widget = NULL;
+          IdeTweaksGroup *group;
+
+          if (!(category_page = g_hash_table_lookup (categories, category)))
+            {
+              g_autofree char *page_id = g_strdup_printf ("plugin_%s_page", category_id);
+              g_autoptr(IdeTweaksGroup) first_group = ide_tweaks_group_new ();
+
+              category_page = ide_tweaks_page_new ();
+              GTK_BUILDABLE_GET_IFACE (category_page)->set_id (GTK_BUILDABLE (category_page), page_id);
+              ide_tweaks_page_set_title (category_page, category);
+              ide_tweaks_page_set_show_icon (category_page, FALSE);
+              ide_tweaks_item_insert_after (IDE_TWEAKS_ITEM (category_page),
+                                            IDE_TWEAKS_ITEM (t_section),
+                                            NULL);
+              ide_tweaks_item_insert_after (IDE_TWEAKS_ITEM (first_group),
+                                            IDE_TWEAKS_ITEM (category_page),
+                                            NULL);
+              g_hash_table_insert (categories, (char *)category, category_page);
+            }
+
+          group = IDE_TWEAKS_GROUP (ide_tweaks_item_get_first_child (IDE_TWEAKS_ITEM (category_page)));
+          widget = ide_tweaks_widget_new ();
+          g_signal_connect_object (widget,
+                                   "create-for-item",
+                                   G_CALLBACK (create_plugin_toggle),
+                                   plugin,
+                                   0);
+
+          ide_tweaks_item_insert_after (IDE_TWEAKS_ITEM (widget),
+                                        IDE_TWEAKS_ITEM (group),
+                                        NULL);
+        }
+    }
 }
diff --git a/src/libide/gui/ide-application-private.h b/src/libide/gui/ide-application-private.h
index 69186b18c..9cf7b2f07 100644
--- a/src/libide/gui/ide-application-private.h
+++ b/src/libide/gui/ide-application-private.h
@@ -22,6 +22,7 @@
 
 #include <libide-core.h>
 #include <libide-gtk.h>
+#include <libide-tweaks.h>
 
 #include <libpeas/peas.h>
 
@@ -119,6 +120,7 @@ void            _ide_application_add_resources            (IdeApplication
                                                            const char              *path);
 void            _ide_application_remove_resources         (IdeApplication          *self,
                                                            const char              *path);
-GListModel     *_ide_application_list_plugins             (IdeApplication          *self);
+void            _ide_application_add_plugin_tweaks        (IdeApplication          *self,
+                                                           IdeTweaksPage           *page);
 
 G_END_DECLS


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