o
    �õ±j@K  ã                   @  sª   d dl mZ d dlmZ d dlmZmZ d dlmZ d dl	m
Z
 ddlmZmZ ddlmZmZmZmZ dd	lmZmZ dd
lmZmZmZ dgZdZG dd„ deƒZdS )é    )Úannotations)ÚPoint)ÚFilterOrBoolÚ	to_filter)ÚKeyBindingsBase)Ú
MouseEventé   )Ú	ContainerÚScrollOffsets)ÚAnyDimensionÚ	DimensionÚsum_layout_dimensionsÚto_dimension)ÚMouseHandlerÚMouseHandlers)ÚCharÚScreenÚWritePositionÚScrollablePanei'  c                
   @  s¼   e Zd ZdZdddeddddddf
dQdd„ZdRdd„ZdSdd„ZdTd!d"„ZdUd#d$„Z	dVd0d1„Z
dWd4d5„ZdXd8d9„ZdYd;d<„ZdZd=d>„Zd[d?d@„Zd\dBdC„Zd]dEdF„Zd^dLdM„Zd_dOdP„ZdS )`r   aK  
    Container widget that exposes a larger virtual screen to its content and
    displays it in a vertical scrollbale region.

    Typically this is wrapped in a large `HSplit` container. Make sure in that
    case to not specify a `height` dimension of the `HSplit`, so that it will
    scale according to the content.

    .. note::

        If you want to display a completion menu for widgets in this
        `ScrollablePane`, then it's still a good practice to use a
        `FloatContainer` with a `CompletionsMenu` in a `Float` at the top-level
        of the layout hierarchy, rather then nesting a `FloatContainer` in this
        `ScrollablePane`. (Otherwise, it's possible that the completion menu
        is clipped.)

    :param content: The content container.
    :param scrolloffset: Try to keep the cursor within this distance from the
        top/bottom (left/right offset is not used).
    :param keep_cursor_visible: When `True`, automatically scroll the pane so
        that the cursor (of the focused window) is always visible.
    :param keep_focused_window_visible: When `True`, automatically scroll the
        pane so that the focused window is visible, or as much visible as
        possible if it doesn't completely fit the screen.
    :param max_available_height: Always constraint the height to this amount
        for performance reasons.
    :param width: When given, use this width instead of looking at the children.
    :param height: When given, use this height instead of looking at the children.
    :param show_scrollbar: When `True` display a scrollbar on the right.
    NTú^ÚvÚcontentr	   Úscroll_offsetsúScrollOffsets | NoneÚkeep_cursor_visibler   Úkeep_focused_window_visibleÚmax_available_heightÚintÚwidthr   ÚheightÚshow_scrollbarÚdisplay_arrowsÚup_arrow_symbolÚstrÚdown_arrow_symbolÚreturnÚNonec                 C  sh   || _ |p
tddd�| _t|ƒ| _t|ƒ| _|| _|| _|| _t|ƒ| _	t|	ƒ| _
|
| _|| _d| _d S )Nr   )ÚtopÚbottomr   )r   r
   r   r   r   r   r   r   r   r    r!   r"   r$   Úvertical_scroll)Úselfr   r   r   r   r   r   r   r    r!   r"   r$   © r+   ú˜/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/prompt_toolkit/layout/scrollable_pane.pyÚ__init__4   s   




zScrollablePane.__init__c                 C  s   d| j ›d�S )NzScrollablePane(ú)©r   ©r*   r+   r+   r,   Ú__repr__P   ó   zScrollablePane.__repr__c                 C  s   | j  ¡  d S ©N)r   Úresetr0   r+   r+   r,   r4   S   r2   zScrollablePane.resetÚmax_available_widthr   c                 C  s>   | j d ur
t| j ƒS | j |¡}|  ¡ rtt d¡|gƒS |S )Nr   )r   r   r   Úpreferred_widthr    r   r   Úexact)r*   r5   Úcontent_widthr+   r+   r,   r6   V   s   

zScrollablePane.preferred_widthc                 C  sB   | j d ur
t| j ƒS |  ¡ r|d8 }| j || j¡}td|jd�S )Nr   r   )ÚminÚ	preferred)r   r   r    r   Úpreferred_heightr   r   r:   )r*   r   r   Ú	dimensionr+   r+   r,   r;   d   s   

zScrollablePane.preferred_heightÚscreenr   Úmouse_handlersr   Úwrite_positionr   Úparent_styleÚerase_bgÚboolÚz_indexú
int | Nonec                 C  s(  |   ¡ }|r|jd }n|j}| j || j¡j}	t|	|jƒ}	t|	| jƒ}	t	t
d|d�d�}
|j|
_tdd||	d�}tƒ }| j |
|||||¡ |
 ¡  ddlm} |ƒ jj}z|
j| }W n	 tyf   Y nw |  |j|	||
j |¡¡ |  ||
||¡ |  ||||¡ |j}|j}t|j|| ƒ|_t|j||j ƒ|_|  ||
|¡ |
jrªd|_|
j ¡ D ]6\}}d|j  kr¿|jk rån q¯| j |j!  krÑ|j| j  k rån q¯t"|j| |j!| | j  d	�|j|< q¯|
j# ¡ D ]\}}|  $t"|j| |j!| | j  d	�|¡|j#|< që|�r|  %||	|¡ d
S d
S )z˜
        Render scrollable pane content.

        This works by rendering on an off-screen canvas, and copying over the
        visible region.
        r   ú )ÚcharÚstyle)Údefault_charr   )ÚxposÚyposr   r   )Úget_appT©ÚxÚyN)&r    r   r   r;   r   r:   Úmaxr   r9   r   r   Úshow_cursorr   r   Úwrite_to_screenÚdraw_all_floatsÚprompt_toolkit.applicationrK   ÚlayoutÚcurrent_windowÚ"visible_windows_to_write_positionsÚKeyErrorÚ_make_window_visibleÚcursor_positionsÚgetÚ_copy_over_screenÚ_copy_over_mouse_handlersrJ   rI   Ú_copy_over_write_positionsÚitemsrM   r)   rN   r   Úmenu_positionsÚ_clip_point_to_visible_areaÚ_draw_scrollbar)r*   r=   r>   r?   r@   rA   rC   r    Úvirtual_widthÚvirtual_heightÚtemp_screenÚtemp_write_positionÚtemp_mouse_handlersrK   Úfocused_windowÚvisible_win_write_posrJ   rI   ÚwindowÚpointr+   r+   r,   rQ   s   s–   ÿþÿú
ÿÿ
üÿÿ

þÿ€þýÿzScrollablePane.write_to_screenrj   r   c                 C  sŒ   |j |jk r|j|jd�}|j|jk r|j|jd�}|j |j|j kr/|j|j|j d d�}|j|j|j krD|j|j|j d d�}|S )zV
        Ensure that the cursor and menu positions always are always reported
        )rM   )rN   r   )rM   rI   Ú_replacerN   rJ   r   r   )r*   rj   r?   r+   r+   r,   r`   ê   s   z*ScrollablePane._clip_point_to_visible_areard   rb   c                 C  s’   |j }|j}t|jƒD ];}|j|| j  }|j||  }	|j|| j  }
|j||  }t|ƒD ]}|| |	|| < ||
v rE|
| ||| < q/qdS )zU
        Copy over visible screen content and "zero width escape sequences".
        N)rJ   rI   Úranger   Údata_bufferr)   Úzero_width_escapes)r*   r=   rd   r?   rb   rJ   rI   rN   Útemp_rowÚrowÚtemp_zero_width_escapesrn   rM   r+   r+   r,   r[   û   s    
ÿ€üøz ScrollablePane._copy_over_screenrf   c                   s�   |j ‰|j‰i ‰ d‡ ‡‡‡fdd„}|j}|j}t|jƒD ](}||v rE||ˆj  }	||ˆ  }
t|ƒD ]}||	v rD||	| ƒ|
|ˆ < q4qdS )zá
        Copy over mouse handlers from virtual screen to real screen.

        Note: we take `virtual_width` because we don't want to copy over mouse
              handlers that we possibly have behind the scrollbar.
        Úhandlerr   r%   c                   s,   ˆ ˆvrd‡ ‡‡‡fdd„}|ˆˆ < ˆˆ  S )	z:Wrap mouse handler. Translate coordinates in `MouseEvent`.Úeventr   r%   r&   c                   s@   t t| jjˆ | jjˆj ˆ d�| j| j| jd�}ˆ |ƒ d S )NrL   )ÚpositionÚ
event_typeÚbuttonÚ	modifiers)	r   r   rt   rM   rN   r)   ru   rv   rw   )rs   Ú	new_event)rr   r*   rI   rJ   r+   r,   Únew_handler.  s   
þù	zYScrollablePane._copy_over_mouse_handlers.<locals>.wrap_mouse_handler.<locals>.new_handlerN)rs   r   r%   r&   r+   )rr   ry   ©Úmouse_handler_wrappersr*   rI   rJ   )rr   r,   Úwrap_mouse_handler*  s   zDScrollablePane._copy_over_mouse_handlers.<locals>.wrap_mouse_handlerN)rr   r   r%   r   )rJ   rI   r>   rl   r   r)   )r*   r>   rf   r?   rb   r|   Úmouse_handlers_dictÚtemp_mouse_handlers_dictrN   Útemp_mouse_rowÚ	mouse_rowrM   r+   rz   r,   r\     s   €úz(ScrollablePane._copy_over_mouse_handlersc                 C  sP   |j }|j}|j ¡ D ]\}}t|j| |j | | j |j|jd�|j|< qdS )z3
        Copy over window write positions.
        )rI   rJ   r   r   N)rJ   rI   rV   r^   r   r)   r   r   )r*   r=   rd   r?   rJ   rI   ÚwinÚ	write_posr+   r+   r,   r]   I  s   úÿz)ScrollablePane._copy_over_write_positionsc                 C  ó
   | j  ¡ S r3   )r   Úis_modalr0   r+   r+   r,   r„   \  ó   
zScrollablePane.is_modalúKeyBindingsBase | Nonec                 C  rƒ   r3   )r   Úget_key_bindingsr0   r+   r+   r,   r‡   _  r…   zScrollablePane.get_key_bindingsúlist[Container]c                 C  s   | j gS r3   r/   r0   r+   r+   r,   Úget_childrenb  s   zScrollablePane.get_childrenÚvisible_heightrc   rh   Úcursor_positionúPoint | Nonec                 C  sä   d}|| }|   ¡ r.|dur.| j}|j| d |j }|j|j }	t||ƒ}tdt||	ƒƒ}|  ¡ rX|j|krC|j	|j | }
|j	}n|j	}
|j	|j | }t||
ƒ}t||ƒ}||kr^|}| j
|krf|| _
| j
|k rp|| _
dS dS )aÅ  
        Scroll the scrollable pane, so that this window becomes visible.

        :param visible_height: Height of this `ScrollablePane` that is rendered.
        :param virtual_height: Height of the virtual, temp screen.
        :param visible_win_write_pos: `WritePosition` of the nested window on the
            temp screen.
        :param cursor_position: The location of the cursor position of this
            window on the temp screen.
        r   Nr   )r   r   rN   r(   r'   rO   r9   r   r   rJ   r)   )r*   rŠ   rc   rh   r‹   Ú
min_scrollÚ
max_scrollÚoffsetsÚcpos_min_scrollÚcpos_max_scrollÚwindow_min_scrollÚwindow_max_scrollr+   r+   r,   rX   e  sF   ÿ

ÿþÿÿþÿ




ÿz#ScrollablePane._make_window_visibleÚcontent_heightc                   sR  |j }|  ¡ }|r|d8 }z"|j t|ƒ }| jt|ƒ }tt|td|| ƒƒƒ‰ t|| ƒ‰W n
 ty9   Y dS w d‡ ‡fdd	„}|j|j	 d }	|j
}
|j}|r`t| jd
ƒ||
 |	< |
d7 }
d}d}d}d}t|ƒD ],}d}||ƒr€||d ƒs}|}n|}n||d ƒr‰|}n|}td|ƒ||
 |	< |
d7 }
ql|r§t| jd
ƒ||
 |	< dS dS )zš
        Draw the scrollbar on the screen.

        Note: There is some code duplication with the `ScrollbarMargin`
              implementation.
        é   r   Nrp   r   r%   rB   c                   s   ˆ|   koˆˆ  kS   S )z/True if we should display a button on this row.r+   )rp   ©Úscrollbar_heightÚscrollbar_topr+   r,   Úis_scroll_buttonÃ  s   z8ScrollablePane._draw_scrollbar.<locals>.is_scroll_buttonzclass:scrollbar.arrowzclass:scrollbar.backgroundz*class:scrollbar.background,scrollbar.startzclass:scrollbar.buttonz$class:scrollbar.button,scrollbar.endÚ rE   )rp   r   r%   rB   )r   r!   Úfloatr)   r   r9   rO   ÚZeroDivisionErrorrI   r   rJ   rm   r   r"   rl   r$   )r*   r?   r”   r=   Úwindow_heightr!   Úfraction_visibleÚfraction_abover™   rI   rJ   rm   Úscrollbar_backgroundÚscrollbar_background_startÚscrollbar_buttonÚscrollbar_button_endÚirG   r+   r–   r,   ra   §  sV   
ÿÿÿ
ÿÿzScrollablePane._draw_scrollbar)r   r	   r   r   r   r   r   r   r   r   r   r   r   r   r    r   r!   r   r"   r#   r$   r#   r%   r&   )r%   r#   )r%   r&   )r5   r   r%   r   )r   r   r   r   r%   r   )r=   r   r>   r   r?   r   r@   r#   rA   rB   rC   rD   r%   r&   )rj   r   r?   r   r%   r   )
r=   r   rd   r   r?   r   rb   r   r%   r&   )
r>   r   rf   r   r?   r   rb   r   r%   r&   )r=   r   rd   r   r?   r   r%   r&   )r%   rB   )r%   r†   )r%   rˆ   )
rŠ   r   rc   r   rh   r   r‹   rŒ   r%   r&   )r?   r   r”   r   r=   r   r%   r&   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚMAX_AVAILABLE_HEIGHTr-   r1   r4   r6   r;   rQ   r`   r[   r\   r]   r„   r‡   r‰   rX   ra   r+   r+   r+   r,   r      s6    #ô





w


3



BN)Ú
__future__r   Úprompt_toolkit.data_structuresr   Úprompt_toolkit.filtersr   r   Úprompt_toolkit.key_bindingr   Úprompt_toolkit.mouse_eventsr   Ú
containersr	   r
   r<   r   r   r   r   r>   r   r   r=   r   r   r   Ú__all__r©   r   r+   r+   r+   r,   Ú<module>   s    