Skip to content

Commit 9f80226

Browse files
authored
Merge pull request #5121 from Textualize/fr-fix
Fr fix
2 parents 4a822bc + 11122f9 commit 9f80226

File tree

3 files changed

+205
-7
lines changed

3 files changed

+205
-7
lines changed

src/textual/_resolve.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,23 +196,30 @@ def resolve_box_models(
196196
Returns:
197197
List of resolved box models.
198198
"""
199-
margin_width, margin_height = margin
200-
201-
fraction_width = Fraction(max(0, size.width - margin_width))
202-
fraction_height = Fraction(max(0, size.height - margin_height))
203199

200+
margin_width, margin_height = margin
201+
fraction_width = Fraction(size.width)
202+
fraction_height = Fraction(size.height)
203+
fraction_zero = Fraction(0)
204204
margin_size = size - margin
205205

206+
margins = [widget.styles.margin.totals for widget in widgets]
207+
206208
# Fixed box models
207209
box_models: list[BoxModel | None] = [
208210
(
209211
None
210212
if _dimension is not None and _dimension.is_fraction
211213
else widget._get_box_model(
212-
size, viewport_size, fraction_width, fraction_height
214+
size,
215+
viewport_size,
216+
max(fraction_zero, fraction_width - margin_width),
217+
max(fraction_zero, fraction_height - margin_height),
213218
)
214219
)
215-
for (_dimension, widget) in zip(dimensions, widgets)
220+
for (_dimension, widget, (margin_width, margin_height)) in zip(
221+
dimensions, widgets, margins
222+
)
216223
]
217224

218225
if None not in box_models:
Lines changed: 153 additions & 0 deletions
Loading

tests/snapshot_tests/test_snapshots.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from textual import events, on
99
from textual.app import App, ComposeResult
1010
from textual.binding import Binding, Keymap
11-
from textual.containers import Center, Grid, Middle, Vertical, VerticalScroll
11+
from textual.containers import Center, Container, Grid, Middle, Vertical, VerticalScroll
1212
from textual.pilot import Pilot
1313
from textual.renderables.gradient import LinearGradient
1414
from textual.screen import ModalScreen, Screen
@@ -2336,3 +2336,41 @@ def compose(self) -> ComposeResult:
23362336
yield Label("100%")
23372337

23382338
assert snap_compare(BackgroundTintApp())
2339+
2340+
2341+
def test_fr_and_margin(snap_compare):
2342+
"""Regression test for https://github.com/Textualize/textual/issues/5116"""
2343+
2344+
# Check margins can be independently applied to widgets with fr unites
2345+
2346+
class FRApp(App):
2347+
CSS = """
2348+
#first-container {
2349+
background: green;
2350+
height: auto;
2351+
}
2352+
2353+
#second-container {
2354+
margin: 2;
2355+
background: red;
2356+
height: auto;
2357+
}
2358+
2359+
#third-container {
2360+
margin: 4;
2361+
background: blue;
2362+
height: auto;
2363+
}
2364+
"""
2365+
2366+
def compose(self) -> ComposeResult:
2367+
with Container(id="first-container"):
2368+
yield Label("No margin - should extend to left and right")
2369+
2370+
with Container(id="second-container"):
2371+
yield Label("A margin of 2, should be 2 cells around the text")
2372+
2373+
with Container(id="third-container"):
2374+
yield Label("A margin of 4, should be 4 cells around the text")
2375+
2376+
assert snap_compare(FRApp())

0 commit comments

Comments
 (0)