File size: 3,015 Bytes
5b76e0f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | from __future__ import annotations
from abc import ABC, abstractmethod
import sys
from typing import ClassVar, NamedTuple, TYPE_CHECKING
from .geometry import Region, Size, Spacing
if sys.version_info >= (3, 10):
from typing import TypeAlias
else: # pragma: no cover
from typing_extensions import TypeAlias
if TYPE_CHECKING:
from .widget import Widget
ArrangeResult: TypeAlias = "tuple[list[WidgetPlacement], set[Widget]]"
DockArrangeResult: TypeAlias = "tuple[list[WidgetPlacement], set[Widget], Spacing]"
class WidgetPlacement(NamedTuple):
"""The position, size, and relative order of a widget within its parent."""
region: Region
margin: Spacing
widget: Widget
order: int = 0
fixed: bool = False
class Layout(ABC):
"""Responsible for arranging Widgets in a view and rendering them."""
name: ClassVar[str] = ""
def __repr__(self) -> str:
return f"<{self.name}>"
@abstractmethod
def arrange(
self, parent: Widget, children: list[Widget], size: Size
) -> ArrangeResult:
"""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.
Returns:
Iterable[WidgetPlacement]: An iterable of widget location
"""
def get_content_width(self, widget: Widget, container: Size, viewport: Size) -> int:
"""Get the width of the content.
Args:
widget (Widget): The container widget.
container (Size): The container size.
viewport (Size): The viewport size.
Returns:
int: Width of the content.
"""
width: int | None = None
widget_gutter = widget.gutter.width
for child in widget.displayed_children:
if not child.is_container:
child_width = (
child.get_content_width(container, viewport)
+ widget_gutter
+ child.gutter.width
)
width = child_width if width is None else max(width, child_width)
if width is None:
width = container.width
return width
def get_content_height(
self, widget: Widget, container: Size, viewport: Size, width: int
) -> int:
"""Get the content height.
Args:
widget (Widget): The container widget.
container (Size): The container size.
viewport (Size): The viewport.
width (int): The content width.
Returns:
int: Content height (in lines).
"""
if not widget.displayed_children:
height = container.height
else:
placements, *_ = widget._arrange(Size(width, container.height))
height = max(
placement.region.bottom + placement.margin.bottom
for placement in placements
)
return height
|