From 88720808a29e36f700b7602b26e625e2d0a3f52d Mon Sep 17 00:00:00 2001 From: Dan Lawrence Date: Sat, 13 Apr 2024 21:18:41 +0100 Subject: [PATCH] More always on top tests --- pygame_gui/core/ui_window_stack.py | 17 +++++++--- pygame_gui/elements/ui_window.py | 2 +- tests/test_core/test_window_stack.py | 48 ++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 6 deletions(-) diff --git a/pygame_gui/core/ui_window_stack.py b/pygame_gui/core/ui_window_stack.py index ee519cde..1401107e 100644 --- a/pygame_gui/core/ui_window_stack.py +++ b/pygame_gui/core/ui_window_stack.py @@ -68,11 +68,8 @@ def add_new_window(self, window: IWindowInterface): def refresh_window_stack_from_window(self, window_to_refresh_from: IWindowInterface): if window_to_refresh_from in self.stack: popped_windows_to_add_back = [] - window = self.stack.pop() - while window != window_to_refresh_from: - popped_windows_to_add_back.append(window) - window = self.stack.pop() - popped_windows_to_add_back.append(window_to_refresh_from) + + # first clear out the top stack try: top_window = self.top_stack.pop() while top_window is not None: @@ -84,6 +81,16 @@ def refresh_window_stack_from_window(self, window_to_refresh_from: IWindowInterf except IndexError: pass + # then clear out everything above our target + window = self.stack.pop() + while window != window_to_refresh_from: + popped_windows_to_add_back.append(window) + window = self.stack.pop() + + # add back our target as well + popped_windows_to_add_back.append(window_to_refresh_from) + + # reverse the list and re-add them popped_windows_to_add_back.reverse() for old_window in popped_windows_to_add_back: self.add_new_window(old_window) diff --git a/pygame_gui/elements/ui_window.py b/pygame_gui/elements/ui_window.py index 5c81c4fc..bc121aee 100644 --- a/pygame_gui/elements/ui_window.py +++ b/pygame_gui/elements/ui_window.py @@ -108,8 +108,8 @@ def always_on_top(self) -> bool: @always_on_top.setter def always_on_top(self, value: bool): if value != self._always_on_top: - self._always_on_top = value self.window_stack.remove_window(self) + self._always_on_top = value self.window_stack.add_new_window(self) def set_blocking(self, state: bool): diff --git a/tests/test_core/test_window_stack.py b/tests/test_core/test_window_stack.py index 469fc544..db3c11b2 100644 --- a/tests/test_core/test_window_stack.py +++ b/tests/test_core/test_window_stack.py @@ -3,6 +3,8 @@ from pygame_gui.core.ui_window_stack import UIWindowStack from pygame_gui.elements.ui_window import UIWindow +from pygame_gui.elements.ui_drop_down_menu import UIDropDownMenu +from pygame_gui.ui_manager import UIManager class TestWindowStack: @@ -180,6 +182,52 @@ def test_move_window_to_front_with_always_on_top(self, _init_pygame, default_ui_ assert stack.top_stack[0] == window_always_on_top assert window_always_on_top.layer > window_2.layer + def test_swap_window_always_on_top(self, _init_pygame, default_ui_manager, + _display_surface_return_none): + manager = UIManager((800, 600)) + stack = manager.ui_window_stack + window = UIWindow(pygame.Rect(100, 100, 200, 200), window_display_title="Test Window", + manager=manager, element_id='test_window') + window_always_on_top = UIWindow(pygame.Rect(100, 100, 200, 200), window_display_title="Test Window", + manager=manager, element_id='test_window', + always_on_top=True) + + window_always_on_top.always_on_top = False + window.always_on_top = True + + assert len(stack.stack) == 1 + assert len(stack.top_stack) == 1 + assert window == stack.top_stack[0] + assert window_always_on_top == stack.stack[0] + + def test_update_always_on_top(self, _init_pygame, default_ui_manager, + _display_surface_return_none): + manager = UIManager((800, 600)) + + window = UIWindow(pygame.Rect(100, 100, 200, 200), window_display_title="Test Window", + manager=manager, element_id='test_window') + assert manager.get_window_stack().get_full_stack()[-1].get_top_layer() == 5 + UIWindow(pygame.Rect(100, 100, 200, 200), window_display_title="Test Window", + manager=manager, element_id='test_window') + assert manager.get_window_stack().get_full_stack()[-1].get_top_layer() == 8 + UIWindow(pygame.Rect(100, 100, 200, 200), window_display_title="Test Window", + manager=manager, element_id='test_window', always_on_top=True) + assert manager.get_window_stack().get_full_stack()[-1].get_top_layer() == 11 + window_always_on_top_2 = UIWindow(pygame.Rect(100, 100, 200, 200), window_display_title="Test Window", + manager=manager, element_id='test_window', always_on_top=True) + + assert manager.get_window_stack().get_full_stack()[-1].get_top_layer() == 14 + + assert window_always_on_top_2.get_top_layer() == 14 + assert window.layer_thickness == 2 + + UIDropDownMenu(["Test", "Test"], "Test", (50, 50, 120, 30), container=window, manager=manager) + assert window.layer_thickness == 2 + window.update(0.4) + assert window.layer_thickness == 4 + assert manager.get_window_stack().get_full_stack()[-1].get_top_layer() == 16 + assert window_always_on_top_2.get_top_layer() == 16 + if __name__ == '__main__': pytest.console_main()