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

Correct the typing of SplitContainer content to be list/Sequence #2638

Merged
merged 3 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions changes/2638.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The type of SplitContainer's content was modified to be a list, rather than a tuple.
18 changes: 8 additions & 10 deletions core/src/toga/widgets/splitcontainer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import sys
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Sequence
rmartin16 marked this conversation as resolved.
Show resolved Hide resolved

from toga.app import App
from toga.constants import Direction
Expand All @@ -27,7 +27,7 @@ def __init__(
id: str | None = None,
style: StyleT | None = None,
direction: Direction = Direction.VERTICAL,
content: tuple[SplitContainerContentT, SplitContainerContentT] = (None, None),
content: Sequence[SplitContainerContentT] = [None, None],
rmartin16 marked this conversation as resolved.
Show resolved Hide resolved
):
"""Create a new SplitContainer.

Expand All @@ -42,10 +42,10 @@ def __init__(
of the container. Defaults to both panels being empty.
"""
super().__init__(id=id, style=style)
self._content: tuple[SplitContainerContentT, SplitContainerContentT] = (
self._content: Sequence[SplitContainerContentT] = [
rmartin16 marked this conversation as resolved.
Show resolved Hide resolved
None,
None,
)
]
rmartin16 marked this conversation as resolved.
Show resolved Hide resolved

# Create a platform specific implementation of a SplitContainer
self._impl = self.factory.SplitContainer(interface=self)
Expand All @@ -71,7 +71,7 @@ def focus(self) -> None:
pass

@property
def content(self) -> tuple[SplitContainerContentT, SplitContainerContentT]:
def content(self) -> Sequence[SplitContainerContentT]:
rmartin16 marked this conversation as resolved.
Show resolved Hide resolved
"""The widgets displayed in the SplitContainer.

This property accepts a sequence of exactly 2 elements, each of which can be
Expand All @@ -89,9 +89,7 @@ def content(self) -> tuple[SplitContainerContentT, SplitContainerContentT]:
return self._content

@content.setter
def content(
self, content: tuple[SplitContainerContentT, SplitContainerContentT]
) -> None:
def content(self, content: Sequence[SplitContainerContentT]) -> None:
try:
if len(content) != 2:
raise TypeError()
Expand Down Expand Up @@ -128,10 +126,10 @@ def content(
widget.window = self.window

self._impl.set_content(
tuple(w._impl if w is not None else None for w in _content),
list(w._impl if w is not None else None for w in _content),
flex,
)
self._content = tuple(_content)
self._content = list(_content)
self.refresh()

@Widget.app.setter
Expand Down
18 changes: 9 additions & 9 deletions core/tests/widgets/test_splitcontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_widget_created():
assert splitcontainer._impl.interface == splitcontainer
assert_action_performed(splitcontainer, "create SplitContainer")

assert splitcontainer.content == (None, None)
assert splitcontainer.content == [None, None]
assert splitcontainer.direction == toga.SplitContainer.VERTICAL


Expand All @@ -52,14 +52,14 @@ def test_widget_created_with_values(content1, content2):
assert splitcontainer._impl.interface == splitcontainer
assert_action_performed(splitcontainer, "create SplitContainer")

assert splitcontainer.content == (content1, content2)
assert splitcontainer.content == [content1, content2]
assert splitcontainer.direction == toga.SplitContainer.HORIZONTAL

# The content has been assigned to the widget
assert_action_performed_with(
splitcontainer,
"set content",
content=(content1._impl, content2._impl),
content=[content1._impl, content2._impl],
flex=[1, 1],
)

Expand Down Expand Up @@ -212,10 +212,10 @@ def test_set_content_widgets(
assert_action_performed_with(
splitcontainer,
"set content",
content=(
content=[
content2._impl if include_left else None,
content3._impl if include_right else None,
),
],
flex=[1, 1],
)

Expand Down Expand Up @@ -248,10 +248,10 @@ def test_set_content_flex(
assert_action_performed_with(
splitcontainer,
"set content",
content=(
content=[
content2._impl if include_left else None,
content3._impl if include_right else None,
),
],
flex=[2, 3],
)

Expand Down Expand Up @@ -284,10 +284,10 @@ def test_set_content_flex_mixed(
assert_action_performed_with(
splitcontainer,
"set content",
content=(
content=[
content2._impl if include_left else None,
content3._impl if include_right else None,
),
],
flex=[1, 3],
)

Expand Down
6 changes: 3 additions & 3 deletions docs/reference/api/containers/splitcontainer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Usage
left_container = toga.Box()
right_container = toga.ScrollContainer()

split = toga.SplitContainer(content=(left_container, right_container))
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
Expand All @@ -65,7 +65,7 @@ at time of creation, or by setting the ``direction`` attribute:
left_container = toga.Box()
right_container = toga.ScrollContainer()

split.content = (left_container, right_container)
split.content = [left_container, right_container]

By default, the space of the SplitContainer will be evenly divided between the
two panels. To specify an uneven split, you can provide a flex value when specifying
Expand All @@ -80,7 +80,7 @@ and right panels.
left_container = toga.Box()
right_container = toga.ScrollContainer()

split.content = ((left_container, 3), (right_container, 2))
split.content = [(left_container, 3), (right_container, 2)]

This only specifies the initial split; the split can be modified by the user
once it is displayed.
Expand Down
Loading