o
    'š£h…%  ã                   @   s  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„ deƒZd	d
„ Zdd„ ZG dd„ deƒZG dd„ deƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Z ddedfdd „Z!e"e#e$fZ%dddde	fd!d"„Z&dS )#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                       s@   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Z‡  Z	S )ÚPyObjCMessageRunnerzs
    Wraps a Python function and its arguments and allows it to be posted to the
    MainThread's `NSRunLoop`.
    c                    s   t ƒ  ¡ } | s	dS || _| S )z)
        Designated initializer.
        N)r   ÚinitÚ_payload)ÚselfÚpayload©Ú	__class__© úl/Users/merlin/projects/employee-monitoring-system/venv/lib/python3.10/site-packages/PyObjCTools/AppHelper.pyÚinitWithPayload_4   s
   
z$PyObjCMessageRunner.initWithPayload_c                 C   s   |   | jdd¡ dS )zQ
        Posts a message to the Main thread, to be executed immediately.
        NF©Z5performSelectorOnMainThread_withObject_waitUntilDone_ÚscheduleCallWithDelay_©r   r   r   r   r   @   s   ÿzPyObjCMessageRunner.callAfterc                 C   s   |   | j|d¡ dS )zo
        Posts a message to the Main thread, to be executed after the given
        delay, in seconds.
        FNr    ©r   Údelayr   r   r   Ú
callLater_H   s   ÿzPyObjCMessageRunner.callLater_c                 C   s4   t  ¡ sJ dƒ‚|s|  ¡  dS |  | jd|¡ dS )z<
        This is run once we're on the Main thread.
        ú)Call is not executing on the Main thread!N)r   ÚisMainThreadÚperformCallZ&performSelector_withObject_afterDelay_r#   r   r   r   r!   Q   s
   z*PyObjCMessageRunner.scheduleCallWithDelay_c                 C   s.   t  ¡ sJ dƒ‚| j\}}}||i |¤Ž dS )z,
        Actually runs the payload.
        r&   N)r   r'   r   )r   ÚfuncÚargsÚkwargsr   r   r   r(   _   s   zPyObjCMessageRunner.performCall)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r%   r!   r(   Ú__classcell__r   r   r   r   r   .   s    	r   c                 O   s0   t  ¡  ¡ }t ¡  | ||f¡}| ¡  ~~dS )z5
    Call a function on the Main thread (async).
    N)r   Úallocr   r   r   r   )r)   r*   r+   ÚpoolÚrunnerr   r   r   r   l   s
   r   c                 O   s2   t  ¡  ¡ }t ¡  |||f¡}| | ¡ ~~dS )zC
    Call a function on the Main thread after a delay (async).
    N)r   r1   r   r   r   r%   )r$   r)   r*   r+   r2   r3   r   r   r   r   w   s
   
r   c                   @   s   e Zd Zdd„ ZdS )Ú#PyObjCAppHelperApplicationActivatorc                 C   s   t ƒ  d¡ d S ©NT)r   ZactivateIgnoringOtherApps_)r   ZaNotificationr   r   r   ÚactivateNow_ƒ   s   z0PyObjCAppHelperApplicationActivator.activateNow_N)r,   r-   r.   r6   r   r   r   r   r4   ‚   s    r4   c                       s\   e Zd Zi Zedd„ ƒZ‡ fdd„Zdd„ Zedd„ ƒZed	d
„ ƒZ	dd„ Z
dd„ Z‡  ZS )ÚPyObjCAppHelperRunLoopStopperc                 C   s   t  ¡ }| j |¡S ©N)r   ÚcurrentRunLoopÚ
singletonsÚget©ÚclsÚrunLoopr   r   r   ÚcurrentRunLoopStopperŠ   s   z3PyObjCAppHelperRunLoopStopper.currentRunLoopStopperc                    s   t ƒ  ¡ } d| _d| _| S )NF)r   r   Ú
shouldStopÚ	isConsoler"   r   r   r   r      s   
z"PyObjCAppHelperRunLoopStopper.initc                 C   s   | j  S r8   )r@   r"   r   r   r   Ú	shouldRun•   s   z'PyObjCAppHelperRunLoopStopper.shouldRunc                 C   s    || j v r	tdƒ‚|| j |< d S )Nz+Stopper already registered for this runLoop©r:   Ú
ValueError)r=   ZrunLoopStopperr>   r   r   r   ÚaddRunLoopStopper_toRunLoop_˜   s   
z:PyObjCAppHelperRunLoopStopper.addRunLoopStopper_toRunLoop_c                 C   s   || j vr	tdƒ‚| j |= d S )Nz'Stopper not registered for this runLooprC   r<   r   r   r   Ú removeRunLoopStopperFromRunLoop_ž   s   
z>PyObjCAppHelperRunLoopStopper.removeRunLoopStopperFromRunLoop_c                 C   s.   d| _ | jstƒ d urtƒ  | ¡ d S d S d S r5   )r@   rA   r   Ú
terminate_r"   r   r   r   Ústop¤   s   
þz"PyObjCAppHelperRunLoopStopper.stopc                 C   s   |   ¡  d S r8   )rH   )r   Zsenderr   r   r   ÚperformStop_¬   s   z*PyObjCAppHelperRunLoopStopper.performStop_)r,   r-   r.   r:   Úclassmethodr?   r   rB   rE   rF   rH   rI   r0   r   r   r   r   r7   ‡   s    


r7   c                  C   sD   t  ¡ } | du rtƒ durtƒ  d¡ dS dS t d| ddd¡ dS )zx
    Stop the current event loop if possible
    returns True if it expects that it was successful, False otherwise
    NTFg        zperformStop:)r7   r?   r   rG   r   Z@scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_)Ústopperr   r   r   r   °   s   

ÿr   c                 C   s   t j| dt j t j dS )z[
    Return a selector that can be used as the delegate callback for
    sheet methods
    s   v@:@)Ú	signature)ÚobjcÚselectorZ_C_NSInteger)Úmethr   r   r   r   Á   s   ÿr   c                  C   s4   t jt ¡ d d… Ž d  ¡ } tddddd d|  ƒS )Né   r   z An unexpected error has occurredú%@ÚContinueZQuitz(%s))Ú	tracebackÚformat_exception_onlyÚsysÚexc_infoÚstripr   )ZexceptionInfor   r   r   ÚunexpectedErrorAlertPanelË   s   úrX   c                  C   s&   dd l } t ¡  |  t ¡ d ¡ dS )Nr   rP   T)ÚpdbrS   Ú	print_excZpost_mortemrU   rV   )rY   r   r   r   ÚunexpectedErrorAlertPdb×   s   r[   c                 C   sL   t  ¡ }|d ur| ¡  d S tƒ d urtƒ  d ¡ d S dd l}| d¡ d S )Nr   é   )r7   r?   rH   r   rG   ÚosÚ_exit)ÚsignumrK   r]   r   r   r   ÚmachInterruptß   s   
r`   c                  C   s&   dd l } ddlm} |  | jt¡ d S )Nr   )ÚMachSignals)ÚsignalÚPyObjCToolsra   ÚSIGINTr`   )rb   ra   r   r   r   ÚinstallMachInterruptë   s   re   Fg      @c              	   C   sÐ   | d u rt j} |rtƒ  t ¡ }t ¡  ¡ }d|_t 	||¡ zB| 
¡ rR| |¡}| 
¡ s.n,t |¡}|d ur<| |¡}| ||¡sF| ¡  | 
¡ s$W t |¡ d S W t |¡ d S W t |¡ d S t |¡ w r5   )rU   Úargvre   r   r9   r7   r1   r   rA   rE   rB   ZlimitDateForMode_r   ZdateWithTimeIntervalSinceNow_ZearlierDate_ZrunMode_beforeDate_rH   rF   )rf   ÚinstallInterruptÚmodeZ
maxTimeoutr>   rK   ZnextfireÚsoonr   r   r   r   ò   s0   



÷ô÷	r   c              	   C   s°  | du rt j} |du rdtjv }|r-ddlm} | ¡  t ¡  	¡ }t
 ¡  |dtd¡ nd}|du r7|r7d}|du rB|r@t}nt}t ¡ }t ¡  	¡ }t ||¡ tƒ du }	zr| ¡ rºz|	rkd}	|rftƒ  || ƒ ntƒ  ¡  W nC ty~   t ¡  Y n<   t  ¡ \}
}}t|tjƒr˜t|ƒ}t d|ƒ n|ƒ sªt dd	ƒ t ¡  t  !d¡ n	t dd	ƒ t ¡  Y nn| ¡ s\W |durÃ| "¡  t #|¡ dS |durÒ| "¡  t #|¡ w )
zŒRun the event loop, ask the user if we should continue if an
    exception is caught. Use this function instead of NSApplicationMain().
    NZUSE_PDBr   )Ú	DebuggingzactivateNow:TFrQ   zAn exception has occurred:)$rU   rf   r]   Úenvironrc   rj   ZinstallVerboseExceptionHandlerr4   r1   r   r   ZdefaultCenterZ!addObserver_selector_name_object_r	   r[   rX   r   r9   r7   rE   r   rB   re   ÚrunÚ
RAISETHESErS   rZ   rV   Ú
isinstancerM   ÚerrorÚstrr   ÚexitZremoveExceptionHandlerrF   )rf   ZunexpectedErrorAlertrg   rY   Úmainrj   Z	activatorr>   rK   ZfirstRunÚexctypeÚeÚtbZ	error_strr   r   r   r     sp   

ÿ


€

€æ€þr   )'r/   Ú__all__r]   rU   rS   rM   ZAppKitr   r	   r
   r   Z
Foundationr   r   r   r   r   r   r   r   r   r   r   r   r   r4   r7   r   r   rX   r[   r`   re   r   Ú
SystemExitÚMemoryErrorÚKeyboardInterruptrm   r   r   r   r   r   Ú<module>   s:    	,>)

ÿ
û