Repository: MyreMylar/pygame_gui
Pull request #282 (2022-07-20 16:28:59)

Fix for file dialog file list not updating when resizing the dialog
Issue was deeper in, in UISelectionList. Likely scope for reducing code duplication in UISelectionList.


Patch:
diff --git a/pygame_gui/elements/ui_selection_list.py b/pygame_gui/elements/ui_selection_list.py
index d1a7103..20c07df 100644
--- a/pygame_gui/elements/ui_selection_list.py
+++ b/pygame_gui/elements/ui_selection_list.py
@@ -470,6 +470,38 @@ class UISelectionList(UIElement):
         container_width = self.relative_rect.width - (2 * border_and_shadow)
         container_height = self.relative_rect.height - (2 * border_and_shadow)
         self.list_and_scroll_bar_container.set_dimensions((container_width, container_height))
+        self.lowest_list_pos = (self.total_height_of_list -
+                                self.list_and_scroll_bar_container.relative_rect.height)
+        inner_visible_area_height = self.list_and_scroll_bar_container.relative_rect.height
+        if self.total_height_of_list > inner_visible_area_height:
+            # we need a scroll bar
+            self.current_scroll_bar_width = self.scroll_bar_width
+            percentage_visible = inner_visible_area_height / max(self.total_height_of_list, 1)
+            if self.scroll_bar is not None:
+                self.scroll_bar.set_visible_percentage(percentage_visible)
+            else:
+                self.scroll_bar = UIVerticalScrollBar(pygame.Rect(-self.scroll_bar_width,
+                                                                  0,
+                                                                  self.scroll_bar_width,
+                                                                  inner_visible_area_height),
+                                                      visible_percentage=percentage_visible,
+                                                      manager=self.ui_manager,
+                                                      parent_element=self,
+                                                      container=self.list_and_scroll_bar_container,
+                                                      anchors={'left': 'right',
+                                                               'right': 'right',
+                                                               'top': 'top',
+                                                               'bottom': 'bottom'})
+                self.join_focus_sets(self.scroll_bar)
+        else:
+            if self.scroll_bar is not None:
+                self.scroll_bar.kill()
+                self.scroll_bar = None
+            self.current_scroll_bar_width = 0
+
+        if self.scroll_bar is not None:
+            self.scroll_bar.has_moved_recently = True
+            self.update(0.0)
 
     def set_relative_position(self, position: Union[pygame.math.Vector2,
                                                     Tuple[int, int],


Test patch:
diff --git a/tests/test_elements/test_ui_selection_list.py b/tests/test_elements/test_ui_selection_list.py
index e382207..bcf05e3 100644
--- a/tests/test_elements/test_ui_selection_list.py
+++ b/tests/test_elements/test_ui_selection_list.py
@@ -552,6 +552,30 @@ class TestUISelectionList:
         assert selection_list.relative_right_margin == 200
         assert selection_list.relative_bottom_margin == 200
 
+        selection_list = UISelectionList(relative_rect=pygame.Rect(30, 30, 100, 500),
+                                         item_list=['item 1', 'item 2', 'item 3', 'item 4',
+                                                    'item 5', 'item 6', 'item 7', 'item 8',
+                                                    'item 9', 'item 10', 'item 11', 'item 12',
+                                                    'item 13', 'item 14', 'item 15', 'item 16'],
+                                         manager=default_ui_manager,
+                                         container=test_container,
+                                         allow_multi_select=True,
+                                         anchors={'left': 'left',
+                                                  'right': 'left',
+                                                  'top': 'top',
+                                                  'bottom': 'top'})
+
+        # check creating an destroying scrollbar by resizing
+        assert selection_list.scroll_bar is None
+
+        selection_list.set_dimensions((100, 50))
+
+        assert selection_list.scroll_bar is not None
+
+        selection_list.set_dimensions((100, 500))
+
+        assert selection_list.scroll_bar is None
+
     def test_kill(self, _init_pygame, default_ui_manager: IUIManagerInterface,
                   _display_surface_return_none):
         selection_list = UISelectionList(relative_rect=pygame.Rect(0, 0, 50, 80),