
    AHj                       U d Z ddlmZ ddlmZ ddlmZmZmZm	Z	m
Z
mZmZ ddlZddlmZ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 ddlmZ ddl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) ddl*m+Z+m,Z, ddl-m.Z. erddl/m0Z0 ddl1m2Z2  G d de
      Z3dZ4de5d<    G d d      Z6ejn                  jq                  d       G d de6             Z9ejn                  jq                  d       G d d e6             Z:ejn                  jq                  d       G d! d"e6             Z;ejn                  jq                  d       G d# d$             Z<y)%a  

The compositor handles combining widgets into a single screen (i.e. compositing).

It also stores the results of that process, so that Textual knows the widgets on
the screen and their locations. The compositor uses this information to answer
queries regarding the widget under an offset, or the style under an offset.

Additionally, the compositor can render portions of the screen which may have updated,
without having to render the entire screen.
    )annotations)
itemgetter)TYPE_CHECKINGCallableIterableMapping
NamedTupleSequencecastN)ConsoleConsoleOptionsRenderableTypeRenderResult)Control)Segment)Style)errors)cell_len)visible_screen_stack)	loop_last)NULL_SPACINGOffsetRegionSizeSpacing)MapGeometry)StripStripRenderable)Widget)	TypeAlias)Screenc                  0    e Zd ZU dZded<   ded<   ded<   y)ReflowResultzCThe result of a reflow operation. Describes the chances to widgets.set[Widget]hiddenshownresizedN)__name__
__module____qualname____doc____annotations__     K/root/tools/cai/cai_env/lib/python3.12/site-packages/textual/_compositor.pyr#   r#   /   s    Mr.   r#   zdict[Widget, MapGeometry]r    CompositorMapc                      e Zd ZdZddZy)CompositorUpdatezQAn update generated by the compositor, which also doubles as console renderables.c                     y)Render the update to raw data, suitable for writing to terminal.

        Args:
            console: Console instance.

        Returns:
            Raw data with escape sequences.
         r-   )selfconsoles     r/   render_segmentsz CompositorUpdate.render_segments>   s     r.   Nr7   r   returnstr)r(   r)   r*   r+   r8   r-   r.   r/   r2   r2   ;   s
    [	r.   r2   T)angularc                  <    e Zd ZdZddZ	 	 	 	 	 	 ddZd	dZd
dZy)LayoutUpdatezBA renderable containing the result of a render for a given region.c                     || _         || _        y N)stripsregion)r6   rA   rB   s      r/   __init__zLayoutUpdate.__init__N   s    r.   c              #  L  K   | j                   j                  }t        j                         }t        j
                  }t        t        | j                  | j                   j                              D ].  \  }\  }} |||      j                   |E d {    |r+| 0 y 7 wr@   )rB   xr   liner   move_tor   	enumeraterA   ysegment)	r6   r7   optionsrE   new_linerG   lastrI   rF   s	            r/   __rich_console__zLayoutUpdate.__rich_console__R   s|      KKMM<<>//(4;;)NO 	OD)1d!Q-'''OO		s   BB$B"B$B$c                   g }|j                   }| j                  j                  }t        j                  }t        t        | j                  | j                  j                              D ]M  \  }\  }} | |||      j                  j                          ||j                  |             |rF |d       O dj                  |      S )r4   
r5   )appendrB   rE   r   rG   r   rH   rA   rI   rJ   textrenderjoin)	r6   r7   	sequencesrQ   rE   rG   rM   rI   rF   s	            r/   r8   zLayoutUpdate.render_segments^   s      "	!!KKMM//(4;;)NO 	OD)1d71a=((--.4;;w'(t		
 wwy!!r.   c              #  (   K   | j                    y wr@   )rB   r6   s    r/   __rich_repr__zLayoutUpdate.__rich_repr__r   s     kks   N)rA   list[Strip]rB   r   r:   Noner7   r   rK   r   r:   r   r9   r:   zrich.repr.Resultr(   r)   r*   r+   rC   rN   r8   rX   r-   r.   r/   r>   r>   J   s1    L

)7
	
"(r.   r>   c                  6    e Zd ZdZdddZ	 	 	 	 	 	 ddZd	dZy)
InlineUpdatez'A renderable to write an inline update.c                     || _         || _        y r@   )rA   clear)r6   rA   ra   s      r/   rC   zInlineUpdate.__init__z   s    
r.   c              #     K   t        j                         }t        | j                        D ]  \  }}|E d {    |r|  y 7 wr@   )r   rF   r   rA   )r6   r7   rK   rL   rM   rF   s         r/   rN   zInlineUpdate.__rich_console__~   sA      <<>#DKK0 	JD$OO	s   5AAAAc                   g }|j                   }t        | j                        D ]'  \  }} ||j                  |             |r  |d       ) | j                  r(t        | j                        dkD  r |d        |d       t        | j                        dkD  rF| j                  rt        | j                        nt        | j                        dz
  } |d| d       n |d        |d       dj                  |      S )	r4   rP      z[Jz[zAz[6nr5   )rQ   r   rA   rS   ra   lenrT   )r6   r7   rU   rQ   rM   strip
back_liness          r/   r8   zInlineUpdate.render_segments   s      "	!!$T[[1 	KD%5<<()t	 ::4;;!#t8t{{a-1ZZT[[)S=MPQ=QJU:,c*+4Lywwy!!r.   NF)rA   rY   ra   boolr:   rZ   r[   r9   )r(   r)   r*   r+   rC   rN   r8   r-   r.   r/   r_   r_   v   s,    1)7	"r.   r_   c                  L    e Zd ZdZ	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZd	dZd
dZy)ChopsUpdatez6A renderable that applies updated spans to the screen.c                .    || _         || _        || _        y)a  A renderable which updates chops (fragments of lines).

        Args:
            chops: A mapping of offsets to list of segments, per line.
            crop: Region to restrict update to.
            chop_ends: A list of the end offsets for each line
        N)chopsspans	chop_ends)r6   rn   ro   rp   s       r/   rC   zChopsUpdate.__init__   s     

"r.   c              #    K   t         j                  }t        j                         }| j                  }| j
                  }| j                  d   d   }t        }| j                  D ]  \  }	}
}||	   }||	   }t        ||j                               D ]  \  }\  }}|||kD  s||
k  r||cxkD  r|
k\  r(n n%||k  r  |||	      j                   |E d {    Jt        |      }||
k  r?|D ]9  }| ||j                        z   }||
kD  r |||	      j                   |  n|}; n |||	      j                   ||k  r|E d {    |D ]"  }||k\  r | | ||j                        z  }$  |	|k7  s|   y 7 7 ?w)Nr   )r   rG   r   rF   rn   rp   ro   r   zipitemsrJ   iterrR   )r6   r7   rK   rG   rL   rn   rp   last_y	_cell_lenrI   x1x2rF   endsendrE   rg   iter_segmentsrJ   next_xs                       r/   rN   zChopsUpdate.__rich_console__   s     //<<>

NN	B"	 %	IAr28DQ<D#&tTZZ\#: 5Za=r6SBY<R<C2I!!Q-///$$$ $Ur6#0 #!"Yw||%<!<!B;")!Q-"7"77")M!"# "!Q-///"9,,,#0 57!%Yw||44	575B F{K%	 % -s+   CF	FA3F	F3F	=	F	F	c           	        g }|j                   }t        j                  }| j                  }| j                  }| j
                  d   d   }| j
                  D ]  \  }}	}
||   }||   }t        ||j                               D ]  \  }\  }}|||
kD  s||	k  r|
|cxkD  r|	k\  rCn n@||
k  r; | |||      j                  j                          ||j                  |             e|j                  dt        ||
      |z
        } | |||      j                  j                          ||j                  |              ||k7  s |d        dj                  |      }|S )r4   rr   r   rP   r5   )rQ   r   rG   rn   rp   ro   rs   rt   rJ   rR   rS   cropminrT   )r6   r7   rU   rQ   rG   rn   rp   rv   rI   rx   ry   rF   rz   r{   rE   rg   terminal_sequencess                    r/   r8   zChopsUpdate.render_segments   sT     "	!!//

NN	B" 	IAr28DQ<D#&tTZZ\#: .Za=r6SBY<R<C2I71a=005565<<01

1c#rlQ&67wq!},,112u||G,-.  F{t)	,  WWY/!!r.   c              #  $   K   dE d {    y 7 w)Nr-   r-   rW   s    r/   rX   zChopsUpdate.__rich_repr__  s     s   N)rn   $Sequence[Mapping[int, Strip | None]]ro   zlist[tuple[int, int, int]]rp   list[list[int]]r:   rZ   r[   r9   r\   r]   r-   r.   r/   rl   rl      sY    @#3# *# #	#
 
#"//)7/	/b)"Vr.   rl   c                     e Zd ZdZddZddZe	 	 	 	 d d       Zd!dZd"dZ	d#dZ
ed$d       Zed%d	       Z	 d&	 	 	 	 	 	 	 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d0dZed1d       Z	 d2	 	 	 d3dZ	 	 	 d4	 	 	 	 	 	 	 d5dZ	 	 d6	 	 	 	 	 	 	 d7dZd8d9dZd:dZd2d;dZ	 	 	 	 	 	 d<dZd=dZ d>dZ!y)?
CompositorzcResponsible for storing information regarding the relative positions of Widgets and rendering them.c                    i | _         d| _        d | _        d | _        t	               | _        d | _        d | _        t        dd      | _	        d | _
        t	               | _        d | _        y )NTr   )	_full_map_full_map_invalidated_visible_map_layerssetwidgets_visible_widgetsrootr   size_cuts_dirty_regions_layers_visiblerW   s    r/   rC   zCompositor.__init__  sp    (*%)"26@D %(E MQ $(	 AJ	 .2
 ,/5 RVr.   c                    | j                   j                          d| _        d| _        | j                  j                          d| _        d| _        y)z?Remove all references to widgets (used when the screen closes).N)r   ra   r   r   r   r   r   rW   s    r/   ra   zCompositor.clear9  sA      $#r.   c              #    K   i }|j                   }|D ]:  \  }}}}|||z   f}t        |||z         D ]  }	 ||	g       j                  |        < t        dd      }
t	        |j                               D ]d  \  }	}t        |      dk(  r|	g|d     |j                          |d   \  }}||
   D ]  \  }}||k  r	||kD  s|}|	||f |}|}  |	||f f yw)a  Converts the regions to horizontal spans. Spans will be combined if they overlap
        or are contiguous to produce optimal non-overlapping spans.

        Args:
            regions: An iterable of Regions.

        Returns:
            Yields tuples of (Y, X1, X2).
        rd   Nr   )
setdefaultrangerQ   slicesortedrt   rf   sort)clsregionsinline_rangesr   region_xregion_ywidthheightspanrI   slice_remainingrangesrx   ry   next_x1next_x2s                   r/   _regions_to_spanszCompositor._regions_to_spansB  s     ;="--
18 	/-Hhvh./D8X%67 /1b!((./	/
  4. 3 3 56 	"IAv6{ao6!9o%B(.(? %$GW"}"R<!(B "bk)$$% "bk!	"s   C CCc              #  L   K   d| j                   f d| j                  f y w)Nr   r   )r   r   rW   s    r/   rX   zCompositor.__rich_repr__h  s$     dii%%s   "$c                   d| _         d| _        d| _        d| _        d| _        || _        || _        | j                  }|j                         }| j                  ||d      \  }}|j                         }||z
  }| j                  |z
  }	|| _        || _
        |j                         |j                         z  }
||z  }|j                  }|| j                  vr3d |
D        D ch c]  }|r| }}| j                  j                  |       |
D ch c]3  \  }^}}||v r(||   j                  j                  |j                  k7  r|5 }}}}t        |	||      S c c}w c c}}}w )zReflow (layout) widget and its children.

        Args:
            parent: The root widget.
            size: Size of the area to be filled.

        Returns:
            Hidden, shown, and resized widgets.
        NFvisible_onlyc              3  l   K   | ],  \  }}|j                   j                  |j                         . y wr@   clipintersectionrB   .0_map_geometrys      r/   	<genexpr>z$Compositor.reflow.<locals>.<genexpr>  3      '< !%%22<3F3FG   24)r%   r&   r'   )r   r   r   r   r   r   r   r   keys_arrange_rootr   rt   rB   r   updater#   )r6   parentr   old_mapold_widgetsmapr   new_widgetsshown_widgetshidden_widgetschangescommon_widgetsscreen_regionrB   r   widgetr   resized_widgetss                     r/   reflowzCompositor.reflowl  s    
# $ 		 ..lln))&$U)KWhhj $k1 /  ))+/ %{2  3 33+2  G  &&w/ )0
 
$!.(WV_-C-C-H-HFKK-W 
 

 !#
 	

s   "E8Ec                   d| _         d| _        d| _        d| _        d| _        || _        || _        | j                  xs i }| j                  ||d      \  }}|| _        || _	        |j                         |j                         z
  }|j                         |j                         z  }|j                  }|| j                  vr3d |D        D 	ch c]  }	|	r|	 }
}	| j                  j                  |
       |S c c}	w )a	  Reflow only the visible children.

        This is a fast-path for scrolling.

        Args:
            parent: The root widget.
            size: Size of the area to be filled.

        Returns:
            Set of widgets that were exposed by the scroll.
        NTr   c              3  l   K   | ],  \  }}|j                   j                  |j                         . y wr@   r   r   s      r/   r   z,Compositor.reflow_visible.<locals>.<genexpr>  r   r   )r   r   r   r   r   r   r   r   r   r   r   rt   rB   r   r   )r6   r   r   r   r   r   exposed_widgetsr   r   rB   r   s              r/   reflow_visiblezCompositor.reflow_visible  s
    
# $%)"		 ##)r))&$T)JW  ((*w||~5 ))+/  3 33+2  G  &&w/s   C8c                    | j                   i S | j                  rGd| _        | j                  | j                   | j                  d      \  }}|| _        d| _        d| _        | j                  S )z4Lazily built compositor map that covers all widgets.NFr   )r   r   r   r   r   r   r   )r6   r   _widgetss      r/   full_mapzCompositor.full_map  si     99I%%).D& ..tyy$))RW.XMC DN$(D! $D~~r.   c                   | j                   | j                  | j                  n| j                  xs i }| j                  j                  }|j
                  }t        j
                  }|j                         D 	cg c]$  \  }\  }}}}	}	}	}	 ||      r |||      r||||f& }
}}}}}	|
j                  t        d      d       |
D 	ci c]  \  }	}}}|||f c}}}}	| _         | j                   S c c}	}}}}w c c}}}}	w )znGet a mapping of widgets on to region and clip.

        Returns:
            Visible widget mapping.
        r   Tkeyreverse)
r   r   r   r   rB   overlapsr   rt   r   r   )r6   r   screen	in_screenr   r   rB   orderr   r   visible_widgetss              r/   r   zCompositor.visible_widgets  s      ( $$0 !!nn* 
 YY%%FIH
 BE =F=VUD!Q1V$$)? -O 
   Z]D AFU% %+B1ffd&%D! $$$%s   =)C7D 
c           
     J  	
 i 	t               j                  t               }|j                  d}j                  
t        f	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d	
fd |j                  j                  d|j                  dt               |z  	fS )a3  Arrange a widget's children based on its layout attribute.

        Args:
            root: Top level widget.
            size: Size of visible area (screen).
            visible_only: Only update visible widgets (used in scrolling).

        Returns:
            Compositor map and set of widgets.
        r   c	                   | j                   sy| j                  }	|	j                  d      x}
|
dk(  }|r	 )|        n (|        |j                  |	j                        }|j
                  }| j                  r*| j                  r|n| j                  |      }|j                  }| j                  r| j                  |j
                        }|j                  }/j                  |       |j                  |      }| j                  r| j                   s|j"                  j$                  j&                  | j(                  j*                  | j,                  z
  z
  }| j/                  t0        j2                  |       | j/                  t0        j4                  |       || j6                  _        .r,|j;                  ||j<                  z
  | j>                  z         }n|j@                  }|jC                  |j$                        }|j<                  }|| j>                  z
  }|D cg c]  }|jE                  -jF                  |        }}tI        | jJ                        D ci c]  \  }}||
 }}}|jL                  }| jN                  H ||j                  | j                  j                        |||j
                  |||      +| jN                  <   tQ        |      D ]l  \  }}}}}} }!}" ||jR                  d      }#| r	||z   |z   }$n||z   |z   }$||#||ffz   }%| jN                  ! *|||$|!rdn|%||!r,n|||jT                         |dz  }n |r^| jV                  s| jX                  r*| j[                  |      D ]  \  }&}' ||'|||||'|      +|&<     |||||j
                  |||      +| <   yy|r& |||||j
                  |||      +| j\                  <   yyc c}w c c}}w )an  Called recursively to place a widget and its children in the map.

            Args:
                widget: The widget to add.
                virtual_region: The Widget region relative to its container.
                region: The region the widget will occupy.
                order: Painting order information.
                layer_order: The order of the widget in its layer.
                clip: The clipping region (i.e. the viewport which contains it).
                visible: Whether the widget should be visible by default.
                    This may be overridden by the CSS rule `visibility`.
            N
visibilityvisibler   ))rd   r   r   rd   )/_is_mountedstylesget_ruleshrinkgutterr   is_scrollableloading_get_scrollable_regionreset_offsetis_containerarranger   r   r   	_anchored_anchor_releasedspatial_maptotal_regionbottomcontainer_sizer   scrollbar_size_horizontalset_reactiver   scroll_yscroll_target_yvertical_scrollbar_reactive_positionget_visible_placementsoffsetscroll_offset
placementsunionprocess_offsetrB   rH   layersget_cover_widgetreversedlayerscroll_spacingshow_vertical_scrollbarshow_horizontal_scrollbar_arrange_scrollbars_render_widget)0r   virtual_regionrB   r   layer_orderr   r   dock_gutter_MapGeometryr   r   container_regionr   child_regionr   arrange_resultarranged_widgetssub_clipnew_scroll_yr   placement_offsetplacement_scroll_offset	placementindex
layer_namelayers_to_indexget_layer_index
sub_regionsub_region_offsetr   
sub_widgetzfixedoverlayabsolutelayer_indexwidget_regionwidget_orderchrome_widgetchrome_regionadd_new_invisible_widgetadd_new_widget
add_widgetr   no_clipr   r   r   s0                                           r/   r!  z,Compositor._arrange_root.<locals>.add_widget#  sA   . %%]]F$ool;;
H$	1v&(0  &}}V]];-22N ## ~~ %667GH   ,88&&%+^^L4E4E%FN'5'='=$NN#34  $00>H''0G0G*66CCJJ & 5 5 < <"("B"B!C % ++FOO\J++F,B,BLQGS11D#%3%J%J$|':'::V=Q=QQ&
 &4%>%>
#/#5#5n6Q6Q#RL (8'>'>$.>AUAU.U+ *4"% "00>UV"J " 2;6==1I'-E: #E)'O '
 '6&9&9O++74@"MM&--*>*>?! "KK**'5F001( "*-") 	")" &5j6F6F&J  *-> >AQ Q *
 !+-> >AX X * (-a0M/O'O!//7& * * -07\ ++2 ' . = =	 $q(E")H  66!;;<B<V<V,= 8M= 2> - % $ . . - +2C. #/$))&&##CK' : -9KK"".F))* s"
's   #OO))r   r   r   T)r   r   r  r   rB   r   r   z tuple[tuple[int, int, int], ...]r  intr   r   r   rj   r  r   r  ztype[MapGeometry]r:   rZ   )r   addrB   r   r   )r6   r   r   r   invisible_widgetsr  r  r   r!  r   r"  r   s     ``  @@@@@@r/   r   zCompositor._arrange_root  s      "u ),#4#8#8 ++ /:x	x	"x	 x	 4	x	
 x	 x	 x	 !x	 ,x	 x	 x	v 	KKKKKK		
 	$$G|r.   c                    | j                   | j                   n| j                  }| j                  "t        |j	                         d d      | _        | j                  S )z(Get widgets and geometry in layer order.c                     | d   j                   S )Nrd   )r   )items    r/   <lambda>z#Compositor.layers.<locals>.<lambda>  s    d1gmm r.   Tr   )r   r   r   r   rt   )r6   r   s     r/   r   zCompositor.layers  sP     $(#4#4#@ddnn<<!		!;TDL ||r.   c                   | j                   t        | j                  j                        D cg c]  }g  }}|D cg c]  }|j                   }}t
        j                  }t        }| j                  j                         D ]<  \  }\  }}	 |||	      }
|
\  }}}}|s||
|f} ||||z         D ]  } ||   |        > || _         | j                   S c c}w c c}w )zVisible widgets and regions in layers order.

        Returns:
            Lists visible widgets per layer. Widgets are give as a tuple of
            (WIDGET, CROPPED_REGION, REGION). CROPPED_REGION is clipped by
            the container.

        )	r   r   r   r   rQ   r   r   r   rt   )r6   rI   layers_visibler   layers_visible_appendsr   _ranger   rB   r   cropped_region_xr   _widthregion_heightwidget_locations                   r/   r+  zCompositor.layers_visible  s     '*/		0@0@*ABQbBNB@N%Ouell%O"%O!..LF*.*>*>*D*D*F C&!-fd!;6D3Hfm '-~v&FO#Hh.FG C1.q1/BCC $2D ### C%Os
   	CCc                ~    || j                   v xs. | j                  duxr || j                  v xs || j                  v S )zCheck if the widget was included in the last update.

        Args:
            widget: A widget.

        Returns:
            `True` if the widget was in the last refresh, or `False` if it wasn't.
        N)r   r   r   r6   r   s     r/   __contains__zCompositor.__contains__  sI     dll" '!!-M&D<M<M2M'&	
r.   c                   	 | j                   $	 | j                   |   j                  j                  S | j                  |   j                  j                  S # t        $ r Y .w xY w# t        $ r t        j                  d      w xY w)zGet the offset of a widget.

        Args:
            widget: Widget to query.

        Returns:
            Offset of widget.
        Widget is not in layout)r   rB   r   KeyErrorr   r   NoWidgetr4  s     r/   
get_offsetzCompositor.get_offset   s    	=  ,,,V4;;BBB ==(//666     	=//";<<	=s-   A$ "A "A$ 	A!A$  A!!A$ $Bc                   t         j                  }t        | j                        |cxkD  rdk\  rAn n>| j                  t	        |         D ]#  \  }}} ||||      s|j
                  s||fc S  t        j                  d| d| d      )a  Get the widget under a given coordinate.

        Args:
            x: X Coordinate.
            y: Y Coordinate.

        Raises:
            errors.NoWidget: If there is not widget underneath (x, y).

        Returns:
            A tuple of the widget and its region.
        r   z#No widget under screen coordinate (z, ))r   containsrf   r+  r#  r   r   r9  r6   rE   rI   r=  r   r.  rB   s          r/   get_widget_atzCompositor.get_widget_at3  s     ??t""#a,1,262E2Ec!f2M *.NAq1fnn!6>)* oo CA3b1MNNr.   c              #     K   t         j                  }t        | j                        |cxkD  rdk\  r:n y| j                  |   D ]$  \  }}} ||||      s|j                  s||f & yyw)zGet all widgets under a given coordinate.

        Args:
            x: X coordinate.
            y: Y coordinate.

        Returns:
            Sequence of (WIDGET, REGION) tuples.
        r   N)r   r=  rf   r+  r   r>  s          r/   get_widgets_atzCompositor.get_widgets_atH  sk      ??t""#a,1,262E2Ea2H ).NAq1fnn &.() -s   AA,A,"
A,c                   	 | j                  ||      \  }}|| j
                  vrt        j                         S ||j                  z  }||j                  z  }t        j                  |j                  j                         |j                  t        d||j                  d            }|st        j                         S d}|d   D ]:  }||j                  z  }||k  s|j                   xs t        j                         c S  t        j                         S # t        j                  $ r t        j                         cY S w xY w)zGet the Style at the given cell or Style.null()

        Args:
            x: X position within the Layout.
            y: Y position within the Layout.

        Returns:
            The Style at the cell (x, y) within the Layout.
        r   rd   )r?  r   r9  r   nullr   rE   rI   r   r   app_background_screensrender_linesr   r   cell_lengthstyle)r6   rE   rI   r   rB   linesr{   rJ   s           r/   get_style_atzCompositor.get_style_atX  s   	 !//15NFF ---::<	VXX	VXX  !?!?@##F1aq$AB::<Qx 	5G7&&&C3w}}4

4	5
 zz|)  	 ::<	 s   D 'D?>D?c                   	 | j                  ||      \  }}|| j                  vry||j                  j
                  k\  r|j                  j                  \  }}|j                  j                  \  }}||j                  |z   z  }||j                  |z   z  }|dk  ryt        j                  |j                  j                         |j                  |      }d}d}	d}
d}d}|D ]  }|t!        |j"                        z  }|j$                  }||j&                  |j(                  }d|v r|j(                  d   \  }}
|t!        |j"                        z   }||k  ri||	k\  rd||dz
  k(  rt!        |j"                        }n,|j+                  ||	z
        \  }}t!        |j"                        }||
dfc S t-        ||z   |
      fc S |}	 ||
dfS t-        ||
      fS # t        j                  $ r Y yw xY w)a  Get the Style at the given cell, the offset within the content.

        Args:
            x: X position within the Layout.
            y: Y position within the Layout.

        Returns:
            A tuple of the widget at (x, y) and the offset within the widget.
        )NNr   Nr   rd   )r?  r   r9  r   content_regionr   bottom_right_inclusiver   top_leftrE   rI   r   r   rD  rE  render_linerf   rR   rH  _metametasplit_cellsr   )r6   rE   rI   r   rB   gutter_leftgutter_rightrF   r{   startoffset_yoffset_x	offset_x2rJ   rH  rQ  segment_offsetfirstr   s                      r/   get_widget_and_offset_atz#Compositor.get_widget_and_offset_atz  s   	!//15NFF ---%%,,,((??DAq$*MM$:$:!\	VXX##	VXX$$q5  !?!?@!!!$#	 	G3w||$$CMME U[[%<zzt#).H)=&Hh (3w||+< <I3w1:a<-0->N'.':':1u9'EHE1-0_N%'/ !   "(>(A8!L  
 E)	,  0RRfY6QRR]  		s   G G+*G+c                X   | j                   t        j                  d      	 | j                  s	 | j                  |   S | j                  	 | j                  |   S | j                  |   }|S # t
        $ r Y 8w xY w# t
        $ r Y +w xY w# t
        $ r t        j                  d      w xY w)aJ  Get information regarding the relative position of a widget in the Compositor.

        Args:
            widget: The Widget in this layout you wish to know the Region of.

        Raises:
            NoWidget: If the Widget is not contained in this Layout.

        Returns:
            Widget's composition information.
        r7  )r   r   r9  r   r   r8  r   r   )r6   r   rB   s      r/   find_widgetzCompositor.find_widget  s     99//";<<	-->>&11   ,,,V44 ]]6*F M   
     	=//";<<	=sQ   B
 A, B
 A; B
 ,	A85B
 7A88B
 ;	BB
 BB
 
B)c                   | j                   | j                   S | j                  \  }}t        |      D cg c]  }d|g }}t        j                  }t
        j                  }| j                  j                         D ]6  \  }} |||      \  }	}
}}|s|s|	|	|z   f}||
|
|z    D ]  } |||        8 |D cg c]  }t        t        |             c}| _         | j                   S c c}w c c}w )zGet vertical cuts.

        A cut is every point on a line where a widget starts or ends.

        Returns:
            A list of cuts for every line.
        r   )r   r   r   r   r   listextendr   valuesr   r   )r6   r   r   r   cutsr   r`  rB   r   rE   rI   region_widthr1  region_cutscut	line_cutss                   r/   rb  zCompositor.cuts  s     ::!::		v$)&M2qE
22** 00779 	-LFD0<VT0J-Aq, !l"23A$56 -C3,-		- ?CCfS^,C
zz 3 Ds   C(9C-Nc              #    K   d}t         }| j                  }|r?|j                  }|j                         D cg c]  \  }\  }} ||      r|||f }	}}}n*|j                         D cg c]  \  }\  }}|||f }	}}}|j                  }
|j
                  }|	D ]  \  }}} |||      r4|||j                   |dd|j                  |j                              f D |
||      \  }}}}|sU|sX|||j                   |||j                  z
  ||j                  z
  ||            f  yc c}}}w c c}}}w w)zGet rendered widgets (lists of segments) in the composition.

        Args:
            crop: Region to crop to, or `None` for entire screen.

        Returns:
            An iterable of <region>, <clip region>, and <strips>
        Tr   N)r   r   r   rt   r   contains_regionrF  r   r   rE   rI   )r6   r   _rich_traceback_guard_Regionr   crop_overlapsr   rB   r   widget_regionsr   rh  new_xnew_y	new_width
new_heights                   r/   _get_renderszCompositor._get_renders  s     !%.. MMM />.C.C.E *FNVT & &N  />.C.C.E *FNVT &N 
 ++!11$2 	 FFDtV,''"LL"MM	  7C646P3ui++# % 0 % 0 ) *	 #	s*   7D>D0D>,D7>A2D>1D>4A
D>c                    t        j                  |g n|       | j                  j                  }|s|| j                  v r| j                  |      S | j                         S )aK  Render an update renderable.

        Args:
            full: Perform a full update if `True`, otherwise a partial update.
            screen_stack: Screen stack list. Defaults to None.
            simplify: Simplify segments.

        Returns:
            A renderable for the update, or `None` if no update was required.
        )simplify)r   r   r   rB   r   render_full_updaterender_partial_update)r6   fullscreen_stackrs  r   s        r/   render_updatezCompositor.render_update6  s[    " 	  |';N		((=D$7$77**H*==--//r.   c                p    t        j                  |g n|       | j                  |      }t        ||      S )a  Render an inline update.

        Args:
            size: Inline size.
            screen_stack: Screen stack list. Defaults to None.
            clear: Also clear below the inline update (set when size decreases).

        Returns:
            A renderable.
        )ra   )r   r   render_stripsr_   )r6   r   rw  ra   rA   s        r/   render_inlinezCompositor.render_inlineN  s6      	  |';N##D)F%00r.   c                   | j                   j                  }| j                  j                          |}| j	                  |d       }|rM|D cg c]A  }t        j                  |j                               j                         j                         C }}n0|D cg c]%  }t        j                  |j                               ' }}t        ||      S c c}w c c}w )zRender a full update.

        Args:
            simplify: Simplify the segments (combine contiguous segments).

        Returns:
            A LayoutUpdate renderable.
        c                     yNTr-   rI   s    r/   r)  z/Compositor.render_full_update.<locals>.<lambda>n      r.   )r   rB   r   ra   _render_chopsr   rT   ra  rs  discard_metar>   )r6   rs  r   r   rn   choprz  s          r/   rt  zCompositor.render_full_updateb  s     		((!!#""48 QVHL

4;;=)224AACM  DII4UZZ6IMIM=99 Js   AC*Cc                   | j                   j                  }| j                  j                         }| j                  j	                          |r_t        j                  |      j                  |      }t        | j                  |            }|D ch c]  \  }}}|
 c}}j                  }ny| j                  ||      }| j                  D 	cg c]  }	|	dd 	 }
}	t        |||
      S c c}}w c c}	w )zRender a partial update.

        Returns:
            A ChopsUpdate if there is anything to update, otherwise `None`.
        Nrd   )r   rB   r   copyra   r   
from_unionr   r_  r   r5  r  rb  rl   )r6   r   update_regionsr   ro   rI   r   is_rendered_linern   cut_setrp   s              r/   ru  z Compositor.render_partial_updatez  s     		((,,113!!#$$^4AA-PD//?@E167gaA7DD""4)9:04		:WWQR[:	:5%33  8 ;s   C(C.c                    || j                   }| j                  |j                  d       }|d|j                   D cg c]%  }t	        j
                  |j                               ' }}|S c c}w )zRender to a list of strips.

        Args:
            size: Size of render.

        Returns:
            A list of strips with the screen content.
        Nc                     yr~  r-   r  s    r/   r)  z*Compositor.render_strips.<locals>.<lambda>  r  r.   )r   r  rB   r   r   rT   ra  )r6   r   rn   r  rz  s        r/   rz  zCompositor.render_strips  sa     <99D""4;;??D]t{{?STtDKKM2TT Us   *A+c           	        | j                   }t        dt        j                        }|D cg c]  } ||dd        }}| j	                  |      }t
        j                  }|D ]  \  }	}
} ||	|
      }|j                  }|j                  \  }}t        |j                  |      D ]  \  }} ||      s||   }||   D cg c]  }||cxk\  r|k\  sn n| }}|j                  |dd D cg c]  }||z
  	 c}      }|j                  }t        ||      D ]  \  }} ||      |||<      |S c c}w c c}w c c}w )zRender update 'chops'.

        Args:
            crop: Region to crop to.
            is_rendered_line: Callable to check if line should be rendered.

        Returns:
            Chops structure.
        z.Callable[[list[int]], dict[int, Strip | None]]Nrr   rd   )rb  r   dictfromkeysrq  r   r   rE   column_spanrs   
line_rangedivider   )r6   r   r  rb  r  r  rn   rendersr   rB   r   rA   render_regionrender_x	first_cutlast_cutrI   rg   
chops_linere  
final_cuts
cut_stripsget_chops_lines                          r/   r  zCompositor._render_chops  s^    yyH$--X7;<G'#2,'<<
 ##D)**$+ 	0 FD&(6M$H"/";";Ix 8 8&A 05'*"1X
-1!WWcS9UI9UcW
W"\\ZPQPR^*Tc3>*TU
 ","%j*"= 0JC%c*2*/
300	0& 7 =$ X*Ts   D1>D6D6*D;c                4    t        | j                               S r@   )r   rz  rW   s    r/   __rich__zCompositor.__rich__  s    t11344r.   c                   | j                   s0|j                  | j                  j                               sd| _         g }|j                  }| j                  j
                  }| j                  j                         |z  D ]\  } ||      \  }}|j                  }|j                  }	|j                         D ]$  }
 |	|
j                  |            }|s ||       & ^ | j                  j                  |       y)zsUpdate the given widgets in the composition.

        Args:
            widgets: Set of Widgets to update.
        TN)r   issubsetr   r   rQ   __getitem__r   r   _exchange_repaint_regions	translater   r   )r6   r   r   
add_region
get_widgetr   rB   r   r   r   dirty_regionupdate_regions               r/   update_widgetszCompositor.update_widgets  s     ))'2B2B  %%'3
 *.D& "^^
))55
**//1G; 	.F%f-LFD]]F,,L & @ @ B . ,\-C-CF-K L }-.		. 	""7+r.   )r:   rZ   )r   zIterable[Region]r:   zIterable[tuple[int, int, int]]r\   )r   r   r   r   r:   r#   )r   r   r   r   r:   r$   )r:   r0   )r:   z#dict[Widget, tuple[Region, Region]])T)r   r   r   r   r   rj   r:   z!tuple[CompositorMap, set[Widget]])r:   z list[tuple[Widget, MapGeometry]])r:   z)list[list[tuple[Widget, Region, Region]]])r   r   r:   rj   )r   r   r:   r   )rE   r#  rI   r#  r:   ztuple[Widget, Region])rE   r#  rI   r#  r:   zIterable[tuple[Widget, Region]])rE   r#  rI   r#  r:   r   )rE   r#  rI   r#  r:   z#tuple[Widget | None, Offset | None])r   r   r:   r   )r:   r   r@   )r   zRegion | Noner:   z,Iterable[tuple[Region, Region, list[Strip]]])FNF)rv  rj   rw  list[Screen] | Noners  rj   r:   zRenderableType | None)NF)r   r   rw  r  ra   rj   r:   r   ri   )rs  rj   r:   r>   )r:   zChopsUpdate | None)r   zSize | Noner:   rY   )r   r   r  zCallable[[int], bool]r:   r   )r:   r   )r   r$   r:   rZ   )"r(   r)   r*   r+   rC   ra   classmethodr   rX   r   r   propertyr   r   r   r   r+  r5  r:  r?  rA  rJ  r[  r]  rb  rq  rx  r{  rt  ru  rz  r  r  r  r-   r.   r/   r   r     s   mV:$ #"&#"	'#" #"J&@
D.`   % %< >B]]"&]6:]	*]~   $ $4
 =&O*)  D<S<S<S	,<S|>  < %)?!?	5?F ,0	00 *0 	0
 
06 -1	11 *1 	1
 
1(:04(,, 0, 
.	,\5,r.   r   )=r+   
__future__r   operatorr   typingr   r   r   r   r	   r
   r   	rich.reprrichrich.consoler   r   r   r   rich.controlr   rich.segmentr   
rich.styler   textualr   textual._cellsr   textual._contextr   textual._loopr   textual.geometryr   r   r   r   r   textual.map_geometryr   textual.stripr   r   textual.widgetr   typing_extensionsr    textual.screenr!   r#   r0   r,   r2   reprautor>   r_   rl   r   r-   r.   r/   <module>r     s-  
 #     N N       # 1 # H H , 0 !+%:  7y 6  (# ( (V )"# )" )"X q" q qh N, N, N,r.   