Skip to content

menu ​

go
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.

变量 ​

go
var ErrUnsupported = platform.ErrUnsupported

ErrUnsupported is platform.ErrUnsupported.

函数 ​

go
func Popup(s *tango.Surface, x, y int, m *Menu) error

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 ​

go
func RegisterContext(a *tango.App, name string, m *Menu) error

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 ​

go
func RegisterContextFunc(a *tango.App, name string, fn func(Context) *Menu) error

RegisterContextFunc is RegisterContext with a menu built per request, on the main thread, from the click context; returning nil shows nothing.

func SetAppMenu ​

go
func SetAppMenu(a *tango.App, m *Menu) error

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.

func SetSurfaceMenu ​

go
func SetSurfaceMenu(a *tango.App, s *tango.Surface, m *Menu) error

SetSurfaceMenu sets the window menu of s (Windows/Linux; on macOS the backend decides, usually the shared menu bar).

类型 ​

Context ​

go
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 ​

go
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 ​

go
func (it *Item) IsChecked() bool

IsChecked reads Checked; safe from any goroutine.

func (*Item) SetChecked ​

go
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 ​

go
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 ​

go
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 ​

go
func (it *Item) SetLabel(label string)

SetLabel changes the label. Call Menu.Update to apply the change to native menus.

go
type Menu struct {
	Items []*Item
}

Menu is a list of items: the menu bar, or a submenu.

func New ​

go
func New() *Menu

New returns an empty menu.

go
func (m *Menu) Add(label string, onClick func()) *Item

Add appends a clickable item.

go
func (m *Menu) Check(label string, checked bool, onClick func()) *Item

Check appends a checkable item. Clicking it toggles Checked before onClick runs; read the new state with IsChecked.

go
func (m *Menu) Radio(group, label string, checked bool, onClick func()) *Item

Radio appends a radio item of group. Clicking it checks it and unchecks the other items of group in m before onClick runs.

go
func (m *Menu) Role(r Role) *Item

Role appends a standard item.

go
func (m *Menu) Separator() *Item

Separator appends a separator.

go
func (m *Menu) Submenu(label string) *Menu

Submenu appends an item opening a new submenu and returns the submenu.

go
func (m *Menu) Update() error

Update 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 ​

go
type Role string

Role is a platform standard item whose label and action the backend supplies.

go
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"
)

以 MIT 许可发布