Saltar al contenido principal

MenuItem

Clase: MenuItem

Agregue elementos a los menús y menús de contexto de la aplicación nativa.

Proceso: principal

See Menu for examples.

[!WARNING] Electron's built-in classes cannot be subclassed in user code. For more information, see the FAQ.

new MenuItem(options)

  • options Object
    • click Función (opcional) - Sera llamada con click(menuItem, window, event) cuando el elemento del menú sea clickeado.
      • menuItem MenuItem
      • window BaseWindow | undefined - This will not be defined if no window is open.
      • event KeyboardEvent
    • role string (optional) - Can be undo, redo, cut, copy, paste, pasteAndMatchStyle, delete, selectAll, reload, forceReload, toggleDevTools, resetZoom, zoomIn, zoomOut, toggleSpellChecker, togglefullscreen, window, minimize, close, help, about, services, hide, hideOthers, unhide, quit, showSubstitutions, toggleSmartQuotes, toggleSmartDashes, toggleTextReplacement, startSpeaking, stopSpeaking, zoom, front, appMenu, fileMenu, editMenu, viewMenu, shareMenu, recentDocuments, toggleTabBar, selectNextTab, selectPreviousTab, showAllTabs, mergeAllWindows, clearRecentDocuments, moveTabToNewWindow or windowMenu - Define the action of the menu item, when specified the click property will be ignored. See roles.
    • Cadena type (opcional)
      • normal
      • separator
      • submenu
      • casilla de verificación
      • radio
      • header - Only available on macOS 14 and up.
      • palette - Only available on macOS 14 and up.
    • label string (opcional)
    • sublabel string (optional) macOS - Available in macOS >= 14.4
    • toolTip string (opcional) macOS - Texto flotante para es elemento del menú.
    • accelerator string (optional) - An Accelerator string.
    • icon (NativeImage | string) (optional)
    • enabled boolean (opcional) - Si es falso, el elemento de menú será gris y no se podrá hacer click en él.
    • acceleratorWorksWhenHidden boolean (optional) macOS - default is true, and when false will prevent the accelerator from triggering the item if the item is not visible.
    • visible boolean (opcional) - Si es falso, el elemento del menú será totalmente invisible.
    • checked boolean (opcional) - Solo debe especificarse para elementos del menú tipo checkbox o radio.
    • registerAccelerator boolean (opcional) Linux Windows - Si es false, el acelerador no se registrará en el sistema, pero seguirá siendo visible. Defaults to true.
    • sharingItem SharingItem (opcional) macOS - el elemento que se compartirá cuando el role es shareMenu.
    • submenu (MenuItemConstructorOptions[] | Menu) (opcional) - Debería especificarse para los items del tipo submenu. Si submenu es especificado, el type: 'submenu' puede ser omitido. Si el valor no es un Menu entonces automáticamente será convertido a uno usando Menu.buildFromTemplate.
    • id string (opcional) - único dentro de un solo menú. Si se define, puede ser usado como referencia a este elemento por el atributo posición.
    • before string[] (optional) - Inserts this item before the item with the specified id. Si el ítem referenciado no existe el ítem sera insertado al final del menu. También implica que el ítem del menu en cuestión debería colocarse in el mismo “group” como el ítem.
    • after string[] (optional) - Inserts this item after the item with the specified id. Si el ítem referenciado no existe se insertara al final del menú.
    • beforeGroupContaining string[] (optional) - Provides a means for a single context menu to declare the placement of their containing group before the containing group of the item with the specified id.
    • afterGroupContaining string[] (optional) - Provides a means for a single context menu to declare the placement of their containing group after the containing group of the item with the specified id.
note

acceleratorWorksWhenHidden is specified as being macOS-only because accelerators always work when items are hidden on Windows and Linux. La opción esta expuesta a los usuarios para darles a ellos la opción de apagarla, ya que esto es posible el el desarrollo nativo de macOS.

Propiedades de la instancia

Las siguientes propiedades están disponibles en instancias del menú de elementos:

A string indicating the item's unique id. This property can be dynamically changed.

Un string indicando la etiqueta visible del elemento.

Una función que se desencadena cuando los elementos del menú reciben un evento click. Puede ser llamado con menuItem.click(event, focusedWindow, focusedWebContents).

Un Menú (opcional) que contiene el submenú del menú elemento, si está presente.

Un string indicando el tipo del elemento. Can be normal, separator, submenu, checkbox, radio, header or palette.

note

header and palette are only available on macOS 14 and up.

Una string (opcional) indicando el rol del elemento, si está establecido. Can be undo, redo, cut, copy, paste, pasteAndMatchStyle, delete, selectAll, reload, forceReload, toggleDevTools, resetZoom, zoomIn, zoomOut, toggleSpellChecker, togglefullscreen, window, minimize, close, help, about, services, hide, hideOthers, unhide, quit, startSpeaking, stopSpeaking, zoom, front, appMenu, fileMenu, editMenu, viewMenu, shareMenu, recentDocuments, toggleTabBar, selectNextTab, selectPreviousTab, showAllTabs, mergeAllWindows, clearRecentDocuments, moveTabToNewWindow or windowMenu

Un Accelerator (opcional) indicando el acelerador del elemento, si está establecido.

Un Accelerator | null indicando el user-assigned accelerator del elemento para el menú del elemento.

[!NOTE] This property is only initialized after the MenuItem has been added to a Menu. Ya sea a través de Menu.buildFromTemplate o a través de Menu.append()/insert(). Accediendo antes de la inicialización solo devolverá null.

A NativeImage | string (optional) indicating the item's icon, if set.

A string indicating the item's sublabel.

A string indicating the item's hover text.

A boolean indicating whether the item is enabled. This property can be dynamically changed.

A boolean indicating whether the item is visible. This property can be dynamically changed.

A boolean indicating whether the item is checked. This property can be dynamically changed.

Un elemento del menú checkbox que cambiará la propiedad verificado en sí y no cuando se selecciona.

Un elemento del menú radio que activará su propiedad verificado cuando se haga click en él, y que desactivará su propiedad para todos los elementos adyacentes en el mismo menú.

Puede añadir la función click para comportamientos adicionales.

A boolean indicating if the accelerator should be registered with the system or just displayed.

Esta propiedad puede ser cambiada dinámicamente.

Un SharingItem indicando el elemento a compartir cuando el role es shareMenu.

Esta propiedad puede ser cambiada dinámicamente.

A number indicating an item's sequential unique id.

Un Menu del cual el elemento es parte.