Skip to content

Commit 2e1d2d0

Browse files
authored
Merge pull request #1332 from Textualize/container-offsets-fix
Fix container offsets
2 parents 3c425c3 + 7ba70ad commit 2e1d2d0

File tree

9 files changed

+215
-4
lines changed

9 files changed

+215
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2626
- Type selectors can now contain numbers https://github.com/Textualize/textual/issues/1253
2727
- Fixed visibility not affecting children https://github.com/Textualize/textual/issues/1313
2828
- Fixed issue with auto width/height and relative children https://github.com/Textualize/textual/issues/1319
29+
- Fixed issue with offset applied to containers https://github.com/Textualize/textual/issues/1256
2930

3031
## [0.5.0] - 2022-11-20
3132

docs/examples/guide/layout/combining_layouts.css

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@
55
grid-rows: 1fr;
66
}
77

8-
#left-pane > Static {
8+
#left-pane > Static {
99
background: $boost;
1010
color: auto;
1111
margin-bottom: 1;
1212
padding: 1;
1313
}
1414

1515
#left-pane {
16+
height: 100%;
1617
row-span: 2;
1718
background: $panel;
1819
border: dodgerblue;
1920
}
2021

2122
#top-right {
23+
height: 100%;
2224
background: $panel;
2325
border: mediumvioletred;
2426
}
@@ -31,6 +33,7 @@
3133
}
3234

3335
#bottom-right {
36+
height: 100%;
3437
layout: grid;
3538
grid-size: 3;
3639
grid-columns: 1fr;

examples/calculator.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Screen {
1111
margin: 1 2;
1212
min-height: 25;
1313
min-width: 26;
14+
height: 100%;
1415
}
1516

1617
Button {

src/textual/_compositor.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,9 @@ def add_widget(
371371
)
372372

373373
# Container region is minus border
374-
container_region = region.shrink(widget.styles.gutter)
374+
container_region = region.shrink(widget.styles.gutter).translate(
375+
layout_offset
376+
)
375377
container_size = container_region.size
376378

377379
# Widgets with scrollbars (containers or scroll view) require additional processing
@@ -393,7 +395,7 @@ def add_widget(
393395
widgets.update(arranged_widgets)
394396

395397
# An offset added to all placements
396-
placement_offset = container_region.offset + layout_offset
398+
placement_offset = container_region.offset
397399
placement_scroll_offset = placement_offset - widget.scroll_offset
398400

399401
_layers = widget.layers

src/textual/_styles_cache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def render(
198198
if crop is None:
199199
crop = size.region
200200

201-
width, height = size
201+
width, _height = size
202202
if width != self._width:
203203
self.clear()
204204
self._width = width

src/textual/geometry.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,7 @@ def contains_region(self, other: Region) -> bool:
635635
and (y2 >= oy2 >= y1)
636636
)
637637

638+
@lru_cache(maxsize=1024)
638639
def translate(self, offset: tuple[int, int]) -> Region:
639640
"""Move the offset of the Region.
640641

tests/snapshot_tests/__snapshots__/test_snapshots.ambr

Lines changed: 157 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from textual.app import App, ComposeResult
2+
from textual.containers import Vertical
3+
from textual.widgets import Label, Static
4+
5+
6+
class Box(Static):
7+
DEFAULT_CSS = """
8+
Box {
9+
border: solid white;
10+
background: darkblue;
11+
width: 16;
12+
height: auto;
13+
}
14+
"""
15+
16+
def compose(self) -> ComposeResult:
17+
yield Label("FOO\nBAR\nBAZ")
18+
19+
20+
class OffsetsApp(App):
21+
22+
CSS = """
23+
24+
#box1 {
25+
offset: 5 5;
26+
}
27+
28+
#box2 {
29+
offset: 15 10;
30+
}
31+
32+
"""
33+
34+
def compose(self) -> ComposeResult:
35+
yield Box(id="box1")
36+
yield Box(id="box2")
37+
38+
39+
if __name__ == "__main__":
40+
app = OffsetsApp()
41+
app.run()

tests/snapshot_tests/test_snapshots.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ def test_columns_height(snap_compare):
151151
assert snap_compare("snapshot_apps/columns_height.py")
152152

153153

154+
def test_offsets(snap_compare):
155+
"""Test offsets of containers"""
156+
assert snap_compare("snapshot_apps/offsets.py")
157+
158+
154159
# --- Other ---
155160

156161

0 commit comments

Comments
 (0)