[gtk/wip/otte/viewport-position] viewport: Keep scroll position in percent




commit 3744345bb4409d2f8c21b8712d2b47e9f1edd363
Author: Benjamin Otte <otte redhat com>
Date:   Tue Dec 28 04:21:41 2021 +0100

    viewport: Keep scroll position in percent
    
    Previously, the code tried to keep the absolute scroll position
    unchanged, which meant that when the child or the viewport changed size,
    the top left of the viewport would keep locked to the same coordinate in
    the child.
    
    With the new code, we keep the point of the viewport unchanged that
    matches the percentage the scrollbar has been scrolled.
    
    In particular, that means a centered child stays centered and a child
    stays at the bottom (or top) when the scrollbar is scolled to the end
    (or start, like before).

 gtk/gtkviewport.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
---
diff --git a/gtk/gtkviewport.c b/gtk/gtkviewport.c
index 7116592544..25e4a4fd52 100644
--- a/gtk/gtkviewport.c
+++ b/gtk/gtkviewport.c
@@ -150,11 +150,17 @@ viewport_set_adjustment_values (GtkViewport    *viewport,
                                 int             child_size)
 {
   GtkAdjustment *adjustment;
-  double upper, value;
+  double upper, value, old_range;
 
   adjustment = viewport->adjustment[orientation];
   upper = child_size;
   value = gtk_adjustment_get_value (adjustment);
+  old_range = gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_page_size (adjustment);
+  if (old_range > 0)
+    {
+      double percentage = value / old_range;
+      value = percentage * (child_size - viewport_size);
+    }
 
   /* We clamp to the left in RTL mode */
   if (orientation == GTK_ORIENTATION_HORIZONTAL &&


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