|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | + |
| 5 | +from textual.geometry import NULL_OFFSET, Region, Size |
| 6 | +from textual.layout import ArrangeResult, Layout, WidgetPlacement |
| 7 | + |
| 8 | +if TYPE_CHECKING: |
| 9 | + |
| 10 | + from textual.widget import Widget |
| 11 | + |
| 12 | + |
| 13 | +class StreamLayout(Layout): |
| 14 | + name = "stream" |
| 15 | + |
| 16 | + def arrange( |
| 17 | + self, parent: Widget, children: list[Widget], size: Size, greedy: bool = True |
| 18 | + ) -> ArrangeResult: |
| 19 | + parent.pre_layout(self) |
| 20 | + viewport = parent.app.size |
| 21 | + |
| 22 | + placements: list[WidgetPlacement] = [] |
| 23 | + if not children: |
| 24 | + return [] |
| 25 | + width = size.width |
| 26 | + first_child_styles = children[0].styles |
| 27 | + y = first_child_styles.margin.top |
| 28 | + previous_margin = 0 |
| 29 | + null_offset = NULL_OFFSET |
| 30 | + |
| 31 | + for widget in children: |
| 32 | + styles = widget.styles |
| 33 | + overlay = styles.overlay == "screen" |
| 34 | + absolute = styles.has_rule("position") and styles.position == "absolute" |
| 35 | + margin = styles.margin |
| 36 | + top, right, bottom, left = margin |
| 37 | + margin_width = left + right |
| 38 | + y += max(top, previous_margin) |
| 39 | + previous_margin = bottom |
| 40 | + height = widget.get_content_height(size, viewport, width - margin_width) |
| 41 | + height += styles.gutter.height |
| 42 | + if (max_height := styles.max_height) is not None and max_height.is_cells: |
| 43 | + height = min(height, int(max_height.value)) |
| 44 | + placements.append( |
| 45 | + WidgetPlacement( |
| 46 | + Region(left, y, width - margin_width, height), |
| 47 | + null_offset, |
| 48 | + margin, |
| 49 | + widget, |
| 50 | + 0, |
| 51 | + False, |
| 52 | + overlay, |
| 53 | + absolute, |
| 54 | + ) |
| 55 | + ) |
| 56 | + y += height |
| 57 | + |
| 58 | + return placements |
0 commit comments