Menu

Menu UI component is used to create menus in a menu bar or create a submenu in context menu. It extends MenuItem class. Menu object can be added to MenuBar, ContextMenu, or another Menu object to create a submenu.

Constructor

Extends

Methods

addChild(component, …data_or_indexopt) → {this}

Adds a child component to this container component. Overrides UIComponent.addChild() to validate components being added.

Parameters:
NameTypeAttributesDescription
componentObject

UI Component that can be a child of this component.

data_or_indexany<optional>
<repeatable>

Argument to pass index at which to insert the child. By default component is added to the end.

Returns:
Type: 
this

keyboardShortcut(keys)

Sets keyboard shortcut (accelerators) to menu item type of components: MenuItem, CheckBoxMenuItem, RadioButtonMenuItem. See examples below. Allows to activate the menu item action using specified keyboard key combination on keyboard. This shortcut is usually display in the right side of the mnu item's text.

Parameters:
NameTypeDescription
keysstring

Key combination as a string. See example how to construct it.

Returns:

String Description of keyboard shortcut

Example
Modifiers: CTRL|CONTROL|SHIFT|ALT|ALTGRAPH|META

Keys: A-Z|0-9|NUMPAD[0-9]|SPACE|INSERT|ESCAPE|F[1-24]|TAB|ADD|SUBTRACT|DELETE|BACK_SPACE|BACK_SLASH|
BRACELEFT|BRACERIGHT|OPEN_BRACKET|CLOSE_BRACKET|SEMICOLON|COMMA|CAPS_LOCK|BACK_QUOTE|MINUS|PERIOD|
UP|DOWN|RIGHT|LEFT|MULTIPLY|DIVIDE|NUM_LOCK|HOME|END|PAGE_UP|PAGE_DOWN|PAUSE|SCROLL_LOCK|WINDOWS|
CONTEXT_MENU|DECIMAL|ENTER

Example key combinations: CTRL+Q, ALT+F1, CTRL+SHIFT+5, CTRL+N4, META+SHIFT+6, DELETE+SHIFT, SHIFT+UP, CTRL+OPEN_BRACKET, ATL+DOWN

menuItem1.keyboardShortcut("CTRL+Q");
menuItemUP.keyboardShortcut("SHIFT+NUMPAD[8]");

mnemonic(keyopt) → {string|MenuItem}

Gets or sets the mnemonic (keyboard shortcut key) for menu item type of components: Menu, MenuItem, CheckBoxMenuItem, RadioButtonMenuItem. Its not same as setting keyboardShortcut(...). A mnemonic allows users to activate the menu item using the keyboard (typically Alt + key).
NOTE: Not all systems and UIs show underline under mnemonic character. Some systems only show it after Alt key was pressed.

Parameters:
NameTypeAttributesDescription
keystring<optional>

A single character representing the mnemonic key (case-insensitive). If omitted, acts as a getter.

Inherited From
Returns:

If called without arguments, returns the current mnemonic key. If a key is provided, sets the mnemonic and returns this MenuItem instance.

Type: 
string | MenuItem
Example
menuItem.text("Quit").mnemonic("q"); // Makes letter Q to be used as the keyboard shortcut key

onPopupEvents(callbackopt, removeopt) → {Array.<function()>|ComboBox}

Registers, unregisters and gets a list of handler functions for the menu popup events (when the menu becomes expended and collapsed).

Parameters:
NameTypeAttributesDescription
callbackfunction<optional>

Event handler function that receives event Object as an argument. Check example below for event Object format.

removeboolean<optional>

Set to true to unregister the handler function.

Returns:

Returns an array of registered handler functions if arguments are omitted, otherwise returns this component instance.

Type: 
Array.<function()> | ComboBox
Example
mainForm.mainMenuBar = new MenuBar();
let handler = function(e){console.log(e);}
mainForm.fileMenu = new Menu("File").addTo(mainForm.mainMenuBar).onPopupEvents(handler);

// event Object format:
const e = {
  target: object,        // The component that triggered the event.
  type: string,          // `show`, `hide`, `cancel`.
  invoker: Object        // UI Component that invoked the popup, or `null` if it was invoked programmatically.
  x: number,             // X position relative to invoker. Only present when invoker is not `null`.
  y: number,             // Y position relative to invoker. Only present when invoker is not `null`.
  screenX: number,       // X position relative to default screen.
  screenY: number,       // Y position relative to default screen.
  interactive: boolean,  // Indicates if the popup was triggered by user's interaction (not programmatically).
};

open(positionopt, invokeropt) → {MenuItem}

Opens this menu item and all of its parent menus or context menu.

Parameters:
NameTypeAttributesDefaultDescription
positionobject<optional>
null

Example: {x: 100, y: 100}. Use only when root ancestor of this item is ContextMenu, to specify position where to position the ContextMenu. If invoker is specified, then position is relative to position of invoker component, otherwise it's a screen position.

invokerobject<optional>
null

Used to set invoker if the context menu isn't assigned to any object and you want to be able to get invoker by using "invoker" getter.

Inherited From
Returns:

Returns this MenuItem instance.

Type: 
MenuItem