
    jj                    .   d dl mZ d dlZd dlZd dlmZmZ d dlmZm	Z	m
Z
 d dl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 ddlmZmZ ddlmZmZmZ ej>                  rd dl m!Z!m"Z"m#Z# d dl$m%Z%  G d de      Z& G d de      Z' G d deee      Z(y)    )annotationsN)chainrepeat)CanvasCombineCompositeCanvasSolidCanvas)Command)remove_defaults)is_mouse_press   )Sizing
WHSettings) WidgetContainerListContentsMixinWidgetContainerMixin_ContainerElementSizingFlag)MonitoredFocusListMonitoredList)WidgetWidgetErrorWidgetWarning)IterableIteratorSequence)Literalc                      e Zd ZdZy)	PileErrorzPile related errors.N__name__
__module____qualname____doc__     Z/home/ubuntu/.cache/pipx/cdb5059130eadca/lib/python3.12/site-packages/urwid/widget/pile.pyr   r      s    r#   r   c                      e Zd ZdZy)PileWarningzPile related warnings.Nr   r"   r#   r$   r&   r&      s     r#   r&   c                      e Zd ZdZd(dZ	 d)	 	 	 	 	 d* fdZd+ fdZd, fdZd-dZd.dZ	d/dZ
d/d	Zed
        Zej                  d        Zed        Zej                  d        Ze	 	 d0d       Zej                  	 	 	 	 d1d       Zeej&                  df	 	 	 	 	 d2d       Zed3d       Zej                  d4d       Zd3dZd4dZed        Zej                  d        Zed.d       Zej                  d5d       Zd6dZ	 d)	 	 	 	 	 	 	 	 	 d7dZ	 d8	 	 	 d9dZ	 d8	 	 	 	 	 d:dZ	 d8	 	 	 	 	 d;dZd<d= fdZd>d Z 	 d8	 	 	 	 	 d?d!Z!d@d"Z"d8dAd#Z#dBd$Z$dCd%Z%	 	 	 	 	 	 	 	 dDd&Z&	 	 	 	 	 	 	 	 	 	 	 	 	 	 dEd'Z' xZ(S )FPilezA
    A pile of widgets stacked vertically from top to bottom
    c                   | j                   s)t        t        j                  t        j                  f      S d}d}d}t               }t        j                  t        j                  z  t        j                  z  }t        j                  t        j                  z  }t        | j                         D ]  \  }\  }\  }	}
|j                         }t        j                  }|	t        j                  k(  r|t        j                  z  }t        j                  |v r|t        j                  z  }t        j                  |v r|t        j                  z  }t        j                  |v r|t        j                  t        j                  hz  r|t        j                  z  }n|	t        j                  k(  rL|t        j                  z  }t        j                  |v r|t        j                  z  }|t        j                  z  }nZt        j                   }t        j                  |v r|t        j                  z  }t        j                  |v r|t        j                  z  }||z  swt#        j$                  d| d|	j&                   ddj)                  |      j+                          t,        d       t        t        j                  t        j                  f      c S |t        j                  z  r(|j/                  t        j                         ||z  sd} n/|t        j                  z  rd}|t        j                  z  sd} |sB|r|j/                  t        j                         |r|j/                  t        j                         t        |      S )	a  Sizing supported by widget.

        :return: Calculated widget sizing
        :rtype: frozenset[Sizing]

        Due to the nature of container with mutable contents, this method cannot be cached.

        Rules:
        * WEIGHT BOX -> BOX
        * GIVEN BOX -> FLOW (height is known) & BOX (can be shrinked/padded)
        * PACK BOX -> Unsupported

        * WEIGHT FLOW -> FLOW
        * GIVEN FLOW -> Unsupported
        * PACK FLOW -> FLOW

        * WEIGHT FIXED -> Need also FLOW or/and BOX to properly render due to width calculation
        * GIVEN FIXED -> Unsupported
        * PACK FIXED -> FIXED (widget knows its size)

        >>> from urwid import BigText, ProgressBar, SolidFill, Text, Thin3x3Font
        >>> font = Thin3x3Font()

        # BOX-only widget
        >>> Pile((SolidFill("#"),))
        <Pile box widget (1 item)>

        # GIVEN BOX -> BOX/FLOW
        >>> Pile(((10, SolidFill("#")),))
        <Pile box/flow widget (1 item)>

        # FLOW-only
        >>> Pile((ProgressBar(None, None),))
        <Pile flow widget (1 item)>

        # FIXED -> FIXED
        >>> Pile(((WHSettings.PACK, BigText("0", font)),))
        <Pile fixed widget (1 item)>

        # FLOW/FIXED -> FLOW/FIXED
        >>> Pile(((WHSettings.PACK, Text("text")),))
        <Pile fixed/flow widget (1 item)>

        # FLOW + FIXED widgets -> FLOW/FIXED
        >>> Pile((ProgressBar(None, None), (WHSettings.PACK, BigText("0", font))))
        <Pile fixed/flow widget (2 items) focus_item=0>

        # GIVEN BOX + FIXED widgets -> BOX/FLOW/FIXED (GIVEN BOX allows overriding its height & allows any width)
        >>> Pile(((10, SolidFill("#")), (WHSettings.PACK, BigText("0", font))))
        <Pile widget (2 items) focus_item=0>

        # Invalid sizing combination -> use fallback settings (and produce warning)
        >>> Pile(((WHSettings.WEIGHT, 1, BigText("0", font)),))
        <Pile box/flow widget (1 item)>

        # Special case: empty pile widget sizing is impossible to calculate
        >>> Pile(())
        <Pile box/flow widget ()>
        FzSizing combination of widget z not supported:  |   
stacklevelT)contents	frozensetr   BOXFLOWsetr   FIXED	enumeratesizingNONEr   WEIGHT	WH_WEIGHTGIVENWH_GIVENWH_PACKwarningswarnnamejoinupperr&   add)self
strict_boxhas_flow	has_fixed	supportedbox_flow_fixed
flow_fixedidxwidget	size_kind_size_weightw_sizingflags                r$   r6   zPile.sizing$   s   x }}fjj&++677
	!$	 (++.I.N.NNQlQrQrr 	 1558S8Y8YY
8A$--8P ,	!4C4&39l}}H.33DJ---3===::)7;;;D;;(*7<<<D<<8+FJJ;T0T7===Dj...3<<<::)7;;;D7<<<D 3::;;(*7<<<D<<8+7===D.(3C58HHXXYZ]ZbZbckZlZrZrZtYuv 
 !&**fkk!:;;1555fjj)j(!%J16661777 	Y,	!\ fkk*fll+##r#   c                    d _         t         	          t                _         j                  j                   j                          j                  j                   fd        j                  j                   j                         t        |      D ]  \  }}|}t        |t              s/ j                  j                  |t        j                   dff       n|d   t"        j$                  t        j&                  hv r3|\  }} j                  j                  |t        j&                  dff       nt)        |      dk(  s%|d   t"        j*                  t        j,                  hv r6|dd \  }} j                  j                  |t        j,                  |ff       nJ|d   t        j                   k(  r&|\  }}} j                  j                  |||ff       nt/        d|      ||j1                         r|}t        |t2              ryt5        j6                  |d	t8        d
         j                  r	|| _        d _        y)aA  
        :param widget_list: child widgets
        :type widget_list: iterable
        :param focus_item: child widget that gets the focus initially.
            Chooses the first selectable widget if unset.
        :type focus_item: Widget or int

        *widget_list* may also contain tuples such as:

        (*given_height*, *widget*)
            always treat *widget* as a box widget and give it *given_height* rows, where given_height is an int
        (``'pack'``, *widget*)
            allow *widget* to calculate its own height by calling its :meth:`rows` method,
            i.e. treat it as a flow widget.
        (``'weight'``, *weight*, *widget*)
            if the pile is treated as a box widget then treat widget as a box
            widget with a height based on its relative weight value, otherwise treat the same as (``'pack'``, *widget*).

        Widgets not in a tuple are the same as (``'weight'``, ``1``, *widget*)`

        .. note:: If the Pile is treated as a box widget there must be at least
            one ``'weight'`` tuple in :attr:`widget_list`.
        Fc                $    j                         S N)_invalidate)frC   s    r$   <lambda>zPile.__init__.<locals>.<lambda>   s    D<L<L<N r#   r   r   N   z!initial widget list item invalid  is not a Widgetr,   r-   )_selectablesuper__init__r   	_contentsset_modified_callback_contents_modifiedset_focus_changed_callbackset_validate_contents_modified_validate_contents_modifiedr5   
isinstancetupler/   appendr   r8   r   r2   PACKlenr4   r:   r   
selectabler   r=   r>   r&   focuspref_col)	rC   widget_list
focus_itemioriginalwrT   height	__class__s	   `       r$   r[   zPile.__init__   s   B !   	 	,,T-D-DE112NO55d6V6VW$[1 	SKAxAa'$$a**;*;Q)?%@A1&++z771$$a*//4)@%ABQ1!z7G7G(H HbcF	$$a**:*:F)C%DE1*** 61$$a!V%56"CH< PQQ!alln
a(%56PQR'	S* ==Z3#DJr#   c                    t        | j                        dkD  rdt        | j                         d}n| j                  rd}nd}g t        |          |S )Nr   (z items)z(1 item)z())rf   r/   rZ   _repr_words)rC   contents_stringrp   s     r$   rs   zPile._repr_words   sU    t}}! !#dmm"4!5W=O]](O"O8$&888r#   c                    i t         |          dt        | j                        dkD  r| j                  nd i}t        |t        j                        S )Nrk   r   )rZ   _repr_attrsrf   r\   focus_positionr
   r(   r[   )rC   attrsrp   s     r$   rv   zPile._repr_attrs   sJ    q57&(q,sSWSaSaObefOf8K8Klpqudmm44r#   c              #    K   g }| j                   D ]  \  }\  }}|t        j                  k(  r|j                  ||f       0|t        j                  k(  r"|j                  t        j                  |f       e|t        j
                  k(  sy|dk(  r|j                  |       |j                  t        j
                  ||f        d|f d| j                   r| j                  nd f y w)Nr   rj   rk   )r\   r   r:   rd   re   r8   rw   )rC   rj   
w_instancer6   amounts        r$   __rich_repr__zPile.__rich_repr__   s     
 	 	 -1NN 		P(J()))""FJ#78:??*""JOOZ#@A:,,,Q;&&z2&&
(9(96:'NO		P [((4>>D//tKKs   BC*
A C*c                ,    t        | j                        S rR   )rf   r\   rC   s    r$   __len__zPile.__len__  s    4>>""r#   c                f    t        d | j                  D              | _        | j                          y)z\Recalculate whether this widget should be selectable whenever the contents has been changed.c              3  B   K   | ]  \  }}|j                           y wrR   )rg   ).0rn   os      r$   	<genexpr>z*Pile._contents_modified.<locals>.<genexpr>  s     H$!Qq||~Hs   N)anyr/   rY   rS   r~   s    r$   r^   zPile._contents_modified  s%    H$--HHr#   c           	        g }	 |D ]z  }|\  }\  }}t        |t        j                  t        j                  t        j                  hv|d uxr t        |t        t        f       xs |dk  f      sj|j                  |       | 	 |rt        d|      y # t        t        f$ r-}t        d|       j                  |j                        |d }~ww xY w)Nr   zadded content invalid: )r   r   re   r:   r8   rb   intfloatrd   	TypeError
ValueErrorr   with_traceback__traceback__)	rC   slc	new_itemsinvalid_itemsitem_wtnexcs	            r$   ra   z Pile._validate_contents_modified  s    LN	h! /!
FQ*//:3C3CZEVEV!WW$U
1sEl0K,K,TqSTu "((./ 5m5FGHH  :& 	h5cU;<KKCL]L]^dgg	hs   A+B 0B C$(CCc                     t        j                  dt        d       t        d  j                  D               fd}j                  |       S )z
        A list of the widgets in this Pile

        .. note:: only for backwards compatibility. You should use the new
            standard container property :attr:`contents`.
        only for backwards compatibility. You should use the new standard container property `contents`.API will be removed in version 5.0.rV   r-   c              3  &   K   | ]	  \  }}|  y wrR   r"   )r   rn   r   s      r$   r   z#Pile.widget_list.<locals>.<genexpr>5  s     7A17s   c                      _         y rR   )rj   mlrC   s   r$   user_modifiedz'Pile.widget_list.<locals>.user_modified7  s    !Dr#   r=   r>   DeprecationWarningr   r/   r]   rC   r   r   s   ` @r$   rj   zPile.widget_list'  sL     	2		
 777	" 	  /	r#   c                
   | j                   }t        |t        | j                  t	        d t
        j                  dff                  D cg c]  \  }\  }}||f c}}}| _        |t        |      k  r|| _         y y c c}}}w Nr   )rw   zipr   r/   r   r   r8   rf   )rC   widgetsrw   newrn   optionss         r$   rj   zPile.widget_list=  s    ,, (+dmmVTJ4E4Eq3I,J%KL(
 
#lq' 'N
 CL("0D )
s   A>c                     t        j                  dt        d       t        d  j                  D               fd}j                  |       S )z
        A list of the options values for widgets in this Pile.

        .. note:: only for backwards compatibility. You should use the new
            standard container property :attr:`contents`.
        r   rV   r-   c              3     K   | ]Z  \  }\  }}t         j                  t        j                  t         j                  t        j
                  ij                  ||      |f \ y wrR   )r   r:   r   r4   re   r2   get)r   rn   rT   ro   s       r$   r   z"Pile.item_types.<locals>.<genexpr>Y  sR      
 ;Av joov{{KOOPQSTUW]^
s   A A"c                      _         y rR   )
item_typesr   s   r$   r   z&Pile.item_types.<locals>.user_modified_  s
     DOr#   r   r   s   ` @r$   r   zPile.item_typesK  sU     	2		
  
 #'--
 
	! 	  /	r#   c                   t        j                  dt        d       | j                  }t	        || j
                        D cg c]]  \  \  }}\  }}|t        j                  t        j                  t        j                  t        j                  ij                  ||      |ff_ c}}}}| _        |t        |      k  r|| _        y y c c}}}}w )Nr   rV   r-   )r=   r>   r   rw   r   r/   r   r4   r   r:   r2   re   r   rf   )rC   r   rw   new_t
new_heightrn   r   s          r$   r   zPile.item_typese  s    2		
 ,, 8;:t}}7U
 
3$%lq' &,,
 0 0&++zOSSTY[`acmno
 C
O+"0D ,	
s   A"C
c                    | j                   S )a  
        The contents of this Pile as a list of (widget, options) tuples.

        options currently may be one of

        (``'pack'``, ``None``)
            allow widget to calculate its own height by calling its
            :meth:`rows <Widget.rows>` method, i.e. treat it as a flow widget.
        (``'given'``, *n*)
            Always treat widget as a box widget with a given height of *n* rows.
        (``'weight'``, *w*)
            If the Pile itself is treated as a box widget then
            the value *w* will be used as a relative weight for assigning rows
            to this box widget. If the Pile is being treated as a flow
            widget then this is the same as (``'pack'``, ``None``) and the *w*
            value is ignored.

        If the Pile itself is treated as a box widget then at least one
        widget must have a (``'weight'``, *w*) options value, or the Pile will
        not be able to grow to fill the required number of rows.

        This list may be modified like a normal list and the Pile widget
        will updated automatically.

        .. seealso:: Create new options tuples with the :meth:`options` method
        r\   r~   s    r$   r/   zPile.contentsu  s    F ~~r#   c                "    || j                   d d  y rR   r   )rC   cs     r$   r/   zPile.contents  s     qr#   r   c                    | t         j                  k(  rt         j                  dfS | t         j                  t         j                  hv r|| |fS t	        d| d|      )a,  
        Return a new options tuple for use in a Pile's :attr:`contents` list.

        :param height_type: ``'pack'``, ``'given'`` or ``'weight'``
        :param height_amount: ``None`` for ``'pack'``, a number of rows for
            ``'fixed'`` or a weight value (number) for ``'weight'``
        Nz!invalid combination: height_type=z, height_amount=)r   re   r:   r8   r   )height_typeheight_amounts     r$   r   zPile.options  sd    " *//)OOT**:++Z->->??MD]//;K?JZ[hZklmmr#   c                T    | j                   sy| j                   | j                     d   S )z4the child widget in focus or None when Pile is emptyNr   )r/   rw   r~   s    r$   rh   z
Pile.focus  s(     }}}}T001!44r#   c                    t        |t              r|| _        yt        | j                        D ]  \  }\  }}||k(  s|| _         y t        d|      )aT  
        Set the item in focus, for backwards compatibility.

        .. note:: only for backwards compatibility. You should use the new
            standard container property :attr:`focus_position`.
            to set the position by integer index instead.

        :param item: element to focus
        :type item: Widget or int
        N#Widget not found in Pile contents: )rb   r   rw   r5   r/   r   rC   r   rl   rn   _optionss        r$   rh   z
Pile.focus  sa     dC "&D )$-- 8 	A}8qy&'#	 >thGHHr#   c                    t        j                  dt        d       | j                  sy| j                  | j                     d   S )z
        Return the widget in focus, for backwards compatibility.  You may
        also use the new standard container property .focus to get the
        child widget in focus.
        for backwards compatibility.You may also use the new standard container property .focus to get the child widget in focus.API will be removed in version 5.0.rV   r-   Nr   )r=   r>   r   r/   rw   r~   s    r$   	get_focuszPile.get_focus  sC     	2 	
 }}}}T001!44r#   c                    t        j                  dt        d       t        |t              r|| _        y t        | j                        D ]  \  }\  }}||k(  s|| _         y  t        d|      )Nr   rV   r-   r   )	r=   r>   r   rb   r   rw   r5   r/   r   r   s        r$   	set_focuszPile.set_focus  sz    2 	
 dC "&D )$-- 8 	A}8qy&'#	 >thGHHr#   c                R    t        j                  dt        d       | j                  S Nzonly for backwards compatibility.You should use the new standard container properties `focus` and `focus_position` to get the child widget in focus or modify the focus position.API will be removed in version 4.0.rV   r-   r=   r>   r   rh   r~   s    r$   rk   zPile.focus_item  s'    2 	
 zzr#   c                J    t        j                  dt        d       || _        y r   r   )rC   new_items     r$   rk   zPile.focus_item  s$    2 	
 
r#   c                \    | j                   st        d      | j                   j                  S )z
        index of child widget in focus.
        Raises :exc:`IndexError` if read when Pile is empty, or when set to an invalid index.
        z No focus_position, Pile is empty)r/   
IndexErrorrh   r~   s    r$   rw   zPile.focus_position  s'     }}?@@}}"""r#   c                    	 |dk  s|t        | j                        k\  rt        d|       	 || j                  _        y# t        $ r-}t        d|       j	                  |j
                        |d}~ww xY w)zf
        Set the widget in focus.

        position -- index of child widget to be made focus
        r   z!No Pile child widget at position N)rf   r/   r   r   r   r   rh   )rC   positionr   s      r$   rw   zPile.focus_position  s    	x!|x3t}}+== #DXJ!OPP  > '  	x@
KL[[\_\m\mntww	xs   +A   	A6	(A11A6c                    | j                         sy| j                  sy| j                  || j                               \  }}}| j                  || j                            | j
                  S )z4Return the preferred column for the cursor, or None.Nrh   )rg   r/   get_rows_sizes_update_pref_col_from_focusrw   ri   )rC   size_	size_argss       r$   get_pref_colzPile.get_pref_col+  s^     }}--d$//:K-L1i((43F3F)GH}}r#   c                   t        j                  dt        d       | j                  |   \  }\  }}|t        j
                  k(  r	|sy|d   fS |st        d| d| d      |d   }|t        j                  k(  r||fS |t        j                  k(  r,t        |      dk(  r|s| j                  ||      }|||   fS |fS t        d	|       )
zU
        Return a size appropriate for passing to self.contents[i][0].render
        zget_item_size is going to be deprecated and can be removed soon.This method is not used by the urwid codebase and `get_rows_sizes` is used for the similar purposes.rV   r-   r"   r   zElement z using parameters z& and do not have full size informationzUnsupported item height rules: )r=   r>   PendingDeprecationWarningr/   r   re   r   r:   r8   rf   get_item_rows)	rC   r   rl   rh   	item_rowsr   rT   ro   maxcols	            r$   get_item_sizezPile.get_item_size7  s     	s%		
 --*KQ
G:hqc);A3>deffa
   F##
!!!4yA~  $ 2 24 ?I	!--99!=>>r#   c           
        | j                   syi i i g }g }i }g }i }t        | j                         D ]n  \  }\  }\  }	}
|j                         }|xr | j                  |k(  }|	t        j
                  k(  rt        j                  |v r |j                  d|      \  |<   |<   d|<   t        j                  |v r|j                  |||f       |t        j                  t        j                  hz  rt        d| d|	j                                |	t        j                  k(  rI|
|<   t        j                  |v r|j                  |       t        d| d|	j                                |
dk  r*d|<   d|<   t        j                  |v rd|<   bd||<   it        j                  |v r|t        j                  t        j                  hz  r|j                  d|      \  }}||<   t        j                  |v rX|j                  |
g       j                  |       |j                  |
       |j                  |
|       t!        ||
   |      ||
<   "|j                  |||f       8t        j                  |v r|j                  |||f       `t        d|	 d	|        st        d
      t!        j#                               }|D ]'  \  }}}||<   |j%                  |f|      |<   |f|<   ) |rXt!        d |j'                         D              }|D ]3  }t!        t)        ||z  dz         d      }||   D ]  }||<   ||f|<    5 |D ]  }||<   ||   f|<    t+        fdt-        t/                    D              t+        fdt-        t/                    D              t+        fdt-        t/                    D              fS )zGet rows widths, heights and render size parameters

        Fixed case expect widget sizes calculation with several cycles for unknown height cases.
        )r"   r"   r"   r"   zUnsupported sizing  for r   r   )r   r   zUnsupported combination of z, z&No widgets providing width informationc              3  ,   K   | ]  \  }}||z    y wrR   r"   )r   weightro   s      r$   r   z-Pile._get_fixed_rows_sizes.<locals>.<genexpr>  s     *j~vv6F?*js         ?r   c              3  (   K   | ]	  }|     y wrR   r"   )r   rJ   widthss     r$   r   z-Pile._get_fixed_rows_sizes.<locals>.<genexpr>  s     <#&+<   c              3  (   K   | ]	  }|     y wrR   r"   r   rJ   heightss     r$   r   z-Pile._get_fixed_rows_sizes.<locals>.<genexpr>       >3'#,>r   c              3  (   K   | ]	  }|     y wrR   r"   r   rJ   w_h_argss     r$   r   z-Pile._get_fixed_rows_sizes.<locals>.<genexpr>       @C(3-@r   )r/   r5   r6   rh   r   re   r   r4   packr2   rd   r   rA   r:   r1   
setdefaultmaxvaluesrowsitemsr   rc   rangerf   )rC   rh   flowboxweightedweightsweight_max_sizesrJ   rK   rL   size_weightrN   focusedwidthro   	max_widthmax_weighted_coefficientr   r   r   r   s                     @@@r$   _get_fixed_rows_sizeszPile._get_fixed_rows_sizes_  s    }}!#"$HJ/1)++-7@7O /	W3C3&29k}}H4

f 4GJOO+<<8+06B0H-F3K$&HSM;;(*KKg 676<<"==#&9(5IZH[$\]]j...*::)JJsO#&9(5IZH[$\]]!s ;;(*$(HSM$*HSM)h&**fkk9R.R &B 8v#s::)''R8??DNN;/$//VD478H8UW]4^$[1 KKg 67(VS'23"=i[8* UVV_/	Wb DEE(	$( 	) FC#F3K!;;	|W=GCL&LHSM	)
 '**jQaQgQgQi*j'j$* 8S!9F!BS!HI1M#F+ 8C#)GCL%.$7HSM88  	6C#F3K&5HSM	6
 <s6{);<<>%G*=>>@5X+?@@
 	
r#   c                   |d   }| j                   s|fddfS |ft        | j                         z  }g }g }| j                  }t        | j                         D ]  \  }\  }	\  }
}t	        |	t
              r|	j                         }nHt        j                  |	dt        d       t        t        j                  t        j                  f      }|xr ||k(  }|
t        j                  k(  r%|j!                  |       |j!                  ||f       t        j                  |v r6|j!                  |	j#                  |f|             |j!                  |f       t        j$                  |v rJ|
t        j&                  k(  r7|j!                  |	j)                  d|      d          |j!                  d       [t        j                  d	| d
| d|
j+                          dt        j                  j+                          d	t        d       |j!                  |	j#                  |f|             |j!                  |f        |t-        |      t-        |      fS )zGet rows widths, heights and render size parameters

        Flow case is the simplest one: minimum cycles in the logic and no widgets manipulation.
        Here we can make some shortcuts
        r   r   r"   rX   r,   r-   r   r   Unusual widget  sizing r   #). Assuming wrong sizing and using  for height calculation)r/   rf   rw   r5   rb   r   r6   r=   r>   r&   r0   r   r2   r1   r   r:   rd   r   r4   re   r   rA   rc   )rC   r   rh   r   r   r   r   rw   rl   rn   rT   ro   rN   r   s                 r$   _get_flow_rows_sizeszPile._get_flow_rows_sizes  s    a}}9dB&&!'	C,> >CE,,#,T]]#; 	+A;Av!V$88:%56PQR$fkk6::%>?3^ 3GJ$$$v& 01(qvvviwv?@	*)a:??.Bqvvb'2156#%aS
%	{ K77={{7H7H7J6KKbd 	 qvvviwv?@	*5	+8 gh88r#   c                   |s| j                  |      S t        |      dk(  r| j                  ||      S |\  }}| j                  s|f|fdfS |}|ft        | j                        z  }i i }i | j                  t        | j                        D ]  \  }\  }	\  }
}t        |	t              r|	j                         }nHt        j                  |	dt        d       t        t        j                  t        j                  f      }|xr |k(  }|
t         j"                  k(  r||<   ||f|<   ||z  }|
t         j$                  k(  st        |      dk(  rt        j                  |v r|f}n~t        j&                  |v r|
t         j$                  k(  rd}nVt        j                  d| d| d	|
j)                          d
t        j                  j)                          d	t        d       |f}|	j+                  ||      d   }||<   ||<   ||z  }|r|||<   d|<   |df|<    t-        |j/                               }|dk  r|v rg }g }j1                         D ]0  \  }}|s	|k  r|j3                  |        |j3                  |       2 t        |      t        |      z
  }|sd t5        |ddd   |      D        }nT|dkD  r%g |d| d t5        |ddd   ||d       D        }n*g |d|dz
  d   d t5        ||dz
  dd   |      D        }|D ]  }|   dc}|<   ||z  }|dkD  s n t7        |d      }t9        |fd      D ]L  }|dk(  rd}n6t;        t=        |      ||   z  |z  dz         }|s|k(  rd}||z  }|||   z  }||<   ||f|<   N |t?        fdtA        t                    D              t?        fdtA        t                    D              fS )z3Get rows widths, heights and render size parametersr   r   r"   rX   r,   r-   r   r   r   r   r   r   c              3  .   K   | ]  }|D ]  }|   y wrR   r"   r   pairelements      r$   r   z&Pile.get_rows_sizes.<locals>.<genexpr>?  s     \tW[\G7\7\   Nc              3  .   K   | ]  }|D ]  }|   y wrR   r"   r   s      r$   r   z&Pile.get_rows_sizes.<locals>.<genexpr>C  s     `$[_`PWg`g`r  c              3  .   K   | ]  }|D ]  }|   y wrR   r"   r   s      r$   r   z&Pile.get_rows_sizes.<locals>.<genexpr>H  s     c$^bcSZgcgcr  c                    | k7  | fS rR   r"   )rJ   rw   s    r$   rU   z%Pile.get_rows_sizes.<locals>.<lambda>S  s    SN5JC4P r#   )keyr   c              3  (   K   | ]	  }|     y wrR   r"   r   s     r$   r   z&Pile.get_rows_sizes.<locals>.<genexpr>d  r   r   c              3  (   K   | ]	  }|     y wrR   r"   r   s     r$   r   z&Pile.get_rows_sizes.<locals>.<genexpr>e  r   r   )!r   rf   r   r/   rw   r5   rb   r   r6   r=   r>   r&   r0   r   r2   r1   r   r:   re   r4   rA   r   sumr   r   rd   r   r   sortedr   r   rc   r   )rC   r   rh   r   maxrow	remainingr   r   rl   rn   rT   ro   rN   r   w_h_argitem_height
sum_weightbeforeafterrJ   offsetindexesr   rw   r   r   s                          @@@r$   r   zPile.get_rows_sizes  sL    --E-::t9>,,T,??}}9vi++	!'	C,> >"$#%HJ,,#,T]]#; #	*A;Av!V$88:%56PQR$fkk6::%>?3^ 3GJ$$$#
%v.V#	joo%Ta;;(*7=iG\\X-!z2F GMM)!HXJeAGGI; O;;A;;;L;L;N:OOfh##$	  &iGffWg6q9(
%[(	$
%qkG#	*J *+
>n8 FE&}} *V^+c*S)* [3u:-F\s52;/G\!GV_`#eDbDk6&'?*K`
2
R/0c#eFQJ4D"4D.Ev*Nc
  '.s|Q$V#	q=	 	1%	((PQ 	+CA~5+hsm;jH3NO~ 5DT!	hsm+
GCL#TNHSM	+  >%G*=>>@5X+?@@
 	
r#   c                    |rt         |   ||      S | j                  ||      \  }}}t        |      t	        |      fS )zGet packed sized for widget.)rZ   r   r   r   r  )rC   r   rh   r   r   r   rp   s         r$   r   z	Pile.packh  sC    7<e,,!00u=FS\**r#   c                >    t        | j                  ||      d         S )zA list of the number of rows used by each widget in self.contents.

        This method is a normally used only by `get_item_size` for the BOX case..
        r   )listr   rC   r   rh   s      r$   r   zPile.get_item_rowso  s!    
 D''e4Q788r#   c                   | j                  ||      \  }}}g }t        t        ||| j                              D ]P  \  }\  }}	\  }
}| j                  |
k(  }d }|dkD  r|
j                  |	|xr |      }|s=|j                  |||f       R |st        d|d   g |dd  dd         S t        |      }t        |      dk(  rG|d   |j                         k7  r1t        |      }|j                  d|d   |j                         z
         |S )Nr   r   r*   r   rV   )r   r5   r   r/   rh   renderrd   r   r   rf   r   r   pad_trim_top_bottom)rC   r   rh   _widthsr   r   combinelistrl   ro   w_sizern   r   
item_focuscanvouts                  r$   r  zPile.renderv  s   
 '+&9&9$&F#)+4S)T]]5[+\ 	:'A'AqJDzxxe.B
xC""D!Z#89	: sDG^tABx^^A->??K(t9>d1g3!#&C##AtAw';<
r#   c                   | j                         syt        | j                  d      sy| j                  }| j	                  |d      \  }}}| j                  j                  ||         x}|\  }}|dkD  r|t        |d|       z  }||fS y)z2Return the cursor coordinates of the focus widget.Nget_cursor_coordsTr   r   )rg   hasattrrh   rw   r   r$  r  )	rC   r   rl   r  r   r   coordsxys	            r$   r$  zPile.get_cursor_coords  s     tzz#67&*&9&9$d&9&K#)jj229Q<@@FMDAq1uS!%%a4Kr#   c                >    t        | j                  ||      d         S r   )r  r   r  s      r$   r   z	Pile.rows  s    4&&tU3A677r#   c                   | j                   s|S | j                  }| j                  || j                               \  }}}| j                         rP| j                  j                  ||   |      }| j                  |   t        j                  t        j                  hvr|S | j                  |   t        j                  k(  rt        t        |dz
  dd            }n+t        t        |dz   t        | j                                     }|D ]  }| j                   |   d   j                         s$| j                  || j                            || _        t        | j                  d      s y||   }	| j                  |   t        j                  k(  rt        t        |	dz
  dd            }
nt        t        |	            }
|
D ]9  }| j                  j                  || j                     | j                   |      s8  y  y |S )ztPass the keypress to the widget in focus.

        Unhandled 'up' and 'down' keys may cause a focus change.
        r   r   r  r   move_cursor_to_coordsN)r/   rw   r   rg   rh   keypress_command_mapr	   UPDOWNrc   r   rf   r   r%  r+  ri   )rC   r   r  rl   r  r   r   
candidatesjr   rowlistrows               r$   r,  zPile.keypress  s   
 }}J&*&9&9$dooFW&9&X#)??**%%ilC8C  %gjj',,-GG
S!WZZ/uQUB34JuQUC,>?@J 	A==#A&113,,Yt7J7J-KL"#D4::'>?1:D  %3dQhB 78d, ::33Id>Q>Q4RTXTaTacfg #	( 
r#   c                |    t        | j                  d      sy| j                  j                  |      x}|| _        yy)z+Update self.pref_col from the focus widget.r   N)r%  rh   r   ri   )rC   r  ri   s      r$   r   z Pile._update_pref_col_from_focus  s<     tzz>2

//77HD$DM Er#   c                @   || _         d}d}| j                  ||      \  }}}t        t        ||| j                              D ]  \  }	\  }
}\  }}||
z   |kD  r n||
z  } y|j                         syt        |d      r|j                  ||||z
        du ry|	| _        y)z#Capture pref col and set new focus.Tr   r   Fr+  )	ri   r   r5   r   r/   rg   r%  r+  rw   )rC   r   colr3  rh   wrowr  r   r   rl   rr  rn   r   s                 r$   r+  zPile.move_cursor_to_coords  s      &*&9&9$e&9&L#)&/GY0V&W 	"A"66Aqax#~AID	
 ||~1-.13J3J6SVX[^bXb3cgl3lr#   c           	        d}| j                  ||      \  }}	}
t        t        |	|
| j                              D ]   \  }\  }}\  }}||z   |kD  r||z
  } n||z  }" yt	        |      r|dk(  r|j                         r|| _        t        |d      sKt        j                  |j                  j                   d|j                  j                   dt        d	       y|j                  ||||||xr | j                  |k(        S )
z]Pass the event to the contained widget.

        May change focus on button 1 press.
        r   r   Fr   mouse_event.z is not subclass of WidgetrV   r-   )r   r5   r   r/   r   rg   rw   r%  r=   r>   rp   r   r   r   r:  rh   )rC   r   eventbuttonr6  r3  rh   r7  r  r   r   rl   ro   r  rn   r   
target_rows                    r$   r:  zPile.mouse_event  s	    &*&9&9$e&9&L#)+4S)T]]5[+\ 	'A'Af}s" 4Z
FND		 % Vq[Q\\^"#Dq-(MM;;))*!AKK,@,@+AA[\"
 }}VUFCUE^tzz]^__r#   )returnzfrozenset[Sizing]rR   )rj   zIterable[Widget | tuple[Literal['pack', WHSettings.PACK] | int, Widget] | tuple[Literal['given', WHSettings.GIVEN], int, Widget] | tuple[Literal['weight', WHSettings.WEIGHT], int | float, Widget]]rk   zWidget | int | Noner?  None)r?  z	list[str])r?  zdict[str, typing.Any])r?  z4Iterator[tuple[str | None, typing.Any] | typing.Any])r?  r   )r?  r@  )r?  zMonitoredFocusList[Widget, tuple[Literal[WHSettings.PACK], None] | tuple[Literal[WHSettings.GIVEN], int] | tuple[Literal[WHSettings.WEIGHT], int | float]])r   zSequence[Widget, tuple[Literal[WHSettings.PACK], None] | tuple[Literal[WHSettings.GIVEN], int] | tuple[Literal[WHSettings.WEIGHT], int | float]]r?  r@  )r   z/Literal['pack', 'given', 'weight'] | WHSettingsr   zint | float | Noner?  z~tuple[Literal[WHSettings.PACK], None] | tuple[Literal[WHSettings.GIVEN], int] | tuple[Literal[WHSettings.WEIGHT], int | float])r?  zWidget | None)r   zWidget | intr?  r@  )r   r   r?  r@  )r   (tuple[()] | tuple[int] | tuple[int, int]r?  z
int | None)
r   rA  rl   r   rh   boolr   zlist[int] | Noner?  rA  )F)rh   rB  r?  Etuple[Sequence[int], Sequence[int], Sequence[tuple[int] | tuple[()]]])r   
tuple[int]rh   rB  r?  rC  )r   z(tuple[int, int] | tuple[int] | tuple[()]rh   rB  r?  zWtuple[Sequence[int], Sequence[int], Sequence[tuple[int, int] | tuple[int] | tuple[()]]])r"   F)r   rA  rh   rB  r?  ztuple[int, int])r   ztuple[int] | tuple[int, int]rh   rB  r?  z	list[int])r   rA  rh   rB  r?  zSolidCanvas | CompositeCanvas)r   rA  r?  ztuple[int, int] | None)r   rD  rh   rB  r?  r   )r   rA  r  strr?  z
str | None)r  rA  r?  r@  )r   rA  r6  r   r3  r   r?  rB  )r   rA  r<  rE  r=  r   r6  r   r3  r   rh   rB  r?  zbool | None))r   r   r    r!   r6   r[   rs   rv   r|   r   r^   ra   propertyrj   setterr   r/   staticmethodr   r8   r   rh   r   r   rk   rw   r   r   r   r   r   r   r   r  r$  r   r,  r   r+  r:  __classcell__)rp   s   @r$   r(   r(      sM   }$N +/E
E (E 
EN95L*#
I&  * 1 1  2 1 1 "
" "H __	
	 
	 	 GQGXGX,-nDn)n	9	n n, 5 5 \\I I(5"I" 	 	 	 	 # # ' '
" '+&?6&? &? 	&?
 $&? 
2&?T a
a
 
Oa
L /9/9 /9 
O	/9h s
6s
 s
 
a	s
j+9 6  
'	6$8(T%6  	
 
:#`6#` #` 	#`
 #` #` #` 
#`r#   r(   ))
__future__r   typingr=   	itertoolsr   r   urwid.canvasr   r   r   urwid.command_mapr	   urwid.split_reprr
   
urwid.utilr   	constantsr   r   	containerr   r   r   monitored_listr   r   rK   r   r   r   TYPE_CHECKINGcollections.abcr   r   r   typing_extensionsr   r   r&   r(   r"   r#   r$   <module>rW     sr    "   # D D % , % ) j j = 6 6	<<) !- !z`6')I z`r#   