Skip to content

Commit

Permalink
Merge pull request #4369 from Textualize/auto-width
Browse files Browse the repository at this point in the history
width fix
  • Loading branch information
willmcgugan authored Apr 1, 2024
2 parents 3626ace + 6dabbca commit a2ba9eb
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed

- Fix priority bindings not appearing in footer when key clashes with focused widget https://github.com/Textualize/textual/pull/4342
- Reverted auto-width change https://github.com/Textualize/textual/pull/4369

### Changed

Expand Down
2 changes: 1 addition & 1 deletion src/textual/_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def get_content_width(self, widget: Widget, container: Size, viewport: Size) ->
if not widget._nodes:
width = 0
else:
arrangement = widget._arrange(Size(container.width, 0))
arrangement = widget._arrange(Size(0, 0))
return arrangement.total_region.right
return width

Expand Down
2 changes: 1 addition & 1 deletion src/textual/_resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def resolve_fraction_unit(
remaining_space: Fraction,
resolve_dimension: Literal["width", "height"] = "width",
) -> Fraction:
"""Calculate the fraction
"""Calculate the fraction.
Args:
widget_styles: Styles for widgets with fraction units.
Expand Down
9 changes: 9 additions & 0 deletions src/textual/widgets/_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import TYPE_CHECKING, cast

import rich.repr
from rich.cells import cell_len
from rich.console import ConsoleRenderable, RenderableType
from rich.text import Text, TextType
from typing_extensions import Literal, Self
Expand All @@ -15,6 +16,7 @@

from ..binding import Binding
from ..css._error_tools import friendly_list
from ..geometry import Size
from ..message import Message
from ..pad import HorizontalPad
from ..reactive import reactive
Expand Down Expand Up @@ -203,6 +205,13 @@ def __init__(
self.active_effect_duration = 0.3
"""Amount of time in seconds the button 'press' animation lasts."""

def get_content_width(self, container: Size, viewport: Size) -> int:
try:
return max([cell_len(line) for line in self.label.plain.splitlines()]) + 2
except ValueError:
# Empty string label
return 2

def __rich_repr__(self) -> rich.repr.Result:
yield from super().__rich_repr__()
yield "variant", self.variant, "default"
Expand Down
157 changes: 157 additions & 0 deletions tests/snapshot_tests/__snapshots__/test_snapshots.ambr

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions tests/snapshot_tests/snapshot_apps/width_100.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from textual.app import App, ComposeResult
from textual.containers import Vertical
from textual.widgets import Label


class Width100PCentApp(App[None]):

CSS = """
Vertical {
border: solid red;
width: auto;
Label {
border: solid green;
}
#first {
width: 100%;
}
#second {
width: auto;
}
}
"""

def compose(self) -> ComposeResult:
with Vertical():
yield Label("I want to be 100% of my parent", id="first")
yield Label(
"I want my parent to be wide enough to wrap me and no more", id="second"
)


if __name__ == "__main__":
Width100PCentApp().run()
6 changes: 6 additions & 0 deletions tests/snapshot_tests/test_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ async def run_before(pilot):
run_before=run_before,
)


def test_markdown_space_squashing(snap_compare):
assert snap_compare(SNAPSHOT_APPS_DIR / "markdown_whitespace.py")

Expand Down Expand Up @@ -1174,3 +1175,8 @@ def test_welcome(snap_compare):
def test_button_with_console_markup(snap_compare):
"""Regression test for https://github.com/Textualize/textual/issues/4328"""
assert snap_compare(SNAPSHOT_APPS_DIR / "button_markup.py")


def test_width_100(snap_compare):
"""Regression test for https://github.com/Textualize/textual/issues/4360"""
assert snap_compare(SNAPSHOT_APPS_DIR / "width_100.py")

0 comments on commit a2ba9eb

Please sign in to comment.