From 5b0d78ef11925982128af3c5f649f2bebdadc615 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Mon, 16 Oct 2023 15:53:25 +0100 Subject: [PATCH] Make button animation duration configurable; Switch off button animation in snapshot test --- src/textual/widgets/_button.py | 15 +- .../__snapshots__/test_snapshots.ambr | 128 +++++++++--------- tests/snapshot_tests/test_snapshots.py | 13 +- 3 files changed, 82 insertions(+), 74 deletions(-) diff --git a/src/textual/widgets/_button.py b/src/textual/widgets/_button.py index 413c3b2258..185c8c6684 100644 --- a/src/textual/widgets/_button.py +++ b/src/textual/widgets/_button.py @@ -151,15 +151,15 @@ class Button(Static, can_focus=True): BINDINGS = [Binding("enter", "press", "Press Button", show=False)] - ACTIVE_EFFECT_DURATION = 0.3 - """When buttons are clicked they get the `-active` class for this duration (in seconds)""" - label: reactive[TextType] = reactive[TextType]("") """The text label that appears within the button.""" variant = reactive("default") """The variant name for the button.""" + active_effect_duration = 0.3 + """Amount of time in seconds the button 'press' animation lasts.""" + class Pressed(Message): """Event sent when a `Button` is pressed. @@ -252,10 +252,11 @@ def press(self) -> Self: def _start_active_affect(self) -> None: """Start a small animation to show the button was clicked.""" - self.add_class("-active") - self.set_timer( - self.ACTIVE_EFFECT_DURATION, partial(self.remove_class, "-active") - ) + if self.active_effect_duration > 0: + self.add_class("-active") + self.set_timer( + self.active_effect_duration, partial(self.remove_class, "-active") + ) def action_press(self) -> None: """Activate a press of the button.""" diff --git a/tests/snapshot_tests/__snapshots__/test_snapshots.ambr b/tests/snapshot_tests/__snapshots__/test_snapshots.ambr index 4b23ecded8..1cdea4285c 100644 --- a/tests/snapshot_tests/__snapshots__/test_snapshots.ambr +++ b/tests/snapshot_tests/__snapshots__/test_snapshots.ambr @@ -33595,141 +33595,139 @@ font-weight: 700; } - .terminal-1791594084-matrix { + .terminal-1538625093-matrix { font-family: Fira Code, monospace; font-size: 20px; line-height: 24.4px; font-variant-east-asian: full-width; } - .terminal-1791594084-title { + .terminal-1538625093-title { font-size: 18px; font-weight: bold; font-family: arial; } - .terminal-1791594084-r1 { fill: #05080f } - .terminal-1791594084-r2 { fill: #e1e1e1 } - .terminal-1791594084-r3 { fill: #c5c8c6 } - .terminal-1791594084-r4 { fill: #1e2226;font-weight: bold } - .terminal-1791594084-r5 { fill: #35393d } - .terminal-1791594084-r6 { fill: #454a50 } - .terminal-1791594084-r7 { fill: #fea62b } - .terminal-1791594084-r8 { fill: #e2e3e3;font-weight: bold } - .terminal-1791594084-r9 { fill: #000000 } - .terminal-1791594084-r10 { fill: #e2e3e3 } - .terminal-1791594084-r11 { fill: #14191f } + .terminal-1538625093-r1 { fill: #454a50 } + .terminal-1538625093-r2 { fill: #e1e1e1 } + .terminal-1538625093-r3 { fill: #c5c8c6 } + .terminal-1538625093-r4 { fill: #24292f;font-weight: bold } + .terminal-1538625093-r5 { fill: #000000 } + .terminal-1538625093-r6 { fill: #fea62b } + .terminal-1538625093-r7 { fill: #e2e3e3;font-weight: bold } + .terminal-1538625093-r8 { fill: #e2e3e3 } + .terminal-1538625093-r9 { fill: #14191f } - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - BorderApp + BorderApp - - - - ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔ - ascii - ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ - ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔+------------------- ascii --------------------+ - blank|| - ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁|| - ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔|I must not fear.| - dashed|Fear is the mind-killer.| - ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁|Fear is the little-death that brings | - ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔|total obliteration.| - double|I will face my fear.| - ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▅▅|I will permit it to pass over me and | - ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔|through me.| - heavy|And when it has gone past, I will turn| - ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁|the inner eye to see its path.| - ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔|Where the fear has gone there will be | - hidden|nothing. Only I will remain.| - ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁|| - ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔|| - hkey+----------------------------------------------+ - ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ - ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔ - inner - ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ + + + + ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔ + ascii + ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ + ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔+------------------- ascii --------------------+ + blank|| + ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁|| + ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔|I must not fear.| + dashed|Fear is the mind-killer.| + ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁|Fear is the little-death that brings | + ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔|total obliteration.| + double|I will face my fear.| + ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▅▅|I will permit it to pass over me and | + ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔|through me.| + heavy|And when it has gone past, I will turn| + ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁|the inner eye to see its path.| + ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔|Where the fear has gone there will be | + hidden|nothing. Only I will remain.| + ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁|| + ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔|| + hkey+----------------------------------------------+ + ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ + ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔ + inner + ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ diff --git a/tests/snapshot_tests/test_snapshots.py b/tests/snapshot_tests/test_snapshots.py index 1eef1bf1b1..65091a3445 100644 --- a/tests/snapshot_tests/test_snapshots.py +++ b/tests/snapshot_tests/test_snapshots.py @@ -5,7 +5,7 @@ from tests.snapshot_tests.language_snippets import SNIPPETS from textual.widgets.text_area import Selection, BUILTIN_LANGUAGES -from textual.widgets import TextArea, Input +from textual.widgets import TextArea, Input, Button from textual.widgets.text_area import TextAreaTheme # These paths should be relative to THIS directory. @@ -686,7 +686,16 @@ async def run_before(pilot) -> None: def test_textual_dev_border_preview(snap_compare): - assert snap_compare(SNAPSHOT_APPS_DIR / "dev_previews_border.py", press=["enter"]) + async def run_before(pilot): + buttons = pilot.app.query(Button) + for button in buttons: + button.active_effect_duration = 0 + + assert snap_compare( + SNAPSHOT_APPS_DIR / "dev_previews_border.py", + press=["enter"], + run_before=run_before, + ) def test_textual_dev_colors_preview(snap_compare):