Classe : MenuItem
Classe : MenuItem
Ajoute des éléments aux menus et menus contextuels natifs de l’application.
Processus : Main
Voir Menu
pour des exemples.
new MenuItem(options)
- Objet
options
- Fonction (facultatif)
click
- Sera appelée avecclick(menuItem, browserWindow, event)
lorsque l'élément de menu est cliqué.menuItem
MenuItembrowserWindow
BrowserWindow | undefined - Cela ne sera pas défini si aucune fenêtre n'est ouverte.event
KeyboardEvent
role
string (optional) - Can beundo
,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
,mergeAllWindows
,clearRecentDocuments
,moveTabToNewWindow
orwindowMenu
- Define the action of the menu item, when specified theclick
property will be ignored. Voir rôles.type
string (optional) - Can benormal
,separator
,submenu
,checkbox
orradio
.label
string (optional)sublabel
string (optional)toolTip
string (optional) macOS - Hover text for this menu item.accélérateur
Accelerator (facultatif)icon
(NativeImage | string) (optional)enabled
boolean (optional) - If false, the menu item will be greyed out and unclickable.acceleratorWorksWhenHidden
boolean (optional) macOS - default istrue
, and whenfalse
will prevent the accelerator from triggering the item if the item is not visible`.visible
boolean (optional) - If false, the menu item will be entirely hidden.checked
boolean (optional) - Should only be specified forcheckbox
orradio
type menu items.registerAccelerator
boolean (optional) Linux Windows - If false, the accelerator won't be registered with the system, but it will still be displayed. Defaults to true.sharingItem
SharingItem (optional) macOS - The item to share when therole
isshareMenu
.sous-menu
(MenuItemConstructorOptions[] | Menu) (facultatif) - Doit être spécifié poursous-menu
type éléments de menu. Sisous-menu
est spécifié, letype: 'submenu'
peut être omis. Si la valeur n'est pas unMenu
alors elle sera automatiquement convertie en un en utilisantMenu.buildFromTemplate
.id
string (optional) - Unique within a single menu. If defined then it can be used as a reference to this item by the position attribute.before
string[] (optional) - Inserts this item before the item with the specified label. Si l'élément référencé n'existe pas, l'élément sera inséré à la fin du menu. Implique également que l'élément de menu en question doit être placé dans le même « groupe » que l'élément.after
string[] (optional) - Inserts this item after the item with the specified label. Si l'élément référencé n'existe pas, l'élément sera inséré à la fin de le menu.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 label.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 label.
- Fonction (facultatif)
Remarque : acceleratorWorksWhenHidden
est spécifié comme étant macOS uniquement parce que les accélérateurs fonctionnent toujours lorsque des éléments sont cachés sous Windows et Linux. L'option est exposée aux utilisateurs pour leur donner la possibilité de la désactiver, car cela est possible dans le développement natif de macOS. Cette propriété n'est utilisable que sur macOS Haute Sierra 10.13 ou plus récente.
Rôles
Les rôles permettent à des éléments du menu d'avoir des comportements prédéfinis.
Il est préférable de spécifier rôle
pour tout élément de menu qui correspond à un rôle standard, plutôt que d'essayer d'implémenter manuellement le comportement dans une fonction clic
. Le comportement rôle
intégré donnera la meilleure expérience native.
Les valeurs label
et accélérateur
sont optionnelles lorsque vous utilisez un rôle
et par défaut des valeurs appropriées pour chaque plateforme.
Chaque lien de menu doit avoir soit un rôle
, label
, soit dans le cas d'un séparateur un type
.
La propriété role
peut avoir les valeurs suivantes :
undo
about
- Trigger a native about panel (custom message box on Window, which does not provide its own).redo
cut
copy
paste
pasteAndMatchStyle
selectAll
delete
minimize
- Minimise la fenêtre courante.close
- Ferme la fenêtre courante.quitter
- Quitter l'application.reload
- Recharge la fenêtre courante.forceReload
- Recharge la fenêtre courante ignorant le cache.toggleDevTools
- Bascule les outils de développement dans la fenêtre actuelle.togglefullscreen
- Basculer en mode plein écran dans la fenêtre actuelle.resetZoom
- Réinitialise le niveau de zoom de la page ciblée à la taille d'origine.zoomIn
- Zoom sur la page ciblée par 10%.zoomOut
- Zoom arrière de la page ciblée de 10%.toggleSpellChecker
- Enable/disable builtin spell checker.fileMenu
- Menu par défaut entier "Fichier" (Close / Quitter)editMenu
- Tout le menu "Edit" par défaut (Annuler, Copier, etc.).viewMenu
- Menu "Affichage" par défaut (Recharger, Activer/désactiver les outils de développement, etc.)windowMenu
- Menu par défaut entier "Windows" (Minimize, Zoom, etc.).
Les rôles supplémentaires suivants sont disponibles sur macOS:
appMenu
- Tout le menu "App" par défaut (About, Services, etc.)hide
- Mappez à l'actioncaché
.hideOthers
- Mappez à l'actionhideOtherApplications
.unhide
- Mappez à l'actionunhideAllApplications
.showSubstitutions
- Map to theorderFrontSubstitutionsPanel
action.toggleSmartQuotes
- Map to thetoggleAutomaticQuoteSubstitution
action.toggleSmartDashes
- Map to thetoggleAutomaticDashSubstitution
action.toggleTextReplacement
- Map to thetoggleAutomaticTextReplacement
action.startSpeaking
- Carte à l'actionstartSpeaking
.stopSpeaking
- Carte à l'actionstopSpeaking
.front
- Mappez à l'actionarrangeInFront
.zoom
- Mappez à l'actionperformZoom
.toggleTabBar
- Mappez à l'actiontoggleTabBar
.selectNextTab
- Mappez à l'actionselectNextTab
.selectPreviousTab
- Mappez à l'actionselectPreviousTab
.mergeAllWindows
- Mappez à l'actionmergeAllWindows
.moveTabToNewWindow
- Mappez à l'actionmoveTabToNewWindow
.window
- Le sous-menu est un menu "Windows".help
- Le sous-menu est un menu "Aide".services
- Le sous-menu est un menu "Services". This is only intended for use in the Application Menu and is not the same as the "Services" submenu used in context menus in macOS apps, which is not implemented in Electron.recentDocuments
- Le sous-menu est un menu "Ouvrir Récents".clearRecentDocuments
- Carte à l'actionclearRecentDocuments
.shareMenu
- The submenu is share menu. ThesharingItem
property must also be set to indicate the item to share.
Lorsque vous spécifiez un rôle
sur macOS, label
et accélérateur
sont les seules options qui affecteront l'élément de menu. Toutes les autres options seront ignorées. La minuscule rôle
, par exemple toggledevtools
, est toujours supportée.
Note: The enabled
and visibility
properties are not available for top-level menu items in the tray on macOS.
Propriétés d'instance
Les propriétés suivantes sont disponibles pour les instances de MenuItem
:
menuItem.id
A string
indicating the item's unique id, this property can be dynamically changed.
menuItem.label
A string
indicating the item's visible label.
menuItem.click
Une Function
qui est activée lorsque l'élément MenuItem reçoit un événement de clic. It can be called with menuItem.click(event, focusedWindow, focusedWebContents)
.
event
KeyboardEventfocusedWindow
BrowserWindowfocusedWebContents
WebContents
menuItem.submenu
Un Menu
(facultatif) contenant le sous-menu de l'élément de menu s'il est présent.
menuItem.type
A string
indicating the type of the item. Can be normal
, separator
, submenu
, checkbox
or radio
.
menuItem.role
A string
(optional) indicating the item's role, if set. 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
, mergeAllWindows
, clearRecentDocuments
, moveTabToNewWindow
or windowMenu
menuItem.accelerator
An Accelerator
(optional) indicating the item's accelerator, if set.
menuItem.userAccelerator
Readonly macOS
An Accelerator | null
indicating the item's user-assigned accelerator for the menu item.
Note: This property is only initialized after the MenuItem
has been added to a Menu
. Either via Menu.buildFromTemplate
or via Menu.append()/insert()
. Accessing before initialization will just return null
.
menuItem.icon
A NativeImage | string
(optional) indicating the item's icon, if set.
menuItem.sublabel
A string
indicating the item's sublabel.
menuItem.toolTip
macOS
A string
indicating the item's hover text.
menuItem.enabled
A boolean
indicating whether the item is enabled, this property can be dynamically changed.
menuItem.visible
A boolean
indicating whether the item is visible, this property can be dynamically changed.
menuItem.checked
A boolean
indicating whether the item is checked, this property can be dynamically changed.
Une case à cocher
lien de menu activera et éteindra la propriété cochée
lorsque sera sélectionné.
Un lien de menu radio
activera sa propriété cochée
une fois cliqué, et désactivera cette propriété pour tous les éléments adjacents du même menu.
Vous pouvez ajouter une fonction clic
pour un comportement supplémentaire.
menuItem.registerAccelerator
A boolean
indicating if the accelerator should be registered with the system or just displayed.
This property can be dynamically changed.
menuItem.sharingItem
macOS
A SharingItem
indicating the item to share when the role
is shareMenu
.
This property can be dynamically changed.
menuItem.commandId
A number
indicating an item's sequential unique id.
menuItem.menu
Un Menu
dont l'item fait partie.