Make scroll bars show the right position on scroll and zoom

This commit is contained in:
Arjan Molenaar 2024-08-20 17:26:25 +02:00
parent 0c5009ef44
commit 36ef0962f3
2 changed files with 18 additions and 3 deletions

View File

@ -331,9 +331,11 @@ class GtkView(Gtk.DrawingArea, Gtk.Scrollable):
qtree.add(item=item, bounds=(x, y, w, h))
def update_scrolling(self) -> None:
self._scrolling.update_adjustments(
self.get_width(), self.get_height(), self._qtree.soft_bounds
)
matrix = Matrix(*self._matrix) # type: ignore[misc]
matrix.set(x0=0, y0=0)
bounds = Rectangle(*transform_rectangle(matrix, self._qtree.soft_bounds))
self._scrolling.update_adjustments(self.get_width(), self.get_height(), bounds)
def _debug_draw_bounding_box(self, cr, width, height):
for item in self.get_items_in_rectangle((0, 0, width, height)):
@ -386,6 +388,7 @@ class GtkView(Gtk.DrawingArea, Gtk.Scrollable):
# Test if scale or rotation changed
if tuple(matrix)[:4] != old_matrix_values[:4]:
self.update_scrolling()
self._scrolling.update_position(-matrix[4], -matrix[5])
self.update_back_buffer()
def on_resize(self, _width: int, _height: int) -> None:

View File

@ -1,5 +1,6 @@
from __future__ import annotations
from math import isclose
from typing import Callable
from gi.repository import Gtk
@ -57,6 +58,17 @@ class Scrolling:
else:
raise AttributeError(f"Unknown property {prop.name}")
def update_position(self, x: float, y: float) -> None:
if self.hadjustment and not isclose(self.hadjustment.get_value(), x):
self.hadjustment.handler_block(self._hadjustment_handler_id)
self.hadjustment.set_value(x)
self.hadjustment.handler_unblock(self._hadjustment_handler_id)
if self.vadjustment and not isclose(self.vadjustment.get_value(), y):
self.vadjustment.handler_block(self._vadjustment_handler_id)
self.vadjustment.set_value(y)
self.vadjustment.handler_unblock(self._vadjustment_handler_id)
def update_adjustments(self, width: int, height: int, bounds: Rectangle) -> None:
"""Update scroll bar values (adjustments in GTK).