
    Mh6                        d Z ddlmZ ddl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)	GeneratorIterable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       \/var/www/html/test/jupyter/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__J   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_windowsM   sE       IIKK 	 	D$'' 


	 	r.   Iterable[UIControl]c              #  J   K   |                                  D ]}|j        V  d S N)r    contentr$   r   s     r&   find_all_controlszLayout.find_all_controlsU   s=      ..00 	$ 	$I#####	$ 	$r.   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: )r1   r(   r8   r   buffernamer"   
ValueErrorr   r   is_focusablecurrent_controlr   r   r    r+   r   r6   r   reversedr   )r$   r9   controlwindowscws         r&   r"   zLayout.focusY   s    eS!! >	1133  g}55 '.:MQV:V:VJJw'''FFVEVVVWWW v&& 6	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$YPUYY   ',### e666 * *A!!V,, *1G1G1I1I *q))) "$+..  AG||./+ $
  *1!*D'F L5LL  r.   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)r1   r(   current_bufferr@   r   r   rC   r   r   r+   r   )r$   r9   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ur.   r   c                &    | j         d         j        S )zI
        Get the :class:`.UIControl` to currently has the focus.
        )r   r6   r,   s    r&   rC   zLayout.current_control   s    
 {2&&r.   rE   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    r6   r+   rA   )r$   rE   windows      r&   rC   zLayout.current_control   sQ    
 ++-- 	 	F~((&,# ) CDDDr.   r   c                    | j         d         S )z=Return the :class:`.Window` object that is currently focused.rO   )r   r,   s    r&   r+   zLayout.current_window   s     {2r.   c                :    | j                             |           dS )z8Set the :class:`.Window` object to be currently focused.N)r   r   )r$   r9   s     r&   r+   zLayout.current_window   s      	5!!!!!r.   c                    | j         | j        v S )z#True if we are searching right now.)rC   r   r,   s    r&   is_searchingzLayout.is_searching   s     #t'888r.   BufferControl | Nonec                r    | j         }t          |t                    r| j                            |          S dS )zY
        Return the :class:`.BufferControl` in which we are searching or `None`.
        N)rC   r1   r   r   get)r$   rE   s     r&   search_target_buffer_controlz#Layout.search_target_buffer_control   s;     &g233 	$((1114r.   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_arear1   r   r6   rB   )r$   rH   s     r&   get_focusable_windowszLayout.get_focusable_windows   s^      
 --// 	 	A!V$$ )?)?)A)A 	 	r.   list[Window]c                R    | j         fd|                                 D             S )zO
        Return a list of :class:`.Window` objects that are focusable.
        c                    g | ]}|v |	S  ra   ).0rH   r#   s     r&   
<listcomp>z8Layout.get_visible_focusable_windows.<locals>.<listcomp>   s#    PPPa1;O;O;O;O;Or.   )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Pr.   Buffer | Nonec                L    | j         }t          |t                    r|j        S dS )zD
        The currently focused :class:`~.Buffer` or `None`.
        N)rC   r1   r   r?   r$   
ui_controls     r&   rK   zLayout.current_buffer  s,    
 )
j-00 	%$$tr.   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   r1   r   r6   r   r?   r@   )r$   ri   rH   s      r&   get_buffer_by_namezLayout.get_buffer_by_name  sj    
  	, 	,A!V$$ ,AI})M)M ,9#(K779++++tr.   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.)
        )rC   r1   r   rg   s     r&   buffer_has_focuszLayout.buffer_has_focus  s     )
*m444r.   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.
        rO   )r   r6   
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   NrO   )lenr   r,   s    r&   
focus_lastzLayout.focus_last+  s7     t{a+crc*DKKK  r.   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rd   rs   indexr+   rA   r"   r$   rF   rw   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   Nrv   rx   s      r&   focus_previouszLayout.focus_previousB  rz   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       '''''''''''r.   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 ::r.   c                J    i dfd | j                    | _        dS )z=
        Update child->parent relationships mapping.
        r%   r   r   r   c                V    |                                  D ]}| |<    |           d S r5   )get_children)r%   rG   parentsr   s     r&   r   z-Layout.update_parents_relations.<locals>.walkk  s>    ^^%%  
Q r.   N)r%   r   r   r   )r   r   )r$   r   r   s    @@r&   update_parents_relationszLayout.update_parents_relationse  sP     	 	 	 	 	 	 	
 	T^ 'r.   c                j    | j                                          | j                                         d S r5   )r   clearr   resetr,   s    r&   r   zLayout.resett  s3    
 	!!!r.   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   KeyErrorr7   s     r&   
get_parentzLayout.get_parent}  s7    
	(33 	 	 	44	s    
r5   )r   r	   r   r   r   r   )r   r(   )r   r/   )r   r3   )r9   r:   r   r   )r9   r:   r   rI   )r   r   )rE   r   r   r   )r   r   )r9   r   r   r   )r   rI   )r   rV   )r   rZ   )r   r^   )r   re   )ri   r(   r   re   )r   r   )r   r~   )r   r   r   r   )__name__
__module____qualname____doc__r'   r-   r    r8   r"   rM   propertyrC   setterr+   rU   rY   r]   rd   rK   rk   rm   rq   rt   ry   r}   r   r\   r   r   r   ra   r.   r&   r   r      s         48 0  0  0  0  0DU U U U   $ $ $ $L L L L\   2 ' ' ' X' 	E 	E 	E 	E    X " " " " 9 9 9 X9    X   Q Q Q Q    X	 	 	 	 5 5 5 X5 + + + X++ + + +' ' ' ' ' ' ' ' ( ( ( (   ( ( ( (        r.   r   c                      e Zd ZdS )r   N)r   r   r   ra   r.   r&   r   r     s        Dr.   r   Fr   r   r>   rI   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=   )r1   r
   filterr   r   )r   r>   rG   s      r&   r   r     s       	y"677   ""
 	
OOO##%% 4 4{333333333334 4r.   N)F)r   r   r>   rI   r   r~   )r   
__future__r   typingr   r   r   prompt_toolkit.bufferr   
containersr	   r
   r   r   r   controlsr   r   r   __all__r(   r:   r   	Exceptionr   r   ra   r.   r&   <module>r      s_    # " " " " " - - - - - - - - - - ( ( ( ( ( (              D C C C C C C C C C   fi=> h h h h h h h hV	 	 	 	 	 	 	 	4 4 4 4 4 4 4r.   