Menu
A menu of actions in a popover, opened from a button or by right-clicking.
Loading the live example…
import (
"github.com/egoist/mygo/ui"
"github.com/oneo-me/onevra/source/components"
)
type menuDemo struct {
last string
pinned bool
}
func (d *menuDemo) View(c *ui.Context) {
actions := func(m *components.Menu) {
if m.Item("Open").Shortcut("⌘O").Chosen() {
d.last = "Open"
}
if m.Item("Pinned").Checked(d.pinned).Chosen() {
d.pinned = !d.pinned
}
m.Group("Move to", func(m *components.Menu) {
for _, folder := range []string{"Desktop", "Documents"} {
if m.Item(folder).Chosen() {
d.last = "Move to " + folder
}
}
})
if m.Item("Delete").Disabled(d.pinned).Chosen() {
d.last = "Delete"
}
}
ui.Column(c).Gap(16).AlignItems(ui.Center).Children(func() {
ui.Row(c).Gap(8).Children(func() {
components.MenuButton(c, components.Button(c, "Actions", components.ButtonSecondary), actions)
components.ContextMenu(c, components.Button(c, "Right-click me", components.ButtonOutline), actions)
})
text := "Choose an action"
if d.last != "" {
text = "Chose " + d.last
}
components.Text(c, text).TextColor(c.Theme().TextMuted)
})
}components.MenuButton opens a menu below a button, and components.ContextMenu opens one where an element is right-clicked, or below it for the menu key. Build the element first, then call either with it every frame; ask each item whether it was Chosen(), which also closes the menu.
Group puts items under a heading, which sets them apart without a separator. The menu has the look of the other popovers, and the keys of a menu: Up and Down move past disabled items, Enter chooses and Escape closes.
For menus of the system's own, as in the menu bar, use MyGo's Menu and ContextMenu, shown on the button page.
API
ContextMenu
func ContextMenu(c *ui.Context, target ui.Element, build func(m *Menu))ContextMenu gives target a menu, drawn in a Popover with the look of the other popovers, that opens where target is right-clicked (Control-clicked on macOS), or for the menu key or Shift+F10 while it or an element inside it has the focus, below it unless the pointer rests on it. It leaves target's clicks to it and its parents. Call it every frame after building target, which must be keyed where it repeats. build fills the menu while it shows, and the item chosen reports Chosen, which closes it:
components.ContextMenu(c, row, func(m *components.Menu) {
if m.Item("Rename").Chosen() {
app.renaming = i
}
m.Group("Move to", func(m *components.Menu) {
for _, f := range app.folders {
if m.Item(f.Name).Chosen() {
app.move(i, f)
}
}
})
m.Separator()
if m.Item("Delete").Disabled(note.Locked).Chosen() {
app.delete(i)
}
})A disabled target opens none. Up and Down move among the items, past the disabled ones, Enter and Space choose one, and Escape or a press outside closes the menu.
pkg.go.devMenuButton
func MenuButton(c *ui.Context, button ui.Element, build func(m *Menu))MenuButton makes button open a menu below it, as ContextMenu's, when it is clicked or activated with Enter or Space, and close it when clicked again. Call it every frame after building button, instead of reading button's Clicked:
more := components.IconButton(c, lucide.Ellipsis(), "More")
components.MenuButton(c, more, func(m *components.Menu) {
if m.Item("Duplicate").Chosen() {
app.duplicate()
}
})Menu
type Menu struct {
// contains filtered or unexported fields
}Menu is a menu being built, by the function given to ContextMenu or MenuButton. Its items show in the order they are added.
Methods
func (m *Menu) Group(label string, build func(m *Menu))Group adds the items build adds under a heading of label, which sets them apart without a separator.
func (m *Menu) Item(label string) *MenuItemItem adds an item showing label.
func (m *Menu) Separator()Separator adds a line between groups of items. It shows only between items, once.
MenuItem
type MenuItem struct {
// contains filtered or unexported fields
}MenuItem is an item of a Menu. Its options are set before Chosen, which reports whether it was chosen.
Methods
func (it *MenuItem) Checked(on bool) *MenuItemChecked shows a check mark by the item when on is true, as for a setting that choosing the item toggles. The menu leaves room for it either way.
func (it *MenuItem) Chosen() boolChosen reports whether the item was chosen in this frame, which closes the menu.
func (it *MenuItem) Disabled(d bool) *MenuItemDisabled shows the item grayed out when d is true: it cannot be chosen.
func (it *MenuItem) Shortcut(hint string) *MenuItemShortcut shows a key hint, such as "⌘B", after the item's label. It only shows the key; the application handles it.