From 1ce647350d1655f4320e61b069dd5deaacffb39e Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Sat, 16 Dec 2023 18:38:36 +0000 Subject: [PATCH] Button optimization (#3885) * optimize button * optimize * simplify * fix test * explicit none * sleep for animation * test fix * wait snapshots * remove tab delay * reduce delay * fix * schdule animations * remove import [skip ci] --- src/textual/_doc.py | 2 ++ src/textual/app.py | 3 +++ src/textual/widgets/_button.py | 8 +++----- src/textual/widgets/_tabs.py | 2 +- tests/snapshot_tests/test_snapshots.py | 6 ++++-- 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/textual/_doc.py b/src/textual/_doc.py index 8639f1d25d..54aa7008be 100644 --- a/src/textual/_doc.py +++ b/src/textual/_doc.py @@ -127,6 +127,8 @@ async def auto_pilot(pilot: Pilot) -> None: if wait_for_animation: await pilot.wait_for_scheduled_animations() await pilot.pause() + await pilot.pause() + await pilot.wait_for_scheduled_animations() svg = app.export_screenshot(title=title) app.exit(svg) diff --git a/src/textual/app.py b/src/textual/app.py index 835ffc2741..fcbb0b729d 100644 --- a/src/textual/app.py +++ b/src/textual/app.py @@ -1185,6 +1185,9 @@ async def _press_keys(self, keys: Iterable[str]) -> None: if key.startswith("wait:"): _, wait_ms = key.split(":") await asyncio.sleep(float(wait_ms) / 1000) + await wait_for_idle(0) + await app._animator.wait_until_complete() + await wait_for_idle(0) else: if len(key) == 1 and not key.isalnum(): key = _character_to_key(key) diff --git a/src/textual/widgets/_button.py b/src/textual/widgets/_button.py index e1163a4737..c4c5843d13 100644 --- a/src/textual/widgets/_button.py +++ b/src/textual/widgets/_button.py @@ -156,7 +156,7 @@ class Button(Widget, can_focus=True): label: reactive[TextType] = reactive[TextType]("") """The text label that appears within the button.""" - variant = reactive("default") + variant = reactive("default", init=False) """The variant name for the button.""" class Pressed(Message): @@ -204,10 +204,8 @@ def __init__( if label is None: label = self.css_identifier_styled - self.label = self.validate_label(label) - - self.variant = self.validate_variant(variant) - + self.label = label + self.variant = variant self.active_effect_duration = 0.3 """Amount of time in seconds the button 'press' animation lasts.""" diff --git a/src/textual/widgets/_tabs.py b/src/textual/widgets/_tabs.py index 02639e7c7a..eaa6dfa694 100644 --- a/src/textual/widgets/_tabs.py +++ b/src/textual/widgets/_tabs.py @@ -596,7 +596,7 @@ def animate_underline() -> None: underline.animate("highlight_start", start, duration=0.3) underline.animate("highlight_end", end, duration=0.3) - self.set_timer(0.05, animate_underline) + self.set_timer(0.02, lambda: self.call_after_refresh(animate_underline)) else: underline.highlight_start = start underline.highlight_end = end diff --git a/tests/snapshot_tests/test_snapshots.py b/tests/snapshot_tests/test_snapshots.py index 66cbe6a534..d67e67098a 100644 --- a/tests/snapshot_tests/test_snapshots.py +++ b/tests/snapshot_tests/test_snapshots.py @@ -541,7 +541,7 @@ def test_richlog_scroll(snap_compare): def test_tabs_invalidate(snap_compare): assert snap_compare( SNAPSHOT_APPS_DIR / "tabs_invalidate.py", - press=["tab", "right"], + press=["tab", "right", "wait:350"], ) @@ -680,7 +680,9 @@ 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"]) + assert snap_compare( + SNAPSHOT_APPS_DIR / "quickly_change_tabs.py", press=["p", "wait:350"] + ) def test_fr_unit_with_min(snap_compare):