A menu is a list of options or actions presented to the user as a popup.
Here are the available methods for Menu Component.
Examples
Example - Basic menu
class Main extends App
{
onStart()
{
this.main = ui.addLayout("main", "Linear", "VCenter,FillXY")
this.btn = ui.addButton(this.main, "Show Menu", "Primary")
this.btn.setOnTouch( this.onTouch )
var items = ["Item 1", "Item 2", "Item 3"]
this.menu = ui.addMenu(this.btn, items)
this.menu.setOnTouch( this.menuTouch )
}
onTouch()
{
this.menu.show()
}
menuTouch( item )
{
ui.showPopup( item )
}
}
from hybrid import ui
def OnStart():
global menu
main = ui.addLayout("main", "Linear", "VCenter,FillXY")
btn = ui.addButton(main, "Show Menu", "Primary")
btn.setOnTouch(onTouch)
items = ["Item 1", "Item 2", "Item 3"]
menu = ui.addMenu(btn, items)
menu.setOnTouch(menuTouch)
def onTouch(event):
menu.show()
def menuTouch(item, icon, index):
ui.showPopup(item)
Example - Changing anchor component
class Main extends App
{
onStart()
{
this.main = ui.addLayout("main", "Linear", "VCenter,FillXY")
this.main.childSpacing = "Even";
this.btn1 = ui.addButton(this.main, "Button 1", "Primary")
this.btn1.setOnTouch( this.onTouch1 )
this.btn2 = ui.addButton(this.main, "Button 2", "Secondary")
this.btn2.setOnTouch( this.onTouch2 )
var items = ["Item 1", "Item 2", "Item 3"]
this.menu = ui.addMenu(null, items)
this.menu.setOnTouch( this.menuTouch )
}
onTouch1()
{
this.menu.show( this.btn1 )
}
onTouch2()
{
this.menu.show( this.btn2 )
}
menuTouch( item )
{
ui.showPopup( item )
}
}
from hybrid import ui
def OnStart():
global btn1, menu, btn2
main = ui.addLayout("main", "Linear", "VCenter,FillXY")
main.setChildSpacing("Evenly")
btn1 = ui.addButton(main, "Button 1", "Primary")
btn1.setOnTouch(onTouch1)
btn2 = ui.addButton(main, "Button 2", "Secondary")
btn2.setOnTouch(onTouch2)
items = ["Item 1", "Item 2", "Item 3"]
menu = ui.addMenu(None, items)
menu.setOnTouch(menuTouch)
def onTouch1(event):
menu.show(btn1)
def onTouch2(event):
menu.show(btn2)
def menuTouch(item, icon, index):
ui.showPopup(item)
Example - With icons and disabled items
class Main extends App
{
onStart()
{
this.main = ui.addLayout("main", "Linear", "VCenter,FillXY")
this.btn = ui.addButton(this.main, "Show Menu", "Primary")
this.btn.setOnTouch( this.onTouch )
var items = ["person:Item 1", "delete:Item 2", "favorite:Item 3"]
this.menu = ui.addMenu(this.btn, items, "Icons,Primary,Dense")
this.menu.setEnabled(1, false)
this.menu.setOnTouch( this.menuTouch )
}
onTouch()
{
this.menu.show()
}
menuTouch( item )
{
ui.showPopup( item )
}
}
from hybrid import ui
def OnStart():
global menu
main = ui.addLayout("main", "Linear", "VCenter,FillXY")
btn = ui.addButton(main, "Show Menu", "Primary")
btn.setOnTouch(onTouch)
items = ["person:Item 1", "delete:Item 2", "favorite:Item 3"]
menu = ui.addMenu(btn, items, "Icons,Primary,Dense")
menu.setEnabled(1, False)
menu.setOnTouch(menuTouch)
def onTouch(event):
menu.show()
def menuTouch(item, icon, index):
ui.showPopup(item)
Example - Anchor position on mouse / touch
class Main extends App
{
onStart()
{
this.main = ui.addLayout("main", "Linear", "VCenter,FillXY")
this.main.setOnContextMenu( this.onMenu )
this.txt = ui.addText(this.main, "Right click anywhere on the screen.\n(Long press if your on mobile phone)", "Multiline")
var items = ["person:Item 1", "delete:Item 2", "favorite:Item 3"]
this.menu = ui.addMenu(null, items, "Icons,Dense")
this.menu.setOnTouch( this.menuTouch )
}
onMenu( pos )
{
this.menu.show(pos.x, pos.y)
}
menuTouch( item )
{
ui.showPopup( item )
}
}
class Main extends App
onStart()
this.main = ui.addLayout("main", "Linear", "VCenter,FillXY")
this.main.setOnContextMenu( this.onMenu )
this.txt = ui.addText(this.main, "Right click anywhere on the screen.\n(Long press if your on mobile phone)", "Multiline")
items = ["person:Item 1", "delete:Item 2", "favorite:Item 3"]
this.menu = ui.addMenu(null, items, "Icons,Dense")
this.menu.setOnTouch( this.menuTouch )
onMenu( pos )
this.menu.show(pos.x, pos.y)
menuTouch( item )
ui.showPopup( item )
Properties
The following properties are available on the Menu object:
Methods
The following methods are available on the Menu object:
Boolean: Value can be `true` or `false`
Boolean: Values can be `true` or `false`. `false` to disable.
Boolean: Values can be `true` or `false`. `false` to disable an item.
Number: Fraction of the parent width `[0-1]`.
Number: Fraction of the parent height `[0-1]`.
Number: The index at which to add the item.
Number: The index of the item.
Number: The index of the item to remove.
Number: Left padding of the menu item.
Number: Top padding of the menu item.
Number: Right padding of the menu item.
Number: Bottom padding of the menu item.
Number: The position of the menu from the left of the parent or anchor component. The unit is in pixels.
Number: The position of the menu from the top of the parent or anchor component. The unit is in pixels.
String: “A comma separated options.
Theme Color: `Primary`”, “ `Secondary`”, “ `Error`
Utils: `Dense`”, “ `Icon`”
String: “The new item to be added.”
String: “The name of the item.”
String: “The name of the item to remove.”
String: “The name of the corresping item the menu.”
String: “Unit of measurement. Can be `rem`”, “ `px`”, “ `%`”, “ or `v` for viewport.”
Object: The component where to anchor the Menu.
Object: The component where to anchor the menu. It can be a `Button` or an `Image`.
List: A list of items to be shown in the pop-up menu. You can also pass a comma separated string. For menu with icon the format is `
icon Add a `colon` before an item to display it as the label text.
List: The list items to show.
function(
item ,
icon ,
index
)
men.addItem
Adds an item in the menu list.
men.getEnabled
Get the enabled state of an item the menu popup.
men.getEnabledByName
Get the enabled state of an item in the menu popup.
men.hide
Hide the pop-up menu.
men.itemPadding
Sets or returns the padding of each menu item. See also setItemPadding method.
men.list
Sets or returns the items in the menu. You can also pass a comma separated string.
men.popItem
Removes the last item. This will return the item being removed.
men.removeItemByIndex
Removes an item in the menu items list by its index.
men.removeItemByName
Removes an item in the menu items list by its name.
men.setAutoFocus
Sets the autofocus value of the menu items.
men.setEnabled
Enable or disable an item in the menu popup.
men.setEnabledByName
Enable or disable an item in the menu popup by its name.
men.setItemPadding
Sets the padding of the menu item. See itemPadding property for equivalent setter/getter property.
men.setList
Updates the list items on the menu.
men.setOnTouch
Adds a callback handler when the menu is touch.
men.setPosition
Set the position of the menu from the left and top of the anchor component.
men.shiftItem
Removes the first item. This will return the item being removed.
men.show
Show the menu pop-up.
If you passed a parent on initialization, then the menu si anchored on that component.