From 4a204558382cd478629eaf8569e634f1a9417d0f Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Thu, 28 Apr 2022 13:26:29 +0100 Subject: [PATCH] moved layout --- src/textual/_layout.py | 40 ++++++++++++++++++++++++++++++++++++++++ src/textual/app.py | 3 ++- src/textual/widget.py | 5 +++-- 3 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 src/textual/_layout.py diff --git a/src/textual/_layout.py b/src/textual/_layout.py new file mode 100644 index 000000000..ea459a3e0 --- /dev/null +++ b/src/textual/_layout.py @@ -0,0 +1,40 @@ +from __future__ import annotations + +from abc import ABC, abstractmethod +from typing import ClassVar, NamedTuple, TYPE_CHECKING + + +from .geometry import Region, Offset, Size + + +if TYPE_CHECKING: + from .widget import Widget + + +class WidgetPlacement(NamedTuple): + """The position, size, and relative order of a widget within its parent.""" + + region: Region + widget: Widget | None = None # A widget of None means empty space + order: int = 0 + + +class Layout(ABC): + """Responsible for arranging Widgets in a view and rendering them.""" + + name: ClassVar[str] = "" + + @abstractmethod + def arrange( + self, parent: Widget, size: Size, scroll: Offset + ) -> tuple[list[WidgetPlacement], set[Widget]]: + """Generate a layout map that defines where on the screen the widgets will be drawn. + + Args: + parent (Widget): Parent widget. + size (Size): Size of container. + scroll (Offset): Offset to apply to the Widget placements. + + Returns: + Iterable[WidgetPlacement]: An iterable of widget location + """ diff --git a/src/textual/app.py b/src/textual/app.py index c0be02030..2341f2c6b 100644 --- a/src/textual/app.py +++ b/src/textual/app.py @@ -182,7 +182,8 @@ class App(Generic[ReturnType], DOMNode): self._return_value = result self.close_messages_no_wait() - def compose(self) -> Iterable[Widget]: + def compose(self) -> ComposeResult: + """Yield child widgets for a container.""" return yield diff --git a/src/textual/widget.py b/src/textual/widget.py index 161169958..e2a4b53c7 100644 --- a/src/textual/widget.py +++ b/src/textual/widget.py @@ -39,7 +39,7 @@ from .renderables.opacity import Opacity if TYPE_CHECKING: - from .app import App + from .app import App, ComposeResult from .scrollbar import ( ScrollBar, ScrollTo, @@ -115,7 +115,8 @@ class Widget(DOMNode): self.app.register(self, *anon_widgets, **widgets) self.screen.refresh() - def compose(self) -> Iterable[Widget]: + def compose(self) -> ComposeResult: + """Yield child widgets for a container.""" return yield