Skip to content

Commit

Permalink
Button optimization (#3885)
Browse files Browse the repository at this point in the history
* 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]
  • Loading branch information
willmcgugan authored Dec 16, 2023
1 parent ac9fd78 commit 1ce6473
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/textual/_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions src/textual/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 3 additions & 5 deletions src/textual/widgets/_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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."""

Expand Down
2 changes: 1 addition & 1 deletion src/textual/widgets/_tabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions tests/snapshot_tests/test_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)


Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 1ce6473

Please sign in to comment.