
    G@d7                     :   d Z ddlmZ ddlZddl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mZm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mZ e	eef         Zeeee          ee          ee          f         Z!ee ee!         f         Z" G d de          Z#dS )z
Main menu Plugin.
    )OrderedDictN)DictListTupleOptionalUnion)QKeySequence)SpyderAPIError)PLUGIN_REGISTRY)SpyderPluginV2SpyderDockablePluginPlugins_)MENU_SEPARATOR
SpyderMenu)ApplicationMenuApplicationMenus)set_menu_iconsSpyderActionc                      e Zd ZdZeZdZdZed             Zd Z	d Z
d Zd Zd Zd	 Zd
 Zd Z	 ddededefdZ	 	 	 	 	 ddedee         dee         dee         dee         defdZdefdZ	 ddedee         fdZdedefdZdS ) MainMenumainmenuFc                       t          d          S )Nz
Main menusr        >lib/python3.11/site-packages/spyder/plugins/mainmenu/plugin.pyget_namezMainMenu.get_name*   s    r   c                 ,    |                      d          S )Ngenprefs)create_iconselfs    r   get_iconzMainMenu.get_icon.   s    
+++r   c                      t          d          S )Nz)Provide main application menu management.r   r"   s    r   get_descriptionzMainMenu.get_description1   s    <===r   c                    t                      | _        i | _        | j        } |t          j        t          d                      |t          j        t          d          d            |t          j        t          d          d            |t          j	        t          d          d            |t          j
        t          d          d            |t          j        t          d          d           |                     t          j                  r# |t          j        t          d	                     |                     t          j                  r# |t          j        t          d
                      |t          j        t          d                      |t          j        t          d                      |t          j        t          d                     d S )Nz&Filez&EditFdynamicz&SearchzSour&cez&Runz&Debugz	C&onsolesz	&Projectsz&Toolsz&Viewz&Help)r   _APPLICATION_MENUS_ITEM_QUEUEcreate_application_menur   Filer   EditSearchSourceRunDebugis_plugin_enabledr   IPythonConsoleConsolesProjectsToolsViewHelp)r#   create_app_menus     r   on_initializezMainMenu.on_initialize4   s   "--- 
 6(-qzz:::(-qzz5IIII(/9uMMMM(/9uMMMM(,aiiGGGG(.(UKKKK!!'"899 	GO,5q~~FFF!!'"233 	GO,5q~~FFF(.(<<<(-qzz:::(-qzz:::::r   c                 f    | j                                         D ]}|                                 d S N)r*   values_render)r#   menus     r   on_mainwindow_visiblezMainMenu.on_mainwindow_visibleN   s;     +2244 	 	DLLNNNN	 	r   c                    |                                 }|D ]q}t          |dd          r"|j        |                    |j                   5|                                (|                     |                                           qrdS )z
        Show action shortcuts in menu.

        Parameters
        ----------
        menu: SpyderMenu
            Instance of a spyder menu.
        _shown_shortcutFN)actionsgetattrrC   setShortcutr@   _show_shortcutsr#   r@   menu_actionsactions       r   rG   zMainMenu._show_shortcutsZ   s     ||~~" 
	 
	Fv0%88 	)5&&v'=>>>*$$V[[]]3333 
	 
	r   c                 $   |                                 }|D ]x}t          |dd          r)|j        !|                    t	                                 <|                                (|                     |                                           xydS )z
        Hide action shortcuts in menu.

        Parameters
        ----------
        menu: SpyderMenu
            Instance of a spyder menu.
        rC   FN)rD   rE   rC   rF   r	   r@   _hide_shortcutsrH   s       r   rL   zMainMenu._hide_shortcutsp   s     ||~~" 
	 
	Fv0%88 	)5&&|~~666*$$V[[]]3333 
	 
	r   c                 p   t           D ]}t          j        |          }t          |t                    r|j        dk    r3| j                                        }|j                                         i	 |j	                                         # t          $ r |j                                         Y w xY wdS )z3Hide options menu when menubar is pressed in macOS.editorN)r   
get_plugin
isinstancer   CONF_SECTIONrN   get_current_editorstackr@   hideoptions_menuAttributeError_options_menu)r#   plugin_nameplugin_instanceeditorstacks       r   _hide_options_menuszMainMenu._hide_options_menus   s    * 	= 	=K-8EEO/+?@@ 
="/8;;"&+"E"E"G"GK$))++++='499;;;;) = = ='5::<<<<<=
=	= 	=s   2B#B21B2c                 R    t           j        dk    r j        D ]} j        |         }|z|j                            |f fd	           |j                            |f fd	           |j                            |fd           |j                             j                   dS dS )zSetup menus.darwinNc                 .                         |           S r=   rG   r@   r#   s    r   <lambda>z'MainMenu._setup_menus.<locals>.<lambda>       $*>*>t*D*D r   c                 .                         |           S r=   rL   r_   s    r   r`   z'MainMenu._setup_menus.<locals>.<lambda>   ra   r   c                 "    t          | d          S NFr   r@   s    r   r`   z'MainMenu._setup_menus.<locals>.<lambda>   s    .u*E*E r   )sysplatformr*   aboutToShowconnectaboutToHiderZ   r#   menu_idr@   s   `  r   _setup_menuszMainMenu._setup_menus   s     <8##2 	G 	G.w7#$,,$(DDDDDF F F$,,$(DDDDDF F F$,,$(EEEG G G$,,T-EFFF $#	G 	Gr   Trn   titler)   c                     | j         v r"t          d                    |                    t           j        ||          }||_        | j         |<    j                                                            |           t          j	        dk    rz|j
                            |f fd	           |j                            |f fd	           |j
                            |fd           |j
                             j                   | j        v r@ j                            |          }|D ]#}|\  }}}	}
                     ||||	|
           $|S )z
        Create a Spyder application menu.

        Parameters
        ----------
        menu_id: str
            The menu unique identifier string.
        title: str
            The localized menu title to be displayed.
        z Menu with id "{}" already added!r(   r\   c                 .                         |           S r=   r^   r_   s    r   r`   z2MainMenu.create_application_menu.<locals>.<lambda>       $"6"6t"<"< r   c                 .                         |           S r=   rc   r_   s    r   r`   z2MainMenu.create_application_menu.<locals>.<lambda>   rs   r   c                 "    t          | d          S re   rf   rg   s    r   r`   z2MainMenu.create_application_menu.<locals>.<lambda>   s    .u"="= r   )rn   sectionbeforebefore_section)r*   r
   formatr   mainrn   menuBaraddMenurh   ri   rj   rk   rl   rZ   r+   popadd_item_to_application_menu)r#   rn   rp   r)   r@   pending_itemspendingitemrv   before_itemrx   s   `          r   r,   z MainMenu.create_application_menu   s    d--- 299'BBD D D ty%AAA+/(	##D))) <8##$$ <<<<<> > >$$ <<<<<> > >$$ ===? ? ?$$T%=>>>d&&& ,0099M( G G07.wn11'7&~ 2 G G G G r   Nr   rv   rw   rx   omit_idc           
         t          |t          t          f          s|st          d          t          j        | j        j        t          j        | j        j	        t          j
        | j        j        t          j        | j        j        t          j        | j        j        i}||v r9||         }|                    t"                     |                    |           dS || j        vr@| j                            |g           }	|	                    ||||f           |	| j        |<   dS |                     |          }
|
                    |||||           dS )a  
        Add action or widget `item` to given application menu `section`.

        Parameters
        ----------
        item: SpyderAction or SpyderMenu
            The item to add to the `menu`.
        menu_id: str or None
            The application menu unique string identifier.
        section: str or None
            The section id in which to insert the `item` on the `menu`.
        before: str
            Make the item appear before the given object identifier.
        before_section: Section or None
            Make the item section (if provided) appear before another
            given section.
        omit_id: bool
            If True, then the menu will check if the item to add declares an
            id, False otherwise. This flag exists only for items added on
            Spyder 4 plugins. Default: False

        Notes
        -----
        Must provide a `menu` or a `menu_id`.
        zDA menu only accepts items objects of type SpyderAction or SpyderMenu)rv   rw   rx   r   N)rP   r   r   r
   r   r.   _mainedit_menu_actionsr/   search_menu_actionsr0   source_menu_actionsr1   run_menu_actionsr2   debug_menu_actionsappendr   r*   r+   getget_application_menu
add_action)r#   r   rn   rv   rw   rx   r   app_menu_actionsrD   pending_menu_itemsr@   s              r   r~   z%MainMenu.add_item_to_application_menu   sn   > $z :;; 	?G 	?  "> ? ? ? !4:#?#TZ%C#TZ%C $*"="DJ$A
 &&&&w/GNN>***NN4     d555%)%5%9%9'2%F%F""))4&+9+; < < <,> )))0099gf/=w   P P P P Pr   c                     || j         v rZ| j                             |          }| j                                                            |                                           dS dS )z
        Remove a Spyder application menu.

        Parameters
        ----------
        menu_id: str
            The menu unique identifier string.
        N)r*   r}   rz   r{   removeAction
menuActionrm   s      r   remove_application_menuz MainMenu.remove_application_menu  sb     d---*..w77DI,,T__->->????? .-r   item_idc                 l   || j         vr"t          d                    |                    t          j        | j        j        | j        j        ft          j        | j        j	        | j        j
        ft          j        | j        j        | j        j        ft          j        | j        j        | j        j        ft          j        | j        j        | j        j        fi}t          j        | j        j        t          j        | j        j
        t          j        | j        j        t          j        | j        j        t          j        | j        j        i}|                     |          }||v r||         }||         }d}t+          |          D ]l\  }}	d}
t-          |	t.                    st1          |	d          r|	j        }
n,t-          |	t4                    st1          |	d          r|	j        }
|

|
|k    r|} nm|,|                    |           |                    |           dS dS |                    |           dS )a  
        Remove action or widget from given application menu by id.

        Parameters
        ----------
        item_id: str
            The item identifier to remove from the given menu.
        menu_id: str or None
            The application menu unique string identifier.
        z{} is not a valid menu_idN	action_idrn   )r*   r
   ry   r   r.   r   r   	edit_menur/   r   search_menur0   r   source_menur1   r   run_menur2   r   
debug_menur   	enumeraterP   r   hasattrr   r   rn   r}   remove_action)r#   r   rn   r   	app_menusr@   rD   positionirJ   this_item_ids              r   !remove_item_from_application_menuz*MainMenu.remove_item_from_application_menu  s.    $111 !<!C!CG!L!LMMM
 !
,dj.B$D#
.
0F&H#
.
0F&H 
+TZ-@#B"
-tz/D%F
 !4:#7#TZ%;#TZ%; $*"5"DJ$9
	 ((11&&&&w/GW%DH&w// 
 
	6#v|44 2442#)#3LL 44 2	222#)>L+0G0G HE#H%%%""7+++++ $# w'''''r   returnc           	          || j         vrGt          d                    |t          | j                                                                       | j         |         S )z
        Return an application menu by menu unique id.

        Parameters
        ----------
        menu_id: ApplicationMenu
            The menu unique identifier string.
        z:Application menu "{0}" not found! Available menus are: {1})r*   r
   ry   listkeys)r#   rn   s     r   r   zMainMenu.get_application_menuX  sf     $111 !!'T$"9">">"@"@AA"C "C   &w//r   )T)NNNNFr=   )__name__
__module____qualname__NAMErQ   	CONF_FILECAN_BE_DISABLEDstaticmethodr   r$   r&   r;   rA   rG   rL   rZ   ro   strboolr,   ItemTyper   r~   r   r   r   r   r   r   r   r   r   $   s       DLIO  \, , ,> > >; ; ;4    ,  ,= = = G G G$ 15) )s )3 ))-) ) ) )X ?C>B=AEI5::P :P :P.6sm:P.6sm:P .6c]:P 6>c]	:P
 /3:P :P :P :Px@s @ @ @ @ DH;( ;( ;(3;C=;( ;( ;( ;(z0C 0J 0 0 0 0 0 0r   r   )$__doc__collectionsr   rh   typingr   r   r   r   r   
qtpy.QtGuir	   spyder.api.exceptionsr
   'spyder.api.plugin_registration.registryr   spyder.api.pluginsr   r   r   spyder.api.translationsr   spyder.api.widgets.menusr   r   spyder.plugins.mainmenu.apir   r   spyder.utils.qthelpersr   r   r   r   ItemSectionBefore	ItemQueuer   r   r   r   <module>r      s   
 $ # # # # # 



 5 5 5 5 5 5 5 5 5 5 5 5 5 5 $ # # # # # 1 0 0 0 0 0 C C C C C C L L L L L L L L L L % % % % % % ? ? ? ? ? ? ? ? I I I I I I I I ? ? ? ? ? ? ? ? z)*hsmXc]HSM9; d,--.	D0 D0 D0 D0 D0~ D0 D0 D0 D0 D0r   