
    AHj2P                       d dl mZ d dlZd dlmZmZ d dlmZ d dlm	Z	 d dl
mZmZmZmZ d dlmZmZ d dlmZ d d	lmZ d d
lmZ d dlmZmZ d dlmZmZ d dlmZ erd dl m!Z! e"e#e$f   Z%	 ee&ge&f   Z'	  G d de(      Z) ed      Z*e G d de             Z+ G d de      Z,e G d de,             Z- G d d      Z. G d d      Z/y)    )annotationsN)ABCabstractmethod)	dataclass)partial)TYPE_CHECKINGAnyCallableTypeVar)Protocolruntime_checkable)_time)invoke)cached_property)DEFAULT_EASINGEASING)AnimationLevelCallbackType)Timer)Appc                      e Zd ZdZy)AnimationErrorz+An issue prevented animation from starting.N)__name__
__module____qualname____doc__     I/root/tools/cai/cai_env/lib/python3.12/site-packages/textual/_animator.pyr   r      s    5r   r   
ReturnTypec                  (    e Zd ZdZ	 	 	 	 	 	 	 	 ddZy)
AnimatablezProtocol for objects that can have their intrinsic values animated.

    For example, the transition between two colors can be animated
    because the class [`Color`][textual.color.Color.blend] satisfies this protocol.
    c                     y Nr   )selfdestinationfactors      r   blendzAnimatable.blend.   s     	r   N)r%   r    r&   r    r'   floatreturnr    )r   r   r   r   r(   r   r   r   r"   r"   &   s,    '1;@	r   r"   c                  b    e Zd ZU dZded<   	 e	 d	 	 	 	 	 d	d       Zd
dZeddd       ZddZ	y)	AnimationNCallbackType | Noneon_completec                    t        d      )zCall the animation, return a boolean indicating whether animation is in-progress or complete.

        Args:
            time: The current timestamp

        Returns:
            True if the animation has finished, otherwise False.
         NotImplementedError)r%   timeapp_animation_levels      r   __call__zAnimation.__call__8   s     ""%%r   c                d   K   | j                   t        | j                          d{    yy7 w)zMCalls the [`on_complete`][Animation.on_complete] callback if one is provided.N)r.   r   r%   s    r   invoke_callbackzAnimation.invoke_callbackH   s.     '))*** (*s   %0.0c                   K   t         w)z}Stop the animation.

        Args:
            complete: Flag to say if the animation should be taken to completion.
        r1   r%   completes     r   stopzAnimation.stopM   s      "!s   	c                     yNFr   r%   others     r   __eq__zAnimation.__eq__V   s    r   fullr3   r)   r4   r   r*   boolr*   NoneTr;   rE   r*   rG   r@   objectr*   rE   )
r   r   r   r.   __annotations__r   r5   r8   r<   rA   r   r   r   r,   r,   4   sa    '+K$+3 /5&& ,& 
	& &+
 " "r   r,   c                      e Zd ZU ded<   ded<   ded<   ded<   ded	<   ded
<   ded<   ded<   dZded<   dZded<   	 	 d	 	 	 	 	 ddZdddZddZy)SimpleAnimationrK   objstr	attributer)   
start_timedurationzfloat | Animatablestart_value	end_valuefinal_valueEasingFunctioneasingNr-   r.   rC   r   levelc                @   | j                   dk(  s|dk(  s|dk(  r;| j                  dk(  r,t        | j                  | j                  | j
                         yt        d|| j                  z
  | j                   z        }| j                  |      }|dk(  r| j
                  }n[t        | j                  t              rHt        | j                  t              sJ d       | j                  j                  | j                  |      }nt        | j                  t        t        f      sJ d| j                         t        | j                  t        t        f      sJ d	| j                         | j                  | j                  kD  r;| j                  |      }| j                  | j                  | j                  z
  |z  z   }n=d
| j                  |      z
  }| j                  | j                  | j                  z
  |z  z   }t        | j                  | j                  |       |d
k\  S )Nr   nonebasicrC   Tg      ?zend_value must be animatablez!`start_value` must be float, not z`end_value` must be float, not    )rS   rY   setattrrO   rQ   rV   minrR   rX   
isinstancerT   r"   rU   r(   intr)   )r%   r3   r4   r'   eased_factorvalues         r   r5   zSimpleAnimation.__call__h   s    MMQ"f,"g-

f$DHHdnnd.>.>?S4$//1T]]BC{{6*S=$$E((*5
 .-.  $$**4>><HE  3, H243C3C2FGH  e D00BCD  ~~ 0 00#{{62$$~~(8(88LHI 
  !4;;v#66NNd&6&6&G<%WW  	$..%0{r   c                   K   |r+t        | j                  | j                  | j                         | j	                          d{    y7 w)a  Stop the animation.

        Args:
            complete: Flag to say if the animation should be taken to completion.

        Note:
            [`on_complete`][Animation.on_complete] will be called regardless
            of the value provided for `complete`.
        N)r^   rO   rQ   rU   r8   r:   s     r   r<   zSimpleAnimation.stop   s5      DHHdnndnn=""$$$s   AAA	Ac                    t        |t              r4| j                  |j                  k(  xr | j                  |j                  k(  S yr>   )r`   rN   rV   rS   r?   s     r   rA   zSimpleAnimation.__eq__   s?    e_-  E$5$55 4MMU^^3 r   rB   rD   rH   rI   rJ   )	r   r   r   rL   r.   rY   r5   r<   rA   r   r   r   rN   rN   Z   sv    	KNO##!!'+K$+"E>"M BH**0>*	*X%r   rN   c                  T    e Zd ZddZddddeddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d	dZy)
BoundAnimatorc                     || _         || _        y r$   )	_animator_obj)r%   animatorrO   s      r   __init__zBoundAnimator.__init__   s    !	r   .N        rC   )rV   rS   speeddelayrX   r.   rY   c                  t        | j                  |      }
t        |t              rt	        |
d      r|
j                  |      }t        |t              r	t        |   n|}| j                  j                  | j                  |||||||||	
      S )an  Animate an attribute.

        Args:
            attribute: Name of the attribute to animate.
            value: The value to animate to.
            final_value: The final value of the animation. Defaults to `value` if not set.
            duration: The duration (in seconds) of the animation.
            speed: The speed of the animation.
            delay: A delay (in seconds) before the animation starts.
            easing: An easing method.
            on_complete: A callable to invoke when the animation is finished.
            level: Minimum level required for the animation to take place (inclusive).
        parse)	rQ   rc   rV   rS   rn   ro   rX   r.   rY   )	getattrrj   r`   rP   hasattrrq   r   ri   animate)r%   rQ   rc   rV   rS   rn   ro   rX   r.   rY   rT   easing_functions               r   r5   zBoundAnimator.__call__   s    4 dii3eS!gk7&C  %%e,E,6vs,C&.~~%%II#"# & 
 	
r   )rk   AnimatorrO   rK   r*   rG   )rQ   rP   rc   zstr | float | AnimatablerV   rK   rS   float | Nonern   rw   ro   r)   rX   EasingFunction | strr.   r-   rY   r   r*   rG   )r   r   r   rl   r   r5   r   r   r   rg   rg      s     "!%"'5+/ &,
,
 (,

 ,
 ,
 ,
 ,
 %,
 ),
 ,
 
,
r   rg   c                  F   e Zd ZdZdddZedd       Zedd       ZddZddZ	ddZ
ddZd	d
d
edd
dd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d dZd!dZd	d
d
ed
dd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d"dZ	 	 	 	 	 	 d#dZd#dZ	 d$	 	 	 	 	 	 	 d%dZd&dZddZd'dZddZddZy
)(rv   zGAn object to manage updates to a given attribute over a period of time.c                d    i | _         	 i | _        	 || _        	 t        |d|z  d| d      | _        y)zInitialise the animator object.

        Args:
            app: The application that owns the animator.
            frames_per_second: The number of frames/second to run the animation at.
        r]   rv   T)namecallbackpauseN)_animations
_scheduledappr   _timer)r%   r   frames_per_seconds      r   rl   zAnimator.__init__   sD     ;=[57a4!!
r   c                *    t        j                         S )z!The timer that runs the animator.asyncioEventr7   s    r   _idle_eventzAnimator._idle_event        }}r   c                *    t        j                         S )z1Flag if no animations are currently taking place.r   r7   s    r   _complete_eventzAnimator._complete_event   r   r   c                   K   | j                   j                          | j                  j                          | j                  j	                          yw)zStart the animator task.N)r   setr   r   _startr7   s    r   startzAnimator.start  s:       "s   AAc                N  K   	 | j                   j                          | j                  j                          | j                  j                          y# t        j                  $ r Y Jw xY w# | j                  j                          | j                  j                          w xY ww)zStop the animator task.N)r   r<   r   CancelledErrorr   r   r   r7   s    r   r<   zAnimator.stop  s     	'KK   "  $$&	 %% 		   "  $$&s3   B%A 5B%A)&A, (A))A, ,6B""B%c                    t        | |      S )zBind the animator to a given object.

        Args:
            obj: The object to bind to.

        Returns:
            The bound animator.
        )rg   )r%   rO   s     r   bindzAnimator.bind  s     T3''r   c                X    t        |      |f}|| j                  v xs || j                  v S )a1  Does the object/attribute pair have an ongoing or scheduled animation?

        Args:
            obj: An object to check for.
            attribute: The attribute on the object to test for.

        Returns:
            `True` if that attribute is being animated for that object, `False` if not.
        )idr~   r   )r%   rO   rQ   keys       r   is_being_animatedzAnimator.is_being_animated  s1     #w	"d&&&@#*@@r   .Nrm   rC   )rV   rS   rn   rX   ro   r.   rY   c                  | j                  |       t        | j                  ||||||||	|

      }|rO| j                  j	                          | j
                  j                  ||      | j                  t        |      |f<   y |        y)a  Animate an attribute to a new value.

        Args:
            obj: The object containing the attribute.
            attribute: The name of the attribute.
            value: The destination value of the attribute.
            final_value: The final value, or ellipsis if it is the same as ``value``.
            duration: The duration of the animation, or ``None`` to use speed.
            speed: The speed of the animation.
            easing: An easing function.
            delay: Number of seconds to delay the start of the animation by.
            on_complete: Callback to run after the animation completes.
            level: Minimum level required for the animation to take place (inclusive).
        rV   rS   rn   rX   r.   rY   N)	_record_animationr   _animater   clearr   	set_timerr   r   )r%   rO   rQ   rc   rV   rS   rn   rX   ro   r.   rY   animate_callbacks               r   rt   zAnimator.animate)  s    8 	y)"MM##
   &&(48HH4F4F'5DOORWi01 r   c                     y)ztCalled when an attribute is to be animated.

        Args:
            attribute: Attribute being animated.
        Nr   )r%   rQ   s     r   r   zAnimator._record_animationZ  s    r   r   c               D   t        ||      st        d|d|d      ||||J d       t        |      |f}
	 | j                  |
= |du r|}| j                         }t        |t              r	t        |   n|}d}t        |d      r% t        |d      |t        ||      |||||||		      }|t        |t        t        f      st        |t              st        d	|d
      t        ||      }||k(  r| j                  j                  |
d       y||}n:t        |d      r|j!                  |      |xs dz  }nt#        ||z
        |xs dz  }t%        ||||||||| t'        | j(                  j*                  |      nd|	
      }|J d       | j                  j-                  |
      }|||k(  ry|| j                  |
<   | j.                  j1                          | j2                  j5                          | j6                  j5                          y# t        $ r Y w xY w)az  Animate an attribute to a new value.

        Args:
            obj: The object containing the attribute.
            attribute: The name of the attribute.
            value: The destination value of the attribute.
            final_value: The final value, or ellipsis if it is the same as ``value``.
            duration: The duration of the animation, or ``None`` to use speed.
            speed: The speed of the animation.
            easing: An easing function.
            on_complete: Callback to run after the animation completes.
            level: Minimum level required for the animation to take place (inclusive).
        zCan't animate attribute z on z; attribute does not existNz.An Animation should have a duration OR a speed.__textual_animation__)rS   rn   rX   r.   rY   zDon't know how to animate zA; Can only animate <int>, <float>, or objects with a blend methodget_distance_to2   )	rQ   rR   rS   rT   rU   rV   rX   r.   rY   z!animation expected to be non-None)rs   AttributeErrorr   r   KeyError	_get_timer`   rP   r   rr   ra   r)   r"   r   r~   popr   absrN   r   r   
call_latergetr   resumer   r   r   )r%   rO   rQ   rc   rV   rS   rn   rX   r.   rY   animation_keyrR   ru   	animationrT   animation_durationcurrent_animations                    r   r   zAnimator._animatea  s   4 sI& *9-tC7B\]  $!2	<;	< 

 C),	. #K^^%
,6vs,C&.&*	3/0=%<=Y'!&'
I ec5\2:z< %0	 :V V 
 "#y1Ke#  $$]D9#%-"5"34).)>)>{)K*& *-U[-@)AU[b)Q&'#%+''& #. DHH//=I" $I&II$ ,,00?(->)-K*3' ""$U  		s   H 	HHc                  K   	 | j                   j                  |      }|j                          |rE|j                  8t        |j                         d{    | j                  ||       d{    yyy# t        $ r Y yw xY w7 07 w)zStop a scheduled animation.

        Args:
            key: The key for the animation to stop.
            complete: Should the animation be moved to its completed state?
        N)r   r   r   r<   	_callbackr   _stop_running_animation)r%   r   r;   schedules       r   _stop_scheduled_animationz"Animator._stop_scheduled_animation  s     	**3/H 	
 **6 ++,,, ..sH=== 78  		 - >sE   B
A7 6B
BB
/B0B
7	B B
BB
B
c                   K   	 | j                   j                  |      }|j                  |       d{    y# t        $ r Y yw xY w7 w)zStop a running animation.

        Args:
            key: The key for the animation to stop.
            complete: Should the animation be moved to its completed state?
        N)r~   r   r   r<   )r%   r   r;   r   s       r   r   z Animator._stop_running_animation  sL     	((,,S1I nnX&&&  		&s/   A
9 A
AA
	AA
AA
c                   K   t        |      |f}|| j                  v r| j                  ||       d{    y|| j                  v r| j	                  ||       d{    yy7 /7 w)aC  Stop an animation on an attribute.

        Args:
            obj: The object containing the attribute.
            attribute: The name of the attribute.
            complete: Should the animation be set to its final value?

        Note:
            If there is no animation scheduled or running, this is a no-op.
        N)r   r   r   r~   r   )r%   rO   rQ   r;   r   s        r   stop_animationzAnimator.stop_animation  sk      #w	"$//!00h???D$$$..sH=== % @=s!   1A'A#(A'A%A'%A'c                P   ddl m} t        |      |f}	 | j                  j	                  |      }t        |t              rt        |||j                         n#t        ||      rt        |||j                         |j                  |j                          yy# t
        $ r Y yw xY w)a  Force stop an animation on an attribute. This will immediately stop the animation,
        without running any associated callbacks, setting the attribute to its final value.

        Args:
            obj: The object containing the attribute.
            attribute: The name of the attribute.

        Note:
            If there is no animation scheduled or running, this is a no-op.
        r   )ScalarAnimationN)textual.css.scalar_animationr   r   r~   r   r   r`   rN   r^   rU   rV   r.   )r%   rO   rQ   r   r   r   s         r   force_stop_animationzAnimator.force_stop_animation  s     	AC),	((,,];I i1CI$7$78	?3CI$9$9:  ,!!# -  		s   B 	B%$B%c                   | j                   s\| j                  j                          | j                  j	                          | j
                  s| j                  j	                          y y | j                  j                  }| j                         }t        | j                   j                               }|D ]G  }| j                   |   } |||      }|s| j                   |= |j                  8|j                          I y r$   )r~   r   r}   r   r   r   r   r   animation_levelr   listkeysr.   )r%   r4   animation_timeanimation_keysr   r   animation_completes          r   r5   zAnimator.__call__+  s    KK  "??$$((* # #'((":":!^^-N!$"2"2"7"7"9:N!/ 0 ,,];	%.~?R%S"%((7 ,,8!--/0r   c                *    t        j                         S )ztGet the current wall clock time, via the internal Timer.

        Returns:
            The wall clock time.
        )r   get_timer7   s    r   r   zAnimator._get_time=  s     ~~r   c                T   K   | j                   j                          d{    y7 w)z$Wait for any animations to complete.N)r   waitr7   s    r   wait_for_idlezAnimator.wait_for_idleG  s     ##%%%   (&(c                T   K   | j                   j                          d{    y7 w)z:Wait for any current and scheduled animations to complete.N)r   r   r7   s    r   wait_until_completezAnimator.wait_until_completeK  s     ""'')))r   )<   )r   r   r   ra   r*   rG   )r*   zasyncio.EventrF   )rO   rK   r*   rg   )rO   rK   rQ   rP   r*   rE   )rO   rK   rQ   rP   rc   r	   rV   rK   rS   rw   rn   rw   rX   rx   ro   r)   r.   r-   rY   r   r*   rG   )rQ   rP   r*   rG   )rO   rK   rQ   rP   rc   r	   rV   rK   rS   rw   rn   rw   rX   rx   r.   r-   rY   r   r*   rG   )r   AnimationKeyr;   rE   r*   rG   rH   )rO   rK   rQ   rP   r;   rE   r*   rG   )rO   rK   rQ   rP   r*   rG   )r*   r)   )r   r   r   r   rl   r   r   r   r   r<   r   r   r   rt   r   r   r   r   r   r   r5   r   r   r   r   r   r   rv   rv      s   Q
*    '	(A& "!%"'5+/ &// / 	/ / / / %/ / )/ / 
/b "!%"'5+/ &p%p% p% 	p% p% p% p% %p% )p% p% 
p%d>>+/>	>:' =A>>&)>59>	>&$60$ &*r   rv   )0
__future__r   r   abcr   r   dataclassesr   	functoolsr   typingr   r	   r
   r   typing_extensionsr   r   textualr   textual._callbackr   textual._compatr   textual._easingr   r   textual._typesr   r   textual.timerr   textual.appr   tuplera   rP   r   r)   rW   	Exceptionr   r    r"   r,   rN   rg   rv   r   r   r   <module>r      s    "  # !  8 8 9  $ + 2 7 c?LS5'5.)6Y 6 \"
 
 
 
# #L Mi M M`1
 1
hn* n*r   