Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix animation of offset with tuple and string #3056

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Raise `ValueError` with improved error message when number of cells inserted using `DataTable.add_row` doesn't match the number of columns in the table https://github.com/Textualize/textual/pull/4742
- Add `Tree.move_cursor` to programmatically move the cursor without selecting the node https://github.com/Textualize/textual/pull/4753
- Added `Footer` component style handling of padding for the key/description https://github.com/Textualize/textual/pull/4651
- Added ability to animate `widget.styles.offset` with `tuple` or `str` https://github.com/Textualize/textual/issues/3028

### Fixed

Expand Down
16 changes: 16 additions & 0 deletions src/textual/css/scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,22 @@ def resolve(self, size: Size, viewport: Size) -> Offset:
round(y.resolve(size, viewport)),
)

@classmethod
def parse(cls, token: str) -> ScalarOffset:
"""Create a scalar offset from a string.

Args:
token: String to parse (e.g. '1 2' or '1.0 2.0')

Returns:
New offset
"""
x, y = token.split()
return cls(
Scalar.parse(x, Unit.WIDTH),
Scalar.parse(y, Unit.HEIGHT),
)


NULL_SCALAR = ScalarOffset(Scalar.from_number(0), Scalar.from_number(0))

Expand Down
7 changes: 7 additions & 0 deletions src/textual/css/styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,13 @@ def __textual_animation__(
# If destination is a number, we can convert that to a scalar
if isinstance(value, (int, float)):
value = Scalar(value, Unit.CELLS, Unit.CELLS)
# If destination is a pair of numbers, we can convert it to a
# scalar offset
elif isinstance(value, tuple) and (
list(map(type, value)) == [int, int]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This basically checks that value is of type tuple[int, int] or tuple[float, float].
This is the best way I found to implement it.
If anyone has any suggestions, please let me know.

or list(map(type, value)) == [float, float]
):
value = ScalarOffset.from_offset(value)

# We can only animate to Scalar
if not isinstance(value, (Scalar, ScalarOffset)):
Expand Down
42 changes: 42 additions & 0 deletions tests/test_animation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from time import perf_counter

import pytest

from textual.app import App, ComposeResult
from textual.css.scalar import NULL_SCALAR, Scalar, ScalarOffset, Unit
from textual.geometry import Offset
from textual.reactive import var
from textual.widgets import Static

Expand Down Expand Up @@ -39,6 +43,44 @@ async def test_animate_height() -> None:
assert static.styles.height.value == 100


destinations = [
ScalarOffset.from_offset((10, 5)),
Offset(10, 5),
(10, 5),
(10.0, 5.0),
"10 5",
"10.0 5.0",
]


@pytest.mark.parametrize("destination", destinations)
async def test_animate_offset(destination) -> None:
"""Test animating styles.offset works."""
# Styles.offset is a ScalarOffset, which makes it more complicated to animate

app = AnimApp()

async with app.run_test() as pilot:
static = app.query_one(Static)
assert static.offset == Offset(0, 0)
assert static.styles.offset == NULL_SCALAR

static.styles.animate("offset", destination, duration=0.5)
start = perf_counter()

# Wait for the animation to finished
await pilot.wait_for_animation()
elapsed = perf_counter() - start
# Check that the full time has elapsed
assert elapsed >= 0.5

# Check the offset reached the destination
assert static.styles.offset == ScalarOffset(
Scalar(10, Unit.CELLS, Unit.WIDTH),
Scalar(5, Unit.CELLS, Unit.HEIGHT),
)


async def test_scheduling_animation() -> None:
"""Test that scheduling an animation works."""

Expand Down
Loading