[gnome-builder] editor: add simple goto line popover



commit 91370c82d697a39f724c52fcfc2527b0b8784fb0
Author: Christian Hergert <christian hergert me>
Date:   Fri Sep 18 16:10:32 2015 -0700

    editor: add simple goto line popover
    
    I don't really like that we use a popover per view for this, but at least
    we can say we have this now. We can optimize the widget overhead later.
    
    We should also go the extra mile to check that the line exists while
    typing them.
    
    We also need keyboard shortcuts.

 data/ui/gb-editor-view.ui           |   54 ++++++--------------
 src/editor/gb-editor-view-private.h |    2 +
 src/editor/gb-editor-view.c         |   92 +++++++++++++++++++++++++++++++++++
 3 files changed, 110 insertions(+), 38 deletions(-)
---
diff --git a/data/ui/gb-editor-view.ui b/data/ui/gb-editor-view.ui
index 8184db1..23e608a 100644
--- a/data/ui/gb-editor-view.ui
+++ b/data/ui/gb-editor-view.ui
@@ -98,17 +98,23 @@
               </object>
             </child>
             <child>
-              <object class="GtkLabel" id="cursor_label">
-                <property name="label">1:1</property>
-                <property name="valign">baseline</property>
+              <object class="GtkMenuButton">
+                <property name="popover">goto_line_popover</property>
+                <property name="focus-on-click">false</property>
                 <property name="visible">true</property>
                 <style>
                   <class name="dim-label"/>
+                  <class name="text-button"/>
+                  <class name="flat"/>
                 </style>
+                <child>
+                  <object class="GtkLabel" id="cursor_label">
+                    <property name="label">1:1</property>
+                    <property name="valign">baseline</property>
+                    <property name="visible">true</property>
+                  </object>
+                </child>
               </object>
-              <packing>
-                <property name="padding">6</property>
-              </packing>
             </child>
             <child>
               <object class="GtkSeparator">
@@ -145,37 +151,9 @@
       </object>
     </child>
   </object>
-  <object class="GtkPopover" id="symbols_popover">
-    <child>
-      <object class="GtkBox">
-        <property name="border-width">12</property>
-        <property name="orientation">vertical</property>
-        <property name="visible">true</property>
-        <style>
-          <class name="linked"/>
-        </style>
-        <child>
-          <object class="GtkSearchEntry" id="symbols_search_entry">
-            <property name="placeholder-text" translatable="yes">Search Symbols</property>
-            <property name="visible">true</property>
-          </object>
-        </child>
-        <child>
-          <object class="GbScrolledWindow">
-            <property name="max-content-height">500</property>
-            <property name="max-content-width">400</property>
-            <property name="min-content-width">200</property>
-            <property name="min-content-height">30</property>
-            <property name="shadow-type">in</property>
-            <property name="visible">true</property>
-            <child>
-              <object class="GtkListBox" id="symbols_listbox">
-                <property name="visible">true</property>
-              </object>
-            </child>
-          </object>
-        </child>
-      </object>
-    </child>
+  <object class="GbSimplePopover" id="goto_line_popover">
+    <property name="visible">true</property>
+    <property name="title" translatable="yes">Go to Line</property>
+    <property name="button-text" translatable="yes">Go</property>
   </object>
 </interface>
diff --git a/src/editor/gb-editor-view-private.h b/src/editor/gb-editor-view-private.h
index 6c278c4..2229acf 100644
--- a/src/editor/gb-editor-view-private.h
+++ b/src/editor/gb-editor-view-private.h
@@ -25,6 +25,7 @@
 #include "gb-editor-document.h"
 #include "gb-editor-frame.h"
 #include "gb-editor-tweak-widget.h"
+#include "gb-simple-popover.h"
 #include "gb-view.h"
 
 G_BEGIN_DECLS
@@ -47,6 +48,7 @@ struct _GbEditorView
   GtkProgressBar      *progress_bar;
   GtkMenuButton       *tweak_button;
   GbEditorTweakWidget *tweak_widget;
+  GbSimplePopover     *goto_line_popover;
 };
 
 G_END_DECLS
diff --git a/src/editor/gb-editor-view.c b/src/editor/gb-editor-view.c
index 13343aa..65ac03b 100644
--- a/src/editor/gb-editor-view.c
+++ b/src/editor/gb-editor-view.c
@@ -19,6 +19,7 @@
 #define G_LOG_DOMAIN "gb-editor-view"
 
 #include <glib/gi18n.h>
+#include <glib/gprintf.h>
 
 #include "gb-editor-frame-private.h"
 #include "gb-editor-view-actions.h"
@@ -26,6 +27,7 @@
 #include "gb-editor-view.h"
 #include "gb-editor-view-addin-private.h"
 #include "gb-editor-view-private.h"
+#include "gb-string.h"
 #include "gb-widget.h"
 
 #define SYMBOL_UPDATE_SECS 10
@@ -505,6 +507,76 @@ gb_editor_view_get_preferred_height (GtkWidget *widget,
 }
 
 static void
+gb_editor_view_goto_line_activate (GbEditorView    *self,
+                                   const gchar     *text,
+                                   GbSimplePopover *popover)
+{
+  gint64 value;
+
+  g_assert (GB_IS_EDITOR_VIEW (self));
+  g_assert (GB_IS_SIMPLE_POPOVER (popover));
+
+  if (!ide_str_empty0 (text))
+    {
+      value = g_ascii_strtoll (text, NULL, 10);
+
+      if ((value > 0) && (value < G_MAXINT))
+        {
+          GtkTextIter iter;
+          GtkTextBuffer *buffer = GTK_TEXT_BUFFER (self->document);
+
+          gtk_widget_grab_focus (GTK_WIDGET (self->frame1->source_view));
+          gtk_text_buffer_get_iter_at_line (buffer, &iter, value - 1);
+          gtk_text_buffer_select_range (buffer, &iter, &iter);
+          ide_source_view_scroll_to_iter (self->frame1->source_view,
+                                          &iter, 0.25, TRUE, 1.0, 0.5, TRUE);
+        }
+    }
+}
+
+static gboolean
+gb_editor_view_goto_line_insert_text (GbEditorView    *self,
+                                      guint            position,
+                                      const gchar     *chars,
+                                      guint            n_chars,
+                                      GbSimplePopover *popover)
+{
+  g_assert (GB_IS_EDITOR_VIEW (self));
+  g_assert (GB_IS_SIMPLE_POPOVER (popover));
+  g_assert (chars != NULL);
+
+  for (; *chars; chars = g_utf8_next_char (chars))
+    {
+      if (!g_unichar_isdigit (g_utf8_get_char (chars)))
+        return GDK_EVENT_STOP;
+    }
+
+  return GDK_EVENT_PROPAGATE;
+}
+
+static void
+gb_editor_view_goto_line_changed (GbEditorView    *self,
+                                  GbSimplePopover *popover)
+{
+  const gchar *text;
+
+  g_assert (GB_IS_EDITOR_VIEW (self));
+  g_assert (GB_IS_SIMPLE_POPOVER (popover));
+
+  text = gb_simple_popover_get_text (popover);
+
+  if (gb_str_empty0 (text))
+    {
+      gb_simple_popover_set_ready (popover, FALSE);
+    }
+  else
+    {
+      /* TODO: check if the line exists */
+      gb_simple_popover_set_ready (popover, TRUE);
+    }
+}
+
+static void
 gb_editor_view__extension_added (PeasExtensionSet  *set,
                                  PeasPluginInfo    *info,
                                  GbEditorViewAddin *addin,
@@ -681,9 +753,11 @@ gb_editor_view_class_init (GbEditorViewClass *klass)
   GB_WIDGET_CLASS_BIND (klass, GbEditorView, progress_bar);
   GB_WIDGET_CLASS_BIND (klass, GbEditorView, tweak_button);
   GB_WIDGET_CLASS_BIND (klass, GbEditorView, tweak_widget);
+  GB_WIDGET_CLASS_BIND (klass, GbEditorView, goto_line_popover);
 
   g_type_ensure (GB_TYPE_EDITOR_FRAME);
   g_type_ensure (GB_TYPE_EDITOR_TWEAK_WIDGET);
+  g_type_ensure (GB_TYPE_SIMPLE_POPOVER);
 }
 
 static void
@@ -700,4 +774,22 @@ gb_editor_view_init (GbEditorView *self)
                            G_CALLBACK (gb_editor_view_hide_reload_bar),
                            self,
                            G_CONNECT_SWAPPED);
+
+  g_signal_connect_object (self->goto_line_popover,
+                           "activate",
+                           G_CALLBACK (gb_editor_view_goto_line_activate),
+                           self,
+                           G_CONNECT_SWAPPED);
+
+  g_signal_connect_object (self->goto_line_popover,
+                           "insert-text",
+                           G_CALLBACK (gb_editor_view_goto_line_insert_text),
+                           self,
+                           G_CONNECT_SWAPPED);
+
+  g_signal_connect_object (self->goto_line_popover,
+                           "changed",
+                           G_CALLBACK (gb_editor_view_goto_line_changed),
+                           self,
+                           G_CONNECT_SWAPPED);
 }


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