Skip to content

Commit a2ba9eb

Browse files
authored
Merge pull request #4369 from Textualize/auto-width
width fix
2 parents 3626ace + 6dabbca commit a2ba9eb

File tree

7 files changed

+211
-2
lines changed

7 files changed

+211
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010
### Fixed
1111

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

1415
### Changed
1516

src/textual/_layout.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def get_content_width(self, widget: Widget, container: Size, viewport: Size) ->
160160
if not widget._nodes:
161161
width = 0
162162
else:
163-
arrangement = widget._arrange(Size(container.width, 0))
163+
arrangement = widget._arrange(Size(0, 0))
164164
return arrangement.total_region.right
165165
return width
166166

src/textual/_resolve.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def resolve_fraction_unit(
8686
remaining_space: Fraction,
8787
resolve_dimension: Literal["width", "height"] = "width",
8888
) -> Fraction:
89-
"""Calculate the fraction
89+
"""Calculate the fraction.
9090
9191
Args:
9292
widget_styles: Styles for widgets with fraction units.

src/textual/widgets/_button.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from typing import TYPE_CHECKING, cast
55

66
import rich.repr
7+
from rich.cells import cell_len
78
from rich.console import ConsoleRenderable, RenderableType
89
from rich.text import Text, TextType
910
from typing_extensions import Literal, Self
@@ -15,6 +16,7 @@
1516

1617
from ..binding import Binding
1718
from ..css._error_tools import friendly_list
19+
from ..geometry import Size
1820
from ..message import Message
1921
from ..pad import HorizontalPad
2022
from ..reactive import reactive
@@ -203,6 +205,13 @@ def __init__(
203205
self.active_effect_duration = 0.3
204206
"""Amount of time in seconds the button 'press' animation lasts."""
205207

208+
def get_content_width(self, container: Size, viewport: Size) -> int:
209+
try:
210+
return max([cell_len(line) for line in self.label.plain.splitlines()]) + 2
211+
except ValueError:
212+
# Empty string label
213+
return 2
214+
206215
def __rich_repr__(self) -> rich.repr.Result:
207216
yield from super().__rich_repr__()
208217
yield "variant", self.variant, "default"

tests/snapshot_tests/__snapshots__/test_snapshots.ambr

Lines changed: 157 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from textual.app import App, ComposeResult
2+
from textual.containers import Vertical
3+
from textual.widgets import Label
4+
5+
6+
class Width100PCentApp(App[None]):
7+
8+
CSS = """
9+
Vertical {
10+
border: solid red;
11+
width: auto;
12+
13+
Label {
14+
border: solid green;
15+
}
16+
17+
#first {
18+
width: 100%;
19+
}
20+
21+
#second {
22+
width: auto;
23+
}
24+
}
25+
"""
26+
27+
def compose(self) -> ComposeResult:
28+
with Vertical():
29+
yield Label("I want to be 100% of my parent", id="first")
30+
yield Label(
31+
"I want my parent to be wide enough to wrap me and no more", id="second"
32+
)
33+
34+
35+
if __name__ == "__main__":
36+
Width100PCentApp().run()

tests/snapshot_tests/test_snapshots.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,7 @@ async def run_before(pilot):
686686
run_before=run_before,
687687
)
688688

689+
689690
def test_markdown_space_squashing(snap_compare):
690691
assert snap_compare(SNAPSHOT_APPS_DIR / "markdown_whitespace.py")
691692

@@ -1174,3 +1175,8 @@ def test_welcome(snap_compare):
11741175
def test_button_with_console_markup(snap_compare):
11751176
"""Regression test for https://github.com/Textualize/textual/issues/4328"""
11761177
assert snap_compare(SNAPSHOT_APPS_DIR / "button_markup.py")
1178+
1179+
1180+
def test_width_100(snap_compare):
1181+
"""Regression test for https://github.com/Textualize/textual/issues/4360"""
1182+
assert snap_compare(SNAPSHOT_APPS_DIR / "width_100.py")

0 commit comments

Comments
 (0)