Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fr fix #5121

Merged
merged 6 commits into from
Oct 17, 2024
Merged

Fr fix #5121

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/textual/_resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,23 +196,30 @@ def resolve_box_models(
Returns:
List of resolved box models.
"""
margin_width, margin_height = margin

fraction_width = Fraction(max(0, size.width - margin_width))
fraction_height = Fraction(max(0, size.height - margin_height))

margin_width, margin_height = margin
fraction_width = Fraction(size.width)
fraction_height = Fraction(size.height)
fraction_zero = Fraction(0)
margin_size = size - margin

margins = [widget.styles.margin.totals for widget in widgets]

# Fixed box models
box_models: list[BoxModel | None] = [
(
None
if _dimension is not None and _dimension.is_fraction
else widget._get_box_model(
size, viewport_size, fraction_width, fraction_height
size,
viewport_size,
max(fraction_zero, fraction_width - margin_width),
max(fraction_zero, fraction_height - margin_height),
)
)
for (_dimension, widget) in zip(dimensions, widgets)
for (_dimension, widget, (margin_width, margin_height)) in zip(
dimensions, widgets, margins
)
]

if None not in box_models:
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 39 additions & 1 deletion tests/snapshot_tests/test_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from textual import events, on
from textual.app import App, ComposeResult
from textual.binding import Binding, Keymap
from textual.containers import Center, Grid, Middle, Vertical, VerticalScroll
from textual.containers import Center, Container, Grid, Middle, Vertical, VerticalScroll
from textual.pilot import Pilot
from textual.renderables.gradient import LinearGradient
from textual.screen import ModalScreen, Screen
Expand Down Expand Up @@ -2336,3 +2336,41 @@ def compose(self) -> ComposeResult:
yield Label("100%")

assert snap_compare(BackgroundTintApp())


def test_fr_and_margin(snap_compare):
"""Regression test for https://github.com/Textualize/textual/issues/5116"""

# Check margins can be independently applied to widgets with fr unites

class FRApp(App):
CSS = """
#first-container {
background: green;
height: auto;
}

#second-container {
margin: 2;
background: red;
height: auto;
}

#third-container {
margin: 4;
background: blue;
height: auto;
}
"""

def compose(self) -> ComposeResult:
with Container(id="first-container"):
yield Label("No margin - should extend to left and right")

with Container(id="second-container"):
yield Label("A margin of 2, should be 2 cells around the text")

with Container(id="third-container"):
yield Label("A margin of 4, should be 4 cells around the text")

assert snap_compare(FRApp())
Loading