mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Merge pull request #1610 from Textualize/fix-1607
Fix #1607 to allow programmatic style changes
This commit is contained in:
95
tests/css/test_programmatic_style_changes.py
Normal file
95
tests/css/test_programmatic_style_changes.py
Normal file
@@ -0,0 +1,95 @@
|
||||
import pytest
|
||||
|
||||
from textual.app import App
|
||||
from textual.containers import Grid
|
||||
from textual.screen import Screen
|
||||
from textual.widgets import Label
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"style, value",
|
||||
[
|
||||
("grid_size_rows", 3),
|
||||
("grid_size_columns", 3),
|
||||
("grid_gutter_vertical", 4),
|
||||
("grid_gutter_horizontal", 4),
|
||||
("grid_rows", "1fr 3fr"),
|
||||
("grid_columns", "1fr 3fr"),
|
||||
],
|
||||
)
|
||||
async def test_programmatic_style_change_updates_children(style: str, value: object):
|
||||
"""Regression test for #1607 https://github.com/Textualize/textual/issues/1607
|
||||
|
||||
Some programmatic style changes to a widget were not updating the layout of the
|
||||
children widgets, which seemed to be happening when the style change did not affect
|
||||
the size of the widget but did affect the layout of the children.
|
||||
|
||||
This test, in particular, checks the attributes that _should_ affect the size of the
|
||||
children widgets.
|
||||
"""
|
||||
|
||||
class MyApp(App[None]):
|
||||
CSS = """
|
||||
Grid { grid-size: 2 2; }
|
||||
Label { width: 100%; height: 100%; }
|
||||
"""
|
||||
|
||||
def compose(self):
|
||||
yield Grid(
|
||||
Label("one"),
|
||||
Label("two"),
|
||||
Label("three"),
|
||||
Label("four"),
|
||||
)
|
||||
|
||||
app = MyApp()
|
||||
|
||||
async with app.run_test() as pilot:
|
||||
sizes = [(lbl.size.width, lbl.size.height) for lbl in app.screen.query(Label)]
|
||||
|
||||
setattr(app.query_one(Grid).styles, style, value)
|
||||
await pilot.pause()
|
||||
|
||||
assert sizes != [
|
||||
(lbl.size.width, lbl.size.height) for lbl in app.screen.query(Label)
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"style, value",
|
||||
[
|
||||
("align_horizontal", "right"),
|
||||
("align_vertical", "bottom"),
|
||||
("align", ("right", "bottom")),
|
||||
],
|
||||
)
|
||||
async def test_programmatic_align_change_updates_children_position(
|
||||
style: str, value: str
|
||||
):
|
||||
"""Regression test for #1607 for the align(_xxx) styles.
|
||||
|
||||
See https://github.com/Textualize/textual/issues/1607.
|
||||
"""
|
||||
|
||||
class MyApp(App[None]):
|
||||
CSS = "Grid { grid-size: 2 2; }"
|
||||
|
||||
def compose(self):
|
||||
yield Grid(
|
||||
Label("one"),
|
||||
Label("two"),
|
||||
Label("three"),
|
||||
Label("four"),
|
||||
)
|
||||
|
||||
app = MyApp()
|
||||
|
||||
async with app.run_test() as pilot:
|
||||
offsets = [(lbl.region.x, lbl.region.y) for lbl in app.screen.query(Label)]
|
||||
|
||||
setattr(app.query_one(Grid).styles, style, value)
|
||||
await pilot.pause()
|
||||
|
||||
assert offsets != [
|
||||
(lbl.region.x, lbl.region.y) for lbl in app.screen.query(Label)
|
||||
]
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,29 @@
|
||||
from textual.app import App
|
||||
from textual.containers import Grid
|
||||
from textual.widgets import Label
|
||||
|
||||
|
||||
class ProgrammaticScrollbarGutterChange(App[None]):
|
||||
CSS = """
|
||||
Grid { grid-size: 2 2; scrollbar-size: 5 5; }
|
||||
Label { width: 100%; height: 100%; background: red; }
|
||||
"""
|
||||
|
||||
def compose(self):
|
||||
yield Grid(
|
||||
Label("one"),
|
||||
Label("two"),
|
||||
Label("three"),
|
||||
Label("four"),
|
||||
)
|
||||
|
||||
def on_key(self, event):
|
||||
if event.key == "s":
|
||||
self.query_one(Grid).styles.scrollbar_gutter = "stable"
|
||||
|
||||
|
||||
app = ProgrammaticScrollbarGutterChange()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app().run()
|
||||
@@ -179,6 +179,16 @@ def test_nested_auto_heights(snap_compare):
|
||||
assert snap_compare("snapshot_apps/nested_auto_heights.py", press=["1", "2", "_"])
|
||||
|
||||
|
||||
def test_programmatic_scrollbar_gutter_change(snap_compare):
|
||||
"""Regression test for #1607 https://github.com/Textualize/textual/issues/1607
|
||||
|
||||
See also tests/css/test_programmatic_style_changes.py for other related regression tests.
|
||||
"""
|
||||
assert snap_compare(
|
||||
"snapshot_apps/programmatic_scrollbar_gutter_change.py", press=["s"]
|
||||
)
|
||||
|
||||
|
||||
# --- Other ---
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user