Skip to content

Commit b36ee03

Browse files
committed
fix fr resolve
1 parent 4a822bc commit b36ee03

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

src/textual/_resolve.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,14 @@ def resolve_box_models(
196196
Returns:
197197
List of resolved box models.
198198
"""
199+
200+
margins = [widget.styles.margin for widget in widgets]
201+
199202
margin_width, margin_height = margin
200203

201-
fraction_width = Fraction(max(0, size.width - margin_width))
202-
fraction_height = Fraction(max(0, size.height - margin_height))
204+
fraction_width = Fraction(size.width)
205+
fraction_height = Fraction(size.height)
206+
fraction_zero = Fraction(0)
203207

204208
margin_size = size - margin
205209

@@ -209,10 +213,13 @@ def resolve_box_models(
209213
None
210214
if _dimension is not None and _dimension.is_fraction
211215
else widget._get_box_model(
212-
size, viewport_size, fraction_width, fraction_height
216+
size,
217+
viewport_size,
218+
max(fraction_zero, fraction_width - margin.width),
219+
max(fraction_zero, fraction_height - margin.height),
213220
)
214221
)
215-
for (_dimension, widget) in zip(dimensions, widgets)
222+
for (_dimension, widget, margin) in zip(dimensions, widgets, margins)
216223
]
217224

218225
if None not in box_models:

tests/snapshot_tests/test_snapshots.py

Lines changed: 34 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,36 @@ 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+
class FRApp(App):
2343+
CSS = """
2344+
#first-container {
2345+
background: green;
2346+
height: auto;
2347+
}
2348+
2349+
#second-container {
2350+
margin: 2;
2351+
background: red;
2352+
height: auto;
2353+
}
2354+
2355+
#third-container {
2356+
margin: 4;
2357+
background: blue;
2358+
}
2359+
"""
2360+
2361+
def compose(self) -> ComposeResult:
2362+
with Container(id="first-container"):
2363+
yield Label("No margin - should extend to left and right")
2364+
2365+
with Container(id="second-container"):
2366+
yield Label("A margin of 2, should be 2 cells around the text")
2367+
2368+
with Center(id="third-container"):
2369+
yield Label("A margin of 4, should be 4 cells around the text")
2370+
2371+
assert snap_compare(FRApp())

0 commit comments

Comments
 (0)