
    AHj                        d Z ddlmZ ddlZddlmZ ddlmZ dgZ	dZ
dZdZd	Zdd
Z	 ddlZddlmZmZ ej&                  j(                  Zd ZddZy# e$ r ddZY yw xY w)z
A version of `time.sleep` that is more accurate than the standard library (even on Python 3.11).

This should only be imported on Windows.
    )annotationsN)sleep)	Coroutiner   l       i  c                J   K   t        j                  |        d{    y7 w)z&Coroutine wrapper around `time.sleep`.N)asyncior   secss    J/root/tools/cai/cai_env/lib/python3.12/site-packages/textual/_win_sleep.pytime_sleep_coror      s     
--
s   #!#)HANDLELARGE_INTEGERc                    K   yw)zBCreates a coroutine that does nothing for when no sleep is needed.N r       r   no_sleep_coror   (   s	     s   c                $   t        d| dz
        dk  r
t               S t        j                  ddt        t
              st              S t        j                  t        j                  t        t        dz                    dddd      s t        j                         t              S t        j                  dddt
              s t        j                         t              S fdfdfdfd	} |       S )
a  A replacement sleep for Windows.

        Note that unlike `time.sleep` this *may* sleep for slightly less than the
        specified time. This is generally not an issue for Textual's use case.

        In order to create a timer that _can_ be cancelled on Windows, we need to
        create a timer and a separate event, and then we wait for either of the two
        things. When Textual wants to quit, we set the cancel event.

        Args:
            secs: Seconds to sleep for.
        r   gMbP?gMb@?Niigc                 0    t         j                          y)zCSets the cancel event so we know we can stop waiting for the timer.N)kernel32SetEvent)cancel_events   r   cancel_innerzsleep.<locals>.cancel_innerY   s    l+r   c                 j   K   t        j                         j                  d        d{    y7 w)z.Cancels the timer by setting the cancel event.N)r   get_running_looprun_in_executor)r   s   r   cancelzsleep.<locals>.cancel]   s&     **,<<T<PPPs   (313c            	         t         j                  dt        j                  t	        dz               dt
              t        k(  rt               yy)zCFunction responsible for waiting for the timer or the cancel event.r   FN)r   WaitForMultipleObjectsctypespointerr   INFINITEWAIT_FAILED
time_sleep)r   	sleep_fortimers   r   
wait_innerzsleep.<locals>.wait_innera   sL     //NNFQJe#DE	  9%r   c                 p  K   	 t        j                         j                  d       d{    	 t        j                         t        j                         y7 0# t         j                  $ r           d{  7    w xY w# t        j                         t        j                         w xY ww)zGWraps the actual sleeping so we can detect if the thread was cancelled.N)r   r   r   CancelledErrorr   CloseHandle)r   r   r%   r&   s   r   waitzsleep.<locals>.waitn   s     3..0@@zRRR
 $$U+$$\2 S)) h $$U+$$\2sC   B6'A AA +B6A B<A?=BB ,B33B6)maxr   r   CreateWaitableTimerExW%CREATE_WAITABLE_TIMER_HIGH_RESOLUTIONTIMER_ALL_ACCESSr   SetWaitableTimerr   byrefr   intr)   CreateEventExW)r
   r*   r   r   r   r$   r%   r&   s     @@@@@@r   r   r   ,   s     4%<(	v ?"//1	
 "9--((LLs9{+B'CDE
   '"9--..tT1>NO  '"9--	,	Q	&		3 vr   c                    t        |       S )zJWrapper around `time.sleep` to match the signature of the main case below.)r   r	   s    r   r   r   "   s    t$$r   )r
   float)r
   r4   returnzCoroutine[None, None, None])__doc__
__future__r   r   timer   r#   typingr   __all__r!   r"   r-   r.   r   r   ctypes.wintypesr   r   windllr   r   	Exceptionr   r   r   <module>r>      sx    #  $ ) (2 % 
^5}}%%HM  %%%s   "A 	AA