
     h6                        d Z ddlmZ ddlmZmZmZmZmZm	Z	 ddl
mZ ddlmZmZmZmZmZ ddlmZmZmZ g dZe	eeeef         Z G d	 d
          Z G d de          ZdddZdS )z
Wrapper for the layout.
    )annotations)Dict	GeneratorIterableListOptionalUnion)Buffer   )AnyContainerConditionalContainer	ContainerWindowto_container)BufferControlSearchBufferControl	UIControl)LayoutInvalidLayoutErrorwalkc                     e Zd ZdZ	 d4d5d	Zd6dZd7dZd8dZd9dZd:dZ	e
d;d            Zej        d<d            Ze
d=d            Zej        d>d            Ze
d?d            Ze
d@d            ZdAd ZdBd"Ze
dCd$            ZdDd&Ze
d?d'            Ze
d;d(            ZdEd)ZdEd*ZdEd+ZdFd-ZdFd.ZdEd/ZdEd0ZdGd3ZdS )Hr   aL  
    The layout for a prompt_toolkit
    :class:`~prompt_toolkit.application.Application`.
    This also keeps track of which user control is focused.

    :param container: The "root" container for the layout.
    :param focused_element: element to be focused initially. (Can be anything
        the `focus` function accepts.)
    N	containerr   focused_elementFocusableElement | NonereturnNonec                N   t          |          | _        g | _        i | _        i | _        |]	 | j                            t          |                                                      n7# t          $ r}t          d          |d }~ww xY w| 
                    |           g | _        d S )Nz>Invalid layout. The layout does not contain any Window object.)r   r   _stacksearch_links_child_to_parentappendnextfind_all_windowsStopIterationr   focusvisible_windows)selfr   r   es       X/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/prompt_toolkit/layout/layout.py__init__zLayout.__init__'   s    
 &i00$& GI
 =?"""4(=(=(?(?#@#@AAAA    (T 
 JJ''' .0s   9A' '
B1BBstrc                (    d| j         d| j        dS )NzLayout(z, current_window=))r   current_windowr'   s    r)   __repr__zLayout.__repr__I   s     TTTD<OTTTT    Generator[Window, None, None]c              #  j   K   |                                  D ]}t          |t                    r|V  dS )zJ
        Find all the :class:`.UIControl` objects in this layout.
        N)r   
isinstancer   )r'   items     r)   r#   zLayout.find_all_windowsL   sE       IIKK 	 	D$'' 


	 	r1   Iterable[UIControl]c              #  J   K   |                                  D ]}|j        V  d S N)r#   contentr'   r   s     r)   find_all_controlszLayout.find_all_controlsT   s=      ..00 	$ 	$I#####	$ 	$r1   valueFocusableElementc                   t          |t                    rg|                                 D ]?}t          |t                    r(|j        j        |k    r|                     |            dS @t          d|d          t          |t                    rb|                                 D ]:}t          |t                    r#|j        |k    r|                     |            dS ;t          d|d          t          |t                    rQ||                                 vrt          d          |
                                st          d          || _        dS t          |          }t          |t                    r1||                                 vrt          d|          || _        dS g }t!          |d          D ]E}t          |t                    r.|j        
                                r|                    |           Ft'          | j                  D ]}||v r
|| _         dS |r|d	         | _        dS t          d
|          )a  
        Focus the given UI element.

        `value` can be either:

        - a :class:`.UIControl`
        - a :class:`.Buffer` instance or the name of a :class:`.Buffer`
        - a :class:`.Window`
        - Any container object. In this case we will focus the :class:`.Window`
          from this container that was focused most recent, or the very first
          focusable :class:`.Window` of the container.
        Nz,Couldn't find Buffer in the current layout: .z7Invalid value. Container does not appear in the layout.z*Invalid value. UIControl is not focusable.z5Invalid value. Window does not appear in the layout: Tskip_hiddenr   z,Invalid value. Container cannot be focused: )r4   r+   r;   r   buffernamer%   
ValueErrorr
   r   is_focusablecurrent_controlr   r   r#   r.   r   r9   r!   reversedr   )r'   r<   controlwindowscws         r)   r%   zLayout.focusX   s    eS!! ?	1133  g}55 '.:MQV:V:VJJw'''FFVEVVVWWW v&& 7	1133  g}55 '.E:Q:QJJw'''FFVEVVVWWW y)) /	D224444 M   %%'' O !MNNN#(D    !''E%(( ! 5 5 7 777$* 5#  
 ',### e666 * *A!!V,, *1G1G1I1I *q))) "$+..  AG||./+ $
  *1!*D'F L5LL  r1   boolc                   t          |t                    r| j        dS | j        j        |k    S t          |t                    r| j        |k    S t          |t
                    r| j        |k    S t          |          }t          |t                    r| j	        |k    S t          |          D ]}|| j	        k    r dS dS )z
        Check whether the given control has the focus.
        :param value: :class:`.UIControl` or :class:`.Window` instance.
        NFT)r4   r+   current_bufferrC   r
   r   rF   r   r   r.   r   )r'   r<   elements      r)   	has_focuszLayout.has_focus   s    
 eS!! 	5"*u&+u44eV$$ 	0&%//eY'' 	'500 ''E%(( *e33  $E{{ $ $G$"555#tt 6ur1   r   c                &    | j         d         j        S )zI
        Get the :class:`.UIControl` to currently has the focus.
        )r   r9   r/   s    r)   rF   zLayout.current_control   s    
 {2&&r1   rH   c                x    |                                  D ]}|j        |k    r
|| _         dS t          d          )zC
        Set the :class:`.UIControl` to receive the focus.
        Nz(Control not found in the user interface.)r#   r9   r.   rD   )r'   rH   windows      r)   rF   zLayout.current_control   sQ    
 ++-- 	 	F~((&,# ) CDDDr1   r   c                    | j         d         S )z=Return the :class:`.Window` object that is currently focused.rR   )r   r/   s    r)   r.   zLayout.current_window   s     {2r1   c                :    | j                             |           dS )z8Set the :class:`.Window` object to be currently focused.N)r   r!   )r'   r<   s     r)   r.   zLayout.current_window   s      	5!!!!!r1   c                    | j         | j        v S )z#True if we are searching right now.)rF   r   r/   s    r)   is_searchingzLayout.is_searching   s     #t'888r1   BufferControl | Nonec                r    | j         }t          |t                    r| j                            |          S dS )zY
        Return the :class:`.BufferControl` in which we are searching or `None`.
        N)rF   r4   r   r   get)r'   rH   s     r)   search_target_buffer_controlz#Layout.search_target_buffer_control   s;     &g233 	$((1114r1   Iterable[Window]c              #     K   |                                  D ]4}t          |t                    r|j                                        r|V  5dS )zl
        Return all the :class:`.Window` objects which are focusable (in the
        'modal' area).
        N)walk_through_modal_arear4   r   r9   rE   )r'   rK   s     r)   get_focusable_windowszLayout.get_focusable_windows   s^      
 --// 	 	A!V$$ )?)?)A)A 	 	r1   list[Window]c                R    | j         fd|                                 D             S )zO
        Return a list of :class:`.Window` objects that are focusable.
        c                    g | ]}|v |	S  rd   ).0rK   r&   s     r)   
<listcomp>z8Layout.get_visible_focusable_windows.<locals>.<listcomp>   s#    PPPa1;O;O;O;O;Or1   )r&   r`   )r'   r&   s    @r)   get_visible_focusable_windowsz$Layout.get_visible_focusable_windows   s3     .PPPP45577PPPPr1   Buffer | Nonec                L    | j         }t          |t                    r|j        S dS )zD
        The currently focused :class:`~.Buffer` or `None`.
        N)rF   r4   r   rB   r'   
ui_controls     r)   rN   zLayout.current_buffer  s,    
 )
j-00 	%$$tr1   buffer_namec                    |                                  D ]T}t          |t                    r=t          |j        t                    r#|j        j        j        |k    r|j        j        c S UdS )zt
        Look in the layout for a buffer with the given name.
        Return `None` when nothing was found.
        N)r   r4   r   r9   r   rB   rC   )r'   rl   rK   s      r)   get_buffer_by_namezLayout.get_buffer_by_name  sj    
  	, 	,A!V$$ ,AI})M)M ,9#(K779++++tr1   c                :    | j         }t          |t                    S )z
        Return `True` if the currently focused control is a
        :class:`.BufferControl`. (For instance, used to determine whether the
        default key bindings should be active or not.)
        )rF   r4   r   rj   s     r)   buffer_has_focuszLayout.buffer_has_focus  s     )
*m444r1   c                l    	 | j         d         j        S # t          $ r | j         d         j        cY S w xY w)zJ
        Get the :class:`.UIControl` to previously had the focus.
        rR   )r   r9   
IndexErrorr/   s    r)   previous_controlzLayout.previous_control!  sF    
	+;r?** 	+ 	+ 	+;r?****	+s    33c                b    t          | j                  dk    r| j        dd         | _        dS dS )z=
        Give the focus to the last focused control.
        r   NrR   )lenr   r/   s    r)   
focus_lastzLayout.focus_last+  s7     t{a+crc*DKKK  r1   c                   |                                  }t          |          dk    r`	 |                    | j                  }|dz   t          |          z  }n# t          $ r d}Y nw xY w|                     ||                    dS dS )z:
        Focus the next visible/focusable Window.
        r   r   Nrg   rv   indexr.   rD   r%   r'   rI   rz   s      r)   
focus_nextzLayout.focus_next2       4466w<<!3d&9:: c'll2    
 JJwu~&&&&&    A A('A(c                   |                                  }t          |          dk    r`	 |                    | j                  }|dz
  t          |          z  }n# t          $ r d}Y nw xY w|                     ||                    dS dS )z>
        Focus the previous visible/focusable Window.
        r   r   Nry   r{   s      r)   focus_previouszLayout.focus_previousB  r}   r~   Iterable[Container]c              #  >   K   t          | j                  E d{V  dS )zX
        Walk through all the layout nodes (and their children) and yield them.
        N)r   r   r/   s    r)   r   zLayout.walkR  s0       '''''''''''r1   c              #     K   | j         }|                                s3|| j        v r*| j        |         }|                                s	|| j        v *t          |          E d{V  dS )zn
        Walk through all the containers which are in the current 'modal' part
        of the layout.
        N)r.   is_modalr    r   )r'   roots     r)   r_   zLayout.walk_through_modal_areaX  s       ---// 	/dd.C&C&C(.D --// 	/dd.C&C&C ::r1   c                J    i dfd | j                    | _        dS )z=
        Update child->parent relationships mapping.
        r(   r   r   r   c                V    |                                  D ]}| |<    |           d S r8   )get_children)r(   rJ   parentsr   s     r)   r   z-Layout.update_parents_relations.<locals>.walkk  s>    ^^%%  
Q r1   N)r(   r   r   r   )r   r    )r'   r   r   s    @@r)   update_parents_relationszLayout.update_parents_relationse  sP     	 	 	 	 	 	 	
 	T^ 'r1   c                j    | j                                          | j                                         d S r8   )r   clearr   resetr/   s    r)   r   zLayout.resett  s3    
 	!!!r1   r   Container | Nonec                @    	 | j         |         S # t          $ r Y dS w xY w)zo
        Return the parent container for the given container, or ``None``, if it
        wasn't found.
        N)r    KeyErrorr:   s     r)   
get_parentzLayout.get_parent}  s7    
	(33 	 	 	44	s    
r8   )r   r   r   r   r   r   )r   r+   )r   r2   )r   r6   )r<   r=   r   r   )r<   r=   r   rL   )r   r   )rH   r   r   r   )r   r   )r<   r   r   r   )r   rL   )r   rY   )r   r]   )r   ra   )r   rh   )rl   r+   r   rh   )r   r   )r   r   )r   r   r   r   )__name__
__module____qualname____doc__r*   r0   r#   r;   r%   rP   propertyrF   setterr.   rX   r\   r`   rg   rN   rn   rp   rt   rw   r|   r   r   r_   r   r   r   rd   r1   r)   r   r      s         48 0  0  0  0  0DU U U U   $ $ $ $M M M M^   2 ' ' ' X' 	E 	E 	E 	E    X " " " " 9 9 9 X9    X   Q Q Q Q    X	 	 	 	 5 5 5 X5 + + + X++ + + +' ' ' ' ' ' ' ' ( ( ( (   ( ( ( (        r1   r   c                      e Zd ZdS )r   N)r   r   r   rd   r1   r)   r   r     s        Dr1   r   Fr   r   rA   rL   r   r   c              #     K   |r+t          | t                    r|                                 sdS | V  |                                 D ]}t	          ||          E d{V  dS )z:
    Walk through layout, starting at this container.
    Nr@   )r4   r   filterr   r   )r   rA   rJ   s      r)   r   r     s       	y"677   ""
 	
OOO##%% 4 4{333333333334 4r1   N)F)r   r   rA   rL   r   r   )r   
__future__r   typingr   r   r   r   r   r	   prompt_toolkit.bufferr
   
containersr   r   r   r   r   controlsr   r   r   __all__r+   r=   r   	Exceptionr   r   rd   r1   r)   <module>r      sq    # " " " " " C C C C C C C C C C C C C C C C ( ( ( ( ( (              D C C C C C C C C C   fi=> i i i i i i i iX	 	 	 	 	 	 	 	4 4 4 4 4 4 4r1   