Skip to content

Commit

Permalink
add a Window.closed attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
samschott committed Aug 17, 2023
1 parent 0b6acc5 commit dfbc5f0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
15 changes: 14 additions & 1 deletion core/src/toga/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def __init__(
self._app = None
self._content = None
self._is_full_screen = False
self._closed = False

self._resizable = resizable
self._closable = closable
Expand Down Expand Up @@ -157,6 +158,15 @@ def app(self, app: App) -> None:
if self.content:
self.content.app = app

@property
def closed(self) -> bool:
"""Whether the window was closed."""
return self._closed

def _check_closed(self) -> None:
if self.closed:
raise RuntimeError("Window was already closed")

@property
def _default_title(self) -> str:
return "Toga"
Expand Down Expand Up @@ -316,10 +326,13 @@ def close(self) -> None:
and unconditionally closed.
Once a window has been closed, it *cannot* be reused. The behavior of any method
on a :class:`~toga.Window` instance after it has been closed is undefined.
or property on a :class:`~toga.Window` instance after it has been closed is
undefined, except for :attr:`closed` which can be used to check if the window
was closed.
"""
self.app.windows -= self
self._impl.close()
self._closed = True

############################################################
# Dialogs
Expand Down
4 changes: 4 additions & 0 deletions core/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ def test_close_direct(window, app):
window.close()

# Window has been closed, but the close handler has *not* been invoked.
assert window.closed
assert window.app == app
assert window not in app.windows
assert_action_performed(window, "close")
Expand All @@ -276,6 +277,7 @@ def test_close_no_handler(window, app):
window._impl.simulate_close()

# Window has been closed, and is no longer in the app's list of windows.
assert window.closed
assert window.app == app
assert window not in app.windows
assert_action_performed(window, "close")
Expand All @@ -294,6 +296,7 @@ def test_close_sucessful_handler(window, app):
window._impl.simulate_close()

# Window has been closed, and is no longer in the app's list of windows.
assert window.closed
assert window.app == app
assert window not in app.windows
assert_action_performed(window, "close")
Expand All @@ -313,6 +316,7 @@ def test_close_rejected_handler(window, app):
window._impl.simulate_close()

# Window has *not* been closed
assert not window.closed
assert window.app == app
assert window in app.windows
assert_action_not_performed(window, "close")
Expand Down

0 comments on commit dfbc5f0

Please sign in to comment.