Skip to content

tray ​

go
import "github.com/tangoui/tango/sys/tray"

本页由 site/tools/apigen 从源码注释生成(按 darwin 构建标签选文件),请勿手改。

Package tray shows an icon in the system tray (macOS menu bar extras, Windows notification area, Linux AppIndicator / StatusNotifierItem).

t, err := tray.New(a, tray.Options{Icon: png, Template: true, Tooltip: "My App"})

The menu bar on macOS wants a monochrome template image while Windows and Linux show a coloured icon; give both and each platform picks its own:

tray.Options{Icon: colourPNG, MacIcon: templatePNG, Template: true}
m := menu.New()
m.Add("Open", show)
m.Separator()
m.Role(menu.Quit)
t.SetMenu(m)
t.OnClick(show)

With a menu set, by default a primary click opens it on macOS and Linux and a right click opens it on Windows; OnClick then fires only where the platform separates the two (Windows left click). Options.MenuOn changes that: MenuOnSecondary opens the menu on a right click and reports left clicks with OnClick (macOS, Windows); MenuOnPrimary opens it on a left click everywhere. Without a menu, every click reaches the handlers. On Linux the icon is a StatusNotifierItem (KDE, GNOME with the AppIndicator extension, most other panels): clicks and MenuOn work as above, and Bounds is the point of the last primary click (1x1). Where no StatusNotifierWatcher runs (or TANGO_LINUX_TRAY=appindicator) it falls back to libayatana-appindicator, which reports neither clicks nor bounds and opens the menu on any click; set a menu there.

All methods are safe from any goroutine; handlers run on the main thread. Changing the tray menu's items takes effect with Menu.Update.

常量 ​

go
const (
	MenuOnDefault   = platform.TrayMenuDefault   // left click on macOS and Linux, right click on Windows
	MenuOnPrimary   = platform.TrayMenuPrimary   // left click; OnClick does not fire while a menu is set
	MenuOnSecondary = platform.TrayMenuSecondary // right click; left clicks reach OnClick
)

变量 ​

go
var ErrUnsupported = platform.ErrUnsupported

ErrUnsupported is platform.ErrUnsupported.

类型 ​

go
type MenuTrigger = platform.TrayMenuTrigger

MenuTrigger chooses which click opens the tray menu.

Options ​

go
type Options struct {
	Icon []byte // PNG; 32x32 or 44x44 px (for 2x displays) works well
	// MacIcon, if set, is shown on macOS instead of Icon: typically a black
	// on transparent PNG (36x36 or 44x44 px) used with Template.
	MacIcon  []byte
	Template bool // macOS: the image is a monochrome template that follows the menu bar's appearance
	Tooltip  string
	Title    string      // macOS: text shown next to the icon
	MenuOn   MenuTrigger // which click opens the menu; see the package comment
}

Options configures a tray icon.

Rect ​

go
type Rect = platform.Rect

Rect is a screen rectangle in logical px, origin top-left of the primary display.

Tray ​

go
type Tray struct {
	// contains filtered or unexported fields
}

Tray is one tray icon. Create it with New.

func New ​

go
func New(a *tango.App, o Options) (*Tray, error)

New adds a tray icon. It returns ErrUnsupported where the platform has no tray (or, on Linux, neither a StatusNotifierWatcher nor an AppIndicator library is available).

func (*Tray) Bounds ​

go
func (t *Tray) Bounds() (Rect, bool)

Bounds returns the icon's rectangle on screen, if the platform knows it (useful to place a popover window under it; see sys/flyout). On Linux it is the 1x1 point of the last click on the icon.

func (*Tray) OnClick ​

go
func (t *Tray) OnClick(fn func())

OnClick adds fn for a primary click (see the package comment for when platforms report it).

func (*Tray) OnDoubleClick ​

go
func (t *Tray) OnDoubleClick(fn func())

OnDoubleClick adds fn for a double click (Windows; macOS reports it after the two single clicks).

func (*Tray) OnRightClick ​

go
func (t *Tray) OnRightClick(fn func())

OnRightClick adds fn for a secondary click.

func (*Tray) Remove ​

go
func (t *Tray) Remove()

Remove takes the icon away. The Tray is unusable afterwards.

func (*Tray) SetIcon ​

go
func (t *Tray) SetIcon(png []byte, template bool)

SetIcon replaces the icon (PNG); template as in Options.Template.

func (*Tray) SetMacIcon ​

go
func (t *Tray) SetMacIcon(png []byte)

SetMacIcon replaces the macOS image (Options.MacIcon); nil falls back to Icon.

func (*Tray) SetMenu ​

go
func (t *Tray) SetMenu(m *menu.Menu) error

SetMenu attaches m to the icon (nil removes the menu). Later changes to m are shown after m.Update().

func (*Tray) SetMenuOn ​

go
func (t *Tray) SetMenuOn(m MenuTrigger)

SetMenuOn chooses which click opens the menu (Options.MenuOn).

func (*Tray) SetTitle ​

go
func (t *Tray) SetTitle(text string)

SetTitle sets the text next to the icon (macOS).

func (*Tray) SetTooltip ​

go
func (t *Tray) SetTooltip(text string)

SetTooltip sets the hover text.

以 MIT 许可发布