fix for min and max with fr unints (#2390)

* fix for min and max with fr unints

* snapshit

* forgot snapshit tests

* fix resolve

* added unit tests

* Windoze fix
This commit is contained in:
Will McGugan
2023-04-27 13:35:16 +01:00
committed by GitHub
parent fe99df95fc
commit dd70a7a2dc
10 changed files with 426 additions and 31 deletions

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,50 @@
from textual.app import App, ComposeResult
from textual.containers import Horizontal, Vertical, VerticalScroll
from textual.widgets import Header, Footer, Static
class ScreenSplitApp(App[None]):
CSS = """
Horizontal {
width: 1fr;
}
Vertical {
width: 1fr;
background: blue;
min-width: 20;
}
#scroll1 {
width: 1fr;
background: $panel;
}
#scroll2 {
width: 2fr;
background: $panel;
}
Static {
width: 1fr;
content-align: center middle;
background: $boost;
}
"""
def compose(self) -> ComposeResult:
yield Header()
with Horizontal():
yield Vertical()
with VerticalScroll(id="scroll1"):
for n in range(100):
yield Static(f"This is content number {n}")
with VerticalScroll(id="scroll2"):
for n in range(100):
yield Static(f"This is content number {n}")
yield Footer()
if __name__ == "__main__":
ScreenSplitApp().run()

View File

@@ -319,7 +319,7 @@ def test_demo(snap_compare):
"""Test the demo app (python -m textual)"""
assert snap_compare(
Path("../../src/textual/demo.py"),
press=["down", "down", "down", "wait:250"],
press=["down", "down", "down", "wait:500"],
terminal_size=(100, 30),
)
@@ -460,3 +460,8 @@ def test_scroll_to_center(snap_compare):
def test_quickly_change_tabs(snap_compare):
# https://github.com/Textualize/textual/issues/2229
assert snap_compare(SNAPSHOT_APPS_DIR / "quickly_change_tabs.py", press=["p"])
def test_fr_unit_with_min(snap_compare):
# https://github.com/Textualize/textual/issues/2378
assert snap_compare(SNAPSHOT_APPS_DIR / "fr_with_min.py")

View File

@@ -1,8 +1,11 @@
from fractions import Fraction
import pytest
from textual._resolve import resolve
from textual._resolve import resolve, resolve_fraction_unit
from textual.css.scalar import Scalar
from textual.geometry import Size
from textual.widget import Widget
def test_resolve_empty():
@@ -56,3 +59,66 @@ def test_resolve(scalars, total, gutter, result):
)
== result
)
def test_resolve_fraction_unit():
"""Test resolving fraction units in combination with minimum widths."""
widget1 = Widget()
widget2 = Widget()
widget3 = Widget()
widget1.styles.width = "1fr"
widget1.styles.min_width = 20
widget2.styles.width = "2fr"
widget2.styles.min_width = 10
widget3.styles.width = "1fr"
styles = (widget1.styles, widget2.styles, widget3.styles)
# Try with width 80.
# Fraction unit should one fourth of width
assert resolve_fraction_unit(
styles,
Size(80, 24),
Size(80, 24),
Fraction(80),
resolve_dimension="width",
) == Fraction(20)
# Try with width 50
# First widget is fixed at 20
# Remaining three widgets have 30 to play with
# Fraction is 10
assert resolve_fraction_unit(
styles,
Size(80, 24),
Size(80, 24),
Fraction(50),
resolve_dimension="width",
) == Fraction(10)
# Try with width 35
# First widget fixed at 20
# Fraction is 5
assert resolve_fraction_unit(
styles,
Size(80, 24),
Size(80, 24),
Fraction(35),
resolve_dimension="width",
) == Fraction(5)
# Try with width 32
# First widget fixed at 20
# Second widget is fixed at 10
# Remaining widget has all the space
# Fraction is 2
assert resolve_fraction_unit(
styles,
Size(80, 24),
Size(80, 24),
Fraction(32),
resolve_dimension="width",
) == Fraction(2)