
    g
fB8                         d Z dZdZdZdZdZddlZddlZ ej                  dd	       dd
l	m
Z
 ddlmZ ddlmZ ddlmZ  G d d      Zy)a  
Utilities for obtaining position-related information about accessible objects.
These utilities are app-type- and toolkit-agnostic. Utilities that might have
different implementations or results depending on the type of app (e.g. terminal,
chat, web) or toolkit (e.g. Qt, Gtk) should be in script_utilities.py file(s).

N.B. There are currently utilities that should never have custom implementations
that live in script_utilities.py files. These will be moved over time.
z$Id$z
$Revision$z$Date$zGCopyright (c) 2024 Igalia, S.L.Copyright (c) 2024 GNOME Foundation Inc.LGPL    NAtspiz2.0)r      )debug)AXObject)AXUtilitiesc                      e Zd ZdZed        Zed        Zed        Zed        Zed        Z	ed        Z
ed        Zed	        Zed
        Zed        Zed        Zed        Zed        Zed        Zedd       Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zy)AXComponentzNUtilities for obtaining position-related information about accessible objects.c                     t         j                  |       }|j                  |j                  dz  z   |j                  |j
                  dz  z   fS )z;Returns the center point of obj with respect to its window.   )r
   get_rectxwidthyheightobjrects     3/usr/lib/python3/dist-packages/orca/ax_component.pyget_center_pointzAXComponent.get_center_point5   sA     ##C(vv

Q&q(@@@    c                 P   t        j                  |       sy	 t        j                  j	                  | t        j
                  j                        }|j                  |j                  fS # t        $ r4}d| }t        j                  t        j                  |d       Y d}~yd}~ww xY w)zBReturns the x, y position tuple of obj with respect to its window.r   (AXComponent: Exception in get_position: TN)r   supports_componentr   	Componentget_position	CoordTypeWINDOW	Exceptionr   printMessage
LEVEL_INFOr   r   r   pointerrormsgs       r   r   zAXComponent.get_position<   s     **3/	OO00eoo6L6LME ww  	<UGDCu//d;	   8A( (	B%1*B  B%c                 r   t        j                  |       st        j                         S 	 t        j                  j                  | t        j                  j                        }|S # t        $ rH}d| }t        j                  t        j                  |d       t        j                         cY d}~S d}~ww xY w)z9Returns the Atspi rect of obj with respect to its window.z$AXComponent: Exception in get_rect: TN)r   r   r   Rectr   get_extentsr   r    r!   r   r"   r#   )r   r   r&   r'   s       r   r   zAXComponent.get_rectL   s     **3/::<	 ??..sEOO4J4JKD   	 8@Cu//d;::<	 s   8A% %	B6.=B1+B61B6c                 D   t        j                         }t        | j                  | j                  | j                  z   dz         }t        |j                  |j                  |j                  z   dz         }t        t        |      j                  t        |                  }t        | j                  | j                  | j                  z   dz         }t        |j                  |j                  |j                  z   dz         }t        t        |      j                  t        |                  }|rD|rB|d   |_        |d   |_        |d   |j                  z
  |_        |d   |j                  z
  |_	        d| d|d|g}	t        j                  t        j                  |	d       |S )z@Returns a rect representing the intersection of rect1 and rect2.r   r   r   z AXComponent: The intersection ofandzis:T)r   r*   ranger   r   sortedsetintersectionr   r   r   printTokensr#   )
rect1rect2result	x_points1	x_points2x_intersection	y_points1	y_points2y_intersectiontokenss
             r   get_rect_intersectionz!AXComponent.get_rect_intersection\   sH    %''577U[[#81#<=	%''577U[[#81#<=	I ; ;C	N KL%''577U\\#9A#=>	%''577U\\#9A#=>	I ; ;C	N KLn%a(FH%a(FH)"-8FL*2.9FM4eUE5RXY%**FD9r   c                 P   t        j                  |       sy	 t        j                  j	                  | t        j
                  j                        }|j                  |j                  fS # t        $ r4}d| }t        j                  t        j                  |d       Y d}~yd}~ww xY w)zBReturns the width, height tuple of obj with respect to its window.r   r   TN)r   r   r   r   get_sizer   r    r!   r   r"   r#   r   r   r$   s       r   r?   zAXComponent.get_sizet   s     **3/	OO,,S%//2H2HIE ww  	<UGDCu//d;	r(   c                 b    t         j                  |       }|j                  xs |j                   S )z0Returns True if obj has a width and height of 0.r
   r   r   r   r   s     r   has_no_sizezAXComponent.has_no_size   s*     ##C(::,--r   c                 T   t         j                  |       }|j                  s|j                  sy|j                  |j
                  cxk(  r%|j                  cxk(  r|j                  cxk(  rdk(  ry |j                  dk  s|j                  dk  rd| d|g}t        j                  t        j                  |d       t        j                  |        t         j                  |       }|j                  dk  s|j                  dk  r(d}t        j                  t        j                  |d       yy)zDReturns True if the rect associated with obj is sizeless or invalid.Tr   z	WARNING: zhas a broken rect:z0AXComponent: Clearing cache did not fix the rectF)r
   r   r   r   r   r   r   r2   r#   r   clear_cacher"   )r   r   r<   r'   s       r   has_no_size_or_invalid_rectz'AXComponent.has_no_size_or_invalid_rect   s     ##C(

dkk66TVV>tzz>T[[>B> ? JJOt{{R/!3(<dCFe..=  %'',D

R4;;#3H""5#3#3S$?r   c                     | j                   dk(  xr1 | j                  dk(  xr  | j                  dk(  xr | j                  dk(  S )z9Returns True if rect's x, y, width, and height are all 0.r   r   r   r   r   )r   s    r   is_empty_rectzAXComponent.is_empty_rect   s;     vv{Stvv{StzzQS4;;RSCSSr   c                     | j                   |j                   k(  xrO | j                  |j                  k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S )z@Returns True if rect1 and rect2 represent the same bounding box.rG   )r3   r4   s     r   is_same_rectzAXComponent.is_same_rect   sZ     ww%''! -577"-u{{*- ,	-r   c                    t        j                  |       syt        j                  |       ry	 t        j                  j                  | ||t        j                  j                        }d| d| d| d| g}t        j                  t        j                  |d       |S # t        $ r4}d| }t        j                  t        j                  |d       Y d}~yd}~ww xY w)	z8Returns True if obj's rect contains the specified point.Fz1AXComponent: Exception in object_contains_point: TNzAXComponent: zcontains point , : )r   r   is_bogusr   r   containsr   r    r!   r   r"   r#   r2   r   r   r   r5   r&   r'   r<   s          r   object_contains_pointz!AXComponent.object_contains_point   s     **3/S!	__--c1a9O9OPF "3/!BqcF8(LM%**FD9  	EeWMCu//d;	s   :B 	C&*CCc                     t         j                  t         j                  |       |      }t         j                  |       S )zCReturns True if the Atspi.Rect associated with obj intersects rect.r
   r=   r   rH   )r   r   r1   s      r   object_intersects_rectz"AXComponent.object_intersects_rect   s6     #889M9Mc9RTXY,,\:::r   c                 F   t         j                  |       }t        |j                        dkD  st        |j                        dkD  r+d| dg}t        j                  t
        j                  |d       y|j                  dk(  s|j                  dk(  rt        j                  |       s+d| dg}t        j                  t
        j                  |d       yt        j                  |       r+d| dg}t        j                  t
        j                  |d       yd| d	g}t        j                  t
        j                  |d       y
|j                  |j                  z   dk  xr |j                  |j                  z   dk  }d| d| g}t        j                  t
        j                  |d       |S )z:Returns True if the rect associated with obj is off-screeni'  zAXComponent: Treatingzas offscreen due to positionTr   z(as offscreen due to size and no childrenz!as offscreen due to size and rolezAXComponent: Treating sizelesszas onscreenFzAXComponent:zis off-screen: )r
   r   absr   r   r   r2   r#   r   r   r   get_child_countr   is_menu)r   r   r<   r5   s       r   object_is_off_screenz AXComponent.object_is_off_screen   sb    ##C(tvv;#dff+"5-s4RSFe..=::?dkkQ.++C0138bc!!%"2"2FDA""3'138[\!!%"2"2FDA6]KFe..=$**$q(ETVVdkk-AA-E #'AB%**FD9r   c                 z    t         j                  t         j                  |       t         j                  |            S )z1Returns True if obj1 and obj2 have the same rect.)r
   rJ   r   )obj1obj2s     r   objects_have_same_rectz"AXComponent.objects_have_same_rect   s.     ''(<(<T(B;CWCWX\C]^^r   c                     t         j                  t         j                  |       t         j                  |            }t         j                  |       S )z@Returns True if the rects associated with obj1 and obj2 overlap.rS   )r[   r\   r1   s      r   objects_overlapzAXComponent.objects_overlap   sD     #88  &(<(<T(BD,,\:::r   c                     t         j                  |       d   }t         j                  |      d   }t        ||z
        |k  S )zNReturns True if obj1 and obj2 are on the same line based on the center points.r   )r
   r   rV   )r[   r\   delta	y1_center	y2_centers        r   on_same_linezAXComponent.on_same_line   sC      006q9	006q9	9y()U22r   c                     t        j                  |       st        j                  |       ryt        j	                  |       }|j
                  dkD  xr |j                  dkD  S )zLReturns True if obj's rect is expected to include the rects of its children.Fr   )r   rX   is_page_tabr
   r   r   r   r   s     r    _object_bounds_includes_childrenz,AXComponent._object_bounds_includes_children   sM     s#{'>'>s'C##C(zzA~1$++/1r   c                     t        j                  |       D ]O  }t        j                  |      rt        j                  |      D ]  }t        j	                  |||      s|c c S  Q y)zGChecks each child to see if it has a descendant at the specified point.N)r   iter_childrenr
   rg   rQ   r   r   r   child
descendants        r   _find_descendant_at_pointz%AXComponent._find_descendant_at_point
  sf     ++C0 	&E;;EB&44U; &
44ZAF%%&	& r   c                    t        j                  |       sy	 t        j                  j	                  | ||t        j
                  j                        }d| d| d| d|g}t        j                  t        j                  |d       |S # t        $ r4}d| }t        j                  t        j                  |d       Y d}~yd}~ww xY w)zAReturns the child (or descendant?) of obj at the specified point.Nz.AXComponent: Exception in get_child_at_point: TzAXComponent: Child ofat rL    is)r   r   r   r   get_accessible_at_pointr   r    r!   r   r"   r#   r2   rP   s          r   _get_object_at_pointz AXComponent._get_object_at_point  s     **3/	__<<S!QH^H^_F *3#aS1#S0A6J%**FD9  	B5'JCu//d;	   :B 	C*B>>Cc                    t         j                  | ||      }|4t         j                  | ||      rt         j                  | ||      }|| S |}|| k(  st	        j
                  |      s|S t         j                  |||      S )=Returns the deepest descendant of obj at the specified point.)r
   rr   rQ   rm   r   rW   _get_descendant_at_pointrj   s        r   rv   z$AXComponent._get_descendant_at_point(  s     00a;=[>>sAqI$>>sAqIJ!
EC<x77>L33E1a@@r   c                     t         j                  | ||      }d| d| d| d|g}t        j                  t        j                  |d       |S )ru   zAXComponent: Descendant ofro   rL   rp   T)r
   rv   r   r2   r#   )r   r   r   r5   r<   s        r   get_descendant_at_pointz#AXComponent.get_descendant_at_point8  sR     55c1a@.s1#Rs#5FO%**FD9r   c                    t        j                  |       sy	 t        j                  j	                  | t        j
                  j                  ||      }d| d| d| d|g}t        j                  t        j                  |d       |S # t        $ r4}d| }t        j                  t        j                  |d       Y d}~yd}~ww xY w)	z.Attempts to scroll obj to the specified point.Fz2AXComponent: Exception in scroll_object_to_point: TNAXComponent: Scrolledzto rL   :)r   r   r   r   scroll_to_pointr   r    r!   r   r"   r#   r2   rP   s          r   scroll_object_to_pointz"AXComponent.scroll_object_to_pointA  s     **3/	__44S%//:P:PRSUVWF *3#aS1#QH%**FD9  	FugNCu//d;	rs   c                 R   t        j                  |       sy	 t        j                  j	                  | |      }d| d|d| g}t        j                  t        j                  |d       |S # t
        $ r4}d| }t        j                  t        j                  |d       Y d}~yd}~ww xY w)zBAttempts to scroll obj to the specified Atspi.ScrollType location.Fz5AXComponent: Exception in scroll_object_to_location: TNrz   torM   )
r   r   r   r   	scroll_tor!   r   r"   r#   r2   )r   locationr5   r&   r'   r<   s         r   scroll_object_to_locationz%AXComponent.scroll_object_to_locationS  s     **3/	__..sH=F *3h"VHN%**FD9  	I%QCu//d;	s    A) )	B&2*B!!B&c                 H    d }t        | t        j                  |            S )z0Returns objects sorted from smallest to largest.c                     t         j                  |       }t         j                  |      }|j                  |j                  z  |j                  |j                  z  z
  S )NrA   )r[   r\   r3   r4   s       r   _size_comparisonz:AXComponent.sort_objects_by_size.<locals>._size_comparisoni  sG    ((.E((.EKK%,,.5;;3MNNr   keyr/   	functools
cmp_to_key)objectsr   s     r   sort_objects_by_sizez AXComponent.sort_objects_by_sizee  s#    	O
 g9#7#78H#IJJr   c                 H    d }t        | t        j                  |            S )z5Returns objects sorted from top-left to bottom-right.c                    t         j                  |       }t         j                  |      }|j                  |j                  z
  xs |j                  |j                  z
  }|sVt	        j
                  |       t	        j
                  |      k(  r+t	        j                  |       t	        j                  |      z
  }t        |d      }t        |d      }|S )Nr   r   )	r
   r   r   r   r   
get_parentget_index_in_parentmaxmin)r[   r\   r3   r4   rvs        r   _spatial_comparisonzAAXComponent.sort_objects_by_position.<locals>._spatial_comparisont  s    ((.E((.E577"7egg&7B (--d3x7J7J47PP11$7(:V:VW[:\\RBRBIr   r   r   )r   r   s     r   sort_objects_by_positionz$AXComponent.sort_objects_by_positionp  s"    	 g9#7#78K#LMMr   N)r   )__name__
__module____qualname____doc__staticmethodr   r   r   r=   r?   rB   rE   rH   rJ   rQ   rT   rY   r]   r_   rd   rg   rm   rr   rv   rx   r}   r   r   r    r   r   r
   r
   2   s   XA A        .      . .  , T T
 - -  ( ; ;  6 _ _
 ; ; 3 3 2 2 	 	  " A A    "  " K K N Nr   r
   )r   __id____version____date____copyright____license__r   girequire_versiongi.repositoryr    r   	ax_objectr   ax_utilitiesr   r
   r   r   r   <module>r      sY   , ;  	   7E "    %PN PNr   