
    AHj;                    $   d dl mZ d dlmZ d dlmZmZ d dlZd dl	m
Z
 d dlmZmZ d dlmZmZ d dlmZ er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 d dlmZ d dl m!Z! d dl"m#Z# d dl$m%Z% ed   Z&	 h dZ' G d de(      Z) G d de%d      Z*y)    )annotations)partial)TYPE_CHECKINGcastN)cell_len)ConsoleRenderableRenderableType)LiteralSelf)events)RenderResult)Style)Binding)ContentContentText)friendly_list)Size)Message)reactive)Widget)defaultprimarysuccesswarningerror>   r   r   r   r   r   c                      e Zd ZdZy)InvalidButtonVariantz6Exception raised if an invalid button variant is used.N)__name__
__module____qualname____doc__     O/root/tools/cai/cai_env/lib/python3.12/site-packages/textual/widgets/_button.pyr   r   #   s    @r#   r   c            
      <    e Zd ZU dZdZdZ edddd      gZ ee	   e
j                        Zded	<   	  ed
d      Z	  edd      Z	  ed      Z	  G d de      Z	 	 d#ddddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d$ fdZd%dZd& fdZd'dZd(dZd)dZd*dZd+dZ	 	 	 	 	 	 d,dZd-dZd.dZd/dZd/dZe 	 d0dddddd	 	 	 	 	 	 	 	 	 	 	 	 	 d1d        Z!e 	 d0dddddd	 	 	 	 	 	 	 	 	 	 	 	 	 d1d!       Z"e 	 d0dddddd	 	 	 	 	 	 	 	 	 	 	 	 	 d1d"       Z# xZ$S )2ButtonzA simple clickable button.

    Clicking the button will send a [Button.Pressed][textual.widgets.Button.Pressed] message,
    unless the `action` parameter is provided.

    Fa  
    Button {
        width: auto;
        min-width: 16;
        height:auto;
        line-pad: 1;
        text-align: center;
        content-align: center middle;
        

        &.-style-flat {
            text-style: bold;
            color: auto 90%;
            background: $surface;
            border: block $surface;
            &:hover {
                background: $primary;
                border: block $primary;
            }
            &:focus {
                text-style: $button-focus-text-style;
            }
            &.-active {
                background: $surface;
                border: block $surface;
                tint: $background 30%;
            }
            &:disabled {
                color: auto 50%;
            }

            &.-primary {
                background: $primary-muted;
                border: block $primary-muted;
                color: $text-primary;
                &:hover {
                    color: $text;
                    background: $primary;
                    border: block $primary;
                }
            }
            &.-success {
                background: $success-muted;
                border: block $success-muted;
                color: $text-success;
                &:hover {
                    color: $text;
                    background: $success;
                    border: block $success;
                }
            }
            &.-warning {
                background: $warning-muted;
                border: block $warning-muted;
                color: $text-warning;
                &:hover {
                    color: $text;
                    background: $warning;
                    border: block $warning;
                }
            }
            &.-error {
                background: $error-muted;
                border: block $error-muted;
                color: $text-error;
                &:hover {
                    color: $text;
                    background: $error;
                    border: block $error;
                }
            }
        }
        &.-style-default {
            text-style: bold;
            color: $button-foreground;
            background: $surface;
            border: none;
            border-top: tall $surface-lighten-1;
            border-bottom: tall $surface-darken-1;
            

            &.-textual-compact {
                border: none !important;
            }

            &:disabled {
                text-opacity: 0.6;
            }

            &:focus {
                text-style: $button-focus-text-style;
                background-tint: $foreground 5%;
            }
            &:hover {
                border-top: tall $surface;
                background: $surface-darken-1;
            }
    
            &.-active {
                background: $surface;
                border-bottom: tall $surface-lighten-1;
                border-top: tall $surface-darken-1;
                tint: $background 30%;
            }

            &.-primary {
                color: $button-color-foreground;
                background: $primary;
                border-top: tall $primary-lighten-3;
                border-bottom: tall $primary-darken-3;

                &:hover {
                    background: $primary-darken-2;
                    border-top: tall $primary;
                }

                &.-active {
                    background: $primary;
                    border-bottom: tall $primary-lighten-3;
                    border-top: tall $primary-darken-3;
                }
            }

            &.-success {
                color: $button-color-foreground;
                background: $success;
                border-top: tall $success-lighten-2;
                border-bottom: tall $success-darken-3;

                &:hover {
                    background: $success-darken-2;
                    border-top: tall $success;
                }

                &.-active {
                    background: $success;
                    border-bottom: tall $success-lighten-2;
                    border-top: tall $success-darken-2;
                }
            }

            &.-warning{
                color: $button-color-foreground;
                background: $warning;
                border-top: tall $warning-lighten-2;
                border-bottom: tall $warning-darken-3;

                &:hover {
                    background: $warning-darken-2;
                    border-top: tall $warning;
                }

                &.-active {
                    background: $warning;
                    border-bottom: tall $warning-lighten-2;
                    border-top: tall $warning-darken-2;
                }
            }

            &.-error {
                color: $button-color-foreground;
                background: $error;
                border-top: tall $error-lighten-2;
                border-bottom: tall $error-darken-3;

                &:hover {
                    background: $error-darken-1;
                    border-top: tall $error;
                }

                &.-active {
                    background: $error;
                    border-bottom: tall $error-lighten-2;
                    border-top: tall $error-darken-2;
                }
            }
        }
    }
    enterpresszPress button)showzreactive[ContentText]labelr   )initz-textual-compact)toggle_classc                  6     e Zd ZdZd fdZedd       Z xZS )Button.PressedzEvent sent when a `Button` is pressed and there is no Button action.

        Can be handled using `on_button_pressed` in a subclass of
        [`Button`][textual.widgets.Button] or in a parent widget in the DOM.
        c                2    || _         	 t        | 	          y N)buttonsuper__init__)selfr1   	__class__s     r$   r3   zButton.Pressed.__init__   s    "(DK.Gr#   c                    | j                   S )zAn alias for [Pressed.button][textual.widgets.Button.Pressed.button].

            This will be the same value as [Pressed.button][textual.widgets.Button.Pressed.button].
            )r1   r4   s    r$   controlzButton.Pressed.control   s     ;;r#   )r1   r&   returnNone)r9   r&   )r   r   r    r!   r3   propertyr8   __classcell__r5   s   @r$   Pressedr.      s!    		
 
	 
	r#   r>   N)nameidclassesdisabledtooltipactioncompactflatc                   t         |   ||||       || j                  }t        j                  |      | _        || _        || _        |	| _        |
| _	        d| _
        	 ||| _        yy)a1  Create a Button widget.

        Args:
            label: The text that appears within the button.
            variant: The variant of the button.
            name: The name of the button.
            id: The ID of the button in the DOM.
            classes: The CSS classes of the button.
            disabled: Whether the button is disabled or not.
            tooltip: Optional tooltip.
            action: Optional action to run when clicked.
            compact: Enable compact button style.
            flat: Enable alternative flat look buttons.
        )r?   r@   rA   rB   Ng?)r2   r3   css_identifier_styledr   	from_textr*   variantrD   rE   rF   active_effect_durationrC   )r4   r*   rJ   r?   r@   rA   rB   rC   rD   rE   rF   r5   s              r$   r3   zButton.__init__  sv    8 	dr7XN=..E&&u-
	&)#K"DL r#   c                    t        | j                  t              sJ 	 t        | j                  j                  j                         D cg c]  }t        |       c}      dz   S c c}w # t        $ r Y yw xY w)N   )
isinstancer*   r   maxplain
splitlinesr   
ValueError)r4   	containerviewportlines       r$   get_content_widthzButton.get_content_width3  sc    $**g...	4::3C3C3N3N3PQ4QRUVVVQ 		s#   +A* 	A%	A* %A* *	A65A6c              #  `   K   t         |          E d {    d| j                  df y 7 w)NrJ   r   )r2   __rich_repr__rJ   )r4   r5   s    r$   rX   zButton.__rich_repr__;  s.     7(***y00 	+s   .,.c                L    |t         vrt        dt        t                      |S )NzValid button variants are )_VALID_BUTTON_VARIANTSr   r   )r4   rJ   s     r$   validate_variantzButton.validate_variant?  s0    00&,];Q-R,ST  r#   c                T    | j                  d|        | j                  d|        y )N-)remove_class	add_class)r4   old_variantrJ   s      r$   watch_variantzButton.watch_variantF  s)    Ak]+,7)}%r#   c                N    | j                  |d       | j                  | d       y )Nz-style-flatz-style-default)	set_class)r4   rF   s     r$   
watch_flatzButton.watch_flatJ  s!    t]+4x!12r#   c                ,    t        j                  |      S )zParse markup for self.label)r   rI   )r4   r*   s     r$   validate_labelzButton.validate_labelN  s      ''r#   c                R    t        | j                  t              sJ | j                  S r0   )rN   r*   r   r7   s    r$   renderzButton.renderR  s     $**g...zzr#   c                "    t        t        |      S r0   )r   r   )r4   
renderable
base_styles      r$   post_renderzButton.post_renderV  s     %z22r#   c                p   K   |j                          | j                  d      s| j                          y y w)N-active)stop	has_classr(   )r4   events     r$   	_on_clickzButton._on_click[  s)     

~~i(JJL )s   46c                6   | j                   s| j                  s| S | j                          | j                  &| j	                  t
        j                  |              | S | j                  | j                  j                  | j                  | j                         | S )zAnimate the button and send the [Pressed][textual.widgets.Button.Pressed] message.

        Can be used to simulate the button being pressed by a user.

        Returns:
            The button instance.
        )default_namespace)rB   display_start_active_affectrD   post_messager&   r>   
call_laterapp
run_action_parentr7   s    r$   r(   zButton.press`  s}     ==K!!#;;fnnT23
  OO##T[[DLL   r#   c                    | j                   dkD  rB| j                  d       | j                  | j                   t        | j                  d             yy)z7Start a small animation to show the button was clicked.r   rn   N)rK   r_   	set_timerr   r^   r7   s    r$   rv   zButton._start_active_affectu  sE    &&*NN9%NN++WT5F5F	-R +r#   c                H    | j                  d      s| j                          yy)zActivate a press of the button.rn   N)rp   r(   r7   s    r$   action_presszButton.action_press}  s    ~~i(JJL )r#   )r?   r@   rA   rB   rF   c          	     &    t        |d|||||      S )a.  Utility constructor for creating a success Button variant.

        Args:
            label: The text that appears within the button.
            name: The name of the button.
            id: The ID of the button in the DOM.
            classes: The CSS classes of the button.
            disabled: Whether the button is disabled or not.
            flat: Enable alternative flat look buttons.

        Returns:
            A [`Button`][textual.widgets.Button] widget of the 'success'
                [variant][textual.widgets.button.ButtonVariant].
        r   r*   rJ   r?   r@   rA   rB   rF   r&   clsr*   r?   r@   rA   rB   rF   s          r$   r   zButton.success  &    2 
 	
r#   c          	     &    t        |d|||||      S )a.  Utility constructor for creating a warning Button variant.

        Args:
            label: The text that appears within the button.
            name: The name of the button.
            id: The ID of the button in the DOM.
            classes: The CSS classes of the button.
            disabled: Whether the button is disabled or not.
            flat: Enable alternative flat look buttons.

        Returns:
            A [`Button`][textual.widgets.Button] widget of the 'warning'
                [variant][textual.widgets.button.ButtonVariant].
        r   r   r   r   s          r$   r   zButton.warning  r   r#   c          	     &    t        |d|||||      S )a+  Utility constructor for creating an error Button variant.

        Args:
            label: The text that appears within the button.
            name: The name of the button.
            id: The ID of the button in the DOM.
            classes: The CSS classes of the button.
            disabled: Whether the button is disabled or not.
            flat: Enable alternative flat look buttons.

        Returns:
            A [`Button`][textual.widgets.Button] widget of the 'error'
                [variant][textual.widgets.button.ButtonVariant].
        r   r   r   r   s          r$   r   zButton.error  s&    2 
 	
r#   )Nr   )r*   ContentText | NonerJ   ButtonVariantr?   
str | Noner@   r   rA   r   rB   boolrC   zRenderableType | NonerD   r   rE   r   rF   r   )rS   r   rT   r   r9   int)r9   zrich.repr.Result)rJ   strr9   r   )r`   r   rJ   r   )rF   r   r9   r:   )r*   r   r9   r   )r9   r   )rj   r	   rk   r   r9   r   )rq   zevents.Clickr9   r:   )r9   r   )r9   r:   r0   )r*   r   r?   r   r@   r   rA   r   rB   r   rF   r   r9   r&   )%r   r   r    r!   ALLOW_SELECTDEFAULT_CSSr   BINDINGSr   r   r   emptyr*   __annotations__rJ   rE   rF   r   r>   r3   rV   rX   r[   ra   rd   rf   rh   rl   rr   r(   rv   r   classmethodr   r   r   r<   r=   s   @r$   r&   r&   '   s    LrKh .uEFH#88K#8#GE G8yu-G*u+=>G4E?D/' , %)!**#
  ")-!*#!*# *#
 *# *# *# *# '*# *# *# *#X1&3(3(36;3	3

*
  %) 
  " 
! 
 	 

  
  
  
  
 
 
  
D  %) 
  " 
! 
 	 

  
  
  
  
 
 
  
D  %) 
  " 
! 
 	 

  
  
  
  
 
 
  
r#   r&   T)	can_focus)+
__future__r   	functoolsr   typingr   r   	rich.reprrich
rich.cellsr   rich.consoler   r	   typing_extensionsr
   r   textualr   textual.appr   
rich.styler   textual.bindingr   textual.contentr   r   textual.css._error_toolsr   textual.geometryr   textual.messager   textual.reactiver   textual.widgetr   r   rZ   	Exceptionr   r&   r"   r#   r$   <module>r      sr    "  &   : + (  # 0 2 ! # % !KL
 O A9 AB
Vt B
r#   