
    lKj8                     6   d Z ddlZddlmZmZ ddlmZmZmZm	Z	 ddl
mZmZ erddlmZ  ej                  e      Z G d d	      Z G d
 d      Z G d d      Z G d d      Z G d d      Z G d d      Z G d d      Z G d d      Z G d d      Zd Zy)aw  
macOS Accessibility action-based patterns.
Wraps AXUIElement actions and attributes into pattern-like interfaces
similar to Windows UIA patterns (InvokePattern, ValuePattern, etc.).

On macOS, there are no formal COM pattern interfaces like Windows UIA.
Instead, patterns are logical groupings of related AX actions and attributes
that provide a consistent interaction API.
    N)TYPE_CHECKINGAny   )GetAttributeSetAttributePerformActionIsAttributeSettable)Action	AttributeControlc                   F    e Zd ZdZdeddfdZdefdZededefd       Z	y)InvokePatternz
    Pattern for invoking (pressing/clicking) elements.
    Equivalent to Windows UIA InvokePattern.

    Applies to: Buttons, links, menu items, and other clickable elements.
    elementreturnNc                     || _         y N_elementselfr   s     f/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/macos_mcp/ax/patterns.py__init__zInvokePattern.__init__'   	        c                 J    t        | j                  t        j                        S )zW
        Invoke the element (press/click).
        Returns True if successful.
        r   r   r
   Pressr   s    r   InvokezInvokePattern.Invoke*   s    
 T]]FLL99r   c                 B    ddl m}  ||       }t        j                  |v S )z1Check if the element supports the Invoke pattern.r   GetActionNames)corer#   r
   r   r   r#   actionss      r   IsSupportedzInvokePattern.IsSupported1   s      	) )||w&&r   )
__name__
__module____qualname____doc__r   r   boolr    staticmethodr'    r   r   r   r      sH         : : 'S 'T ' 'r   r   c                       e Zd ZdZdeddfdZed        Zej                  dd       Zede	fd       Z
de	fd	Zedede	fd
       Zy)ValuePatternz
    Pattern for getting and setting element values.
    Equivalent to Windows UIA ValuePattern.

    Applies to: Text fields, text areas, sliders, combo boxes.
    r   r   Nc                     || _         y r   r   r   s     r   r   zValuePattern.__init__B   r   r   c                 J    t        | j                  t        j                        S )Get the current value.r   r   r   Valuer   s    r   r5   zValuePattern.ValueE   s     DMM9??;;r   c                 N    t        | j                  t        j                  |       yzSet the value.Nr   r   r   r5   r   values     r   r5   zValuePattern.ValueJ        	T]]IOOU;r   c                 L    t        | j                  t        j                         S )z Check if the value is read-only.)r	   r   r   r5   r   s    r   
IsReadOnlyzValuePattern.IsReadOnlyO   s     't}}iooFFFr   c                 L    t        | j                  t        j                  |      S )z(Set the value and return success status.r8   r9   s     r   SetValuezValuePattern.SetValueT   s    DMM9??EBBr   c                 >    ddl m} t        j                   ||       v S )z0Check if the element supports the Value pattern.r   GetAttributeNames)r$   rB   r   r5   r   rB   s     r   r'   zValuePattern.IsSupportedX   s     	,"3G"<<<r   )r   N)r(   r)   r*   r+   r   r   propertyr5   setterr,   r=   r?   r-   r'   r.   r   r   r0   r0   :   s          < < \\< < GD G GC C =S =T = =r   r0   c                       e Zd ZdZdeddfdZedefd       Zej                  deddfd       Zedefd	       Z
edefd
       ZdefdZdefdZededefd       Zy)RangeValuePatternz
    Pattern for elements with numeric range values.
    Equivalent to Windows UIA RangeValuePattern.

    Applies to: Sliders, progress indicators, level indicators.
    r   r   Nc                     || _         y r   r   r   s     r   r   zRangeValuePattern.__init__h   r   r   c                     t        | j                  t        j                        }	 |t	        |      S dS # t
        t        f$ r Y yw xY w)r3           )r   r   r   r5   float	TypeError
ValueErrorr   vals     r   r5   zRangeValuePattern.Valuek   sG     4==)//:	!$5:9c9:& 		   5 5 AAr:   c                 N    t        | j                  t        j                  |       yr7   r8   r9   s     r   r5   zRangeValuePattern.Valuet   r;   r   c                     t        | j                  t        j                        }	 |t	        |      S dS # t
        t        f$ r Y yw xY w)zGet the minimum value.rJ   )r   r   r   MinValuerK   rL   rM   rN   s     r   MinimumzRangeValuePattern.Minimumy   sI     4==)*<*<=	!$5:9c9:& 		rP   c                     t        | j                  t        j                        }	 |t	        |      S dS # t
        t        f$ r Y yw xY w)zGet the maximum value.      Y@)r   r   r   MaxValuerK   rL   rM   rN   s     r   MaximumzRangeValuePattern.Maximum   sI     4==)*<*<=	!$5:;e;:& 		rP   c                 J    t        | j                  t        j                        S )zIncrement the value.)r   r   r
   	Incrementr   s    r   rZ   zRangeValuePattern.Increment       T]]F,<,<==r   c                 J    t        | j                  t        j                        S )zDecrement the value.)r   r   r
   	Decrementr   s    r   r]   zRangeValuePattern.Decrement   r[   r   c                 j    ddl m}  ||       }t        j                  |v xs t        j                  |v S )z5Check if the element supports the RangeValue pattern.r   r"   )r$   r#   r
   rZ   r]   r%   s      r   r'   zRangeValuePattern.IsSupported   s2     	) )7*If.>.>'.IIr   )r(   r)   r*   r+   r   r   rD   rK   r5   rE   rT   rX   r,   rZ   r]   r-   r'   r.   r   r   rG   rG   `   s          u   \\<5 <T < <      >4 >>4 > JS JT J Jr   rG   c                   h    e Zd ZdZd Zedefd       Zedefd       Z	defdZ
ededefd       Zy	)
TogglePatternz
    Pattern for toggling elements (checkboxes, switches).
    Equivalent to Windows UIA TogglePattern.

    Applies to: Checkboxes, toggle switches.
    c                     || _         y r   r   r   s     r   r   zTogglePattern.__init__   r   r   r   c                 r    t        | j                  t        j                        }|y|du s|dk(  ry|dk(  ryy)zd
        Get the current toggle state.
        Returns: 0 = off, 1 = on, 2 = indeterminate.
        r   Tr      r4   rN   s     r   ToggleStatezTogglePattern.ToggleState   s=     4==)//:;$;#(!8r   c                      | j                   dk(  S )zCheck if the toggle is on.r   )rd   r   s    r   IsOnzTogglePattern.IsOn   s     1$$r   c                 J    t        | j                  t        j                        S )zToggle the state.r   r   s    r   TogglezTogglePattern.Toggle       T]]FLL99r   r   c                 D    ddl m}  || t        j                        }|dv S )z1Check if the element supports the Toggle pattern.r   r   )
AXCheckBoxAXToggleAXSwitchr$   r   r   Roler   _GetAttrroles      r   r'   zTogglePattern.IsSupported   s#     	30===r   N)r(   r)   r*   r+   r   rD   intrd   r,   rf   rh   r-   r   r'   r.   r   r   r`   r`      sp      S   %d % %: : >S >T > >r   r`   c                   h    e Zd ZdZdeddfdZedefd       ZdefdZ	defdZ
ededefd	       Zy)
ExpandCollapsePatternz
    Pattern for expanding/collapsing elements.
    Equivalent to Windows UIA ExpandCollapsePattern.

    Applies to: Disclosure triangles, outline rows, combo boxes.
    r   r   Nc                     || _         y r   r   r   s     r   r   zExpandCollapsePattern.__init__   r   r   c                     t        | j                  t        j                        }||du S t        | j                  t        j                        }|dk(  xs |du S )z!Check if the element is expanded.Tr   )r   r   r   Expandedr5   rN   s     r   
IsExpandedz ExpandCollapsePattern.IsExpanded   sP     4==)*<*<=?$;4==)//:ax&3$;&r   c                 d    | j                   s$t        | j                  t        j                        S y)zExpand the element.Trz   r   r   r
   r   r   s    r   ExpandzExpandCollapsePattern.Expand   s!     ==r   c                 d    | j                   r$t        | j                  t        j                        S y)zCollapse the element.Tr|   r   s    r   CollapsezExpandCollapsePattern.Collapse   s!    ?? ==r   c                 B    ddl m}  ||       }t        j                  |v S )z9Check if the element supports the ExpandCollapse pattern.r   rA   )r$   rB   r   ry   r   rB   attrss      r   r'   z!ExpandCollapsePattern.IsSupported   s"     	,!'*!!U**r   )r(   r)   r*   r+   r   r   rD   r,   rz   r}   r   r-   r'   r.   r   r   rv   rv      so          'D ' ' $  +S +T + +r   rv   c                       e Zd ZdZdeddfdZedefd       Zedefd       Z	ede
fd       Zede
fd	       Zd
ede
fdZedede
fd       Zy)ScrollPatternz
    Pattern for scrolling elements.
    Equivalent to Windows UIA ScrollPattern.

    Applies to: Scroll areas, lists, tables.
    r   r   Nc                     || _         y r   r   r   s     r   r   zScrollPattern.__init__   r   r   c                     t        | j                  t        j                        }|r+t        |t        j                        }|	 t        |      dz  S y# t        t        f$ r Y yw xY w)z.Get horizontal scroll position (0.0 to 100.0).rV   rJ   )r   r   r   HorizontalScrollBarr5   rK   rL   rM   )r   h_barrO   s      r   HorizontalScrollPercentz%ScrollPattern.HorizontalScrollPercent   sg     T]]I,I,IJuioo6C :--  ":.    A A%$A%c                     t        | j                  t        j                        }|r+t        |t        j                        }|	 t        |      dz  S y# t        t        f$ r Y yw xY w)z,Get vertical scroll position (0.0 to 100.0).rV   rJ   )r   r   r   VerticalScrollBarr5   rK   rL   rM   )r   v_barrO   s      r   VerticalScrollPercentz#ScrollPattern.VerticalScrollPercent
  sg     T]]I,G,GHuioo6C :--  ":. r   c                 N    t        | j                  t        j                        duS )z*Check if horizontal scrolling is possible.N)r   r   r   r   r   s    r   HorizontallyScrollablez$ScrollPattern.HorizontallyScrollable  s!     DMM9+H+HIQUUUr   c                 N    t        | j                  t        j                        duS )z(Check if vertical scrolling is possible.N)r   r   r   r   r   s    r   VerticallyScrollablez"ScrollPattern.VerticallyScrollable  s      DMM9+F+FGtSSr   	directionc                     t         j                  t         j                  t         j                  t         j                  d}|j                  |j                               }|rt        | j                  |      S y)z
        Scroll by one page in the given direction.

        Args:
            direction: 'up', 'down', 'left', or 'right'.
        )updownleftrightF)	r
   ScrollUpByPageScrollDownByPageScrollLeftByPageScrollRightByPagegetlowerr   r   )r   r   
action_mapactions       r   ScrollByPagezScrollPattern.ScrollByPage!  s]     ''++++--	

 	 12 77r   c                 ~    t        | t        j                        }t        | t        j                        }|duxs |duS )z1Check if the element supports the Scroll pattern.N)r   r   r   r   )r   r   r   s      r   r'   zScrollPattern.IsSupported3  s=     Wi&C&CDWi&A&ABD 5E$55r   )r(   r)   r*   r+   r   r   rD   rK   r   r   r,   r   r   strr   r-   r'   r.   r   r   r   r      s          
 
 
 
u 
 
 V V V Td T Tc d $ 6S 6T 6 6r   r   c                   r    e Zd ZdZdeddfdZedee   fd       Zeded   fd       Z	e
dedefd	       Zy)
SelectionPatternz
    Pattern for elements with selectable children.
    Equivalent to Windows UIA SelectionPattern.

    Applies to: Lists, tables, tab groups.
    r   r   Nc                     || _         y r   r   r   s     r   r   zSelectionPattern.__init__C  r   r   c                 h    t        | j                  t        j                        }|rt	        |      S g S )z4Get the currently selected children as raw elements.)r   r   r   SelectedChildrenlist)r   childrens     r   r   z!SelectionPattern.SelectedChildrenF  s+      y/I/IJ!)tH~1r1r   r   c                     ddl m} t        | j                  t        j
                        }|r|D cg c]  } ||       c}S g S c c}w )z7Get the currently selected children as Control objects.r   r   )r   )controlsr   r   r   r   r   )r   r   r   cs       r   SelectedChildControlsz&SelectionPattern.SelectedChildControlsL  sD     	&y/I/IJ0891GA&99	 :s   Ac                 >    ddl m} t        j                   ||       v S )z4Check if the element supports the Selection pattern.r   rA   )r$   rB   r   r   rC   s     r   r'   zSelectionPattern.IsSupportedV  s     	,))->w-GGGr   )r(   r)   r*   r+   r   r   rD   r   r   r   r-   r,   r'   r.   r   r   r   r   ;  s~          2$s) 2 2
 tI   HS HT H Hr   r   c                       e Zd ZdZdeddfdZedefd       Zedefd       Z	edefd       Z
defd	Zdefd
ZdefdZdefdZededefd       Zy)WindowPatternzo
    Pattern for window management.
    Equivalent to Windows UIA WindowPattern.

    Applies to: Windows.
    r   r   Nc                     || _         y r   r   r   s     r   r   zWindowPattern.__init__f  r   r   c                 R    t        | j                  t        j                        }|du S )z!Check if the window is minimized.T)r   r   r   	MinimizedrN   s     r   IsMinimizedzWindowPattern.IsMinimizedi  s$     4==)*=*=>d{r   c                 R    t        | j                  t        j                        }|du S )z"Check if the window is fullscreen.T)r   r   r   
FullScreenrN   s     r   IsFullScreenzWindowPattern.IsFullScreeno  s$     4==)*>*>?d{r   c                 R    t        | j                  t        j                        }|du S )zCheck if the window is modal.T)r   r   r   ModalrN   s     r   IsModalzWindowPattern.IsModalu  s"     4==)//:d{r   c                     t        | j                  t        j                        }|rt	        |t
        j                        S y)zClose the window.F)r   r   r   CloseButtonr   r
   r   )r   	close_btns     r   ClosezWindowPattern.Close{  s/     	0E0EF	 FLL99r   c                 L    t        | j                  t        j                  d      S )zMinimize the window.Tr   r   r   r   r   s    r   MinimizezWindowPattern.Minimize  s    DMM9+>+>EEr   c                 L    t        | j                  t        j                  d      S )zRestore a minimized window.Fr   r   s    r   RestorezWindowPattern.Restore  s    DMM9+>+>FFr   c                 J    t        | j                  t        j                        S )zBring the window to front.)r   r   r
   Raiser   s    r   r   zWindowPattern.Raise  ri   r   c                 F    ddl m}  || t        j                        }|dk(  S )z1Check if the element supports the Window pattern.r   rk   AXWindowro   rq   s      r   r'   zWindowPattern.IsSupported  s"     	30z!!r   )r(   r)   r*   r+   r   r   rD   r,   r   r   r   r   r   r   r   r-   r'   r.   r   r   r   r   ^  s          T  
 d  
   
t F$ FG G:t : "S "T " "r   r   c                       e Zd ZdZdeddfdZedefd       Zedefd       Z	ed        Z
edefd	       Zed
        Zedefd       Zededefd       Zy)TextPatternz
    Pattern for text content access.
    Equivalent to Windows UIA TextPattern.

    Applies to: Text fields, text areas, static text.
    r   r   Nc                     || _         y r   r   r   s     r   r   zTextPattern.__init__  r   r   c                 h    t        | j                  t        j                        }|t	        |      S dS )zGet the full text content. )r   r   r   r5   r   rN   s     r   TextzTextPattern.Text  s+     4==)//:?s3x22r   c                 R    t        | j                  t        j                        xs dS )z Get the currently selected text.r   )r   r   r   SelectedTextr   s    r   r   zTextPattern.SelectedText  s      DMM9+A+ABHbHr   c                 J    t        | j                  t        j                        S )zGet the selected text range.)r   r   r   SelectedTextRanger   s    r   r   zTextPattern.SelectedTextRange  s     DMM9+F+FGGr   c                 V    t        | j                  t        j                        }||S dS )z#Get the total number of characters.r   )r   r   r   NumberOfCharactersrN   s     r   r   zTextPattern.NumberOfCharacters  s)     4==)*F*FGos,1,r   c                 J    t        | j                  t        j                        S )z Get the visible character range.)r   r   r   VisibleCharacterRanger   s    r   r   z!TextPattern.VisibleCharacterRange  s     DMM9+J+JKKr   c                 V    t        | j                  t        j                        }||S dS )z+Get the line number of the insertion point.r   )r   r   r   InsertionPointLineNumberrN   s     r   r   z$TextPattern.InsertionPointLineNumber  s)     4==)*L*LMos,1,r   c                 j    ddl m}  ||       }t        j                  |v xs t        j                  |v S )z/Check if the element supports the Text pattern.r   rA   )r$   rB   r   r   r   r   s      r   r'   zTextPattern.IsSupported  s3     	,!'*++u4W	8N8NRW8WWr   )r(   r)   r*   r+   r   r   rD   r   r   r   r   rt   r   r   r   r-   r,   r'   r.   r   r   r   r     s          3c 3 3
 Ic I I H H -C - -
 L L -# - -
 XS XT X Xr   r   c                 6    |j                  |       r ||       S y)z
    Get a pattern for an element if supported.

    Args:
        element: AXUIElementRef.
        pattern_class: Pattern class (e.g., InvokePattern, ValuePattern).

    Returns:
        Pattern instance if supported, None otherwise.
    N)r'   )r   pattern_classs     r   
GetPatternr     s        )W%%r   )r+   loggingtypingr   r   r$   r   r   r   r	   enumsr
   r   r   r   	getLoggerr(   loggerr   r0   rG   r`   rv   r   r   r   r   r   r.   r   r   <module>r      s     % 
 !			8	$' '6#= #=L9J 9Jx)> )>X'+ '+TF6 F6R H  HF6" 6"r2X 2Xtr   