a
    §ˆc«&  ã                   @   sÔ   d Z ddlmZmZmZmZmZmZ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 e	eƒ d�ZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )z° Defines KeyBinding and KeyBindings classes, which manage the mapping of
    keystroke events into method calls on controller objects that are supplied
    by the application.
é    )
ÚAnyÚEventÚHasPrivateTraitsÚHasStrictTraitsÚInstanceÚListÚPropertyÚStrÚcached_propertyÚobserve)ÚSequenceTypesé   )ÚKeyBindingEditor)Ú
ListEditor)ÚHGroup)Ú	ModelView)ÚItem)Útoolkit)ÚView)Úeditorc                   @   s`   e Zd ZdZeZeZeƒ Zeƒ Z	e
dƒZdd„ Zdd„ Zeeedƒedƒed	d
d�dd�ƒZdS )Ú
KeyBindingz(Binds one or two keystrokes to a method.ÚKeyBindingsc                    s   t ‡ fdd„| j| jhD ƒƒS )Nc                 3   s   | ]}ˆ |kV  qd S ©N© )Ú.0Úx©Úbindingr   úX/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traitsui/key_bindings.pyÚ	<genexpr>B   ó    z#KeyBinding.match.<locals>.<genexpr>)ÚanyÚbinding1Úbinding2©Úselfr   r   r   r   ÚmatchA   s    zKeyBinding.matchc                 C   s,   || j kr| j| _ d| _|| jkr(d| _d S )NÚ )r"   r#   r$   r   r   r   Úclear_bindingD   s
    

zKeyBinding.clear_bindingr"   r#   ÚdescriptionÚreadonly©ÚstyleF)Zshow_labelsN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚBindingr"   r#   r	   r)   Úmethod_namer   Úownerr&   r(   r   r   r   Útraits_viewr   r   r   r   r   +   s    
üÿr   c                       sØ   e Zd ZdZeeeƒƒZeƒ Z	eƒ Z
edd�Zed dd�Zedd�Zedd�Z‡ fdd„Zg fdd	„Zd
d„ Zdd„ Zdd„ Zdd„ Zedd„ ƒZedƒdd„ ƒZedƒdd„ ƒZdd„ Zdd„ Zeƒ fdd„Z‡  ZS ) r   zA set of key bindings.T)Z	transientÚparent)r   c                    s\   t |ƒdkr"t|d tƒr"|d }| dt|ƒ¡ |d D ]
}| |_q:tƒ jf i |¤Ž d S )Nr   r   Úbindings)ÚlenÚ
isinstancer   Ú
setdefaultÚlistr3   ÚsuperÚ__init__)r%   r6   Útraitsr   ©Ú	__class__r   r   r<   |   s    zKeyBindings.__init__c              	   O   sR   t |tƒrt| ¡ ƒ}nt |tƒs*|g}nt|ƒ}|  tƒ  |¡||| dd¡¡S )zProcesses a keyboard event.Ú	recursiveF)	r8   Údictr:   Úvaluesr   Ú_dor   Zkey_event_to_nameÚget)r%   ÚeventÚcontrollersÚargsÚkwr   r   r   Údo†   s    



üzKeyBindings.doc                 C   sN   i }| j D ]}|||j< q
|j D ](}| |j¡}|dur |j|_|j|_q dS )z1Merges another set of key bindings into this set.N)r6   r2   rD   r"   r#   )r%   Zkey_bindingsZbinding_dicr   r#   r   r   r   Úmerge–   s    

zKeyBindings.mergec                 K   s&   | j | ji |¤Žjf i |  dd¡¤ŽS )z*Returns a clone of the KeyBindings object.ÚprefixÚsuffix)r?   r6   Z	trait_setZ	trait_get)r%   r=   r   r   r   Úclone¢   s    
ÿzKeyBindings.clonec                 C   s4   | j dur| j j | ¡ | `| `| `d | _ | _dS )zDispose of the object.N)r5   ÚchildrenÚremoverF   r6   Ú_root)r%   r   r   r   Údispose¨   s    
zKeyBindings.disposec                 C   s   t | d�}| ¡ }dS )z1Edits a possibly hierarchical set of KeyBindings.)ÚmodelN)ÚKeyBindingsHandlerZedit_traits)r%   Z
model_viewZuir   r   r   Úedit³   s    
zKeyBindings.editc                 C   s   | }|j d ur|j }q|S r   )r5   )r%   Úrootr   r   r   Ú	_get_rootº   s    
zKeyBindings._get_rootz"bindings:items:[binding1,binding2]c                 C   s4   |j dkr0| j|j |jhd�D ]}| |j ¡ qd S )Nr'   ©Úskip)ÚnewÚ_match_bindingÚobjectr(   )r%   rE   Ú	a_bindingr   r   r   Ú_binding_updatedÄ   s    
zKeyBindings._binding_updatedzchildren.itemsc                 C   s6   t |jtƒr |jD ]
}| |_qn|jD ]
}| |_q&dS )z4Handles child KeyBindings being added to the object.N)r8   r[   r   rY   r5   Úadded)r%   rE   Úitemr   r   r   Ú_children_modifiedÊ   s
    


zKeyBindings._children_modifiedc                 C   s&   |  | j¡ | jD ]}| |¡ q|S )zCReturns all of the bindings of this object and all of its children.)Úextendr6   rN   Ú_get_bindings)r%   r6   Úchildr   r   r   rb   Ø   s    
zKeyBindings._get_bindingsc                 C   s¢   |   |¡D ]l}d| j|j| jf }|| j D ]0}t||dƒ}|dur,||Ž }	|	dur,  dS q,|jdkr
|  ¡   dS q
|rž| jD ]}
|
 ||||¡r‚ dS q‚dS )z„Process the specified key for the specified set of controllers for
        this KeyBindings object and all of its children.
        z%s%s%sNFTZedit_bindings)	rZ   rK   r2   rL   rF   ÚgetattrrT   rN   rC   )r%   Úkey_namerF   rG   r@   r   r2   Ú
controllerÚmethodÚresultrc   r   r   r   rC   à   s(    ý


zKeyBindings._doc                    s   ‡ ‡fdd„| j D ƒS )úFReturn all KeyBinding instances that match the given binding.
        c                 3   s$   | ]}|ˆvr|  ˆ ¡r|V  qd S r   ©r&   ©r   r\   ©r   rX   r   r   r     s   þz-KeyBindings._match_binding.<locals>.<genexpr>©r6   ©r%   r   rX   r   rl   r   rZ   ÿ   s    þzKeyBindings._match_binding) r-   r.   r/   r0   r   r   r   r6   r	   rK   rL   rF   r5   r   rU   rN   r<   rI   rJ   rM   rQ   rT   r
   rV   r   r]   r`   rb   rC   Ú	frozensetrZ   Ú__classcell__r   r   r>   r   r   Y   s.   




	

r   c                   @   s`   e Zd ZeeeƒƒZdd„ Zeƒ fdd„Z	dd„ Z
eeddd	ed
d�d�dgdddddd�ZdS )rS   c                 C   s(   |dkr$| j ||hd�D ]
}|  S dS )z9Returns the current binding for a specified key (if any).r'   rW   N)rZ   )r%   r   re   r\   r   r   r   Úkey_binding_for  s    z"KeyBindingsHandler.key_binding_forc                    s   ‡ ‡fdd„| j D ƒS )ri   c                 3   s$   | ]}|ˆvr|  ˆ ¡r|V  qd S r   rj   rk   rl   r   r   r     s   þz4KeyBindingsHandler._match_binding.<locals>.<genexpr>rm   rn   r   rl   r   rZ     s    þz!KeyBindingsHandler._match_bindingc                 C   s*   t t| jj g ¡ƒƒ}|jdd„ d� |S )Nc                 S   s   | j dd … | j fS )Néÿÿÿÿ)r"   )r   r   r   r   Ú<lambda>   r    z6KeyBindingsHandler._bindings_default.<locals>.<lambda>)Úkey)r:   ÚsetrR   rU   rb   Úsort)r%   r6   r   r   r   Ú_bindings_default  s    z$KeyBindingsHandler._bindings_defaultr6   r*   FZcustomr+   )r,   Z
show_labelr   z]|{Click on an entry field, then press the key to assign. Double-click a field to clear it.}<>zUpdate Key BindingsZ	livemodalTgš™™™™™Ù?)ÚtitleÚkindZ	resizableÚwidthÚheightN)r-   r.   r/   r   r   r   r6   rq   ro   rZ   rw   r   r   r   r4   r   r   r   r   rS   	  s&   	üù
ñrS   N)r0   Z
traits.apir   r   r   r   r   r   r   r	   r
   r   Ztraits.trait_baser   Zeditors.key_binding_editorr   Zeditors.list_editorr   Úgroupr   Úhandlerr   r_   r   r   Úviewr   r1   r   r   rS   r   r   r   r   Ú<module>   s   0. 1