o
    �õ±j†6  ã                   @  s¢   d Z ddlmZ ddl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B eB e	B ZG d	d
„ d
ƒZG dd„ deƒZdddd„ZdS )z
Wrapper for the layout.
é    )Úannotations)Ú	GeneratorÚIterable)ÚBufferé   )ÚAnyContainerÚConditionalContainerÚ	ContainerÚWindowÚto_container)ÚBufferControlÚSearchBufferControlÚ	UIControl)ÚLayoutÚInvalidLayoutErrorÚwalkc                   @  s@  e Zd ZdZ	dLdMd	d
„ZdNdd„ZdOdd„ZdPdd„ZdQdd„ZdRdd„Z	e
dSdd„ƒZejdTdd„ƒZe
dUd!d"„ƒZejdVd#d"„ƒZe
dWd$d%„ƒZe
dXd'd(„ƒZdYd*d+„ZdZd-d.„Ze
d[d0d1„ƒZd\d3d4„Ze
dWd5d6„ƒZe
dSd7d8„ƒZd]d9d:„Zd]d;d<„Zd]d=d>„Zd^d@dA„Zd^dBdC„Zd]dDdE„Zd]dFdG„Zd_dJdK„ZdS )`r   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              
   C  st   t |ƒ| _g | _i | _i | _|d u r0z| j t|  ¡ ƒ¡ W n ty/ } zt	dƒ|‚d }~w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   Úe© r"   ú�/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/prompt_toolkit/layout/layout.pyÚ__init__(   s"   
ÿþ€ÿ

zLayout.__init__Ústrc                 C  s   d| j ›d| j›d�S )NzLayout(z, current_window=ú))r   Úcurrent_window©r    r"   r"   r#   Ú__repr__J   s   zLayout.__repr__úGenerator[Window, None, None]c                 c  s$   � |   ¡ D ]
}t|tƒr|V  qdS )zJ
        Find all the :class:`.UIControl` objects in this layout.
        N)r   Ú
isinstancer
   )r    Úitemr"   r"   r#   r   M   s   €
€þzLayout.find_all_windowsúIterable[UIControl]c                 c  s   � |   ¡ D ]}|jV  qd S ©N)r   Úcontent©r    r   r"   r"   r#   Úfind_all_controlsU   s   €
ÿzLayout.find_all_controlsÚvalueÚFocusableElementc                 C  s€  t |tƒr'|  ¡ D ]}t |tƒr|jj|kr|  |¡  dS q	td|›d�ƒ‚t |tƒrM|  ¡ D ]}t |tƒrD|j|krD|  |¡  dS q0td|›d�ƒ‚t |t	ƒri||  ¡ vr\tdƒ‚| 
¡ sdtdƒ‚|| _dS t|ƒ}t |tƒr„||  ¡ vrtd|›�ƒ‚|| _dS g }t|dd�D ]}t |tƒr�|j 
¡ r�| |¡ qŒt| jƒD ]}||v r¯|| _ dS q£|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: )r+   r%   r1   r   ÚbufferÚnamer   Ú
ValueErrorr   r   Úis_focusableÚcurrent_controlr   r
   r   r'   r   r/   r   Úreversedr   )r    r2   ÚcontrolÚwindowsÚcÚwr"   r"   r#   r   Y   sZ   

€

€
ÿ

ÿ

€þ
ÿzLayout.focusÚboolc                 C  sŠ   t |tƒr| jdu rdS | jj|kS t |tƒr| j|kS t |tƒr&| j|kS t|ƒ}t |tƒr4| j	|kS t
|ƒD ]
}|| j	krB dS q8dS )zˆ
        Check whether the given control has the focus.
        :param value: :class:`.UIControl` or :class:`.Window` instance.
        NFT)r+   r%   Úcurrent_bufferr8   r   r   r;   r   r
   r'   r   )r    r2   Úelementr"   r"   r#   Ú	has_focus§   s    








ÿzLayout.has_focusr   c                 C  s   | j d jS )zI
        Get the :class:`.UIControl` to currently has the focus.
        éÿÿÿÿ)r   r/   r(   r"   r"   r#   r;   À   s   zLayout.current_controlr=   c                 C  s,   |   ¡ D ]}|j|kr|| _ dS qtdƒ‚)zC
        Set the :class:`.UIControl` to receive the focus.
        Nz(Control not found in the user interface.)r   r/   r'   r9   )r    r=   Úwindowr"   r"   r#   r;   Ç   s   
þr
   c                 C  s
   | j d S )z=Return the :class:`.Window` object that is currently focused.rE   )r   r(   r"   r"   r#   r'   Ó   s   
zLayout.current_windowc                 C  s   | j  |¡ dS )z8Set the :class:`.Window` object to be currently focused.N)r   r   )r    r2   r"   r"   r#   r'   Ø   s   c                 C  s   | j | jv S )z#True if we are searching right now.)r;   r   r(   r"   r"   r#   Úis_searchingÝ   s   zLayout.is_searchingúBufferControl | Nonec                 C  s    | j }t|tƒr| j |¡S dS )zY
        Return the :class:`.BufferControl` in which we are searching or `None`.
        N)r;   r+   r   r   Úget)r    r=   r"   r"   r#   Úsearch_target_buffer_controlâ   s   
z#Layout.search_target_buffer_controlúIterable[Window]c                 c  s.   � |   ¡ D ]}t|tƒr|j ¡ r|V  qdS )zl
        Return all the :class:`.Window` objects which are focusable (in the
        'modal' area).
        N)Úwalk_through_modal_arear+   r
   r/   r:   )r    r@   r"   r"   r#   Úget_focusable_windowsð   s   €€þzLayout.get_focusable_windowsúlist[Window]c                   s   | j ‰ ‡ fdd„|  ¡ D ƒS )zO
        Return a list of :class:`.Window` objects that are focusable.
        c                   s   g | ]}|ˆ v r|‘qS r"   r"   )Ú.0r@   ©r   r"   r#   Ú
<listcomp>   s    z8Layout.get_visible_focusable_windows.<locals>.<listcomp>)r   rM   r(   r"   rP   r#   Úget_visible_focusable_windowsù   s   z$Layout.get_visible_focusable_windowsúBuffer | Nonec                 C  s   | j }t|tƒr|jS dS )zD
        The currently focused :class:`~.Buffer` or `None`.
        N)r;   r+   r   r7   ©r    Ú
ui_controlr"   r"   r#   rB     s   
zLayout.current_bufferÚbuffer_namec                 C  sB   |   ¡ D ]}t|tƒrt|jtƒr|jjj|kr|jj  S qdS )zt
        Look in the layout for a buffer with the given name.
        Return `None` when nothing was found.
        N)r   r+   r
   r/   r   r7   r8   )r    rV   r@   r"   r"   r#   Úget_buffer_by_name  s   €zLayout.get_buffer_by_namec                 C  s   | 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.)
        )r;   r+   r   rT   r"   r"   r#   Úbuffer_has_focus  s   
zLayout.buffer_has_focusc                 C  s.   z| j d jW S  ty   | j d j Y S w )zJ
        Get the :class:`.UIControl` to previously had the focus.
        éþÿÿÿrE   )r   r/   Ú
IndexErrorr(   r"   r"   r#   Úprevious_control!  s
   ÿzLayout.previous_controlc                 C  s&   t | jƒdkr| jdd… | _dS dS )z=
        Give the focus to the last focused control.
        r   NrE   )Úlenr   r(   r"   r"   r#   Ú
focus_last+  s   ÿzLayout.focus_lastc                 C  sb   |   ¡ }t|ƒdkr/z| | j¡}W n ty   d}Y n	w |d t|ƒ }|  || ¡ dS dS )z:
        Focus the next visible/focusable Window.
        r   r   N©rR   r\   Úindexr'   r9   r   ©r    r>   r_   r"   r"   r#   Ú
focus_next2  ó   ÿøzLayout.focus_nextc                 C  sb   |   ¡ }t|ƒdkr/z| | j¡}W n ty   d}Y n	w |d t|ƒ }|  || ¡ dS dS )z>
        Focus the previous visible/focusable Window.
        r   r   Nr^   r`   r"   r"   r#   Úfocus_previousB  rb   zLayout.focus_previousúIterable[Container]c                 c  s   � t | jƒE dH  dS )zX
        Walk through all the layout nodes (and their children) and yield them.
        N)r   r   r(   r"   r"   r#   r   R  s   €zLayout.walkc                 c  sH   � | j }| ¡ s|| jv r| j| }| ¡ s|| jv st|ƒE dH  dS )zn
        Walk through all the containers which are in the current 'modal' part
        of the layout.
        N)r'   Úis_modalr   r   )r    Úrootr"   r"   r#   rL   X  s   €
ÿzLayout.walk_through_modal_areac                   s(   i ‰ d‡ ‡fdd„‰ˆ| j ƒ ˆ | _dS )	z=
        Update child->parent relationships mapping.
        r!   r	   r   r   c                   s"   |   ¡ D ]
}| ˆ |< ˆ|ƒ qd S r.   )Úget_children)r!   r?   ©Úparentsr   r"   r#   r   k  s   
þz-Layout.update_parents_relations.<locals>.walkN)r!   r	   r   r   )r   r   r(   r"   rh   r#   Úupdate_parents_relationse  s   

zLayout.update_parents_relationsc                 C  s   | j  ¡  | j ¡  d S r.   )r   Úclearr   Úresetr(   r"   r"   r#   rl   t  s   
zLayout.resetr	   úContainer | Nonec                 C  s"   z| j | W S  ty   Y dS w )zo
        Return the parent container for the given container, or ``None``, if it
        wasn't found.
        N)r   ÚKeyErrorr0   r"   r"   r#   Ú
get_parent}  s
   ÿzLayout.get_parentr.   )r   r   r   r   r   r   )r   r%   )r   r*   )r   r-   )r2   r3   r   r   )r2   r3   r   rA   )r   r   )r=   r   r   r   )r   r
   )r2   r
   r   r   )r   rA   )r   rH   )r   rK   )r   rN   )r   rS   )rV   r%   r   rS   )r   r   )r   rd   )r   r	   r   rm   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r$   r)   r   r1   r   rD   Úpropertyr;   Úsetterr'   rG   rJ   rM   rR   rB   rW   rX   r[   r]   ra   rc   r   rL   rj   rl   ro   r"   r"   r"   r#   r      sL    ý
"



N

		
		
	





	r   c                   @  s   e Zd ZdS )r   N)rp   rq   rr   r"   r"   r"   r#   r   ˆ  s    r   Fr   r	   r6   rA   r   rd   c                 c  sF   � |rt | tƒr|  ¡ sdS | V  |  ¡ D ]}t||d�E dH  qdS )z:
    Walk through layout, starting at this container.
    Nr5   )r+   r   Úfilterrg   r   )r   r6   r?   r"   r"   r#   r   Œ  s   €ÿþýþr   N)F)r   r	   r6   rA   r   rd   )rs   Ú
__future__r   Úcollections.abcr   r   Úprompt_toolkit.bufferr   Ú
containersr   r   r	   r
   r   Úcontrolsr   r   r   Ú__all__r%   r3   r   Ú	Exceptionr   r   r"   r"   r"   r#   Ú<module>   s      m