o
    iiK                     @  s   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
 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)DictListOptional)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 th e
        pane so that the focused window is visible, or as much visible as
        possible if it doen'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.   `/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/prompt_toolkit/layout/scrollable_pane.py__init__6   s   




zScrollablePane.__init__c                 C  s   d| j dS )NzScrollablePane()r   r-   r.   r.   r/   __repr__R      zScrollablePane.__repr__c                 C  s   | j   d S N)r   resetr3   r.   r.   r/   r7   U   r5   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-   r8   content_widthr.   r.   r/   r9   X   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>   f   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rd|_|
j D ]6\}}d|j  kr|jk rn q| j |j!  kr|j| j  k rn qt"|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"   r<   r   r   show_cursorr   r   write_to_screendraw_all_floatsprompt_toolkit.applicationrN   layoutcurrent_window"visible_windows_to_write_positionsKeyError_make_window_visiblecursor_positionsget_copy_over_screen_copy_over_mouse_handlersrM   rL   _copy_over_write_positionsitemsrP   r,   rQ   r   menu_positions_clip_point_to_visible_area_draw_scrollbar)r-   r@   rA   rB   rC   rD   rF   r#   virtual_widthvirtual_heighttemp_screentemp_write_positiontemp_mouse_handlersrN   focused_windowvisible_win_write_posrM   rL   windowpointr.   r.   r/   rT   u   s   



zScrollablePane.write_to_screenrm   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
        )rP   )rQ   r   )rP   rL   _replacerQ   rM   r!   r"   )r-   rm   rB   r.   r.   r/   rc      s   z*ScrollablePane._clip_point_to_visible_arearg   re   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)rM   rL   ranger"   data_bufferr,   zero_width_escapes)r-   r@   rg   rB   re   rM   rL   rQ   temp_rowrowtemp_zero_width_escapesrq   rP   r.   r.   r/   r^      s    
z ScrollablePane._copy_over_screenri   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 )NrO   )position
event_typebutton	modifiers)	r
   r   rw   rP   rQ   r,   rx   ry   rz   )rv   	new_event)ru   r-   rL   rM   r.   r/   new_handler0  s   
	zYScrollablePane._copy_over_mouse_handlers.<locals>.wrap_mouse_handler.<locals>.new_handlerN)rv   r
   r(   r)   r.   )ru   r|   mouse_handler_wrappersr-   rL   rM   )ru   r/   wrap_mouse_handler,  s   zDScrollablePane._copy_over_mouse_handlers.<locals>.wrap_mouse_handlerN)ru   r   r(   r   )rM   rL   rA   ro   r"   r,   )r-   rA   ri   rB   re   r   mouse_handlers_dicttemp_mouse_handlers_dictrQ   temp_mouse_row	mouse_rowrP   r.   r}   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.
        )rL   rM   r"   r!   N)rM   rL   rY   ra   r   r,   r"   r!   )r-   r@   rg   rB   rM   rL   win	write_posr.   r.   r/   r`   K  s   z)ScrollablePane._copy_over_write_positionsc                 C  
   | j  S r6   )r   is_modalr3   r.   r.   r/   r   ^     
zScrollablePane.is_modalKeyBindingsBase | Nonec                 C  r   r6   )r   get_key_bindingsr3   r.   r.   r/   r   a  r   zScrollablePane.get_key_bindingslist[Container]c                 C  s   | j gS r6   r2   r3   r.   r.   r/   get_childrend  s   zScrollablePane.get_childrenvisible_heightrf   rk   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   rQ   r+   r*   rR   r<   r   r"   rM   r,   )r-   r   rf   rk   r   
min_scroll
max_scrolloffsetscpos_min_scrollcpos_max_scrollwindow_min_scrollwindow_max_scrollr.   r.   r/   r[   g  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|rt| jd
||
 |	< dS dS )z
        Draw the scrollbar on the screen.

        Note: There is some code duplication with the `ScrollbarMargin`
              implementation.
           r   Nrs   r    r(   rE   c                   s   |   ko  kS   S )z/True if we should display a button on this row.r.   )rs   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 rH   )rs   r    r(   rE   )r"   r$   floatr,   r    r<   rR   ZeroDivisionErrorrL   r!   rM   rp   r   r%   ro   r'   )r-   rB   r   r@   window_heightr$   fraction_visiblefraction_abover   rL   rM   rp   scrollbar_backgroundscrollbar_background_startscrollbar_buttonscrollbar_button_endirJ   r.   r   r/   rd     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)   )r8   r    r(   r   )r!   r    r   r    r(   r   )r@   r   rA   r   rB   r   rC   r&   rD   rE   rF   rG   r(   r)   )rm   r   rB   r   r(   r   )
r@   r   rg   r   rB   r   re   r    r(   r)   )
rA   r   ri   r   rB   r   re   r    r(   r)   )r@   r   rg   r   rB   r   r(   r)   )r(   rE   )r(   r   )r(   r   )
r   r    rf   r    rk   r   r   r   r(   r)   )rB   r   r   r    r@   r   r(   r)   )__name__
__module____qualname____doc__MAX_AVAILABLE_HEIGHTr0   r4   r7   r9   r>   rT   rc   r^   r_   r`   r   r   r   r[   rd   r.   r.   r.   r/   r      s6    #





w


3



BN)!
__future__r   typingr   r   r   prompt_toolkit.data_structuresr   prompt_toolkit.filtersr   r   prompt_toolkit.key_bindingr	   prompt_toolkit.mouse_eventsr
   
containersr   r   r?   r   r   r   r   rA   r   r   r@   r   r   r   __all__r   r   r.   r.   r.   r/   <module>   s    