[gnome-builder/wip/gtk4-port: 164/736] libide/gui: start on transition for preferences




commit 526d583e1d22a1225998c600a3838908d986b15d
Author: Christian Hergert <chergert redhat com>
Date:   Tue Mar 29 02:49:34 2022 -0700

    libide/gui: start on transition for preferences

 src/libide/gui/ide-preferences-builtin.c | 120 +++++++++++++++++++++++++++----
 src/libide/gui/ide-preferences-window.c  |  92 +++++++++++++++++++++++-
 src/libide/gui/ide-preferences-window.h  |  11 +++
 3 files changed, 209 insertions(+), 14 deletions(-)
---
diff --git a/src/libide/gui/ide-preferences-builtin.c b/src/libide/gui/ide-preferences-builtin.c
index 5fa6d1f5b..e011495ba 100644
--- a/src/libide/gui/ide-preferences-builtin.c
+++ b/src/libide/gui/ide-preferences-builtin.c
@@ -29,8 +29,8 @@
 #include <libpeas/peas.h>
 
 #include "ide-preferences-builtin-private.h"
-#include "ide-preferences-language-row-private.h"
 
+#if 0
 static gint
 sort_plugin_info (gconstpointer a,
                   gconstpointer b)
@@ -570,20 +570,114 @@ ide_preferences_builtin_register_sdks (DzlPreferences *preferences)
   /* only the page goes here, plugins will fill in the details */
   dzl_preferences_add_page (preferences, "sdk", _("SDKs"), 550);
 }
+#endif
+
+static const IdePreferencePageEntry pages[] = {
+  { NULL, "visual", "appearance", "org.gnome.Builder-appearance-symbolic", 0, "Appearance" },
+  { NULL, "visual", "editing", "org.gnome.Builder-editing-symbolic", 10, "Editing" },
+  { NULL, "visual", "keyboard", "org.gnome.Builder-shortcuts-symbolic", 20, "Shortcuts" },
+  { NULL, "code", "languages", "org.gnome.Builder-languages-symbolic", 100, "Languages" },
+  { NULL, "code", "completion", "org.gnome.Builder-completion-symbolic", 110, "Completion" },
+  { NULL, "code", "insight", "org.gnome.Builder-diagnostics-symbolic", 120, "Diagnostics" },
+  { NULL, "projects", "projects", "org.gnome.Builder-projects-symbolic", 200, "Projects" },
+  { NULL, "tools", "build", "org.gnome.Builder-build-symbolic", 300, "Build" },
+  { NULL, "tools", "debug", "org.gnome.Builder-debugger-symbolic", 310, "Debugger" },
+  { NULL, "tools", "commands", "org.gnome.Builder-command-symbolic", 320, "Commands" },
+  { NULL, "tools", "sdks", "org.gnome.Builder-sdk-symbolic", 500, "SDKs" },
+  { NULL, "plugins", "plugins", "org.gnome.Builder-plugins-symbolic", 600, "Plugins" },
+};
+
+static const IdePreferenceGroupEntry groups[] = {
+  { "appearance", "style", 0, "Appearance" },
+  { "appearance", "preview", 0, "Style" },
+  { "appearance", "schemes", 10, NULL },
+  { "appearance", "font", 20, NULL },
+  { "appearance", "accessories", 20, NULL },
+
+  { "languages/*", "general", 0, "General" },
+  { "languages/*", "margins", 10, "Margins" },
+  { "languages/*", "spacing", 20, "Spacing" },
+  { "languages/*", "indentation", 30, "Indentation" },
+};
+
+static const IdePreferenceItemEntry lang_items[] = {
+  { "languages/*", "general", "trim", 0, ide_preferences_window_toggle, "Trim Trailing Whitespace", "Upon 
saving, trailing whitepsace from modified lines will be trimmed.", "org.gnome.builder.editor.language", "/*", 
"trim-trailing-whitespace" },
+  { "languages/*", "general", "overwrite", 0, ide_preferences_window_toggle, "Overwrite Braces", "Overwrite 
closing braces", "org.gnome.builder.editor.language", "/*", "overwrite-brances" },
+  { "languages/*", "general", "insert-matching", 0, ide_preferences_window_toggle, "Insert Matching Brace", 
"Insert matching character for [[(\"'", "org.gnome.builder.editor.language", "/*", "insert-matching-brace" },
+  { "languages/*", "general", "insert-trailing", 0, ide_preferences_window_toggle, "Insert Trailing 
Newline", "Ensure files end with a newline", "org.gnome.builder.editor.language", "/*", 
"insert-trailing-newline" },
+
+  { "languages/*", "margins", "show-right-margin", 0, ide_preferences_window_toggle, "Show right margin", 
"Display a margin in the editor to indicate maximum desired width", "org.gnome.builder.editor.language", 
"/*", "show-right-margin" },
+
+#if 0
+  { "languages/*", "spacing", "before-parens", 0, ide_preferences_window_toggle, "Prefer a space before 
opening parentheses" },
+  { "languages/*", "spacing", "before-brackets", 0, ide_preferences_window_toggle, "Prefer a space before 
opening brackets" },
+  { "languages/*", "spacing", "before-braces", 0, ide_preferences_window_toggle, "Prefer a space before 
opening braces" },
+  { "languages/*", "spacing", "before-angles", 0, ide_preferences_window_toggle, "Prefer a space before 
opening angles" },
+#endif
+
+  { "languages/*", "indentation", "insert-spaces", 0, ide_preferences_window_toggle, "Insert spaces instead 
of tabs", "Prefer spaces over tabs", "org.gnome.builder.editor.language", "/*", 
"insert-spaces-instead-of-tabs" },
+  { "languages/*", "indentation", "auto-indent", 0, ide_preferences_window_toggle, "Automatically Indent", 
"Format source code as you type", "org.gnome.builder.editor.language", "/*", "auto-indent" },
+};
+
+static int
+compare_section (gconstpointer a,
+                 gconstpointer b)
+{
+  const IdePreferencePageEntry *pagea = a;
+  const IdePreferencePageEntry *pageb = b;
+
+  return g_strcmp0 (pagea->section, pageb->section);
+}
+
+static void
+ide_preferences_builtin_add_languages (IdePreferencesWindow *window)
+{
+  GtkSourceLanguageManager *langs;
+  const char * const *lang_ids;
+  IdePreferencePageEntry *lpages;
+  guint j = 0;
+
+  g_assert (IDE_IS_PREFERENCES_WINDOW (window));
+
+  langs = gtk_source_language_manager_get_default ();
+  lang_ids = gtk_source_language_manager_get_language_ids (langs);
+  lpages = g_new0 (IdePreferencePageEntry, g_strv_length ((char **)lang_ids));
+
+  for (guint i = 0; lang_ids[i]; i++)
+    {
+      GtkSourceLanguage *l = gtk_source_language_manager_get_language (langs, lang_ids[i]);
+      IdePreferencePageEntry *page;
+      char name[256];
+
+      if (gtk_source_language_get_hidden (l))
+        continue;
+
+      page = &lpages[j++];
+
+      g_snprintf (name, sizeof name, "languages/%s", lang_ids[i]);
+
+      page->parent = "languages";
+      page->section = gtk_source_language_get_section (l);
+      page->name = g_intern_string (name);
+      page->icon_name = NULL;
+      page->title = gtk_source_language_get_name (l);
+    }
+
+  qsort (lpages, j, sizeof *lpages, compare_section);
+  for (guint i = 0; i < j; i++)
+    lpages[i].priority = i;
+
+  ide_preferences_window_add_pages (window, lpages, j, NULL);
+  ide_preferences_window_add_items (window, lang_items, G_N_ELEMENTS (lang_items), window, NULL);
+
+  g_free (lpages);
+}
 
 void
 _ide_preferences_builtin_register (IdePreferencesWindow *window)
 {
-  ide_preferences_builtin_register_appearance (preferences);
-  ide_preferences_builtin_register_editor (preferences);
-  ide_preferences_builtin_register_languages (preferences);
-  ide_preferences_builtin_register_code_insight (preferences);
-  ide_preferences_builtin_register_completion (preferences);
-  ide_preferences_builtin_register_snippets (preferences);
-  ide_preferences_builtin_register_keyboard (preferences);
-  ide_preferences_builtin_register_plugins (preferences);
-  ide_preferences_builtin_register_build (preferences);
-  ide_preferences_builtin_register_projects (preferences);
-  //ide_preferences_builtin_register_vcs (preferences);
-  ide_preferences_builtin_register_sdks (preferences);
+  ide_preferences_window_add_pages (window, pages, G_N_ELEMENTS (pages), NULL);
+  ide_preferences_window_add_groups (window, groups, G_N_ELEMENTS (groups), NULL);
+
+  ide_preferences_builtin_add_languages (window);
 }
diff --git a/src/libide/gui/ide-preferences-window.c b/src/libide/gui/ide-preferences-window.c
index 61a17f8a3..ca8adc1fe 100644
--- a/src/libide/gui/ide-preferences-window.c
+++ b/src/libide/gui/ide-preferences-window.c
@@ -37,6 +37,8 @@ struct _IdePreferencesWindow
   GtkStack           *pages_stack;
   AdwWindowTitle     *pages_title;
 
+  GHashTable         *settings;
+
   const IdePreferencePageEntry *current_page;
 
   guint rebuild_source;
@@ -99,6 +101,59 @@ drop_page_free (gpointer data)
   g_free (drop);
 }
 
+static GSettings *
+ide_preferences_window_get_settings (IdePreferencesWindow         *self,
+                                     const IdePreferenceItemEntry *entry)
+{
+  g_autofree char *key = NULL;
+  g_autofree char *path = NULL;
+  GSettings *settings;
+
+  g_assert (IDE_IS_PREFERENCES_WINDOW (self));
+
+  if (entry->schema_id == NULL)
+    return NULL;
+
+  if (entry->path != NULL &&
+      self->current_page != NULL &&
+      g_str_has_suffix (entry->path, "/*"))
+    {
+      const char *subkey = strrchr (self->current_page->name, '/');
+
+      if (subkey != NULL)
+        {
+          guint j = strlen (entry->path) - 1;
+          char c;
+
+          path = g_malloc0 (strlen (entry->path) + strlen (subkey) + 1);
+          memcpy (path, entry->path, j);
+          while ((c = *(subkey++)))
+            path[j++] = c;
+          path[j] = 0;
+        }
+    }
+
+  if (path == NULL && entry->path != NULL)
+    path = g_strdup (entry->path);
+
+  if (path == NULL)
+    key = g_strdup_printf ("%s:/", entry->schema_id);
+  else
+    key = g_strdup_printf ("%s:%s", entry->schema_id, path);
+
+  if (!(settings = g_hash_table_lookup (self->settings, key)))
+    {
+      if (path)
+        settings = g_settings_new_with_path (entry->schema_id, path);
+      else
+        settings = g_settings_new (entry->schema_id);
+
+      g_hash_table_insert (self->settings, g_steal_pointer (&key), settings);
+    }
+
+  return settings;
+}
+
 static gboolean
 entry_matches (const char *request,
                const char *current)
@@ -208,6 +263,7 @@ ide_preferences_window_dispose (GObject *object)
 {
   IdePreferencesWindow *self = (IdePreferencesWindow *)object;
 
+  g_clear_pointer (&self->settings, g_hash_table_unref);
   g_clear_handle_id (&self->rebuild_source, g_source_remove);
 
   if (self->info.data != NULL)
@@ -249,12 +305,14 @@ ide_preferences_window_class_init (IdePreferencesWindowClass *klass)
 static void
 ide_preferences_window_init (IdePreferencesWindow *self)
 {
-  gtk_widget_init_template (GTK_WIDGET (self));
+  self->settings = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
 
   self->info.pages = g_ptr_array_new_with_free_func (g_free);
   self->info.groups = g_ptr_array_new_with_free_func (g_free);
   self->info.items = g_ptr_array_new_with_free_func (g_free);
   self->info.data = g_array_new (FALSE, FALSE, sizeof (DataDestroy));
+
+  gtk_widget_init_template (GTK_WIDGET (self));
 }
 
 GtkWidget *
@@ -763,3 +821,35 @@ ide_preferences_window_add_item (IdePreferencesWindow  *self,
 
   ide_preferences_window_queue_rebuild (self);
 }
+
+void
+ide_preferences_window_toggle (const char                   *page_name,
+                               const IdePreferenceItemEntry *entry,
+                               AdwPreferencesGroup          *group,
+                               gpointer                      user_data)
+{
+  IdePreferencesWindow *self = user_data;
+  AdwActionRow *row;
+  GtkSwitch *child;
+  GSettings *settings;
+
+  g_return_if_fail (entry != NULL);
+  g_return_if_fail (ADW_IS_PREFERENCES_GROUP (group));
+  g_return_if_fail (IDE_IS_PREFERENCES_WINDOW (self));
+
+  if (!(settings = ide_preferences_window_get_settings (self, entry)))
+    return;
+
+  child = g_object_new (GTK_TYPE_SWITCH,
+                        "valign", GTK_ALIGN_CENTER,
+                        NULL);
+  row = g_object_new (ADW_TYPE_ACTION_ROW,
+                      "title", entry->title,
+                      "subtitle", entry->subtitle,
+                      "activatable-widget", child,
+                      NULL);
+  adw_preferences_group_add (group, GTK_WIDGET (row));
+  adw_action_row_add_suffix (row, GTK_WIDGET (child));
+
+  g_settings_bind (settings, entry->key, child, "active", G_SETTINGS_BIND_DEFAULT);
+}
diff --git a/src/libide/gui/ide-preferences-window.h b/src/libide/gui/ide-preferences-window.h
index d4667aa72..b72eec59a 100644
--- a/src/libide/gui/ide-preferences-window.h
+++ b/src/libide/gui/ide-preferences-window.h
@@ -62,8 +62,12 @@ struct _IdePreferenceItemEntry
   IdePreferenceCallback callback;
 
   /* Callback specific data */
+
+  /* Title/Subtitle for helper functions */
   const char *title;
   const char *subtitle;
+
+  /* Schema info for helper functions */
   const char *schema_id;
   const char *path;
   const char *key;
@@ -104,5 +108,12 @@ void       ide_preferences_window_add_item   (IdePreferencesWindow          *sel
                                               IdePreferenceCallback          callback,
                                               gpointer                       user_data,
                                               GDestroyNotify                 user_data_destroy);
+IDE_AVAILABLE_IN_ALL
+void       ide_preferences_window_toggle     (const char                    *page_name,
+                                              const IdePreferenceItemEntry  *entry,
+                                              AdwPreferencesGroup           *group,
+                                              gpointer                       user_data);
+
+
 
 G_END_DECLS


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