
    g
f?8                         d Z dZdZdZdZdZddlmZ ddlmZ dd	lm	Z	 dd
l
mZ ddlmZ dZdZdZdZdZdZ G d d      Z e       Zd Zy)z1Module to manage the focused object, window, etc.z$Id$z
$Revision$z$Date$zQCopyright (c) 2005-2008 Sun Microsystems Inc.Copyright (c) 2016-2023 Igalia, S.L.LGPL   )braille)debug)script_manager)AXObject)AXUtilitieszcaret-trackingzfocus-trackingzflat-reviewzmouse-reviewzobject-navigatorzsay-allc                   ~    e Zd ZdZd ZddZd Zd Zd Zd Z	d Z
dd
Zd Zd ZddZd Zd Zd Zd Zd ZddZy	)FocusManagerz(Manages the focused object, window, etc.c                 <    d | _         d | _        d | _        d | _        y N)_window_focus_object_of_interest_active_modeselfs    4/usr/lib/python3/dist-packages/orca/focus_manager.py__init__zFocusManager.__init__1   s     #'      c                     d}|r|d| z  }t        j                  t         j                  |d       d| _        d| _        d| _        d| _        y)z!Clears everything we're tracking.z!FOCUS MANAGER: Clearing all statez: TN)r   printMessage
LEVEL_INFOr   r   r   r   )r   reasonmsgs      r   clear_statezFocusManager.clear_state7   sR     2Rx= C5++S$7#'  r   c                     t        j                  | j                        }d| j                  d|g}t        j                  t        j
                  |d       |S )z0Returns the focused object in the active window.z FOCUS MANAGER: Focused object inisT)r   get_focused_objectr   r   printTokensr   )r   resulttokenss      r   find_focused_objectz FocusManager.find_focused_objectC   sG     //=4dllD&Q%**FD9r   c                     | j                   du xr | j                  du }|r'd}t        j                  t        j                  |d       |S )z;Returns True if we have no knowledge about what is focused.Nz+FOCUS MANAGER: Focus and window are unknownT)r   r   r   r   r   )r   r    r   s      r   focus_and_window_are_unknownz)FocusManager.focus_and_window_are_unknownK   sE     $=)=?Cu//d;r   c                     t        j                  | j                        syd}t        j                  t        j
                  |d       y)z+Returns True if the locus of focus is dead.FzFOCUS MANAGER: Focus is deadT)r   is_deadr   r   r   r   )r   r   s     r   focus_is_deadzFocusManager.focus_is_deadU   s9     ,,5++S$7r   c                 N    | j                   y| j                   | j                  k(  S )z8Returns True if the locus of focus is the active window.F)r   r   r   s    r   focus_is_active_windowz#FocusManager.focus_is_active_window_   s$     ;;{{dll**r   c                 v    | j                   duxr* t        j                  | j                   | j                        S )z@Returns True if the locus of focus is inside the current window.N)r   r   is_ancestorr   r   s    r   focus_is_in_active_windowz&FocusManager.focus_is_in_active_windowg   s-     {{$&Z8+?+?T\\+ZZr   Nc                    |d}||}|t         }	 |j                  d|z   dd       || j                  k7  r}d| j                  d	|g}t        j                  t        j
                  |d       || _        |t        k(  r$t        j                  t        j                         nt        j                          	 d
|d| d| dg}t        j                  t        j
                  |d       |j                  d||       || j                  k7  r=d| j                  d	|g}t        j                  t        j
                  |d       || _        yy# t        $ r5}d| }t        j                  t        j
                  |d       Y d}~Wd}~ww xY w# t        $ r4}d| }t        j                  t        j
                  |d       Y d}~d}~ww xY w)zLNotifies interested clients that the current region of interest has changed.N    zmode-changed::r    z=FOCUS MANAGER: Exception emitting mode-changed notification: Tz"FOCUS MANAGER: Switching mode fromtoz"FOCUS MANAGER: Region of interest:(z, )zregion-changedz?FOCUS MANAGER: Exception emitting region-changed notification: z0FOCUS MANAGER: Switching object of interest from)FOCUS_TRACKINGemit	Exceptionr   r   r   r   r   FLAT_REVIEWr   setBrlapiPriorityBRLAPI_PRIORITY_HIGHr   )r   objstart_offset
end_offsetmodeerrorr   r!   s           r   emit_region_changedz FocusManager.emit_region_changedl   s    L%J<!D	<HH%,a4
 4$$$:D<M<MtUYZFe..= $D{"))'*F*FG))+	<:C1\NRTU_T``aAbcFe..=HH%|Z@
 $***H..c;Fe..='*D$	 ++  	<QRWQXYCu//d;;	<"  	<STYSZ[Cu//d;;	<s0   E 5AF 	F*E??F	G*F??Gc                     d| j                   d| j                  g}t        j                  t        j                  |d       | j                   | j                  fS )z:Returns the current mode and associated object of interestzFOCUS MANAGER: Active mode:zObject of interest:T)r   r   r   r   r   r   r!   s     r   &get_active_mode_and_object_of_interestz3FocusManager.get_active_mode_and_object_of_interest   sQ     01B1B')A)AC%**FD9  $":":::r   c                     d| j                   g}t        j                  t        j                  |d       | j                   S )zGReturns the current locus of focus (i.e. the object with visual focus).z FOCUS MANAGER: Locus of focus isT)r   r   r   r   r@   s     r   get_locus_of_focuszFocusManager.get_locus_of_focus   s4     5dkkB%**FD9{{r   c                 >   d|g}t        j                  t         j                  |dd       t        j                  |      }t        j                  ||d       |s7|| j                  k(  r(d}t        j                  t         j                  |d       yt        j                         j                         }|r|r}|j                  sqt        j                  |j                        }	t        j                         j                  |	|j                        }t        j                         j!                  |d       | j                  }
t        j"                  |
      rd}
|/d}t        j                  t         j                  |d       d| _        yt        j"                  |      r+d|d	g}t        j                  t         j                  |d       y|@t        j$                  |      s+d|d
g}t        j                  t         j                  |d       yd|
d|d|g}t        j                  t         j                  |d       || _        | j'                  |t(               |sy|(d}t        j                  t         j                  |d       y|j+                  ||
| j                         y)z=Sets the locus of focus (i.e., the object with visual focus).z/FOCUS MANAGER: Request to set locus of focus toTzSetting locus of focus.z@FOCUS MANAGER: Setting locus of focus to existing locus of focusNzSetting locus of focusz9FOCUS MANAGER: New locus of focus is null (being cleared)z#FOCUS MANAGER: New locus of focus (z) is dead. Not updating.z) is invalid. Not updating.z+FOCUS MANAGER: Changing locus of focus fromr0   z	. Notify:)r<   zBFOCUS MANAGER: Cannot notify active script because there isn't one)r   r   r   r   is_table_cellr   clear_cacher   r   r   
getManagergetActiveScriptappget_applicationsource	getScriptsetActiveScriptr&   is_validr>   r3   locusOfFocusChanged)r   eventr9   notify_scriptforcer!   	recursiver   scriptrI   	old_focuss              r   set_locus_of_focuszFocusManager.set_locus_of_focus   s/    DSI%**FD$?  --c2	S)-FG+TCu//d;  **,<<>fVZZ**5<<8C#..0::3MF%%'77@XYKK	I&I;MCu//d;DKC ;SB\]Fe..=$$S)?Fcd!!%"2"2FDA?[-9%**FD9  > :>VCu//d;""5)T[[Ar   c                     t        j                  | j                  dd       t        j                  | j                        }d| j                  d|g}t        j                  t
        j                  |d       |S )zKReturns True if the window we think is currently active is actually active.Fz,Ensuring the active window is really active.FOCUS MANAGER:z
is active:T)r   rF   r   r   	is_activer   r   r   )r   rY   r!   s      r   active_window_is_activez$FocusManager.active_window_is_active   s_     	T\\52`a))$,,7	"DLL,	J%**FD9r   c                 |    t        j                  |      syt        j                  |      j	                  d      dk(  S )z0Returns True if this object is the desktop frameFz
is-desktoptrue)r   is_framer   get_attributes_dictget)r   windows     r   _is_desktop_framezFocusManager._is_desktop_frame   s6     ##F+++F377EOOr   c                 @   |yt        j                  |dd       t        j                  |      }d|d|g}t        j                  |      s7|j                  d       t        j                  t        j                  |d       yt        j                  |      s7|j                  d       t        j                  t        j                  |d       yt        j                  |      r7|j                  d       t        j                  t        j                  |d       yt        j                  |      d	k(  r7|j                  d
       t        j                  t        j                  |d       y|j                  d       t        j                  t        j                  |d       y)zCReturns True if window can be the active window based on its state.Fz+Checking if window can be the active windowrX   fromzlacks active stateTzlacks showing statezis iconifiedzmutter-x11-framesz3is from app that cannot have the real active windowzcan be active window)r   rF   rJ   r   rY   appendr   r   r   
is_showingis_iconifiedget_name)r   r`   rI   r!   s       r   can_be_active_windowz!FocusManager.can_be_active_window   sB    >VU,YZ&&v."FFC8$$V,MM./e..=%%f-MM/0e..=##F+MM.)e..=S!%88MMOPe..=,-%**FD9r   c           	          g }|xs t        j                  d      }|D ]:  }|j                  t        t	        j
                  || j                                     < |s*d|g}t        j                  t        j                  |d       yt        |      dk(  r1d|d   g}t        j                  t        j                  |d       |d   S d|g}t        j                  t        j                  |d       g d	}g }|D ]h  }t	        j                  t	        j                  |            |v r+d
|dg}t        j                  t        j                  |d       X|j                  |       j t        |      dk(  r1d|d   g}t        j                  t        j                  |d       |d   S d}|r.d|g}t        j                  t        j                  |d       |d   }d|dg}t        j                  t        j                  |d       |S )z:Tries to locate the active window; may or may not succeed.T)must_have_windowz0FOCUS MANAGER: Unable to find active window fromNr   FOCUS MANAGER: Active window isr.   z4FOCUS MANAGER: These windows all claim to be active:)slackdiscordzoutline-clientzwhatsapp-desktop-linuxzFOCUS MANAGER: Suspectingzis a non-active Electron appz.FOCUS MANAGER: Active window is believed to bez2FOCUS MANAGER: Still have multiple active windows:zFOCUS MANAGER: Returningzas active window)r   get_all_applicationsextendlistr   iter_childrenrh   r   r   r   lenrg   rJ   rd   )	r   apps
candidatesrI   r!   suspect_app_namesfilteredframeguesss	            r   find_active_windowzFocusManager.find_active_window  s    
N{77N 	\Cd8#9#9#t?X?X#YZ[	\ H$OFe..=z?a7AGFe..=a= H*U%**FD97  	'E  !9!9%!@AEVV5u>\]!!%"2"2FDA&	' x=AFQRTFe..=A;JHUFe..=QKE,e5GH%**FD9r   c                     d| j                   g}t        j                  t        j                  |dd       | j                   S )zJReturns the currently-active window (i.e. without searching or verifying).rk   T)r   r   r   r   r@   s     r   get_active_windowzFocusManager.get_active_windowD  s6     4T\\B%**FD$?||r   c                    d|g}||j                  d|g       t        j                  t        j                  |d       || j                  k(  r(d}t        j
                  t        j                  |d       nb|d| _        nXt        j                  ||      \  }}||k7  r3d|d|g}t        j                  t        j                  |d       || _        n|| _        |r| j                  d| j                  |       y| j                  r}| j                  rp| j                         s_d| j                  d| j                  g}t        j                  t        j                  |dd       | j                  d| j                  d	       yyyy)
zSets the active window.z.FOCUS MANAGER: Request to set active window toNinTz>FOCUS MANAGER: Setting active window to existing active windowz*FOCUS MANAGER: Correcting active window tozFOCUS MANAGER: Focusz	is not in)rQ   )ro   r   r   r   r   r   r   find_real_app_and_window_forrV   r   r,   )	r   rw   rI   set_window_as_focusrQ   r!   r   real_app
real_frames	            r   set_active_windowzFocusManager.set_active_windowK  sK    CEJ?MM4+&%**FD9DLL RCu//d;]DL#+#H#HPS#T HjU"F
TXZbc!!%"2"2FDA)$##D$,,F\\dkk$2P2P2R,dkk;UFe..dC##D$,,d#K 3Sk\r   )r/   )NNN)TF)NFF)__name__
__module____qualname____doc__r   r   r"   r$   r'   r)   r,   r>   rA   rC   rV   rZ   ra   rh   ry   r{   r    r   r   r
   r
   .   sb    2!
!+[
%+N;;BzP D0dLr   r
   c                      t         S r   )_managerr   r   r   rG   rG   j  s    Or   N)r   __id____version____date____copyright____license__r/   r   r   r   	ax_objectr   ax_utilitiesr   CARET_TRACKINGr3   r6   MOUSE_REVIEWOBJECT_NAVIGATORSAY_ALLr
   r   rG   r   r   r   <module>r      sp   . 87     %!!% 
xL xLv	 >r   