| 
 | 1 | +from __future__ import annotations  | 
 | 2 | + | 
 | 3 | +import logging  | 
 | 4 | + | 
 | 5 | +import asyncio  | 
 | 6 | +from time import time  | 
 | 7 | +from tracemalloc import start  | 
 | 8 | +from typing import Callable  | 
 | 9 | + | 
 | 10 | +from dataclasses import dataclass  | 
 | 11 | + | 
 | 12 | +from ._timer import Timer  | 
 | 13 | +from ._types import MessageTarget  | 
 | 14 | + | 
 | 15 | + | 
 | 16 | +EasingFunction = Callable[[float], float]  | 
 | 17 | + | 
 | 18 | +# https://easings.net/  | 
 | 19 | +EASING = {  | 
 | 20 | +    "none": lambda x: 1.0,  | 
 | 21 | +    "round": lambda x: 0.0 if x < 0.5 else 1.0,  | 
 | 22 | +    "linear": lambda x: x,  | 
 | 23 | +    "in_cubic": lambda x: x * x * x,  | 
 | 24 | +    "in_out_cubic": lambda x: 4 * x * x * x if x < 0.5 else 1 - pow(-2 * x + 2, 3) / 2,  | 
 | 25 | +    "out_cubic": lambda x: 1 - pow(1 - x, 3),  | 
 | 26 | +}  | 
 | 27 | + | 
 | 28 | + | 
 | 29 | +log = logging.getLogger("rich")  | 
 | 30 | + | 
 | 31 | + | 
 | 32 | +@dataclass  | 
 | 33 | +class Animation:  | 
 | 34 | +    obj: object  | 
 | 35 | +    attribute: str  | 
 | 36 | +    start_time: float  | 
 | 37 | +    duration: float  | 
 | 38 | +    start_value: float  | 
 | 39 | +    end_value: float  | 
 | 40 | +    easing_function: EasingFunction  | 
 | 41 | + | 
 | 42 | +    def __call__(self, time: float) -> bool:  | 
 | 43 | + | 
 | 44 | +        if self.duration == 0:  | 
 | 45 | +            value = self.end_value  | 
 | 46 | +        else:  | 
 | 47 | +            progress = min(1.0, (time - self.start_time) / self.duration)  | 
 | 48 | +            if self.end_value > self.start_value:  | 
 | 49 | +                eased_progress = self.easing_function(progress)  | 
 | 50 | +                value = (  | 
 | 51 | +                    self.start_value  | 
 | 52 | +                    + (self.end_value - self.start_value) * eased_progress  | 
 | 53 | +                )  | 
 | 54 | +            else:  | 
 | 55 | +                eased_progress = 1 - self.easing_function(progress)  | 
 | 56 | +                value = (  | 
 | 57 | +                    self.end_value  | 
 | 58 | +                    + (self.start_value - self.end_value) * eased_progress  | 
 | 59 | +                )  | 
 | 60 | + | 
 | 61 | +        setattr(self.obj, self.attribute, value)  | 
 | 62 | +        return value == self.end_value  | 
 | 63 | + | 
 | 64 | + | 
 | 65 | +class BoundAnimator:  | 
 | 66 | +    def __init__(self, animator: Animator, obj: object) -> None:  | 
 | 67 | +        self._animator = animator  | 
 | 68 | +        self._obj = obj  | 
 | 69 | + | 
 | 70 | +    def __call__(  | 
 | 71 | +        self,  | 
 | 72 | +        attribute: str,  | 
 | 73 | +        value: float,  | 
 | 74 | +        *,  | 
 | 75 | +        duration: float | None = None,  | 
 | 76 | +        speed: float | None = None,  | 
 | 77 | +        easing: EasingFunction | str = "in_out_cubic",  | 
 | 78 | +    ) -> None:  | 
 | 79 | +        easing_function = EASING[easing] if isinstance(easing, str) else easing  | 
 | 80 | +        self._animator.animate(  | 
 | 81 | +            self._obj,  | 
 | 82 | +            attribute=attribute,  | 
 | 83 | +            value=value,  | 
 | 84 | +            duration=duration,  | 
 | 85 | +            speed=speed,  | 
 | 86 | +            easing=easing_function,  | 
 | 87 | +        )  | 
 | 88 | + | 
 | 89 | + | 
 | 90 | +class Animator:  | 
 | 91 | +    def __init__(self, target: MessageTarget, frames_per_second: int = 30) -> None:  | 
 | 92 | +        self._animations: dict[tuple[object, str], Animation] = {}  | 
 | 93 | +        self._timer = Timer(target, 1 / frames_per_second, target, callback=self)  | 
 | 94 | + | 
 | 95 | +    async def start(self) -> None:  | 
 | 96 | +        asyncio.get_event_loop().create_task(self._timer.run())  | 
 | 97 | + | 
 | 98 | +    async def stop(self) -> None:  | 
 | 99 | +        self._timer.stop()  | 
 | 100 | + | 
 | 101 | +    def bind(self, obj: object) -> BoundAnimator:  | 
 | 102 | +        return BoundAnimator(self, obj)  | 
 | 103 | + | 
 | 104 | +    def animate(  | 
 | 105 | +        self,  | 
 | 106 | +        obj: object,  | 
 | 107 | +        attribute: str,  | 
 | 108 | +        value: float,  | 
 | 109 | +        *,  | 
 | 110 | +        duration: float | None = None,  | 
 | 111 | +        speed: float | None = None,  | 
 | 112 | +        easing: EasingFunction = EASING["in_out_cubic"],  | 
 | 113 | +    ) -> None:  | 
 | 114 | + | 
 | 115 | +        start_time = time()  | 
 | 116 | + | 
 | 117 | +        animation_key = (obj, attribute)  | 
 | 118 | +        if animation_key in self._animations:  | 
 | 119 | +            self._animations[animation_key](start_time)  | 
 | 120 | + | 
 | 121 | +        start_value = getattr(obj, attribute)  | 
 | 122 | +        if duration is not None:  | 
 | 123 | +            animation_duration = duration  | 
 | 124 | +        else:  | 
 | 125 | +            animation_duration = abs(value - start_value) / (speed or 50)  | 
 | 126 | + | 
 | 127 | +        animation = Animation(  | 
 | 128 | +            obj,  | 
 | 129 | +            attribute=attribute,  | 
 | 130 | +            start_time=start_time,  | 
 | 131 | +            duration=animation_duration,  | 
 | 132 | +            start_value=start_value,  | 
 | 133 | +            end_value=value,  | 
 | 134 | +            easing_function=easing,  | 
 | 135 | +        )  | 
 | 136 | +        self._animations[animation_key] = animation  | 
 | 137 | +        self._timer.resume()  | 
 | 138 | + | 
 | 139 | +    async def __call__(self) -> None:  | 
 | 140 | +        if not self._animations:  | 
 | 141 | +            self._timer.pause()  | 
 | 142 | +        else:  | 
 | 143 | +            animation_time = time()  | 
 | 144 | +            animation_keys = list(self._animations.keys())  | 
 | 145 | +            for animation_key in animation_keys:  | 
 | 146 | +                animation = self._animations[animation_key]  | 
 | 147 | +                if animation(animation_time):  | 
 | 148 | +                    del self._animations[animation_key]  | 
0 commit comments