Skip to content

Commit

Permalink
Ignore window state requests on a hidden window
Browse files Browse the repository at this point in the history
  • Loading branch information
proneon267 committed Nov 28, 2024
1 parent 68b0a44 commit 0d807f6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
5 changes: 4 additions & 1 deletion cocoa/src/toga_cocoa/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,10 @@ def hide(self):
self.native.orderOut(self.native)

def get_visible(self):
return bool(self.native.isVisible)
return (
bool(self.native.isVisible)
or self.get_window_state(in_progress_state=True) == WindowState.MINIMIZED
)

######################################################################
# Window state
Expand Down
4 changes: 3 additions & 1 deletion core/src/toga/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,9 @@ def state(self) -> WindowState:

@state.setter
def state(self, state: WindowState) -> None:
if not self.resizable and state in {
if not self.visible:
raise ValueError("Window state of a hidden window cannot be changed.")
elif not self.resizable and state in {
WindowState.MAXIMIZED,
WindowState.FULLSCREEN,
WindowState.PRESENTATION,
Expand Down
30 changes: 30 additions & 0 deletions core/tests/window/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ def test_visibility(window, app):
)
def test_window_state(window, initial_state, final_state):
"""A window can have different states."""
window.show()
assert window.state == WindowState.NORMAL

window.state = initial_state
Expand Down Expand Up @@ -372,17 +373,44 @@ def test_window_state(window, initial_state, final_state):
)
def test_window_state_same_as_current(window, state):
"""Setting window state the same as current is a no-op."""
window.show()

window.state = state
assert window.state == state

# Reset the EventLog to check that the action was not re-performed.
EventLog.reset()
window.show()

window.state = state
assert window.state == state
assert_action_not_performed(window, f"set window state to {state}")


@pytest.mark.parametrize(
"state",
[
WindowState.NORMAL,
WindowState.MINIMIZED,
WindowState.MAXIMIZED,
WindowState.FULLSCREEN,
WindowState.PRESENTATION,
],
)
def test_hidden_window_state(state):
"""Window state of a hidden window cannot be changed."""
hidden_window = toga.Window(title="Hidden Window")
hidden_window.hide()

with pytest.raises(
ValueError,
match="Window state of a hidden window cannot be changed.",
):
hidden_window.state = state
assert_action_not_performed(hidden_window, f"set window state to {state}")
hidden_window.close()


@pytest.mark.parametrize(
"state",
[
Expand All @@ -394,6 +422,8 @@ def test_window_state_same_as_current(window, state):
def test_non_resizable_window_state(state):
"""Non-resizable window's states other than minimized or normal are no-ops."""
non_resizable_window = toga.Window(title="Non-Resizable Window", resizable=False)
non_resizable_window.show()

with pytest.raises(
ValueError,
match=f"A non-resizable window cannot be set to a state of {state}.",
Expand Down

0 comments on commit 0d807f6

Please sign in to comment.