mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
refinement
This commit is contained in:
@@ -51,8 +51,9 @@ class SimpleAnimation(Animation):
|
|||||||
def __call__(self, time: float) -> bool:
|
def __call__(self, time: float) -> bool:
|
||||||
|
|
||||||
if self.duration == 0:
|
if self.duration == 0:
|
||||||
value = self.final_value
|
setattr(self.obj, self.attribute, self.final_value)
|
||||||
else:
|
return True
|
||||||
|
|
||||||
factor = min(1.0, (time - self.start_time) / self.duration)
|
factor = min(1.0, (time - self.start_time) / self.duration)
|
||||||
eased_factor = self.easing(factor)
|
eased_factor = self.easing(factor)
|
||||||
|
|
||||||
@@ -64,9 +65,7 @@ class SimpleAnimation(Animation):
|
|||||||
), "end_value must be animatable"
|
), "end_value must be animatable"
|
||||||
value = self.start_value.blend(self.end_value, eased_factor)
|
value = self.start_value.blend(self.end_value, eased_factor)
|
||||||
else:
|
else:
|
||||||
assert isinstance(
|
assert isinstance(self.start_value, float), "`start_value` must be float"
|
||||||
self.start_value, float
|
|
||||||
), "`start_value` must be float"
|
|
||||||
assert isinstance(self.end_value, float), "`end_value` must be float"
|
assert isinstance(self.end_value, float), "`end_value` must be float"
|
||||||
if self.end_value > self.start_value:
|
if self.end_value > self.start_value:
|
||||||
eased_factor = self.easing(factor)
|
eased_factor = self.easing(factor)
|
||||||
@@ -77,11 +76,10 @@ class SimpleAnimation(Animation):
|
|||||||
else:
|
else:
|
||||||
eased_factor = 1 - self.easing(factor)
|
eased_factor = 1 - self.easing(factor)
|
||||||
value = (
|
value = (
|
||||||
self.end_value
|
self.end_value + (self.start_value - self.end_value) * eased_factor
|
||||||
+ (self.start_value - self.end_value) * eased_factor
|
|
||||||
)
|
)
|
||||||
setattr(self.obj, self.attribute, value)
|
setattr(self.obj, self.attribute, value)
|
||||||
return value == self.final_value
|
return factor >= 1
|
||||||
|
|
||||||
|
|
||||||
class BoundAnimator:
|
class BoundAnimator:
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ class Animatable:
|
|||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class AnimateTest:
|
class AnimateTest:
|
||||||
"""An object to animate."""
|
"""An object with animatable properties."""
|
||||||
|
|
||||||
foo: float | None = 0 # Plain float that may be set to None on final_value
|
foo: float | None = 0 # Plain float that may be set to None on final_value
|
||||||
bar: Animatable = Animatable(0) # A mock object supporting the animatable protocol
|
bar: Animatable = Animatable(0) # A mock object supporting the animatable protocol
|
||||||
@@ -49,6 +49,8 @@ def test_simple_animation():
|
|||||||
easing=lambda x: x,
|
easing=lambda x: x,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
assert animatable.foo == 0.0
|
||||||
|
|
||||||
assert animation(time) is False
|
assert animation(time) is False
|
||||||
assert animatable.foo == 20.0
|
assert animatable.foo == 20.0
|
||||||
|
|
||||||
@@ -58,7 +60,7 @@ def test_simple_animation():
|
|||||||
assert animation(time + 2.0) is False
|
assert animation(time + 2.0) is False
|
||||||
assert animatable.foo == 40.0
|
assert animatable.foo == 40.0
|
||||||
|
|
||||||
assert animation(time + 2.9) is False
|
assert animation(time + 2.9) is False # Not quite final value
|
||||||
assert pytest.approx(animatable.foo, 49.0)
|
assert pytest.approx(animatable.foo, 49.0)
|
||||||
|
|
||||||
assert animation(time + 3.0) is True # True to indicate animation is complete
|
assert animation(time + 3.0) is True # True to indicate animation is complete
|
||||||
@@ -89,7 +91,7 @@ def test_simple_animation_duration_zero():
|
|||||||
easing=lambda x: x,
|
easing=lambda x: x,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert animation(time) is True
|
assert animation(time) is True # Duration is 0, so this is last value
|
||||||
assert animatable.foo == 50.0
|
assert animatable.foo == 50.0
|
||||||
|
|
||||||
assert animation(time + 1.0) is True
|
assert animation(time + 1.0) is True
|
||||||
|
|||||||
Reference in New Issue
Block a user