U
    PeEw                     @   s&  d Z d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 ddlmZ ddl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Z#ere$ddZ#G dd de%Z&G dd de'Z(G dd de	e(Z)G dd dee(Z*G dd de(eZ+G dd de(eZ,G dd  d e(eZ-G d!d" d"e(eZ.G d#d$ d$eZ/G d%d& d&e(e	Z0G d'd( d(e0Z1G d)d* d*eZ2G d+d, d,e2Z3G d-d. d.eZ4e5d/kr"dd0l6m7Z7 dd1l8m9Z9 dd2l:m;Z; e;<d e =d3 G d4d5 d5e9Z>e> Z?e7e? d6S )7a  
Action Bar
==========

.. versionadded:: 1.8.0

.. image:: images/actionbar.png
    :align: right

The ActionBar widget is like Android's `ActionBar
<http://developer.android.com/guide/topics/ui/actionbar.html>`_, where items
are stacked horizontally. When the area becomes to small, widgets are moved
into the :class:`ActionOverflow` area.

An :class:`ActionBar` contains an :class:`ActionView` with various
:class:`ContextualActionViews <kivy.uix.actionbar.ContextualActionView>`.
An :class:`ActionView` will contain an :class:`ActionPrevious` having title,
app_icon and previous_icon properties. An :class:`ActionView` will contain
subclasses of :class:`ActionItems <ActionItem>`. Some predefined ones include
an :class:`ActionButton`, an :class:`ActionToggleButton`, an
:class:`ActionCheck`, an :class:`ActionSeparator` and an :class:`ActionGroup`.

An :class:`ActionGroup` is used to display :class:`ActionItems <ActionItem>`
in a group. An :class:`ActionView` will always display an :class:`ActionGroup`
after other :class:`ActionItems <ActionItem>`. An :class:`ActionView` contains
an :class:`ActionOverflow`, but this is only made visible when required i.e.
the available area is too small to fit all the widgets. A
:class:`ContextualActionView` is a subclass of an:class:`ActionView`.

.. versionchanged:: 1.10.1
    :class:`ActionGroup` core rewritten from :class:`Spinner` to pure
    :class:`DropDown`
)ActionBarException
ActionItemActionButtonActionToggleButtonActionCheckActionSeparatorActionDropDownActionGroupActionOverflow
ActionViewContextualActionViewActionPrevious	ActionBar    )	BoxLayout)DropDown)Widget)Button)ToggleButton)CheckBox)Spinner)Label)Config)ObjectPropertyNumericPropertyBooleanPropertyStringPropertyListPropertyOptionPropertyAliasPropertyColorProperty)sp)Builder)partial Zkivywindow_iconc                   @   s   e Zd ZdZdS )r   z"
    ActionBarException class
    N__name__
__module____qualname____doc__ r*   r*   6/tmp/pip-unpacked-wheel-xzebddm3/kivy/uix/actionbar.pyr   >   s   r   c                   @   sV   e Zd ZdZedZdd ZeedddZe	dZ
e	dZed	Zed
Ze	dZdS )r   z
    ActionItem class, an abstract class for all ActionBar widgets. To create a
    custom widget for an ActionBar, inherit from this class. See module
    documentation for more information.
    Z90spc                 C   s   t | j| jS N)maxminimum_widthwidthselfr*   r*   r+   get_pack_widthT   s    zActionItem.get_pack_width)r.   r/   T)bindcacheFz,atlas://data/images/defaulttheme/action_itemz1atlas://data/images/defaulttheme/action_item_downN)r&   r'   r(   r)   r   r.   r2   r   
pack_widthr   	importantinside_groupr   Zbackground_normalZbackground_downZmipmapr*   r*   r*   r+   r   E   s"   
		

r   c                   @   s   e Zd ZdZedddZdS )r   a  
    ActionButton class, see module documentation for more information.

    The text color, width and size_hint_x are set manually via the Kv language
    file. It covers a lot of cases: with/without an icon, with/without a group
    and takes care of the padding between elements.

    You don't have much control over these properties, so if you want to
    customize its appearance, we suggest you create you own button
    representation. You can do this by creating a class that subclasses an
    existing widget and an :class:`ActionItem`::

        class MyOwnActionButton(Button, ActionItem):
            pass

    You can then create your own style using the Kv language.
    NTZ	allownoner&   r'   r(   r)   r   Ziconr*   r*   r*   r+   r      s   r   c                       s   e Zd ZdZedZeeZe	dZ
e	dZeddddgZedZe	dZe	dZedZedZ fdd	Zd
d Zdd Z  ZS )r   zN
    ActionPrevious class, see module documentation for more information.
    Tr      z0atlas://data/images/defaulttheme/previous_normalr#   Fc                    s6   |  d |  d tt| jf | | js2d| _d S )Non_press
on_releasezdata/logo/kivy-icon-32.png)Zregister_event_typesuperr   __init__app_iconr1   kwargs	__class__r*   r+   r>     s
    

zActionPrevious.__init__c                 C   s   d S r,   r*   r0   r*   r*   r+   r;     s    zActionPrevious.on_pressc                 C   s   d S r,   r*   r0   r*   r*   r+   r<     s    zActionPrevious.on_release)r&   r'   r(   r)   r   Zwith_previousr   r$   r?   r   Zapp_icon_widthZapp_icon_heightr   colorZprevious_imageZprevious_image_widthZprevious_image_heighttitleZmarkupr>   r;   r<   __classcell__r*   r*   rB   r+   r      s    
		r   c                   @   s   e Zd ZdZedddZdS )r   zR
    ActionToggleButton class, see module documentation for more information.
    NTr8   r9   r*   r*   r*   r+   r     s   r   c                   @   s   e Zd ZdZdS )ActionLabelzK
    ActionLabel class, see module documentation for more information.
    Nr%   r*   r*   r*   r+   rG   (  s   rG   c                   @   s   e Zd ZdZdS )r   zK
    ActionCheck class, see module documentation for more information.
    Nr%   r*   r*   r*   r+   r   /  s   r   c                   @   s   e Zd ZdZedZdS )r   zO
    ActionSeparator class, see module documentation for more information.
    *atlas://data/images/defaulttheme/separatorN)r&   r'   r(   r)   r   background_imager*   r*   r*   r+   r   6  s
   r   c                   @   s   e Zd ZdZdS )r   zN
    ActionDropDown class, see module documentation for more information.
    Nr%   r*   r*   r*   r+   r   E  s   r   c                       s   e Zd ZdZedZedZedZ	e
dddZedZedZ fdd	Zd
d Zdd Z fddZdd Zdd Z  ZS )r   zK
    ActionGroup class, see module documentation for more information.
    FrH   r   normal)rJ   spinner)optionsc                    sp   g  _ g  _tt jf | d _t  _ jj fddd  j fddd  jj fddd d S )	NFc                    s   t  d|rdndS )N_is_openTFsetattr)Zinsvaluer0   r*   r+   <lambda>  s     
z&ActionGroup.__init__.<locals>.<lambda>)Z	attach_toc                     s   t  ddS )Nis_openTrN   argsr0   r*   r+   rQ     s     r<   c                     s   t  ddS )NrR   FrN   rS   r0   r*   r+   rQ     s     )Z
on_dismiss)	list_action_item_list_overflow_itemsr=   r   r>   rM   r   	_dropdownr3   r@   rB   r0   r+   r>     s    zActionGroup.__init__c                 C   s:   |r"| j s"|   | j|  d S |s6| j r6| j  d S r,   )rM   _toggle_dropdownrX   opendismissr1   instancerP   r*   r*   r+   
on_is_open  s    

zActionGroup.on_is_openc                 G   s   | j }d |_|jsd S |jj}|rF| jp@t| jtdd |D |_n| j|_|D ],}d |_t| jt	dg|_|j
|jd qRd S )Nc                 s   s   | ]}|j V  qd S r,   )r5   ).0cr*   r*   r+   	<genexpr>  s     z/ActionGroup._toggle_dropdown.<locals>.<genexpr>0   rU   )rX   Zsize_hint_x	containerchildrendropdown_widthr-   r/   Zsize_hint_yheightr    r3   r[   )r1   largsZddnrd   itemr*   r*   r+   rY     s     zActionGroup._toggle_dropdownc                    sH   t |tr&tt| j|f|| dS t |ts8td| j| dS )z\
        .. versionchanged:: 2.1.0
            Renamed argument `item` to `widget`.
        Nz#ActionGroup only accepts ActionItem)	
isinstancer   r=   r   
add_widgetr   r   rV   appendr1   widgetrT   rA   rB   r*   r+   rj     s    

zActionGroup.add_widgetc                 C   s0   |    | j| j D ]}d|_| j| qd S )NT)clear_widgetsrW   rV   r7   rX   rj   )r1   rh   r*   r*   r+   
show_group  s    zActionGroup.show_groupc                 O   s   | j j|| d S r,   )rX   rn   )r1   rT   rA   r*   r*   r+   rn     s    zActionGroup.clear_widgets)r&   r'   r(   r)   r   use_separatorr   Zseparator_imager   separator_widthr   modere   rR   r>   r^   rY   rj   ro   rn   rF   r*   r*   rB   r+   r   K  s   	r   c                       s2   e Zd ZdZedZdddZ fddZ  ZS )	r	   zN
    ActionOverflow class, see module documentation for more information.
    z)atlas://data/images/defaulttheme/overflowr   c                 O   sX   |dkrdS t |trdS t |ts4td|n |dkrFt| j}| j|| dS )zd
        .. versionchanged:: 2.1.0
             Renamed argument `action_item` to `widget`.
        N,ActionView only accepts ActionItem (got {!r}r   )ri   r   r   r   formatlenrW   insertr1   rm   indexrT   rA   r*   r*   r+   rj     s    


zActionOverflow.add_widgetc                    s*   | j g krd S |   tt||  d S r,   )rW   ro   r=   r
   rj   )r1   parentrB   r*   r+   show_default_items  s    
z!ActionOverflow.show_default_items)r   )	r&   r'   r(   r)   r   Zoverflow_imagerj   rz   rF   r*   r*   rB   r+   r	     s   	
r	   c                       s   e Zd ZdZedZeddddgZedZ	e
dZedZ fddZdd	 Zd fdd	Zdd Z fddZdd Z fddZ fddZ fddZdd Z  ZS )r
   zJ
    ActionView class, see module documentation for more information.
    Nr:   z,atlas://data/images/defaulttheme/action_viewFc                    s<   g | _ g | _tt| jf | d| _| js8t| jd| _d S )Nr#   )rp   )	_list_action_items_list_action_groupr=   r
   r>   _stateoverflow_groupr	   rp   r@   rB   r*   r+   r>   >  s    zActionView.__init__c                 C   s   | j d| d S )Nr   )r{   rv   r\   r*   r*   r+   on_action_previousG  s    zActionView.on_action_previousr   c                    s   |dkrdS t |ts&td|nt |tr@|| _| j|_nlt |tr`| j	| | j|_nLt |t
rr|| _n:tt| j||f|| |dkrt| j}| j|| dS )zc
        .. versionchanged:: 2.1.0
            Renamed argument `action_item` to `widget`.
        Nrs   r   )ri   r   r   rt   r	   r~   rp   r   r|   rk   r   action_previousr=   r
   rj   ru   r{   rv   rw   rB   r*   r+   rj   J  s$    






zActionView.add_widgetc                 C   s$   | j D ]
}||_q| jr || j_d S r,   )r|   rp   r~   )r1   r]   rP   groupr*   r*   r+   on_use_separatorg  s    
zActionView.on_use_separatorc                    s^   t t| j|f|| t|trD|jD ]}|| jkr(| j| q(|| jkrZ| j| d S r,   )r=   r
   remove_widgetri   r	   rV   r{   remove)r1   rm   rT   rA   rh   rB   r*   r+   r   m  s    



zActionView.remove_widgetc                 C   sF   | j d d  }|   | jD ]}|  q| j  g | j_|| _ d S r,   )r{   rn   r|   r~   rV   )r1   lstr   r*   r*   r+   
_clear_allw  s    


zActionView._clear_allc                    s   t t| j}d| _|   | jjs,|| j t| jdkr\| jdd  D ]}d|_	|| qH| j
D ]N}|jdkr|| |  qb|jg kr|t  |jD ]}d|_	|| qqb| j|  d S )Nallr:   FrK   )r=   r
   rj   r}   r   r   ry   ru   r{   r7   r|   rr   ro   rV   r   r~   rz   r1   	super_addchildr   rB   r*   r+   _layout_all  s&    







zActionView._layout_allc                    s   t t| j}d| _|   | jjs,|| j t| jdkr\| jdd  D ]}|| d|_	qH| j
D ]}|| |  qb| j|  d S )Nr   r:   F)r=   r
   rj   r}   r   r   ry   ru   r{   r7   r|   ro   r~   rz   r   rB   r*   r+   _layout_group  s    


zActionView._layout_groupc                    s  t t| j}d| _|   g }g }d}| jjs8|| j | j| jj	 | jj
 }t| jr| jdd  D ]H}|jr|j	| |k r|| d|_||j	7 }q|| qf|| qf|| jk r| jD ]D}|j	| |j |k r|| |  ||j	|j 7 }q|| qt| jd }|| jk rj|d d  D ]:}|j	| |k r.||| ||j	7 }d|_|| q.|j}	|D ]}|	|j qt| j}
|g krt |
j|
j}|D ]}|| q|
  | jjs||
 d S )Nrandomr   r:   F)r=   r
   rj   r}   r   r   ry   r/   r~   r5   r.   ru   r{   r6   r7   rk   r|   rq   ro   rd   r   extendrV   rC   )r1   r   Zhidden_itemsZhidden_groupstotal_widthr/   r   r   Zgroup_indexZextend_hiddenr~   Zover_addrB   r*   r+   _layout_random  sl    






zActionView._layout_randomc                 G   s   d}| j D ]}||j7 }q
| jD ]}|jD ]}||j7 }q*q || jkr\| jdkrX|   d S d}| j D ]}||j7 }qf| jD ]}||j7 }q||| jk r| jdkr|   d S |   d S )Nr   r   r   )	r{   r5   r|   rV   r/   r}   r   r   r   )r1   r/   rT   r   r   r   r*   r*   r+   on_width  s(    








zActionView.on_width)r   )r&   r'   r(   r)   r   r   r   background_colorr   rI   r   rp   r~   r>   r   rj   r   r   r   r   r   r   r   rF   r*   r*   rB   r+   r
     s$   		

Dr
   c                   @   s   e Zd ZdZdS )r   z\
    ContextualActionView class, see the module documentation for more
    information.
    Nr%   r*   r*   r*   r+   r     s   r   c                       st   e Zd ZdZedZeddddgZedZ	e
ddddgZdZ fddZ fd	d
Zdd Z fddZ  ZS )r   a  
    ActionBar class, which acts as the main container for an
    :class:`ActionView` instance. The ActionBar determines the overall
    styling aspects of the bar. :class:`ActionItem`\s are not added to
    this class directly, but to the contained :class:`ActionView` instance.

    :Events:
        `on_previous`
            Fired when action_previous of action_view is pressed.

    Please see the module documentation for more information.
    Nr:   z+atlas://data/images/defaulttheme/action_bar   )on_previousc                    s*   t t| jf | g | _t| jd| _d S )Nr   )r=   r   r>   _stack_cont_action_viewr"   dispatch_emit_previousr@   rB   r*   r+   r>   M  s    zActionBar.__init__c                    s   t |trb| j| |jdk	r@|jj| jd |jj| jd |   t	t
| j|f|| n2t |tr|| _t	t
| j|f|| ntddS )z\
        .. versionchanged:: 2.1.0
            Renamed argument `view` to `widget`.
        NrU   z9ActionBar can only add ContextualActionView or ActionView)ri   r   r   rk   r   Zunbindr   r3   rn   r=   r   rj   r
   action_viewr   rl   rB   r*   r+   rj   R  s    


zActionBar.add_widgetc                 G   s   |    d S r,   )_pop_contextual_action_view)r1   rT   r*   r*   r+   r   g  s    zActionBar.on_previousc                    sJ   | j   |   | j g kr0tt| | j ntt| | j d  dS )zrRemove the current ContextualActionView and display either the
           previous one or the ActionView.
        N)r   poprn   r=   r   rj   r   r0   rB   r*   r+   r   j  s
    

z%ActionBar._pop_contextual_action_view)r&   r'   r(   r)   r   r   r   r   r   rI   r   ZborderZ
__events__r>   rj   r   r   rF   r*   r*   rB   r+   r     s   
r   __main__)runTouchApp)FloatLayout)Factorya  
<MainWindow>:
    ActionBar:
        pos_hint: {'top':1}
        ActionView:
            use_separator: True
            ActionPrevious:
                title: 'Action Bar'
                with_previous: False
            ActionOverflow:
            ActionButton:
                text: 'Btn0'
                icon: 'atlas://data/images/defaulttheme/audio-volume-high'
            ActionButton:
                text: 'Btn1'
            ActionButton:
                text: 'Btn2'
            ActionGroup:
                text: 'Group 1'
                ActionButton:
                    text: 'Btn3'
                ActionButton:
                    text: 'Btn4'
            ActionGroup:
                dropdown_width: 200
                text: 'Group 2'
                ActionButton:
                    text: 'Btn5'
                ActionButton:
                    text: 'Btn6'
                ActionButton:
                    text: 'Btn7'
c                   @   s   e Zd ZdS )
MainWindowN)r&   r'   r(   r*   r*   r*   r+   r     s   r   N)@r)   __all__Zkivy.uix.boxlayoutr   Zkivy.uix.dropdownr   Zkivy.uix.widgetr   Zkivy.uix.buttonr   Zkivy.uix.togglebuttonr   Zkivy.uix.checkboxr   Zkivy.uix.spinnerr   Zkivy.uix.labelr   Zkivy.configr   Zkivy.propertiesr   r   r   r   r   r   r   r   Zkivy.metricsr    Z	kivy.langr!   	functoolsr"   r$   get	Exceptionr   objectr   r   r   r   rG   r   r   r   r   r	   r
   r   r   r&   Z	kivy.baser   Zkivy.uix.floatlayoutr   Zkivy.factoryr   
unregisterload_stringr   Zfloat_layoutr*   r*   r*   r+   <module>   sV   "(Ln *  _


"