Skip to content

Commit

Permalink
More always on top tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MyreMylar committed Apr 13, 2024
1 parent a3c35e8 commit 8872080
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
17 changes: 12 additions & 5 deletions pygame_gui/core/ui_window_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pygame_gui/elements/ui_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
48 changes: 48 additions & 0 deletions tests/test_core/test_window_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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()

0 comments on commit 8872080

Please sign in to comment.