Skip to content

Commit

Permalink
add offset to WidgetPlacement
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Nov 18, 2024
1 parent 435f73b commit 845840e
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 21 deletions.
13 changes: 9 additions & 4 deletions src/textual/_arrange.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import TYPE_CHECKING, Iterable, Mapping, Sequence

from textual._partition import partition
from textual.geometry import Region, Size, Spacing
from textual.geometry import NULL_OFFSET, NULL_SPACING, Region, Size, Spacing
from textual.layout import DockArrangeResult, WidgetPlacement

if TYPE_CHECKING:
Expand Down Expand Up @@ -133,7 +133,8 @@ def _arrange_dock_widgets(
region_offset = region.offset
size = region.size
width, height = size
null_spacing = Spacing()
null_spacing = NULL_SPACING
null_offset = NULL_OFFSET

top = right = bottom = left = 0

Expand Down Expand Up @@ -173,6 +174,7 @@ def _arrange_dock_widgets(
append_placement(
_WidgetPlacement(
dock_region.translate(region_offset),
null_offset,
null_spacing,
dock_widget,
top_z,
Expand Down Expand Up @@ -202,7 +204,8 @@ def _arrange_split_widgets(
placements: list[WidgetPlacement] = []
append_placement = placements.append
view_region = size.region
null_spacing = Spacing()
null_spacing = NULL_SPACING
null_offset = NULL_OFFSET

for split_widget in split_widgets:
split = split_widget.styles.split
Expand All @@ -226,7 +229,9 @@ def _arrange_split_widgets(
raise AssertionError("invalid value for split edge") # pragma: no-cover

append_placement(
_WidgetPlacement(split_region, null_spacing, split_widget, 1, True)
_WidgetPlacement(
split_region, null_offset, null_spacing, split_widget, 1, True
)
)

return placements, view_region
2 changes: 1 addition & 1 deletion src/textual/_compositor.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ def add_widget(
)

# Add all the widgets
for sub_region, _, sub_widget, z, fixed, overlay in reversed(
for sub_region, _, _, sub_widget, z, fixed, overlay in reversed(
placements
):
layer_index = get_layer_index(sub_widget.layer, 0)
Expand Down
17 changes: 13 additions & 4 deletions src/textual/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class WidgetPlacement(NamedTuple):
"""The position, size, and relative order of a widget within its parent."""

region: Region
offset: Offset
margin: Spacing
widget: Widget
order: int = 0
Expand All @@ -92,7 +93,7 @@ class WidgetPlacement(NamedTuple):

@classmethod
def translate(
cls, placements: list[WidgetPlacement], offset: Offset
cls, placements: list[WidgetPlacement], translate_offset: Offset
) -> list[WidgetPlacement]:
"""Move all placements by a given offset.
Expand All @@ -103,10 +104,18 @@ def translate(
Returns:
Placements with adjusted region, or same instance if offset is null.
"""
if offset:
if translate_offset:
return [
cls(region + offset, margin, layout_widget, order, fixed, overlay)
for region, margin, layout_widget, order, fixed, overlay in placements
cls(
region + translate_offset,
offset,
margin,
layout_widget,
order,
fixed,
overlay,
)
for region, offset, margin, layout_widget, order, fixed, overlay in placements
]
return placements

Expand Down
3 changes: 2 additions & 1 deletion src/textual/layouts/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from textual._resolve import resolve
from textual.css.scalar import Scalar
from textual.geometry import Region, Size, Spacing
from textual.geometry import NULL_OFFSET, Region, Size, Spacing
from textual.layout import ArrangeResult, Layout, WidgetPlacement

if TYPE_CHECKING:
Expand Down Expand Up @@ -292,6 +292,7 @@ def apply_height_limits(widget: Widget, height: int) -> int:
add_placement(
WidgetPlacement(
region + offset,
NULL_OFFSET,
(
margin
if gutter_spacing is None
Expand Down
3 changes: 2 additions & 1 deletion src/textual/layouts/horizontal.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import TYPE_CHECKING

from textual._resolve import resolve_box_models
from textual.geometry import Region, Size
from textual.geometry import NULL_OFFSET, Region, Size
from textual.layout import ArrangeResult, Layout, WidgetPlacement

if TYPE_CHECKING:
Expand Down Expand Up @@ -89,6 +89,7 @@ def arrange(
(next_x - x.__floor__()).__floor__(),
content_height.__floor__(),
),
NULL_OFFSET,
box_margin,
widget,
0,
Expand Down
3 changes: 2 additions & 1 deletion src/textual/layouts/vertical.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import TYPE_CHECKING

from textual._resolve import resolve_box_models
from textual.geometry import Region, Size
from textual.geometry import NULL_OFFSET, Region, Size
from textual.layout import ArrangeResult, Layout, WidgetPlacement

if TYPE_CHECKING:
Expand Down Expand Up @@ -93,6 +93,7 @@ def arrange(
content_width.__floor__(),
next_y.__floor__() - y.__floor__(),
),
NULL_OFFSET,
box_margin,
widget,
0,
Expand Down
41 changes: 32 additions & 9 deletions tests/test_arrange.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from textual._arrange import TOP_Z, arrange
from textual.app import App
from textual.geometry import Region, Size, Spacing
from textual.geometry import NULL_OFFSET, Region, Size, Spacing
from textual.layout import WidgetPlacement
from textual.widget import Widget

Expand All @@ -27,9 +27,11 @@ def test_arrange_dock_top():

assert result.placements == [
WidgetPlacement(
Region(0, 0, 80, 1), Spacing(), header, order=TOP_Z, fixed=True
Region(0, 0, 80, 1), NULL_OFFSET, Spacing(), header, order=TOP_Z, fixed=True
),
WidgetPlacement(
Region(0, 1, 80, 23), NULL_OFFSET, Spacing(), child, order=0, fixed=False
),
WidgetPlacement(Region(0, 1, 80, 23), Spacing(), child, order=0, fixed=False),
]
assert result.widgets == {child, header}

Expand All @@ -45,9 +47,16 @@ def test_arrange_dock_left():
result = arrange(container, [child, header], Size(80, 24), Size(80, 24))
assert result.placements == [
WidgetPlacement(
Region(0, 0, 10, 24), Spacing(), header, order=TOP_Z, fixed=True
Region(0, 0, 10, 24),
NULL_OFFSET,
Spacing(),
header,
order=TOP_Z,
fixed=True,
),
WidgetPlacement(
Region(10, 0, 70, 24), NULL_OFFSET, Spacing(), child, order=0, fixed=False
),
WidgetPlacement(Region(10, 0, 70, 24), Spacing(), child, order=0, fixed=False),
]
assert result.widgets == {child, header}

Expand All @@ -63,9 +72,16 @@ def test_arrange_dock_right():
result = arrange(container, [child, header], Size(80, 24), Size(80, 24))
assert result.placements == [
WidgetPlacement(
Region(70, 0, 10, 24), Spacing(), header, order=TOP_Z, fixed=True
Region(70, 0, 10, 24),
NULL_OFFSET,
Spacing(),
header,
order=TOP_Z,
fixed=True,
),
WidgetPlacement(
Region(0, 0, 70, 24), NULL_OFFSET, Spacing(), child, order=0, fixed=False
),
WidgetPlacement(Region(0, 0, 70, 24), Spacing(), child, order=0, fixed=False),
]
assert result.widgets == {child, header}

Expand All @@ -81,9 +97,16 @@ def test_arrange_dock_bottom():
result = arrange(container, [child, header], Size(80, 24), Size(80, 24))
assert result.placements == [
WidgetPlacement(
Region(0, 23, 80, 1), Spacing(), header, order=TOP_Z, fixed=True
Region(0, 23, 80, 1),
NULL_OFFSET,
Spacing(),
header,
order=TOP_Z,
fixed=True,
),
WidgetPlacement(
Region(0, 0, 80, 23), NULL_OFFSET, Spacing(), child, order=0, fixed=False
),
WidgetPlacement(Region(0, 0, 80, 23), Spacing(), child, order=0, fixed=False),
]
assert result.widgets == {child, header}

Expand Down

0 comments on commit 845840e

Please sign in to comment.