fill3: Correctly limit scroll of portals

This commit is contained in:
Andrew Hamilton 2022-01-27 16:51:39 +10:00
parent d192674236
commit 87515dd44f
2 changed files with 24 additions and 14 deletions

View file

@ -245,20 +245,26 @@ class Portal:
y = 0
elif y > (widget_height - portal_height):
y = widget_height - portal_height
return x, y
self.position = x, y
def appearance_for(self, dimensions):
width, height = dimensions
x, y = self.position
def appearance_for(self, portal_dimensions):
width, height = portal_dimensions
try:
appearance = self.widget.appearance_interval((max(y, 0), y+height))
widget_dimensions = self.widget.appearance_dimensions()
if self.is_scroll_limited:
self.limit_scroll(portal_dimensions, widget_dimensions)
x, y = self.position
widget_appearance = self.widget.appearance_interval((max(y, 0), y + height))
except AttributeError:
appearance = self.widget.appearance()[max(y, 0):y+height]
if self.is_scroll_limited:
x, y = self.limit_scroll(dimensions, appearance_dimensions(appearance))
self.last_dimensions = dimensions
widget_appearance = self.widget.appearance()
widget_dimensions = appearance_dimensions(self.widget.appearance())
if self.is_scroll_limited:
self.limit_scroll(portal_dimensions, widget_dimensions)
x, y = self.position
widget_appearance = widget_appearance[max(y, 0):y + height]
self.last_dimensions = portal_dimensions
top = [self.background_char * width] * -y if y < 0 else []
bottom = appearance_resize([row[max(x, 0):x+width] for row in appearance],
bottom = appearance_resize([row[max(x, 0):x + width] for row in widget_appearance],
(width + min(x, 0), height + min(y, 0)),
self.background_char)
if x < 0:

View file

@ -47,13 +47,17 @@ class WidgetTests(unittest.TestCase):
self.assert_string(portal.appearance_for((2, 1)), "r ")
# limit scroll
portal.position = (0, 0)
self.assertEqual(portal.limit_scroll((1, 1), (1, 1)), (0, 0))
portal.limit_scroll((1, 1), (1, 1))
self.assertEqual(portal.position, (0, 0))
portal.position = (-1, 0)
self.assertEqual(portal.limit_scroll((1, 1), (1, 1)), (0, 0))
portal.limit_scroll((1, 1), (1, 1))
self.assertEqual(portal.position, (0, 0))
portal.position = (1, 0)
self.assertEqual(portal.limit_scroll((6, 1), (1, 1)), (0, 0))
portal.limit_scroll((6, 1), (1, 1))
self.assertEqual(portal.position, (0, 0))
portal.is_left_aligned = False
self.assertEqual(portal.limit_scroll((6, 1), (1, 1)), (-5, 0))
portal.limit_scroll((6, 1), (1, 1))
self.assertEqual(portal.position, (-5, 0))
def test_border_widget(self):
contents = fill3.Filler(self.TEXT_A)