mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
refine compose
This commit is contained in:
@@ -5,9 +5,9 @@ from textual.widgets import Static, Button
|
||||
class QuestionApp(App[str]):
|
||||
def compose(self) -> ComposeResult:
|
||||
yield Static("Do you love Textual?")
|
||||
yield (yes := Button("Yes", id="yes"))
|
||||
yes = yield Button("Yes", id="yes")
|
||||
yes.variant = "primary"
|
||||
yield (no := Button("No", id="no"))
|
||||
no = yield Button("No", id="no")
|
||||
no.variant = "error"
|
||||
|
||||
def on_button_pressed(self, event: Button.Pressed) -> None:
|
||||
|
||||
@@ -4,7 +4,6 @@ from types import GeneratorType
|
||||
from typing import TYPE_CHECKING, Iterable
|
||||
|
||||
if TYPE_CHECKING:
|
||||
|
||||
from .app import ComposeResult
|
||||
from .widget import Widget
|
||||
|
||||
@@ -28,17 +27,18 @@ def _compose(compose_result: ComposeResult) -> Iterable[Widget]:
|
||||
or a generator.
|
||||
|
||||
Returns:
|
||||
Iterable[Widget]: In iterable if widgets.
|
||||
Iterable[Widget]: An iterable if widgets.
|
||||
|
||||
"""
|
||||
|
||||
if not isinstance(compose_result, GeneratorType):
|
||||
return compose_result
|
||||
|
||||
if isinstance(compose_result, GeneratorType):
|
||||
try:
|
||||
widget = next(compose_result)
|
||||
send = compose_result.send
|
||||
while True:
|
||||
yield widget
|
||||
widget = compose_result.send(widget)
|
||||
widget = send(widget)
|
||||
except StopIteration:
|
||||
pass
|
||||
else:
|
||||
yield from compose_result
|
||||
|
||||
Reference in New Issue
Block a user