
    AHj                        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  ed      Z G d d	e      Z G d
 de      Z	 d	 	 	 	 	 	 	 ddZy)    )annotations)CallableTypeVar)SelectorSet)parse_selectors)
TokenError)MessageDecoratedTypec                      e Zd ZdZy)OnDecoratorErrorzkErrors related to the `on` decorator.

    Typically raised at import time as an early warning system.
    N__name__
__module____qualname____doc__     C/root/tools/cai/cai_env/lib/python3.12/site-packages/textual/_on.pyr   r      s    r   r   c                      e Zd ZdZy)
OnNoWidgetz;A selector was applied to an attribute that isn't a widget.Nr   r   r   r   r   r      s    Er   r   Nc           	         i }|||d<   |r|j                  |       i |j                         D ]n  \  }}|dk(  r( j                  t        j                  k(  r8t	        d      | j
                  vrt	        d|d j                   dz         	 t        |      |<   p d fd
}|S # t        $ r t	        d|d| d	      dw xY w)a  Decorator to declare that the method is a message handler.

    The decorator accepts an optional CSS selector that will be matched against a widget exposed by
    a `control` property on the message.

    Example:
        ```python
        # Handle the press of buttons with ID "#quit".
        @on(Button.Pressed, "#quit")
        def quit_button(self) -> None:
            self.app.quit()
        ```

    Keyword arguments can be used to match additional selectors for attributes
    listed in [`ALLOW_SELECTOR_MATCH`][textual.message.Message.ALLOW_SELECTOR_MATCH].

    Example:
        ```python
        # Handle the activation of the tab "#home" within the `TabbedContent` "#tabs".
        @on(TabbedContent.TabActivated, "#tabs", pane="#home")
        def switch_to_home(self) -> None:
            self.log("Switching back to the home tab.")
            ...
        ```

    Args:
        message_type: The message type (i.e. the class).
        selector: An optional [selector](/guide/CSS#selectors). If supplied, the handler will only be called if `selector`
            matches the widget from the `control` attribute of the message.
        **kwargs: Additional selectors for other attributes of the message.
    NcontrolzFThe message class must have a 'control' to match with the on decoratorzThe attribute z( can't be matched; have you added it to z.ALLOW_SELECTOR_MATCH?zUnable to parse selector z for z; check for syntax errorsc                t    t        | d      st        | dg        t        | d      j                  f       | S )zLStore message and selector in function attribute, return callable unaltered._textual_on)hasattrsetattrgetattrappend)methodmessage_typeparsed_selectorss    r   	decoratorzon.<locals>.decoratorT   s;     v}-FM2.&--|=M.NOr   )r   r
   returnr
   )	updateitemsr   r	   r   ALLOW_SELECTOR_MATCHr   r   r   )r    selectorkwargs	selectors	attributecss_selectorr"   r!   s   `      @r   onr,      s   F !#I'	) ;=#,??#4 	<	!##w6&\  l???" -UV!**++ABC 	*9,*GY'$   	"+L+;5Kde	s   B++C)N)r    ztype[Message]r'   z
str | Noner(   strr#   z(Callable[[DecoratedType], DecoratedType])
__future__r   typingr   r   textual.css.modelr   textual.css.parser   textual.css.tokenizerr   textual.messager	   r
   	Exceptionr   r   r,   r   r   r   <module>r5      sj    " $ ) - , #(y F F
 9=EE+5EHKE-Er   