summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Otte <otte@redhat.com>2021-12-28 04:21:41 +0100
committerBenjamin Otte <otte@redhat.com>2021-12-28 04:21:41 +0100
commit3744345bb4409d2f8c21b8712d2b47e9f1edd363 (patch)
tree1e8121e422a9b7917d1ad11c238277f80b08ef10
parent6da952100c830df0a97d2b76fc8c9f4430ac9cad (diff)
downloadgtk+-wip/otte/viewport-position.tar.gz
viewport: Keep scroll position in percentwip/otte/viewport-position
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).
-rw-r--r--gtk/gtkviewport.c8
1 files changed, 7 insertions, 1 deletions
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 &&