
    lKj                         d Z ddlmZ ddlmZmZ ddlZ ej                  e      Z	e	j                  ej                          G d d      Zy)z
WatchDog Service for monitoring macOS Accessibility events.
Delegates to the ax module's EventObserver for the underlying AXObserver management.
    )EventObserver)CallableOptionalNc                       e Zd ZdZddefdZd Zd Zede	fd       Z
d Zd	 Zd
ee   fdZd
ee   fdZd
ee   fdZy)WatchDoga  
    Unified WatchDog Service for monitoring macOS Accessibility events.
    Wraps the ax module's EventObserver to track changes across applications.

    The WatchDog helps overcome laziness in the accessibility tree by:
    1. Monitoring focus changes when users interact with UI
    2. Detecting structure changes when UI elements are added/removed
    3. Tracking property changes like value updates

    This allows the tree traversal to be more complete as notifications
    trigger callbacks that can force fresh tree reads.

    Usage:
        watchdog = WatchDog()
        watchdog.set_focus_callback(on_focus_change)
        watchdog.set_structure_callback(on_structure_change)
        watchdog.start()

        # ... run your app ...

        watchdog.stop()
    debounce_intervalc                 &    t        |      | _        y)zm
        Args:
            debounce_interval: Minimum time between events in seconds (default 50ms).
        )r   N)r   	_observer)selfr   s     k/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/macos_mcp/watchdog/service.py__init__zWatchDog.__init__&   s    
 '9JK    c                 &    | j                          | S N)startr   s    r   	__enter__zWatchDog.__enter__-   s    

r   c                 $    | j                          y r   )stop)r   exc_typeexc_valexc_tbs       r   __exit__zWatchDog.__exit__1   s    		r   returnc                 .    | j                   j                  S )z+Check if the watchdog is currently running.)r
   
is_runningr   s    r   r   zWatchDog.is_running4   s     ~~(((r   c                 b    | j                   j                          t        j                  d       y)zStart the watchdog service.zWatchDog service startedN)r
   r   loggerinfor   s    r   r   zWatchDog.start9   s    ./r   c                 b    | j                   j                          t        j                  d       y)zStop the watchdog service.zWatchDog service stoppedN)r
   r   r   r   r   s    r   r   zWatchDog.stop>   s    ./r   callbackc                 &    || j                   _        y)aK  
        Set the callback for focus changes. Pass None to disable.

        Callback signature: callback(element, notification: str, pid: int)
        - element: The AXUIElement that gained focus
        - notification: The notification type (e.g., 'AXFocusedUIElementChanged')
        - pid: Process ID of the application
        N)r
   on_focus_changedr   r!   s     r   set_focus_callbackzWatchDog.set_focus_callbackC   s     +3'r   c                 &    || j                   _        y)aN  
        Set the callback for structure changes. Pass None to disable.

        Callback signature: callback(element, notification: str, pid: int)
        - element: The AXUIElement affected by the structure change
        - notification: The notification type (e.g., 'AXCreated')
        - pid: Process ID of the application
        N)r
   on_structure_changedr$   s     r   set_structure_callbackzWatchDog.set_structure_callbackN   s     /7+r   c                 &    || j                   _        y)aH  
        Set the callback for property changes. Pass None to disable.

        Callback signature: callback(element, notification: str, pid: int)
        - element: The AXUIElement whose property changed
        - notification: The notification type (e.g., 'AXValueChanged')
        - pid: Process ID of the application
        N)r
   on_property_changedr$   s     r   set_property_callbackzWatchDog.set_property_callbackY   s     .6*r   N)g?)__name__
__module____qualname____doc__floatr   r   r   propertyboolr   r   r   r   r   r%   r(   r+    r   r   r   r      sw    .L% L )D ) )0
0
	38H+= 	3	7x/A 	7	6hx.@ 	6r   r   )r/   macos_mcp.axr   typingr   r   logging	getLoggerr,   r   setLevelINFOr   r3   r   r   <module>r:      sC   
 ' % 			8	$  T6 T6r   