[gnome-builder/wip/chergert/snippets-viewer: 4/4] prefs: add simple read-only list of snippets by language



commit 8eb80466d4430978eb14f498758f574102304aac
Author: Christian Hergert <christian hergert me>
Date:   Fri Aug 21 21:58:25 2015 -0700

    prefs: add simple read-only list of snippets by language
    
    We don't have snippet editing yet, but we can at least allow the user to
    discover what snippets are available.

 data/theme/shared.css                  |    9 ++++
 data/ui/gb-editor-settings-widget.ui   |   37 ++++++++++++++++
 src/editor/gb-editor-settings-widget.c |   74 +++++++++++++++++++++++++++++++-
 3 files changed, 119 insertions(+), 1 deletions(-)
---
diff --git a/data/theme/shared.css b/data/theme/shared.css
index 53e2958..0f0c577 100644
--- a/data/theme/shared.css
+++ b/data/theme/shared.css
@@ -39,6 +39,15 @@ GbPreferencesSwitch:prelight {
 }
 
 
+GbEditorSettingsWidget GtkListBoxRow {
+  padding: 6px;
+  border-bottom: 1px solid @borders;
+}
+GbEditorSettingsWidget GtkListBoxRow:last-child {
+  border-bottom: none;
+}
+
+
 /*
  * Editor Floating Search Entry.
  */
diff --git a/data/ui/gb-editor-settings-widget.ui b/data/ui/gb-editor-settings-widget.ui
index 58739fd..8ee12fa 100644
--- a/data/ui/gb-editor-settings-widget.ui
+++ b/data/ui/gb-editor-settings-widget.ui
@@ -242,6 +242,43 @@
         <property name="top_attach">1</property>
       </packing>
     </child>
+    <child>
+      <object class="GtkBox" id="snippets_container">
+        <property name="visible">False</property>
+        <property name="can_focus">False</property>
+        <property name="orientation">vertical</property>
+        <property name="spacing">3</property>
+        <property name="hexpand">True</property>
+        <child>
+          <object class="GtkLabel">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="halign">start</property>
+            <property name="label" translatable="yes">Snippets</property>
+            <property name="margin-bottom">6</property>
+            <attributes>
+                <attribute name="weight" value="bold" />
+            </attributes>
+          </object>
+        </child>
+        <child>
+          <object class="GtkFrame">
+            <property name="margin-start">12</property>
+            <property name="visible">true</property>
+            <child>
+              <object class="GtkListBox" id="snippets">
+                <property name="selection-mode">none</property>
+                <property name="visible">true</property>
+              </object>
+            </child>
+          </object>
+        </child>
+      </object>
+      <packing>
+        <property name="left_attach">0</property>
+        <property name="top_attach">2</property>
+      </packing>
+    </child>
   </template>
   <object class="GtkSizeGroup" id="sizegroup1">
     <property name="mode">vertical</property>
diff --git a/src/editor/gb-editor-settings-widget.c b/src/editor/gb-editor-settings-widget.c
index ea3c1cb..bfb7908 100644
--- a/src/editor/gb-editor-settings-widget.c
+++ b/src/editor/gb-editor-settings-widget.c
@@ -33,6 +33,8 @@ struct _GbEditorSettingsWidget
   GtkCheckButton *insert_spaces_instead_of_tabs;
   GtkCheckButton *overwrite_braces;
   GtkCheckButton *show_right_margin;
+  GtkListBox     *snippets;
+  GtkBox         *snippets_container;
   GtkSpinButton  *right_margin_position;
   GtkSpinButton  *tab_width;
   GtkCheckButton *trim_trailing_whitespace;
@@ -56,14 +58,79 @@ gb_editor_settings_widget_get_language (GbEditorSettingsWidget *widget)
   return widget->language;
 }
 
+static void
+foreach_cb (gpointer data,
+            gpointer user_data)
+{
+  GtkListBoxRow *row;
+  GtkListBox *box = user_data;
+  IdeSourceSnippet *snippet = data;
+  GtkBox *hbox;
+  GtkLabel *label;
+  const gchar *trigger;
+  const gchar *desc;
+
+  g_assert (GTK_IS_LIST_BOX (box));
+
+  trigger = ide_source_snippet_get_trigger (snippet);
+  desc = ide_source_snippet_get_description (snippet);
+
+  row = g_object_new (GTK_TYPE_LIST_BOX_ROW,
+                      "visible", TRUE,
+                      NULL);
+  hbox = g_object_new (GTK_TYPE_BOX,
+                       "visible", TRUE,
+                       "orientation", GTK_ORIENTATION_HORIZONTAL,
+                       NULL);
+  label = g_object_new (GTK_TYPE_LABEL,
+                        "label", trigger,
+                        "hexpand", TRUE,
+                        "visible", TRUE,
+                        "xalign", 0.0f,
+                        NULL);
+  gtk_container_add (GTK_CONTAINER (hbox), GTK_WIDGET (label));
+  label = g_object_new (GTK_TYPE_LABEL,
+                        "label", desc,
+                        "visible", TRUE,
+                        "xalign", 1.0f,
+                        NULL);
+  gb_widget_add_style_class (GTK_WIDGET (label), "dim-label");
+  gtk_container_add (GTK_CONTAINER (hbox), GTK_WIDGET (label));
+  gtk_container_add (GTK_CONTAINER (row), GTK_WIDGET (hbox));
+  gtk_container_add (GTK_CONTAINER (box), GTK_WIDGET (row));
+}
+
+static void
+load_snippets_cb (GObject      *object,
+                  GAsyncResult *result,
+                  gpointer      user_data)
+{
+  IdeSourceSnippetsManager *sm = (IdeSourceSnippetsManager *)object;
+  g_autoptr(GbEditorSettingsWidget) self = user_data;
+  IdeSourceSnippets *snippets;
+
+  if (!ide_source_snippets_manager_load_finish (sm, result, NULL))
+    return;
+
+  snippets = ide_source_snippets_manager_get_for_language_id (sm, self->language);
+  if (snippets == NULL)
+    return;
+
+  ide_source_snippets_foreach (snippets, NULL, foreach_cb, self->snippets);
+
+  if (ide_source_snippets_count (snippets) > 0)
+    gtk_widget_show (GTK_WIDGET (self->snippets_container));
+}
+
 void
 gb_editor_settings_widget_set_language (GbEditorSettingsWidget *widget,
                                         const gchar            *language)
 {
   g_return_if_fail (GB_IS_EDITOR_SETTINGS_WIDGET (widget));
 
-  if (language != widget->language)
+  if (!ide_str_equal0 (language, widget->language))
     {
+      IdeSourceSnippetsManager *sm;
       gchar *path;
 
       g_free (widget->language);
@@ -102,6 +169,9 @@ gb_editor_settings_widget_set_language (GbEditorSettingsWidget *widget,
                        widget->trim_trailing_whitespace, "active",
                        G_SETTINGS_BIND_DEFAULT);
 
+      sm = g_object_new (IDE_TYPE_SOURCE_SNIPPETS_MANAGER, NULL);
+      ide_source_snippets_manager_load_async (sm, NULL, load_snippets_cb, g_object_ref (widget));
+
       g_object_notify_by_pspec (G_OBJECT (widget), gParamSpecs [PROP_LANGUAGE]);
     }
 }
@@ -171,6 +241,8 @@ gb_editor_settings_widget_class_init (GbEditorSettingsWidgetClass *klass)
   GB_WIDGET_CLASS_BIND (klass, GbEditorSettingsWidget, right_margin_position);
   GB_WIDGET_CLASS_BIND (klass, GbEditorSettingsWidget, overwrite_braces);
   GB_WIDGET_CLASS_BIND (klass, GbEditorSettingsWidget, show_right_margin);
+  GB_WIDGET_CLASS_BIND (klass, GbEditorSettingsWidget, snippets);
+  GB_WIDGET_CLASS_BIND (klass, GbEditorSettingsWidget, snippets_container);
   GB_WIDGET_CLASS_BIND (klass, GbEditorSettingsWidget, tab_width);
   GB_WIDGET_CLASS_BIND (klass, GbEditorSettingsWidget, trim_trailing_whitespace);
 


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