menu
import "github.com/tangoui/tango/sys/menu"本页由
site/tools/apigen从源码注释生成(按 darwin 构建标签选文件),请勿手改。
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.
变量
var ErrUnsupported = platform.ErrUnsupportedErrUnsupported is platform.ErrUnsupported.
函数
func Popup
func Popup(s *tango.Surface, x, y int, m *Menu) errorPopup 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
func RegisterContext(a *tango.App, name string, m *Menu) errorRegisterContext 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
func RegisterContextFunc(a *tango.App, name string, fn func(Context) *Menu) errorRegisterContextFunc is RegisterContext with a menu built per request, on the main thread, from the click context; returning nil shows nothing.
func SetAppMenu
func SetAppMenu(a *tango.App, m *Menu) errorSetAppMenu 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.
func SetSurfaceMenu
func SetSurfaceMenu(a *tango.App, s *tango.Surface, m *Menu) errorSetSurfaceMenu sets the window menu of s (Windows/Linux; on macOS the backend decides, usually the shared menu bar).
类型
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.
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
Submenu *Menu
OnClick func() // main thread
// contains filtered or unexported fields
}Item is one menu entry.
func (*Item) IsChecked
func (it *Item) IsChecked() boolIsChecked reads Checked; safe from any goroutine.
func (*Item) SetChecked
func (it *Item) SetChecked(checked bool)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
func (it *Item) SetDisabled(disabled bool)SetDisabled enables or disables the item. Call Menu.Update to apply the change to native menus.
func (*Item) SetIcon
func (it *Item) SetIcon(png []byte)SetIcon sets the PNG icon (nil removes it). Call Menu.Update to apply the change to native menus.
func (*Item) SetLabel
func (it *Item) SetLabel(label string)SetLabel changes the label. Call Menu.Update to apply the change to native menus.
Menu
type Menu struct {
Items []*Item
}Menu is a list of items: the menu bar, or a submenu.
func New
func New() *MenuNew returns an empty menu.
func (*Menu) Add
func (m *Menu) Add(label string, onClick func()) *ItemAdd appends a clickable item.
func (*Menu) Check
func (m *Menu) Check(label string, checked bool, onClick func()) *ItemCheck appends a checkable item. Clicking it toggles Checked before onClick runs; read the new state with IsChecked.
func (*Menu) Radio
func (m *Menu) Radio(group, label string, checked bool, onClick func()) *ItemRadio appends a radio item of group. Clicking it checks it and unchecks the other items of group in m before onClick runs.
func (*Menu) Role
func (m *Menu) Role(r Role) *ItemRole appends a standard item.
func (*Menu) Separator
func (m *Menu) Separator() *ItemSeparator appends a separator.
func (*Menu) Submenu
func (m *Menu) Submenu(label string) *MenuSubmenu appends an item opening a new submenu and returns the submenu.
func (*Menu) Update
func (m *Menu) Update() errorUpdate sends the current state of m to every place it is shown: menus set with SetAppMenu / SetSurfaceMenu or on a tray icon whose tree contains m. Safe from any goroutine; waits until the native menus are updated. Context menus need no update: they are built when they open.
Role
type Role stringRole 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"
)