Skip to content

fix: fix alignment with auto and min height/width #5612

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

Closed
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

### Fixed

- Fixed issue with alignment in containers with auto and min height/width https://github.com/Textualize/textual/issues/5608

### Added

- Added Widget.preflight_checks to perform some debug checks after a widget is instantiated, to catch common errors. https://github.com/Textualize/textual/pull/5588
Expand Down
30 changes: 26 additions & 4 deletions src/textual/_arrange.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,34 @@ def arrange(
if styles.align_horizontal != "left" or styles.align_vertical != "top":
bounding_region = WidgetPlacement.get_bounds(layout_placements)
container_width, container_height = dock_region.size

align_width = container_width
if styles.is_auto_width:
align_width = (
int(
styles.min_width.resolve(
size, viewport, Fraction(container_width)
)
)
if styles.min_width
else 0
)

align_height = container_height
if styles.is_auto_height:
align_height = (
int(
styles.min_height.resolve(
size, viewport, Fraction(container_height)
)
)
if styles.min_height
else 0
)

placement_offset += styles._align_size(
bounding_region.size,
Size(
0 if styles.is_auto_width else container_width,
0 if styles.is_auto_height else container_height,
),
Size(align_width, align_height),
).clamped

if placement_offset:
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions tests/snapshot_tests/test_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -3767,3 +3767,34 @@ def on_mount(self) -> None:
label.border_title = "Border title"

assert snap_compare(BorderTitleApp())


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

You should see a blue label that is centered both horizontally and vertically
within a pink container. The container has auto width and height, but also
a min-width of 20 and a min-height of 3.
"""

class AlignmentApp(App):
CSS = """
Container {
align: center middle;
height: auto;
min-height: 3;
width: auto;
min-width: 20;
background: pink;
}

Label {
background: blue;
}
"""

def compose(self) -> ComposeResult:
with Container():
yield Label("centered")

assert(snap_compare(AlignmentApp()))
Loading