[gnome-text-editor] sourceview: jump to cursor position after rooting widget



commit 14e494b99a8ac584586665f9e86628ab5c96d45b
Author: Christian Hergert <chergert redhat com>
Date:   Sat Jun 18 12:41:21 2022 -0700

    sourceview: jump to cursor position after rooting widget
    
    Doing so before hand will not necessarily scroll reliably. This is also
    done in Builder where the workaround appears to resolve it.
    
    Related #417

 src/editor-source-view.c | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
---
diff --git a/src/editor-source-view.c b/src/editor-source-view.c
index 38c46b3..ac37959 100644
--- a/src/editor-source-view.c
+++ b/src/editor-source-view.c
@@ -693,6 +693,40 @@ editor_source_view_snapshot_layer (GtkTextView      *text_view,
     editor_source_view_draw_search_bubbles (self, snapshot);
 }
 
+static gboolean
+editor_source_view_scroll_to_insert_in_idle_cb (gpointer user_data)
+{
+  EditorSourceView *self = user_data;
+  GtkTextBuffer *buffer;
+  GtkTextMark *mark;
+  GtkTextIter iter;
+
+  g_assert (EDITOR_IS_SOURCE_VIEW (self));
+
+  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self));
+  mark = gtk_text_buffer_get_insert (buffer);
+  gtk_text_buffer_get_iter_at_mark (buffer, &iter, mark);
+
+  editor_source_view_jump_to_iter (GTK_TEXT_VIEW (self), &iter, .25, TRUE, 1.0, 0.5);
+
+  return G_SOURCE_REMOVE;
+}
+
+static void
+editor_source_view_root (GtkWidget *widget)
+{
+  EditorSourceView *self = (EditorSourceView *)widget;
+
+  g_assert (EDITOR_IS_SOURCE_VIEW (self));
+
+  GTK_WIDGET_CLASS (editor_source_view_parent_class)->root (widget);
+
+  g_idle_add_full (G_PRIORITY_LOW,
+                   editor_source_view_scroll_to_insert_in_idle_cb,
+                   g_object_ref (self),
+                   g_object_unref);
+}
+
 static void
 editor_source_view_constructed (GObject *object)
 {
@@ -788,6 +822,8 @@ editor_source_view_class_init (EditorSourceViewClass *klass)
   object_class->get_property = editor_source_view_get_property;
   object_class->set_property = editor_source_view_set_property;
 
+  widget_class->root = editor_source_view_root;
+
   text_view_class->snapshot_layer = editor_source_view_snapshot_layer;
 
   properties [PROP_LINE_HEIGHT] =


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