added ContentSwitcher.add_conent

This commit is contained in:
Will McGugan
2024-06-12 17:33:41 +01:00
parent 9bbf022350
commit ff918b4f3d
4 changed files with 53 additions and 2 deletions

View File

@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## Unreleased
### Added
- Added `ContentSwitcher.add_content`
## [0.67.1] - 2024-06-12
### Changed

View File

@@ -21,7 +21,7 @@ def partition(
"""
result: tuple[list[T], list[T]] = ([], [])
appends = (result[0].append, result[1].append)
appends = (result[1].append, result[0].append)
for value in iterable:
appends[1 if predicate(value) else 0](value)
appends[not predicate(value)](value)
return result

View File

@@ -4,6 +4,7 @@ from __future__ import annotations
from typing import Optional
from ..await_complete import AwaitComplete
from ..containers import Container
from ..css.query import NoMatches
from ..events import Mount
@@ -98,3 +99,34 @@ class ContentSwitcher(Container):
pass
if new:
self.get_child_by_id(new).display = True
def add_content(
self, widget: Widget, *, id: str | None = None, set_current: bool = False
) -> AwaitComplete:
"""Add new content to the `ContentSwitcher`.
Args:
widget: A Widget to add.
id: ID for the widget, or `None` if the widget already has an ID.
set_current: Set the new widget as current (which will cause it to display).
Returns:
An awaitable to wait for the new content to be mounted.
"""
if id is not None and widget.id != id:
widget.id = id
if not widget.id:
raise ValueError(
"Widget must have an ID (or set id parameter when calling add_content)"
)
async def _add_content() -> None:
"""Add new widget and potentially change the current widget."""
widget.display = False
with self.app.batch_update():
await self.mount(widget)
if set_current:
self.current = widget.id
return AwaitComplete(_add_content())

View File

@@ -107,3 +107,16 @@ async def test_set_current_to_unknown_id() -> None:
)
with pytest.raises(NoMatches):
pilot.app.query_one(ContentSwitcher).current = "does-not-exist"
async def test_add_content() -> None:
async with SwitcherApp().run_test() as pilot:
switcher = pilot.app.query_one(ContentSwitcher)
await switcher.add_content(Widget(id="foo"))
assert not switcher.query_one("#foo").display
await switcher.add_content(Widget(), id="bar", set_current=True)
assert not switcher.query_one("#foo").display
assert switcher.query_one("#bar").display
assert switcher.current == "bar"
with pytest.raises(ValueError):
switcher.add_content(Widget())