[gtksourceview/wip/chergert/vim: 75/293] improve half page scroll




commit 37a5b781258ae3714fef741bca2919872292cbdb
Author: Christian Hergert <chergert redhat com>
Date:   Mon Oct 25 23:39:00 2021 -0700

    improve half page scroll

 gtksourceview/vim/gtk-source-vim-state.c | 105 +++++++++++++++++++------------
 1 file changed, 64 insertions(+), 41 deletions(-)
---
diff --git a/gtksourceview/vim/gtk-source-vim-state.c b/gtksourceview/vim/gtk-source-vim-state.c
index 837d732d..9ba952da 100644
--- a/gtksourceview/vim/gtk-source-vim-state.c
+++ b/gtksourceview/vim/gtk-source-vim-state.c
@@ -545,15 +545,43 @@ gtk_source_vim_state_scroll_line (GtkSourceVimState *self,
        gtk_source_vim_state_place_cursor_onscreen (self);
 }
 
+static void
+scroll_half_page_down (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 + rect.height / 2);
+       _gtk_source_view_jump_to_iter (GTK_TEXT_VIEW (view), &iter, 0.0, TRUE, 1.0, 0.0);
+}
+
+static void
+scroll_half_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 + rect.height / 2);
+       _gtk_source_view_jump_to_iter (GTK_TEXT_VIEW (view), &iter, 0.0, TRUE, 1.0, 1.0);
+}
+
 void
 gtk_source_vim_state_scroll_half_page (GtkSourceVimState *self,
-                                       int                count)
+                                      int                count)
 {
        GtkSourceView *view;
        GdkRectangle rect, loc;
        GtkTextIter iter;
-       GtkTextIter top, bottom;
-       int visible_lines;
 
        g_return_if_fail (GTK_SOURCE_IS_VIM_STATE (self));
 
@@ -563,31 +591,26 @@ gtk_source_vim_state_scroll_half_page (GtkSourceVimState *self,
        gtk_source_vim_state_get_buffer (self, &iter, NULL);
        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), &top, rect.x, rect.y);
-       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 < 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);
+       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 / 2);
-               _gtk_source_view_jump_to_iter (GTK_TEXT_VIEW (view), &top, 0.0, TRUE, 1.0, 0.0);
-       }
-       else
+       for (int i = 1; i <= ABS (count); i++)
        {
-               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);
+               if (count > 0)
+                       scroll_half_page_down (self);
+               else
+                       scroll_half_page_up (self);
        }
 
-       gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view), GTK_TEXT_WINDOW_TEXT, loc.x, loc.y, 
&loc.x, &loc.y);
+       gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
+                                              GTK_TEXT_WINDOW_TEXT,
+                                              loc.x, loc.y, &loc.x, &loc.y);
        gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, loc.x, loc.y);
        gtk_source_vim_state_select (self, &iter, &iter);
+
+       gtk_source_vim_state_place_cursor_onscreen (self);
 }
 
 static void
@@ -620,6 +643,26 @@ scroll_page_up (GtkSourceVimState *self)
        _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)
+{
+       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_place_cursor_onscreen (self);
+}
+
 void
 gtk_source_vim_state_place_cursor_onscreen (GtkSourceVimState *self)
 {
@@ -661,23 +704,3 @@ gtk_source_vim_state_place_cursor_onscreen (GtkSourceVimState *self)
                gtk_source_vim_state_select (self, &iter, &iter);
        }
 }
-
-void
-gtk_source_vim_state_scroll_page (GtkSourceVimState *self,
-                                  int                count)
-{
-       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_place_cursor_onscreen (self);
-}


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