
    f6                        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y)z
Wrapper for the layout.
    )annotations)	GeneratorIterableUnion)Buffer   )AnyContainerConditionalContainer	ContainerWindowto_container)BufferControlSearchBufferControl	UIControl)LayoutInvalidLayoutErrorwalkc                  p   e Zd ZdZ	 d	 	 	 	 	 ddZddZd dZd!dZd"d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d*dZd+dZe
d,d       Zd-dZe
d(d       Ze
d$d       Zd.dZd.dZd.dZd/dZd/dZd.dZd.dZd0dZy)1r   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.)
    Nc                2   t        |      | _        g | _        i | _        i | _        |;	 | j                  j                  t        | j                                      g | _        y | j                  |       g | _        y # t        $ r}t        d      |d }~ww xY w)Nz>Invalid layout. The layout does not contain any Window object.)r   	container_stacksearch_links_child_to_parentappendnextfind_all_windowsStopIterationr   focusvisible_windows)selfr   focused_elementes       W/var/www/cvtools/html/venv/lib/python3.12/site-packages/prompt_toolkit/layout/layout.py__init__zLayout.__init__(   s    
 &i0$& GI
 =?"""4(=(=(?#@A .0 JJ' .0 ! (Ts   2A< <	BBBc                <    d| j                   d| j                  dS )NzLayout(z, current_window=))r   current_windowr    s    r#   __repr__zLayout.__repr__J   s%    **;D<O<O;RRSTT    c              #  `   K   | j                         D ]  }t        |t              s|  yw)zJ
        Find all the :class:`.UIControl` objects in this layout.
        N)r   
isinstancer   )r    items     r#   r   zLayout.find_all_windowsM   s,      IIK 	D$'
	s   $..c              #  R   K   | j                         D ]  }|j                    y wN)r   contentr    r   s     r#   find_all_controlszLayout.find_all_controlsU   s*     ..0 	$I###	$s   %'c                   t        |t              ra| j                         D ]?  }t        |t              s|j                  j
                  |k(  s.| j                  |        y t        d|d      t        |t              rW| j                         D ]5  }t        |t              s|j                  |k(  s$| j                  |        y t        d|d      t        |t              r@|| j                         vrt        d      |j                         st        d      || _        yt        |      }t        |t              r(|| j                         vrt        d|      || _        yg }t!        |d      D ]?  }t        |t              s|j"                  j                         s/|j%                  |       A t'        | j(                        D ]  }||v s|| _         y |r|d	   | _        y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,   strr2   r   buffernamer   
ValueErrorr   r   is_focusablecurrent_controlr   r   r   r'   r   r0   r   reversedr   )r    valuecontrolwindowscws         r#   r   zLayout.focusY   s    eS!113 g}5'..:M:MQV:VJJw' KE9TUVWW v&113 g}5'..E:QJJw' KE9TUVWW y)D2244 M  %%' !MNN#(D  !'E%( 5 5 77$OPUyY  ',# e6 *A!!V,1G1G1Iq)*
 "$++. AG|./+ *1!*D' B5)L r*   c                   t        |t              r&| j                  y| 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(  s y y)z
        Check whether the given control has the focus.
        :param value: :class:`.UIControl` or :class:`.Window` instance.
        FT)r,   r7   current_bufferr9   r   r   r<   r   r   r'   r   )r    r>   elements      r#   	has_focuszLayout.has_focus   s    
 eS!""*&&++u44eV$&&%//eY'''500 'E%(**e33  $E{ $G$"5"55#$ r*   c                4    | j                   d   j                  S )zI
        Get the :class:`.UIControl` to currently has the focus.
        )r   r0   r(   s    r#   r<   zLayout.current_control   s    
 {{2&&&r*   c                r    | j                         D ]  }|j                  |k(  s|| _         y t        d      )zC
        Set the :class:`.UIControl` to receive the focus.
        Nz(Control not found in the user interface.)r   r0   r'   r:   )r    r?   windows      r#   r<   zLayout.current_control   sA    
 ++- 	F~~(&,#	
 CDDr*   c                     | j                   d   S )z=Return the :class:`.Window` object that is currently focused.rH   )r   r(   s    r#   r'   zLayout.current_window   s     {{2r*   c                :    | j                   j                  |       y)z8Set the :class:`.Window` object to be currently focused.N)r   r   )r    r>   s     r#   r'   zLayout.current_window   s     	5!r*   c                2    | j                   | j                  v S )z#True if we are searching right now.)r<   r   r(   s    r#   is_searchingzLayout.is_searching   s     ##t'8'888r*   c                r    | j                   }t        |t              r| j                  j	                  |      S y)zY
        Return the :class:`.BufferControl` in which we are searching or `None`.
        N)r<   r,   r   r   get)r    r?   s     r#   search_target_buffer_controlz#Layout.search_target_buffer_control   s4     &&g23$$((11r*   c              #     K   | j                         D ]2  }t        |t              s|j                  j	                         s/| 4 yw)zl
        Return all the :class:`.Window` objects which are focusable (in the
        'modal' area).
        N)walk_through_modal_arear,   r   r0   r;   )r    rB   s     r#   get_focusable_windowszLayout.get_focusable_windows   s>     
 --/ 	A!V$)?)?)A	s   $A	A	A	c                j    | j                   }| j                         D cg c]	  }||v s| c}S c c}w )zO
        Return a list of :class:`.Window` objects that are focusable.
        )r   rT   )r    r   rB   s      r#   get_visible_focusable_windowsz$Layout.get_visible_focusable_windows   s3     ..557Pa1;OPPPs   	00c                T    | j                   }t        |t              r|j                  S y)zD
        The currently focused :class:`~.Buffer` or `None`.
        N)r<   r,   r   r8   r    
ui_controls     r#   rD   zLayout.current_buffer  s(    
 ))
j-0$$$r*   c                    | j                         D ]i  }t        |t              st        |j                  t              s/|j                  j
                  j                  |k(  sS|j                  j
                  c S  y)zt
        Look in the layout for a buffer with the given name.
        Return `None` when nothing was found.
        N)r   r,   r   r0   r   r8   r9   )r    buffer_namerB   s      r#   get_buffer_by_namezLayout.get_buffer_by_name  s]    
  	,A!V$AII})M99##((K799+++	, r*   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.)
        )r<   r,   r   rX   s     r#   buffer_has_focuszLayout.buffer_has_focus  s     ))
*m44r*   c                    	 | 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.
        rH   )r   r0   
IndexErrorr(   s    r#   previous_controlzLayout.previous_control!  s>    
	+;;r?*** 	+;;r?***	+s    "A A c                ^    t        | j                        dkD  r| j                  dd | _        yy)z=
        Give the focus to the last focused control.
        r   NrH   )lenr   r(   s    r#   
focus_lastzLayout.focus_last+  s,     t{{a++cr*DK  r*   c                    | j                         }t        |      dkD  rB	 |j                  | j                        }|dz   t        |      z  }| j                  ||          yy# t        $ r d}Y #w xY w)z:
        Focus the next visible/focusable Window.
        r   r   NrV   rd   indexr'   r:   r   r    r@   rh   s      r#   
focus_nextzLayout.focus_next2  v     446w<!3d&9&9: c'l2JJwu~&      A" "A0/A0c                    | j                         }t        |      dkD  rB	 |j                  | j                        }|dz
  t        |      z  }| j                  ||          yy# t        $ r d}Y #w xY w)z>
        Focus the previous visible/focusable Window.
        r   r   Nrg   ri   s      r#   focus_previouszLayout.focus_previousB  rk   rl   c              #  J   K   t        | j                        E d{    y7 w)zX
        Walk through all the layout nodes (and their children) and yield them.
        N)r   r   r(   s    r#   r   zLayout.walkR  s      '''s   #!#c              #     K   | j                   }|j                         s<|| j                  v r.| j                  |   }|j                         s|| j                  v r.t        |      E d{    y7 w)zn
        Walk through all the containers which are in the current 'modal' part
        of the layout.
        N)r'   is_modalr   r   )r    roots     r#   rS   zLayout.walk_through_modal_areaX  s`      ----/dd.C.C&C((.D --/dd.C.C&C :s   AA1A1)A/*A1c                L    i dfd | j                          | _        y)z=
        Update child->parent relationships mapping.
        c                J    | j                         D ]  }| |<    |        y r/   )get_children)r"   rA   parentsr   s     r#   r   z-Layout.update_parents_relations.<locals>.walkk  s)    ^^% 
Qr*   N)r"   r   returnNone)r   r   )r    rv   r   s    @@r#   update_parents_relationszLayout.update_parents_relationse  s%     	
 	T^^ 'r*   c                l    | j                   j                          | j                  j                          y r/   )r   clearr   resetr(   s    r#   r|   zLayout.resett  s&    
 	!r*   c                @    	 | j                   |   S # t        $ r Y yw xY w)zo
        Return the parent container for the given container, or ``None``, if it
        wasn't found.
        N)r   KeyErrorr1   s     r#   
get_parentzLayout.get_parent}  s*    
	((33 		s    	r/   )r   r	   r!   zFocusableElement | Nonerw   rx   )rw   r7   )rw   zGenerator[Window, None, None])rw   zIterable[UIControl])r>   FocusableElementrw   rx   )r>   r   rw   bool)rw   r   )r?   r   rw   rx   )rw   r   )r>   r   rw   rx   )rw   r   )rw   zBufferControl | None)rw   zIterable[Window])rw   zlist[Window])rw   Buffer | None)r[   r7   rw   r   )rw   rx   )rw   Iterable[Container])r   r   rw   zContainer | None)__name__
__module____qualname____doc__r$   r)   r   r2   r   rF   propertyr<   setterr'   rN   rQ   rT   rV   rD   r\   r^   rb   re   rj   rn   r   rS   ry   r|   r    r*   r#   r   r      sD    48 0 0 1 0 
	 0DU$L\2 ' ' 	E 	E   " " 9 9  Q  	 5 5 + ++' ' ((r*   r   c                      e Zd Zy)r   N)r   r   r   r   r*   r#   r   r     s    r*   r   c              #     K   |r!t        | t              r| j                         sy|  | j                         D ]  }t	        ||      E d{     y7 w)z:
    Walk through layout, starting at this container.
    Nr5   )r,   r
   filterru   r   )r   r6   rA   s      r#   r   r     sU      	y"67  "
O##% 4{33343s   AAAAN)F)r   r   r6   r   rw   r   )r   
__future__r   typingr   r   r   prompt_toolkit.bufferr   
containersr	   r
   r   r   r   controlsr   r   r   __all__r7   r   r   	Exceptionr   r   r   r*   r#   <module>r      sf    # - - (  D C fi=> h hV	 	4r*   