Skip to content

Commit

Permalink
Remove the ability to create a SplitContainer without content.
Browse files Browse the repository at this point in the history
  • Loading branch information
freakboy3742 committed Jun 19, 2023
1 parent 8d64f1d commit 9ddf1cd
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 52 deletions.
1 change: 1 addition & 0 deletions changes/1984.removal.3.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The ability to create a SplitContainer without content has been removed.
12 changes: 9 additions & 3 deletions core/src/toga/widgets/splitcontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(
id=None,
style=None,
direction: Direction = Direction.VERTICAL,
content: list[Widget | tuple[Widget, int]] | None = None,
content: list[Widget | tuple[Widget, int]] = None,
):
"""Create a new SplitContainer.
Expand Down Expand Up @@ -42,8 +42,14 @@ def __init__(
# Create a platform specific implementation of a SplitContainer
self._impl = self.factory.SplitContainer(interface=self)

if content:
self.content = content
# We can't make content the first argument, but we can't allow it to be empty either.
# Raise an error that looks like the one you'd get for missing a required argument.
if content is None:
raise TypeError(
"SplitContainer.__init__() missing 1 required argument: 'content'"
)

self.content = content
self.direction = direction

@property
Expand Down
60 changes: 20 additions & 40 deletions core/tests/widgets/test_splitcontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,26 @@ def splitcontainer(content1, content2):
return toga.SplitContainer(content=[content1, content2])


def test_widget_created():
"A scroll container can be created with no arguments"
splitcontainer = toga.SplitContainer()
def test_create_no_content(app):
"""SplitContainer can't be created without content."""
with pytest.raises(
TypeError,
match=r"SplitContainer.__init__\(\) missing 1 required argument: 'content'",
):
toga.SplitContainer()


def test_widget_created(splitcontainer, content1, content2):
"A split container can be created with minimal arguments"
assert splitcontainer._impl.interface == splitcontainer
assert_action_performed(splitcontainer, "create SplitContainer")

assert splitcontainer.content == []
assert splitcontainer.content == [content1, content2]
assert splitcontainer.direction == toga.SplitContainer.VERTICAL


def test_widget_created_with_values(content1, content2):
"A scroll container can be created with arguments"
"A split container can be created with arguments"
splitcontainer = toga.SplitContainer(
content=[content1, content2],
direction=toga.SplitContainer.HORIZONTAL,
Expand All @@ -68,7 +76,7 @@ def test_widget_created_with_values(content1, content2):
flex=[1, 1],
)

# The scroll container has been refreshed
# The split container has been refreshed
assert_action_performed(splitcontainer, "refresh")


Expand All @@ -77,7 +85,7 @@ def test_assign_to_app(app, splitcontainer, content1, content2):
# Scroll container is initially unassigned
assert splitcontainer.app is None

# Assign the scroll container to the app
# Assign the split container to the app
splitcontainer.app = app

# Scroll container is on the app
Expand All @@ -88,26 +96,12 @@ def test_assign_to_app(app, splitcontainer, content1, content2):
assert content2.app == app


def test_assign_to_app_no_content(app):
"""If the widget is assigned to an app, and there is no content, there's no error"""
splitcontainer = toga.SplitContainer()

# Scroll container is initially unassigned
assert splitcontainer.app is None

# Assign the scroll container to the app
splitcontainer.app = app

# Scroll container is on the app
assert splitcontainer.app == app


def test_assign_to_window(window, splitcontainer, content1, content2):
"""If the widget is assigned to a window, the content is also assigned"""
# Scroll container is initially unassigned
assert splitcontainer.window is None

# Assign the scroll container to the window
# Assign the split container to the window
splitcontainer.window = window

# Scroll container is on the window
Expand All @@ -117,20 +111,6 @@ def test_assign_to_window(window, splitcontainer, content1, content2):
assert content2.window == window


def test_assign_to_window_no_content(window):
"""If the widget is assigned to an app, and there is no content, there's no error"""
splitcontainer = toga.SplitContainer()

# Scroll container is initially unassigned
assert splitcontainer.window is None

# Assign the scroll container to the window
splitcontainer.window = window

# Scroll container is on the window
assert splitcontainer.window == window


def test_disable_no_op(splitcontainer):
"SplitContainer doesn't have a disabled state"
# Enabled by default
Expand Down Expand Up @@ -166,7 +146,7 @@ def test_set_content_widgets(
flex=[1, 1],
)

# The scroll container has been refreshed
# The split container has been refreshed
assert_action_performed(splitcontainer, "refresh")


Expand All @@ -181,7 +161,7 @@ def test_set_content_flex(splitcontainer, content2, content3):
flex=[2, 3],
)

# The scroll container has been refreshed
# The split container has been refreshed
assert_action_performed(splitcontainer, "refresh")


Expand All @@ -196,7 +176,7 @@ def test_set_content_flex_altered(splitcontainer, content2, content3):
flex=[1, 1],
)

# The scroll container has been refreshed
# The split container has been refreshed
assert_action_performed(splitcontainer, "refresh")


Expand Down Expand Up @@ -246,5 +226,5 @@ def test_direction(splitcontainer):
# The direction has been set
assert splitcontainer.direction == toga.SplitContainer.HORIZONTAL

# The scroll container has been refreshed
# The split container has been refreshed
assert_action_performed(splitcontainer, "refresh")
19 changes: 13 additions & 6 deletions docs/reference/api/containers/splitcontainer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,27 @@ Usage
split = toga.SplitContainer(content=[left_container, right_container])
Content can be specified when creating the widget, or after creation by assigning
the ``content`` attribute. The direction of the split can also be configured, either
at time of creation, or by setting the ``direction`` attribute:
The content of the SplitContainer must be specified at time of construction. The
direction of the split can be configured at time of creation, or by setting the
``direction`` attribute:

.. code-block:: python
import toga
split = toga.SplitContainer(direction=toga.SplitContainer.HORIZONTAL)
left_container = toga.Box()
right_container = toga.ScrollContainer()
split.content = [left_container, right_container]
# Make the container a horizontal split initially
# (i.e., two panels in a column, with horizontal splitter between them)
split = toga.SplitContainer(
content=[left_container, right_container],
direction=toga.SplitContainer.HORIZONTAL
)
# Make the container a vertical split (i.e., two panels in a row)
split.direction = toga.SplitContainer.VERTICAL
By default, the content of the SplitContainer will be evenly divided between the
content. To specify an uneven split, you can provide a flex value when specifying
Expand Down
4 changes: 1 addition & 3 deletions examples/tutorial2/tutorial/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,13 @@ def build(app):

right_container.content = right_content

split = toga.SplitContainer()

# The content of the split container can be specified as a simple list:
# split.content = [left_container, right_container]
# but you can also specify "weight" with each content item, which will
# set an initial size of the columns to make a "heavy" column wider than
# a narrower one. In this example, the right container will be twice
# as wide as the left one.
split.content = [(left_container, 1), (right_container, 2)]
split = toga.SplitContainer(content=[(left_container, 1), (right_container, 2)])

# Create a "Things" menu group to contain some of the commands.
# No explicit ordering is provided on the group, so it will appear
Expand Down

0 comments on commit 9ddf1cd

Please sign in to comment.