Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set blocking move to front #594

Merged
merged 5 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pygame_gui/elements/ui_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def __init__(self,
self.resizable = resizable
self.draggable = draggable
self._always_on_top = always_on_top
self._blocking_always_on_top = False

self.edge_hovering = [False, False, False, False]

Expand Down Expand Up @@ -115,11 +116,26 @@ def always_on_top(self, value: bool):
def set_blocking(self, state: bool):
"""
Sets whether this window being open should block clicks to the rest of the UI or not.
On being set True it will move the window to the top of the stack if it is not there already.
Defaults to False.

If there are any 'always on top' windows at this is set True this window will become one of
them, and move to the front of them too.

:param state: True if this window should block mouse clicks.

"""
if state:
if len(self.window_stack.top_stack) > 0:
if not self.always_on_top:
self._blocking_always_on_top = True
self.always_on_top = True
self.window_stack.move_window_to_front(self)
else:
if self._blocking_always_on_top:
self._blocking_always_on_top = False
self.always_on_top = False

self.is_blocking = state

def set_dimensions(self, dimensions: Coordinate, clamp_to_container: bool = False):
Expand Down
23 changes: 23 additions & 0 deletions tests/test_elements/test_ui_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,29 @@ def test_set_blocking(self, _init_pygame, default_ui_manager: IUIManagerInterfac
'pos': button.rect.center}))
assert not button.held

window.set_blocking(False)
window.always_on_top = True
window.set_blocking(True)

default_ui_manager.process_events(pygame.event.Event(pygame.MOUSEBUTTONDOWN,
{'button': pygame.BUTTON_LEFT,
'pos': button.rect.center}))
assert not button.held

window.set_blocking(False)
window.always_on_top = False

UIWindow(pygame.Rect(0, 0, 100, 100), window_display_title="Test On Top Window",
manager=default_ui_manager, element_id='window', always_on_top=True)

window.set_blocking(True)
default_ui_manager.process_events(pygame.event.Event(pygame.MOUSEBUTTONDOWN,
{'button': pygame.BUTTON_LEFT,
'pos': button.rect.center}))
assert not button.held

window.set_blocking(False)

def test_set_minimum_dimensions(self, _init_pygame, default_ui_manager: IUIManagerInterface,
_display_surface_return_none):
window = UIWindow(pygame.Rect(200, 200, 200, 200), window_display_title="Test Window",
Expand Down
Loading