Documentation
¶
Overview ¶
Package menu builds native menus in Go and routes their clicks to Go callbacks.
m := menu.New()
app := m.Submenu("App")
app.Role(menu.About)
app.Separator()
app.Role(menu.Quit)
file := m.Submenu("File")
file.Add("New", func() { ... }).Accelerator = "CmdOrCtrl+N"
wrap := file.Check("Word Wrap", true, nil)
view := m.Submenu("View")
view.Radio("size", "Small", false, nil)
view.Radio("size", "Large", true, nil)
m.Role(menu.EditMenu)
err := menu.SetAppMenu(a, m)
The same Menu can be shown in several places: the menu bar (SetAppMenu / SetSurfaceMenu), a tray icon (package tray), a context menu (Popup, RegisterContext).
Add AppMenu, EditMenu, and WindowMenu directly to the menu bar. Each role supplies an entire submenu. Add individual roles to a named submenu when you need custom contents. Quit already has Command-Q on macOS, so assign that shortcut to just one quit item.
Changing a menu ¶
A Menu is a description. After changing items (SetLabel, SetChecked, SetDisabled, SetIcon, or adding items), call Update on the menu, or on any submenu of it, to send the new state to every place it is shown. Checkable and radio items are the exception: clicking them toggles Checked (radio: checks the item and unchecks the others of its group in the same submenu) and refreshes the native menus by itself, before OnClick runs.
Item fields may be written directly before the menu is first set; after that use the setters (any goroutine), or write fields on the main thread (inside a click callback, or with App.Post).
Click callbacks run on the main thread. Item ids are allocated internally, unique across every menu set.
Index ¶
- Variables
- func Popup(s *tango.Surface, x, y int, m *Menu) error
- func RegisterContext(a *tango.App, name string, m *Menu) error
- func RegisterContextFunc(a *tango.App, name string, fn func(Context) *Menu) error
- func SetAppMenu(a *tango.App, m *Menu) error
- func SetSurfaceMenu(a *tango.App, s *tango.Surface, m *Menu) error
- type Context
- type Item
- type Menu
- func (m *Menu) Add(label string, onClick func()) *Item
- func (m *Menu) Check(label string, checked bool, onClick func()) *Item
- func (m *Menu) Radio(group, label string, checked bool, onClick func()) *Item
- func (m *Menu) Role(r Role) *Item
- func (m *Menu) Separator() *Item
- func (m *Menu) Submenu(label string) *Menu
- func (m *Menu) Update() error
- type Role
Constants ¶
This section is empty.
Variables ¶
var ErrUnsupported = platform.ErrUnsupported
ErrUnsupported is platform.ErrUnsupported.
Functions ¶
func Popup ¶
Popup shows m as a context menu at (x, y) in s's content coordinates (logical px, the page's clientX / clientY at zoom 1) and blocks until the menu closes. If the user chose an item, its OnClick has run (on the main thread) by the time Popup returns.
Safe from any goroutine. Called on the main thread (from a click or message callback) it keeps the UI responsive while waiting, like tango.AwaitAsync. The menu is built when Popup is called; later changes to m do not affect an open popup.
func RegisterContext ¶
RegisterContext makes m the context menu of page elements declaring name, with the attribute data-tango-menu="name" or the CSS property --tango-contextmenu: name. A nil m unregisters name.
The first registration on an app injects the page script: surfaces created afterwards get it on every page load, existing ones only for their current page. Register context menus before creating surfaces.
func RegisterContextFunc ¶
RegisterContextFunc is RegisterContext with a menu built per request, on the main thread, from the click context; returning nil shows nothing.
func SetAppMenu ¶
SetAppMenu sets the application menu: the menu bar on macOS, the menu of every current and future window on Windows and Linux. A nil menu clears it.
Types ¶
type Context ¶
type Context struct {
Surface *tango.Surface
Name string // the declared menu name
X, Y int // click position in the surface's content (CSS px)
// Data is the value of the data-tango-menu-data attribute nearest to
// the clicked element ("" if none), e.g. a row id.
Data string
}
Context describes a context menu request from a page.
type Item ¶
type Item struct {
Label string
Accelerator string // "CmdOrCtrl+Shift+N"
Role Role // non-empty for a standard item; OnClick is not called
Separator bool
Disabled bool
Checked bool
// Checkable shows a check mark slot; clicking toggles Checked.
Checkable bool
// RadioGroup makes a radio item. Items of the same submenu sharing a
// group are exclusive: clicking one checks it and unchecks the others.
RadioGroup string
// Icon is a PNG shown next to the label where supported (macOS,
// Windows); 16x16 logical px is typical. Ignored elsewhere.
Icon []byte
OnClick func() // main thread
// contains filtered or unexported fields
}
Item is one menu entry.
func (*Item) SetChecked ¶
SetChecked checks or unchecks the item. Checking a radio item unchecks the others of its group. Call Menu.Update to apply the change to native menus.
func (*Item) SetDisabled ¶
SetDisabled enables or disables the item. Call Menu.Update to apply the change to native menus.
func (*Item) SetIcon ¶
SetIcon sets the PNG icon (nil removes it). Call Menu.Update to apply the change to native menus.
func (*Item) SetLabel ¶
SetLabel changes the label. Call Menu.Update to apply the change to native menus.
type Menu ¶
type Menu struct {
Items []*Item
}
Menu is a list of items: the menu bar, or a submenu.
func (*Menu) Check ¶
Check appends a checkable item. Clicking it toggles Checked before onClick runs; read the new state with IsChecked.
func (*Menu) Radio ¶
Radio appends a radio item of group. Clicking it checks it and unchecks the other items of group in m before onClick runs.
type Role ¶
type Role string
Role is a platform standard item whose label and action the backend supplies.
const ( Quit Role = "quit" About Role = "about" Hide Role = "hide" Copy Role = "copy" Paste Role = "paste" Cut Role = "cut" SelectAll Role = "selectAll" Undo Role = "undo" Redo Role = "redo" Minimize Role = "minimize" Close Role = "close" AppMenu Role = "appMenu" EditMenu Role = "editMenu" WindowMenu Role = "windowMenu" )