Table of Contents

menu

`menu()` creates a menu object that can be attached to a `window()` with `window.menu(menu)`.

Menu groups and menu items are represented by `menu_item` objects. `menu.add(text)` creates a top-level menu group and returns its `menu_item` object. `menu_item.add(…)` then adds submenu items below that group or item.

`menu_item()` is not a public constructor. Create menu items through `menu.add(text)` or `menu_item.add(text)`.

Constructor

Creates a menu object.

Item Description
Syntax `menu()`
Arguments none
Returns menu object
m = menu();
file = m.add('File');

Properties

`menu` currently exposes no public properties.

Common Object Methods

to_string()

Returns a display string for the object.

Item Description
Syntax `value.to_string()`
Arguments none
Returns string

Example:

text = value.to_string();

`clone()` is not supported for `menu`.

Methods

add(text)

Adds a top-level menu group and returns the group as a `menu_item` object.

Item Description
Syntax `menu.add(text)`
Arguments `text`: top-level menu group text
Returns `menu_item` object
m = menu();
file = m.add('File');

menu_item

`menu_item` represents a menu group, submenu, or selectable menu item. It is returned by `menu.add(text)` and `menu_item.add(…)`.

A `menu_item` can contain child items. When an item has children, it is shown as a submenu. When an item has a callback and no children, it is shown as a selectable command.

Constructor

`menu_item` objects are not created with a public constructor.

Item Description
Syntax created by `menu.add(text)` or `menu_item.add(text)`
Arguments not applicable
Returns `menu_item` object
m = menu();
file = m.add('File');
open_item = file.add('Open');

Properties

`menu_item` currently exposes no public properties.

Common Object Methods

to_string()

Returns a display string for the object.

Item Description
Syntax `value.to_string()`
Arguments none
Returns string

Example:

text = value.to_string();

`clone()` is not supported for `menu_item`.

Methods

add(text)

Adds a submenu item below this menu item and returns the new item as a `menu_item` object.

Use this form when the new item will contain child items, or when you want to keep a reference to it before adding children.

Item Description
Syntax `menu_item.add(text)`
Arguments `text`: menu item text
Returns `menu_item` object
m = menu();
file = m.add('File');
recent = file.add('Recent');
recent.add('Job 1');

add(text, callback)

Adds a selectable menu item below this menu item and attaches a callback.

The callback is called with no arguments when the menu item is selected.

Item Description
Syntax `menu_item.add(text, callback)`
Arguments `text`: menu item text; `callback`: callable object to run when selected
Returns `menu_item` object
function open_file()
{
    print('open');
}
 
m = menu();
file = m.add('File');
file.add('Open', open_file);

Example

function open_file()
{
    print('open');
}
 
function save_file()
{
    print('save');
}
 
m = menu();
file = m.add('File');
file.add('Open', open_file);
file.add('Save', save_file);
 
window().title('Menu').menu(m).show();

Previous: separator

Next: image