a
    õ£Be@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                   @  sH  e Zd ZdZdddeddddddf
ddddd	d
d
ddddddœdd„Zddœdd„Zddœdd„Zd	ddœdd„Zd	d	ddœdd„Z	dddddd dd!œd"d#„Z
d$dd$d%œd&d'„Zdddd	dd(œd)d*„Zdddd	dd+œd,d-„Zddddd.œd/d0„Zddœd1d2„Zd3dœd4d5„Zd6dœd7d8„Zd	d	dd9dd:œd;d<„Zdd	ddd=œd>d?„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ú^Úvr	   zScrollOffsets | Noner   Úintr   ÚstrÚNone)ÚcontentÚscroll_offsetsÚkeep_cursor_visibleÚkeep_focused_window_visibleÚmax_available_heightÚwidthÚheightÚshow_scrollbarÚdisplay_arrowsÚup_arrow_symbolÚdown_arrow_symbolÚreturnc                 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*   úh/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/prompt_toolkit/layout/scrollable_pane.pyÚ__init__4   s    



zScrollablePane.__init__)r%   c                 C  s   d| j ›d�S )NzScrollablePane(ú)©r   ©r)   r*   r*   r+   Ú__repr__P   s    zScrollablePane.__repr__c                 C  s   | j  ¡  d S ©N)r   Úresetr/   r*   r*   r+   r2   S   s    zScrollablePane.resetr   )Ú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)   r3   Zcontent_widthr*   r*   r+   r4   V   s    

zScrollablePane.preferred_width)r   r   r%   c                 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   r7   )r)   r   r   Ú	dimensionr*   r*   r+   r8   d   s    

zScrollablePane.preferred_heightr   r   r   Úboolz
int | None)ÚscreenÚmouse_handlersÚwrite_positionÚparent_styleÚerase_bgÚz_indexr%   c                 C  s2  |   ¡ }|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yÌ   Y n0 |  |j|	||
j |¡¡ |  ||
||¡ |  ||||¡ |j}|j}t|j|| ƒ|_t|j||j ƒ|_|  ||
|¡ |
j�rVd|_|
j ¡ D ]v\}}d|j  k�r„|jk �r`n nL| j |j!  k�r¬|j| j  k �r`n n$t"|j| |j!| | j  d	�|j|< �q`|
j# ¡ D ]6\}}|  $t"|j| |j!| | j  d	�|¡|j#|< �qâ|�r.|  %||	|¡ 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)Zdefault_charr   )ÚxposÚyposr   r    )Úget_appT©ÚxÚyN)&r!   r   r   r8   r   r7   Úmaxr    r6   r   r   Úshow_cursorr   r   Úwrite_to_screenZdraw_all_floatsZprompt_toolkit.applicationrF   ZlayoutZcurrent_windowÚ"visible_windows_to_write_positionsÚKeyErrorÚ_make_window_visibleZcursor_positionsÚgetÚ_copy_over_screenÚ_copy_over_mouse_handlersrE   rD   Ú_copy_over_write_positionsÚitemsrH   r(   rI   r   Zmenu_positionsÚ_clip_point_to_visible_areaÚ_draw_scrollbar)r)   r;   r<   r=   r>   r?   r@   r!   Úvirtual_widthÚvirtual_heightÚtemp_screenZtemp_write_positionÚtemp_mouse_handlersrF   Zfocused_windowÚvisible_win_write_posrE   rD   ZwindowÚpointr*   r*   r+   rL   s   s’    ÿÿú
ÿ
üÿÿ
ÿý

üÿþýzScrollablePane.write_to_screenr   )r\   r=   r%   c                 C  sŒ   |j |jk r|j|jd�}|j|jk r4|j|jd�}|j |j|j kr^|j|j|j d d�}|j|j|j krˆ|j|j|j d d�}|S )zV
        Ensure that the cursor and menu positions always are always reported
        )rH   )rI   r   )rH   rD   Ú_replacerI   rE   r   r    )r)   r\   r=   r*   r*   r+   rU   ê   s    z*ScrollablePane._clip_point_to_visible_area)r;   rY   r=   rW   r%   c                 C  s’   |j }|j}t|jƒD ]v}|j|| j  }|j||  }	|j|| j  }
|j||  }t|ƒD ],}|| |	|| < ||
v r^|
| ||| < q^qdS )zU
        Copy over visible screen content and "zero width escape sequences".
        N)rE   rD   Úranger    Údata_bufferr(   Úzero_width_escapes)r)   r;   rY   r=   rW   rE   rD   rI   Ztemp_rowÚrowZtemp_zero_width_escapesr`   rH   r*   r*   r+   rQ   û   s    
ÿz ScrollablePane._copy_over_screen)r<   rZ   r=   rW   r%   c                   s–   |j ‰|j‰i ‰ dddœ‡ ‡‡‡fdd„}|j}|j}t|jƒD ]P}||v r@||ˆj  }	||ˆ  }
t|ƒD ] }||	v rn||	| ƒ|
|ˆ < qnq@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.
        r   )Úhandlerr%   c                   s2   ˆ ˆvr*dddœ‡ ‡‡‡fdd„}|ˆˆ < ˆˆ  S )z:Wrap mouse handler. Translate coordinates in `MouseEvent`.r   r   )Úeventr%   c                   s@   t t| jjˆ | jjˆj ˆ d�| j| j| jd�}ˆ |ƒ d S )NrG   )ÚpositionÚ
event_typeÚbuttonÚ	modifiers)	r   r   rd   rH   rI   r(   re   rf   rg   )rc   Z	new_event)rb   r)   rD   rE   r*   r+   Únew_handler.  s    
þù	zYScrollablePane._copy_over_mouse_handlers.<locals>.wrap_mouse_handler.<locals>.new_handlerr*   )rb   rh   ©Zmouse_handler_wrappersr)   rD   rE   )rb   r+   Úwrap_mouse_handler*  s    zDScrollablePane._copy_over_mouse_handlers.<locals>.wrap_mouse_handlerN)rE   rD   r<   r^   r    r(   )r)   r<   rZ   r=   rW   rj   Zmouse_handlers_dictZtemp_mouse_handlers_dictrI   Ztemp_mouse_rowZ	mouse_rowrH   r*   ri   r+   rR     s    z(ScrollablePane._copy_over_mouse_handlers)r;   rY   r=   r%   c                 C  sP   |j }|j}|j ¡ D ]4\}}t|j| |j | | j |j|jd�|j|< qdS )z3
        Copy over window write positions.
        )rD   rE   r    r   N)rE   rD   rM   rT   r   r(   r    r   )r)   r;   rY   r=   rE   rD   ÚwinZ	write_posr*   r*   r+   rS   I  s    úz)ScrollablePane._copy_over_write_positionsc                 C  s
   | j  ¡ S r1   )r   Úis_modalr/   r*   r*   r+   rl   \  s    zScrollablePane.is_modalzKeyBindingsBase | Nonec                 C  s
   | j  ¡ S r1   )r   Úget_key_bindingsr/   r*   r*   r+   rm   _  s    zScrollablePane.get_key_bindingszlist[Container]c                 C  s   | j gS r1   r.   r/   r*   r*   r+   Úget_childrenb  s    zScrollablePane.get_childrenzPoint | None)Úvisible_heightrX   r[   Úcursor_positionr%   c                 C  sà   d}|| }|   ¡ r\|dur\| j}|j| d |j }|j|j }	t||ƒ}tdt||	ƒƒ}|  ¡ r°|j|kr†|j	|j | }
|j	}n|j	}
|j	|j | }t||
ƒ}t||ƒ}||kr¼|}| j
|krÌ|| _
| j
|k rÜ|| _
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   rI   r'   r&   rJ   r6   r   r    rE   r(   )r)   ro   rX   r[   rp   Z
min_scrollZ
max_scrollÚoffsetsZcpos_min_scrollZcpos_max_scrollZwindow_min_scrollZwindow_max_scrollr*   r*   r+   rO   e  sD    ÿ

ÿþÿÿþÿ



z#ScrollablePane._make_window_visible)r=   Úcontent_heightr;   r%   c                   s\  |j }|  ¡ }|r|d8 }zD|j t|ƒ }| jt|ƒ }tt|td|| ƒƒƒ‰ t|| ƒ‰W n tyr   Y dS 0 dd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 }
qÞ|�rXt| jd	ƒ||
 |	< dS )zš
        Draw the scrollbar on the screen.

        Note: There is some code duplication with the `ScrollbarMargin`
              implementation.
        é   r   Nr   r:   )ra   r%   c                   s   ˆ|   koˆˆ  kS   S )z/True if we should display a button on this row.r*   )ra   ©Zscrollbar_heightZ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Ú rA   )r    r"   Úfloatr(   r   r6   rJ   ÚZeroDivisionErrorrD   r   rE   r_   r   r#   r^   r$   )r)   r=   rr   r;   Zwindow_heightr"   Zfraction_visibleZfraction_aboveru   rD   rE   r_   Zscrollbar_backgroundZscrollbar_background_startZscrollbar_buttonZscrollbar_button_endÚirC   r*   rt   r+   rV   §  sR    
ÿÿ

ÿzScrollablePane._draw_scrollbar)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚMAX_AVAILABLE_HEIGHTr,   r0   r2   r4   r8   rL   rU   rQ   rR   rS   rl   rm   rn   rO   rV   r*   r*   r*   r+   r      s4   #ô&w3BN)Ú
__future__r   Zprompt_toolkit.data_structuresr   Zprompt_toolkit.filtersr   r   Zprompt_toolkit.key_bindingr   Zprompt_toolkit.mouse_eventsr   Ú
containersr	   r
   r9   r   r   r   r   r<   r   r   r;   r   r   r   Ú__all__r~   r   r*   r*   r*   r+   Ú<module>   s   