[gtksourceview/wip/chergert/vim: 73/293] improve full page scroll




commit 4fec66dbf99431551a185dbbf9dab99a7d128470
Author: Christian Hergert <chergert redhat com>
Date:   Mon Oct 25 23:20:17 2021 -0700

    improve full page scroll

 gtksourceview/vim/gtk-source-vim-state.c | 96 +++++++++++++++++++++++++++-----
 1 file changed, 82 insertions(+), 14 deletions(-)
---
diff --git a/gtksourceview/vim/gtk-source-vim-state.c b/gtksourceview/vim/gtk-source-vim-state.c
index 190df401..9ebc56c4 100644
--- a/gtksourceview/vim/gtk-source-vim-state.c
+++ b/gtksourceview/vim/gtk-source-vim-state.c
@@ -551,10 +551,9 @@ gtk_source_vim_state_scroll_line (GtkSourceVimState *self,
        gtk_text_view_place_cursor_onscreen (GTK_TEXT_VIEW (view));
 }
 
-static void
-_gtk_source_vim_state_scroll_page (GtkSourceVimState *self,
-                                   int                count,
-                                   int                divisor)
+void
+gtk_source_vim_state_scroll_half_page (GtkSourceVimState *self,
+                                       int                count)
 {
        GtkSourceView *view;
        GdkRectangle rect, loc;
@@ -563,7 +562,6 @@ _gtk_source_vim_state_scroll_page (GtkSourceVimState *self,
        int visible_lines;
 
        g_return_if_fail (GTK_SOURCE_IS_VIM_STATE (self));
-       g_return_if_fail (divisor > 0);
 
        if (count == 0)
                count = 1;
@@ -576,20 +574,20 @@ _gtk_source_vim_state_scroll_page (GtkSourceVimState *self,
        gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &bottom, rect.x, rect.y + rect.height);
 
        visible_lines = gtk_text_iter_get_line (&bottom) - gtk_text_iter_get_line (&top);
-       if (visible_lines < divisor)
-               visible_lines = divisor;
+       if (visible_lines < 2)
+               visible_lines = 2;
 
        gtk_text_view_get_iter_location (GTK_TEXT_VIEW (view), &iter, &loc);
        gtk_text_view_buffer_to_window_coords (GTK_TEXT_VIEW (view), GTK_TEXT_WINDOW_TEXT, loc.x, loc.y, 
&loc.x, &loc.y);
 
        if (count > 0)
        {
-               gtk_text_iter_forward_lines (&top, count * visible_lines / divisor);
+               gtk_text_iter_forward_lines (&top, count * visible_lines / 2);
                _gtk_source_view_jump_to_iter (GTK_TEXT_VIEW (view), &top, 0.0, TRUE, 1.0, 0.0);
        }
        else
        {
-               gtk_text_iter_backward_lines (&bottom, -count * visible_lines / divisor);
+               gtk_text_iter_backward_lines (&bottom, -count * visible_lines / 2);
                _gtk_source_view_jump_to_iter (GTK_TEXT_VIEW (view), &bottom, 0.0, TRUE, 1.0, 1.0);
        }
 
@@ -598,16 +596,86 @@ _gtk_source_vim_state_scroll_page (GtkSourceVimState *self,
        gtk_source_vim_state_select (self, &iter, &iter);
 }
 
-void
-gtk_source_vim_state_scroll_half_page (GtkSourceVimState *self,
-                                       int                count)
+static void
+scroll_page_down (GtkSourceVimState *self)
 {
-       return _gtk_source_vim_state_scroll_page (self, count, 2);
+       GtkSourceView *view;
+       GdkRectangle rect;
+       GtkTextIter iter;
+
+       g_assert (GTK_SOURCE_IS_VIM_STATE (self));
+
+       view = gtk_source_vim_state_get_view (self);
+       gtk_text_view_get_visible_rect (GTK_TEXT_VIEW (view), &rect);
+       gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, rect.x, rect.y+rect.height);
+       _gtk_source_view_jump_to_iter (GTK_TEXT_VIEW (view), &iter, 0.0, TRUE, 1.0, 0.0);
+}
+
+static void
+scroll_page_up (GtkSourceVimState *self)
+{
+       GtkSourceView *view;
+       GdkRectangle rect;
+       GtkTextIter iter;
+
+       g_assert (GTK_SOURCE_IS_VIM_STATE (self));
+
+       view = gtk_source_vim_state_get_view (self);
+       gtk_text_view_get_visible_rect (GTK_TEXT_VIEW (view), &rect);
+       gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, rect.x, rect.y);
+       _gtk_source_view_jump_to_iter (GTK_TEXT_VIEW (view), &iter, 0.0, TRUE, 1.0, 1.0);
 }
 
 void
 gtk_source_vim_state_scroll_page (GtkSourceVimState *self,
                                   int                count)
 {
-       return _gtk_source_vim_state_scroll_page (self, count, 1);
+       GtkSourceVimStatePrivate *priv = gtk_source_vim_state_get_instance_private (self);
+       GtkTextIter iter;
+       GdkRectangle rect, loc;
+       gboolean move_insert = FALSE;
+
+       g_return_if_fail (GTK_SOURCE_IS_VIM_STATE (self));
+
+       if (count == 0)
+               count = 1;
+
+       for (int i = 1; i <= ABS (count); i++)
+       {
+               if (count > 0)
+                       scroll_page_down (self);
+               else
+                       scroll_page_up (self);
+       }
+
+       gtk_source_vim_state_get_buffer (self, &iter, NULL);
+       gtk_text_view_get_visible_rect (GTK_TEXT_VIEW (priv->view), &rect);
+       gtk_text_view_get_iter_location (GTK_TEXT_VIEW (priv->view), &iter, &loc);
+
+       if (loc.y < rect.y)
+       {
+               gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (priv->view),
+                                                   &iter, rect.x, rect.y);
+               move_insert = TRUE;
+       }
+       else if (loc.y + loc.height > rect.y + rect.height)
+       {
+
+               gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (priv->view),
+                                                   &iter, rect.x, rect.y + rect.height);
+               gtk_text_view_get_iter_location (GTK_TEXT_VIEW (priv->view), &iter, &loc);
+               if (loc.y + loc.height > rect.y + rect.height)
+                       gtk_text_iter_backward_line (&iter);
+               move_insert = TRUE;
+       }
+
+       if (move_insert)
+       {
+               while (!gtk_text_iter_ends_line (&iter) &&
+                      g_unichar_isspace (gtk_text_iter_get_char (&iter)))
+               {
+                      gtk_text_iter_forward_char (&iter);
+               }
+               gtk_source_vim_state_select (self, &iter, &iter);
+       }
 }


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