Skip to content

Commit

Permalink
Merge pull request #449 from ppizarror/theme-add-alignment-ignore-scr…
Browse files Browse the repository at this point in the history
…ollbar

Added theme.widget_alignment_ignore_scrollbar_thickness
  • Loading branch information
ppizarror committed Feb 17, 2023
2 parents 721d45a + 98e5af3 commit e353a52
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
10 changes: 7 additions & 3 deletions pygame_menu/menu.py
Expand Up @@ -1556,9 +1556,6 @@ def get_rect(wid: 'Widget') -> 'pygame.Rect':
y=menubar_height + padding[0] + d_border)
continue

# Update the position of the widget
widget.set_position(x_coord, y_coord)

# Add the widget translation to the widget for computing the min/max position. This
# feature does not work as intended as there's edge cases not covered, and centering makes
# the translation more difficult
Expand All @@ -1572,6 +1569,13 @@ def get_rect(wid: 'Widget') -> 'pygame.Rect':
min_x = min(min_x, x_coord - padding[3] - sm_left)
min_y = min(min_y, y_coord - padding[0])

# Restore the discounted scrollbar thickness
if self._theme.widget_alignment_ignore_scrollbar_thickness:
x_coord += self._get_scrollbar_thickness()[1] / 2

# Update the position of the widget
widget.set_position(x_coord, y_coord)

# Update position
if min_max_updated:
self._widget_max_position = (max_x, max_y)
Expand Down
4 changes: 4 additions & 0 deletions pygame_menu/themes.py
Expand Up @@ -167,6 +167,8 @@ class Theme(object):
:type title_updates_pygame_display: bool
:param widget_alignment: Widget default `alignment <https://pygame-menu.readthedocs.io/en/latest/_source/themes.html#alignment>`_. See :py:mod:`pygame_menu.locals`
:type widget_alignment: str
:param widget_alignment_ignore_scrollbar_thickness: Widget positioning ignores the scrollbar thickness. If ``True``, the widgets only consider the menu size, ignoring the thickness of the visible scrollbars
:type widget_alignment_ignore_scrollbar_thickness: bool
:param widget_background_color: Background color of a widget, it can be a color, ``None`` (transparent), or a BaseImage object. Background fills the entire widget + the padding
:type widget_background_color: tuple, list, str, int, :py:class:`pygame.Color`, :py:class:`pygame_menu.baseimage.BaseImage`, None
:param widget_background_inflate: Inflate background on x-axis and y-axis (x, y) in px. By default, it uses the highlight margin. This parameter is visual only. For modifying widget size use padding instead
Expand Down Expand Up @@ -284,6 +286,7 @@ class Theme(object):
title_offset: Tuple2NumberType
title_updates_pygame_display: bool
widget_alignment: str
widget_alignment_ignore_scrollbar_thickness: bool
widget_background_color: Optional[Union[ColorType, 'BaseImage']]
widget_background_inflate: Tuple2IntType
widget_background_inflate_to_selection: bool
Expand Down Expand Up @@ -378,6 +381,7 @@ def __init__(self, **kwargs) -> None:

# Generic widget themes
self.widget_alignment = self._get(kwargs, 'widget_alignment', 'alignment', ALIGN_CENTER)
self.widget_alignment_ignore_scrollbar_thickness = self._get(kwargs, 'widget_alignment_ignore_scrollbar_thickness', bool, False)
self.widget_background_color = self._get(kwargs, 'widget_background_color', 'color_image_none')
self.widget_background_inflate = self._get(kwargs, 'background_inflate', 'tuple2int', (0, 0))
self.widget_background_inflate_to_selection = self._get(kwargs, 'widget_background_inflate_to_selection', bool,
Expand Down
22 changes: 22 additions & 0 deletions test/test_widgets.py
Expand Up @@ -542,3 +542,25 @@ def test_widget_floating_zero(self) -> None:
image_widget.translate(-50, 0)
menu.render()
self.assertEqual(image_widget.get_position(), (-42, 60))

def test_widget_center_overflow_ignore_scrollbar_thickness(self) -> None:
"""
Test widget centering if overflow ignores scrollbar thickness.
"""
theme = TEST_THEME.copy()

menu = MenuUtils.generic_menu(width=320, theme=theme)
for i in range(5):
menu.add.button(f'Option{i + 1}')
menu.add.button('Quit', pygame_menu.events.EXIT)

pos_before = menu.get_selected_widget().get_position()
theme.widget_alignment_ignore_scrollbar_thickness = True
menu.render()
pos_after = menu.get_selected_widget().get_position()

# If we ignore scrollbar thickess in position, the difference
# should be equal to the half of the scrollbar thickness (because
# we have centered alignment)
self.assertEqual(pos_after[0] - pos_before[0], menu._get_scrollbar_thickness()[1] / 2) # x
self.assertEqual(pos_after[1], pos_before[1]) # y

0 comments on commit e353a52

Please sign in to comment.