
    XaJj%                        d Z dZddlZddlZddlZddlZddlmZmZm	Z	m
Z
 ddlmZmZmZmZmZmZmZmZmZ ddlmZ  G d de      Zd	 Zd
 Z G d de      Z G d de      Zd Zd Zd Zd Zd Zd Z ddedfdZ!e"e#e$fZ%dddde	fdZ&y)a  AppKit helpers.

Exported functions:
* runEventLoop - run NSApplicationMain in a safer way
* runConsoleEventLoop - run NSRunLoop.run() in a stoppable manner
* stopEventLoop - stops the event loop or terminates the application
* endSheetMethod - set correct signature for NSSheet callbacks
* callAfter - call a function on the main thread (async)
* callLater - call a function on the main thread after a delay (async)
)runEventLooprunConsoleEventLoopstopEventLoopendSheetMethod	callAfter	callLater    N)NSApp+NSApplicationDidFinishLaunchingNotificationNSApplicationMainNSRunAlertPanel)	NSAutoreleasePoolNSDateNSDefaultRunLoopModeNSLogNSNotificationCenterNSObject	NSRunLoopNSThreadNSTimer)superc                   :     e Zd ZdZ fdZd Zd Zd Zd Z xZ	S )PyObjCMessageRunnerzs
    Wraps a Python function and its arguments and allows it to be posted to the
    MainThread's `NSRunLoop`.
    c                 L    t               j                         } | sy|| _        | S )z)
        Designated initializer.
        N)r   init_payload)selfpayload	__class__s     _/Users/ahmed/devFolder/claude-voice/.venv/lib/python3.12/site-packages/PyObjCTools/AppHelper.pyinitWithPayload_z$PyObjCMessageRunner.initWithPayload_4   s$     w||~    c                 >    | j                  | j                  dd       y)zQ
        Posts a message to the Main thread, to be executed immediately.
        NF5performSelectorOnMainThread_withObject_waitUntilDone_scheduleCallWithDelay_r   s    r   r   zPyObjCMessageRunner.callAfter@   s     	BB''u	
r!   c                 >    | j                  | j                  |d       y)zo
        Posts a message to the Main thread, to be executed after the given
        delay, in seconds.
        FNr#   r   delays     r   
callLater_zPyObjCMessageRunner.callLater_H   s    
 	BB''	
r!   c                     t        j                         sJ d       |s| j                          y| j                  | j                  d|       y)z<
        This is run once we're on the Main thread.
        )Call is not executing on the Main thread!N)r   isMainThreadperformCall&performSelector_withObject_afterDelay_r(   s     r   r%   z*PyObjCMessageRunner.scheduleCallWithDelay_Q   sJ     $$&S(SS&  	33D4D4DdERr!   c                 j    t        j                         sJ d       | j                  \  }}} ||i | y)z,
        Actually runs the payload.
        r,   N)r   r-   r   )r   funcargskwargss       r   r.   zPyObjCMessageRunner.performCall_   s=     $$&S(SS&  $}}tV 	dfr!   )
__name__
__module____qualname____doc__r    r   r*   r%   r.   __classcell__r   s   @r   r   r   .   s"    



S
r!   r   c                     t        j                         j                         }t        j                         j	                  | ||f      }|j                          ~~y)z5
    Call a function on the Main thread (async).
    N)r   allocr   r   r    r   )r1   r2   r3   poolrunners        r   r   r   l   sM     ""$))+D &&(994v:NOF
r!   c                     t        j                         j                         }t        j                         j	                  |||f      }|j                  |        ~~y)zC
    Call a function on the Main thread after a delay (async).
    N)r   r;   r   r   r    r*   )r)   r1   r2   r3   r<   r=   s         r   r   r   w   sO     ""$))+D &&(994v:NOF
er!   c                       e Zd Zd Zy)#PyObjCAppHelperApplicationActivatorc                 6    t               j                  d       y NT)r	   activateIgnoringOtherApps_)r   aNotifications     r   activateNow_z0PyObjCAppHelperApplicationActivator.activateNow_   s    **40r!   N)r4   r5   r6   rE    r!   r   r@   r@      s    1r!   r@   c                   d     e Zd Zi Zed        Z fdZd Zed        Zed        Z	d Z
d Z xZS )PyObjCAppHelperRunLoopStopperc                 `    t        j                         }| j                  j                  |      S N)r   currentRunLoop
singletonsgetclsrunLoops     r   currentRunLoopStopperz3PyObjCAppHelperRunLoopStopper.currentRunLoopStopper   s%    **,~~!!'**r!   c                 T    t               j                         } d| _        d| _        | S )NF)r   r   
shouldStop	isConsole)r   r   s    r   r   z"PyObjCAppHelperRunLoopStopper.init   s#    w||~r!   c                     | j                    S rJ   )rS   r&   s    r   	shouldRunz'PyObjCAppHelperRunLoopStopper.shouldRun   s    ??""r!   c                 T    || j                   v rt        d      || j                   |<   y )Nz+Stopper already registered for this runLooprL   
ValueError)rO   runLoopStopperrP   s      r   addRunLoopStopper_toRunLoop_z:PyObjCAppHelperRunLoopStopper.addRunLoopStopper_toRunLoop_   s(    cnn$JKK"0wr!   c                 P    || j                   vrt        d      | j                   |= y )Nz'Stopper not registered for this runLooprX   rN   s     r    removeRunLoopStopperFromRunLoop_z>PyObjCAppHelperRunLoopStopper.removeRunLoopStopperFromRunLoop_   s&    #..(FGGNN7#r!   c                 t    d| _         | j                  s%t               t               j                  |        y y y rB   )rS   rT   r	   
terminate_r&   s    r   stopz"PyObjCAppHelperRunLoopStopper.stop   s4     ~~w"""4( # r!   c                 $    | j                          y rJ   )r`   )r   senders     r   performStop_z*PyObjCAppHelperRunLoopStopper.performStop_   s    		r!   )r4   r5   r6   rL   classmethodrQ   r   rV   r[   r]   r`   rc   r8   r9   s   @r   rH   rH      sV    J+ +# 1 1
 $ $
)r!   rH   c                      t         j                         } | %t               t               j                  d       yyt	        j
                  d| ddd       y)zx
    Stop the current event loop if possible
    returns True if it expects that it was successful, False otherwise
    NTFg        zperformStop:)rH   rQ   r	   r_   r   @scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_)stoppers    r   r   r      sS    
 ,AACG7Gt$LLWndE r!   c                 t    t        j                  | dt         j                  z   t         j                  z         S )z[
    Return a selector that can be used as the delegate callback for
    sheet methods
    s   v@:@)	signature)objcselector_C_NSInteger)meths    r   r   r      s0    
 ==$"3"33d6G6GG r!   c            	          t        j                  t        j                         d d  d   j	                         } t        ddddd d| z        S )N   r   z An unexpected error has occurred%@ContinueQuitz(%s))	tracebackformat_exception_onlysysexc_infostripr   )exceptionInfos    r   unexpectedErrorAlertPanelry      sQ    33S\\^BQ5GHKQQSM* r!   c                      dd l } t        j                          | j                  t	        j
                         d          y)Nr   ro   T)pdbrs   	print_excpost_mortemru   rv   )r{   s    r   unexpectedErrorAlertPdbr~      s+    OOCLLN1%&r!   c                     t         j                         }||j                          y t               t               j	                  d        y dd l} |j                  d       y )Nr      )rH   rQ   r`   r	   r_   os_exit)signumrg   r   s      r   machInterruptr      sG    +AACG		4 r!   c                  X    dd l } ddlm} |j                  | j                  t               y )Nr   )MachSignals)signalPyObjCToolsr   SIGINTr   )r   r   s     r   installMachInterruptr      s    'v}}m4r!   Fg      @c                 `   | t         j                  } |r
t                t        j                         }t
        j                         j                         }d|_        t
        j                  ||       	 |j                         r}|j                  |      }|j                         sn[t        j                  |      }||j                  |      }|j                  ||      s|j!                          |j                         r}t
        j#                  |       y # t
        j#                  |       w xY wrB   )ru   argvr   r   rK   rH   r;   r   rT   r[   rV   limitDateForMode_r   dateWithTimeIntervalSinceNow_earlierDate_runMode_beforeDate_r`   r]   )r   installInterruptmode
maxTimeoutrP   rg   nextfiresoons           r   r   r      s     |xx&&(G+11388:GG!>>wPP!006H$$&77
CD#,,X6..tX> ! 	&FFwO%FFwOs   3BD D-c                    | t         j                  } |dt        j                  v }|rcddlm} |j                          t        j                         j                         }t        j                         j                  |dt        d       nd}||rd}||rt        }nt        }t!        j"                         }t$        j                         j                         }t$        j'                  ||       t)               du }		 |j+                         r3	 |	rd}	|r
t-                 ||        nt)               j/                          	 ||jE                          t$        jG                  |       y# t0        $ r t3        j4                          Y G t        j6                         \  }
}}t9        |t:        j<                        rt?        |      }tA        d|       n] |       s6tA        dd	       t3        j4                          t        jB                  d       n tA        dd	       t3        j4                          Y nxY w|j+                         r9# ||jE                          t$        jG                  |       w xY w)
zRun the event loop, ask the user if we should continue if an
    exception is caught. Use this function instead of NSApplicationMain().
    NUSE_PDBr   )	DebuggingzactivateNow:TFrp   zAn exception has occurred:)$ru   r   r   environr   r   installVerboseExceptionHandlerr@   r;   r   r   defaultCenter!addObserver_selector_name_object_r
   r~   ry   r   rK   rH   r[   r	   rV   r   run
RAISETHESErs   r|   rv   
isinstancerj   errorstrr   exitremoveExceptionHandlerr]   )r   unexpectedErrorAlertr   r{   mainr   	activatorrP   rg   firstRunexctypeetb	error_strs                 r   r   r     s     |xx
{2::%
)002 8==?DDF	**,NN~'RTX	
 	C##: #< &&(G+11388:G!>>wPw$H P!$H',.JGKKM$   ,,.%FFwO-  ##%*!$Ba, #AI$	*-/$ <='')HHQK$ <='')1 !:  ,,.%FFwOs1   >I	 1E)  I	 )H2I	 B(H20I	 	)I2)'r7   __all__r   ru   rs   rj   AppKitr	   r
   r   r   
Foundationr   r   r   r   r   r   r   r   r   r   r   r   r   r@   rH   r   r   ry   r~   r   r   r   
SystemExitMemoryErrorKeyboardInterruptr   r   rF   r!   r   <module>r      s   	 
 
   
 
 
 ;( ;|1( 1
&H &R"		5 
,@SP6 +'89
 
	KPr!   