Menu

Overview

The menu class allows you to create system menus and can be used with a variety of other controls. To use the menu as the system menu, see Window.menu.

Ancestors

  1. None
 

Descendants

  1. None
 

See Also

  1. Window
  2. DropDown

Constructors

Description

Creates a new menu object.

Definition

new Menu(title)

Example

require('Common');
var win = new Window(); // Create a new window.
win.visible = true; // make sure the window is shown.
// Create a menu in OSX
var mainMenu = new Menu();
var appleMenu = new MenuItem(application.name, '');
var fileMenu = new MenuItem('File', '');
var editMenu = new MenuItem('Edit', '');
var windowMenu = new MenuItem('Window', '');
var helpMenu = new MenuItem('Help', '');
mainMenu.appendChild(appleMenu);
mainMenu.appendChild(fileMenu);
mainMenu.appendChild(editMenu);
mainMenu.appendChild(windowMenu);
mainMenu.appendChild(helpMenu);

var appleSubmenu = new Menu(application.name);
appleSubmenu.appendChild(new MenuItem('About '+application.name, ''));
appleSubmenu.appendChild(new MenuItemSeparator());
appleSubmenu.appendChild(new MenuItem('Hide '+application.name, 'h'))
   .addEventListener('click', function() { application.visible = false; });
appleSubmenu.appendChild(new MenuItem('Hide Others', ''))
   .addEventListener('click', function() { application.hideAllOtherApplications(); });
appleSubmenu.appendChild(new MenuItem('Show All', ''))
   .addEventListener('click', function() { application.unhideAllOtherApplications(); });
appleSubmenu.appendChild(new MenuItemSeparator());
appleSubmenu.appendChild(new MenuItem('Quit '+application.name, 'q'))
   .addEventListener('click', function() { process.exit(0); });
appleMenu.submenu = appleSubmenu;

var fileSubmenu = new Menu('File');
fileSubmenu.appendChild(new MenuItem('New File', 'f'));
fileSubmenu.appendChild(new MenuItem('Open...', 'o'));
fileSubmenu.appendChild(new MenuItem('Save', 's'));
fileSubmenu.appendChild(new MenuItem('Save As...', 'S', 'shift'));
fileSubmenu.appendChild(new MenuItemSeparator());
fileSubmenu.appendChild(new MenuItem('Close', 'c', 'cmd'));
fileMenu.submenu = fileSubmenu;

var editSubmenu = new Menu('Edit');
var undo = new MenuItem('Undo', 'u');
undo.addEventListener('click', function() { application.undo(); });
editSubmenu.appendChild(undo);
editSubmenu.appendChild(new MenuItem('Redo', 'r'))
   .addEventListener('click', function() { application.redo(); });
editSubmenu.appendChild(new MenuItemSeparator());
editSubmenu.appendChild(new MenuItem('Copy', 'c'))
   .addEventListener('click', function() { application.copy(); });
editSubmenu.appendChild(new MenuItem('Cut', 'x'))
    .addEventListener('click', function() { application.cut(); });
editSubmenu.appendChild(new MenuItem('Paste', 'p'))
   .addEventListener('click', function() { application.paste(); });
editMenu.submenu = editSubmenu;

var windowSubmenu = new Menu('Window');
windowSubmenu.appendChild(new MenuItem('Minimize', 'm'))
    .addEventListener('click', function() { win.state = "minimized"; });
windowSubmenu.appendChild(new MenuItem('Zoom', ''))
    .addEventListener('click', function() { win.state = "maximized"; });
windowSubmenu.appendChild(new MenuItemSeparator());
windowSubmenu.appendChild(new MenuItem('Bring All to Front', ''))
    .addEventListener('click', function() { win.bringToFront(); });
windowSubmenu.appendChild(new MenuItemSeparator());
windowMenu.submenu = windowSubmenu;

var helpSubmenu = new Menu('Help');
helpSubmenu.appendChild(new MenuItem('Website', ''));
helpSubmenu.appendChild(new MenuItem('Online Documentation', ''));
helpSubmenu.appendChild(new MenuItem('License', ''));
helpMenu.submenu = helpSubmenu;

win.menu = mainMenu;

Methods

appendChild(menuitem)
Description

Appends a menu item to this menu.

Definition

menu.appendChild(menuitem)

Example

require('Common');
var win = new Window(); // Create a new window.
win.visible = true; // make sure the window is shown.
// Create a menu in OSX
var mainMenu = new Menu();
var appleMenu = new MenuItem(application.name, '');
var fileMenu = new MenuItem('File', '');
var editMenu = new MenuItem('Edit', '');
var windowMenu = new MenuItem('Window', '');
var helpMenu = new MenuItem('Help', '');
mainMenu.appendChild(appleMenu);
mainMenu.appendChild(fileMenu);
mainMenu.appendChild(editMenu);
mainMenu.appendChild(windowMenu);
mainMenu.appendChild(helpMenu);

var appleSubmenu = new Menu(application.name);
appleSubmenu.appendChild(new MenuItem('About '+application.name, ''));
appleSubmenu.appendChild(new MenuItemSeparator());
appleSubmenu.appendChild(new MenuItem('Hide '+application.name, 'h'))
   .addEventListener('click', function() { application.visible = false; });
appleSubmenu.appendChild(new MenuItem('Hide Others', ''))
   .addEventListener('click', function() { application.hideAllOtherApplications(); });
appleSubmenu.appendChild(new MenuItem('Show All', ''))
   .addEventListener('click', function() { application.unhideAllOtherApplications(); });
appleSubmenu.appendChild(new MenuItemSeparator());
appleSubmenu.appendChild(new MenuItem('Quit '+application.name, 'q'))
   .addEventListener('click', function() { process.exit(0); });
appleMenu.submenu = appleSubmenu;
win.menu = mainMenu;
Parameters
NameTypeDescription
menuitem MenuItem The submenu item to add to the menu object.
removeChild(menuitem)
Description

Removes a menu item that is on this menu.

Definition

menu.removeChild(menuitem)
Parameters
NameTypeDescription
menuitem MenuItem The submenu item to remove from the menu object.