fill3: Let portal contents be center aligned

This commit is contained in:
Andrew Hamilton 2022-05-26 14:38:37 +10:00
parent 0abca96868
commit e5c48a6128
2 changed files with 29 additions and 6 deletions

View file

@ -4,6 +4,7 @@
import asyncio import asyncio
import contextlib import contextlib
import enum
import itertools import itertools
import os import os
import re import re
@ -192,16 +193,24 @@ class ScrollBar:
return [bar] if self._is_horizontal else [char for char in bar] return [bar] if self._is_horizontal else [char for char in bar]
class Alignment(enum.Enum):
left = enum.auto()
right = enum.auto()
top = enum.auto()
bottom = enum.auto()
center = enum.auto()
class Portal: class Portal:
def __init__(self, widget, position=(0, 0), background_char=" ", def __init__(self, widget, position=(0, 0), background_char=" ",
is_scroll_limited=False, is_left_aligned=True, is_top_aligned=True): is_scroll_limited=False, x_alignment=Alignment.left, y_alignment=Alignment.top):
self.widget = widget self.widget = widget
self.position = position self.position = position
self.background_char = background_char self.background_char = background_char
self.is_scroll_limited = is_scroll_limited self.is_scroll_limited = is_scroll_limited
self.is_left_aligned = is_left_aligned self.x_alignment = x_alignment
self.is_top_aligned = is_top_aligned self.y_alignment = y_alignment
self.last_dimensions = 0, 0 self.last_dimensions = 0, 0
def _scroll_half_pages(self, dx, dy): def _scroll_half_pages(self, dx, dy):
@ -226,13 +235,27 @@ class Portal:
widget_width, widget_height = appearance_dimensions widget_width, widget_height = appearance_dimensions
x, y = self.position x, y = self.position
if widget_width <= portal_width: if widget_width <= portal_width:
x = 0 if self.is_left_aligned else (widget_width - portal_width) if self.x_alignment == Alignment.left:
x = 0
elif self.x_alignment == Alignment.center:
x = (widget_width - portal_width) // 2
elif self.x_alignment == Alignment.right:
x = widget_width - portal_width
else:
raise NotImplementedError
elif x < 0: elif x < 0:
x = 0 x = 0
elif x > (widget_width - portal_width): elif x > (widget_width - portal_width):
x = widget_width - portal_width x = widget_width - portal_width
if widget_height <= portal_height: if widget_height <= portal_height:
y = 0 if self.is_top_aligned else (widget_height - portal_height) if self.y_alignment == Alignment.top:
y = 0
elif self.y_alignment == Alignment.center:
y = (widget_height - portal_height) // 2
elif self.y_alignment == Alignment.bottom:
y = widget_height - portal_height
else:
raise NotImplementedError
elif y < 0: elif y < 0:
y = 0 y = 0
elif y > (widget_height - portal_height): elif y > (widget_height - portal_height):

View file

@ -57,7 +57,7 @@ class WidgetTests(unittest.TestCase):
portal.position = (1, 0) portal.position = (1, 0)
portal.limit_scroll((6, 1), (1, 1)) portal.limit_scroll((6, 1), (1, 1))
self.assertEqual(portal.position, (0, 0)) self.assertEqual(portal.position, (0, 0))
portal.is_left_aligned = False portal.x_alignment = fill3.Alignment.right
portal.limit_scroll((6, 1), (1, 1)) portal.limit_scroll((6, 1), (1, 1))
self.assertEqual(portal.position, (-5, 0)) self.assertEqual(portal.position, (-5, 0))