Skip to content

Commit 1ce6473

Browse files
authored
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]
1 parent ac9fd78 commit 1ce6473

File tree

5 files changed

+13
-8
lines changed

5 files changed

+13
-8
lines changed

src/textual/_doc.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ async def auto_pilot(pilot: Pilot) -> None:
127127
if wait_for_animation:
128128
await pilot.wait_for_scheduled_animations()
129129
await pilot.pause()
130+
await pilot.pause()
131+
await pilot.wait_for_scheduled_animations()
130132
svg = app.export_screenshot(title=title)
131133

132134
app.exit(svg)

src/textual/app.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,9 @@ async def _press_keys(self, keys: Iterable[str]) -> None:
11851185
if key.startswith("wait:"):
11861186
_, wait_ms = key.split(":")
11871187
await asyncio.sleep(float(wait_ms) / 1000)
1188+
await wait_for_idle(0)
1189+
await app._animator.wait_until_complete()
1190+
await wait_for_idle(0)
11881191
else:
11891192
if len(key) == 1 and not key.isalnum():
11901193
key = _character_to_key(key)

src/textual/widgets/_button.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class Button(Widget, can_focus=True):
156156
label: reactive[TextType] = reactive[TextType]("")
157157
"""The text label that appears within the button."""
158158

159-
variant = reactive("default")
159+
variant = reactive("default", init=False)
160160
"""The variant name for the button."""
161161

162162
class Pressed(Message):
@@ -204,10 +204,8 @@ def __init__(
204204
if label is None:
205205
label = self.css_identifier_styled
206206

207-
self.label = self.validate_label(label)
208-
209-
self.variant = self.validate_variant(variant)
210-
207+
self.label = label
208+
self.variant = variant
211209
self.active_effect_duration = 0.3
212210
"""Amount of time in seconds the button 'press' animation lasts."""
213211

src/textual/widgets/_tabs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ def animate_underline() -> None:
596596
underline.animate("highlight_start", start, duration=0.3)
597597
underline.animate("highlight_end", end, duration=0.3)
598598

599-
self.set_timer(0.05, animate_underline)
599+
self.set_timer(0.02, lambda: self.call_after_refresh(animate_underline))
600600
else:
601601
underline.highlight_start = start
602602
underline.highlight_end = end

tests/snapshot_tests/test_snapshots.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ def test_richlog_scroll(snap_compare):
541541
def test_tabs_invalidate(snap_compare):
542542
assert snap_compare(
543543
SNAPSHOT_APPS_DIR / "tabs_invalidate.py",
544-
press=["tab", "right"],
544+
press=["tab", "right", "wait:350"],
545545
)
546546

547547

@@ -680,7 +680,9 @@ def test_scroll_to_center(snap_compare):
680680

681681
def test_quickly_change_tabs(snap_compare):
682682
# https://github.com/Textualize/textual/issues/2229
683-
assert snap_compare(SNAPSHOT_APPS_DIR / "quickly_change_tabs.py", press=["p"])
683+
assert snap_compare(
684+
SNAPSHOT_APPS_DIR / "quickly_change_tabs.py", press=["p", "wait:350"]
685+
)
684686

685687

686688
def test_fr_unit_with_min(snap_compare):

0 commit comments

Comments
 (0)