Documentation
¶
Overview ¶
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.
Index ¶
- Constants
- Variables
- type MenuTrigger
- type Options
- type Rect
- type Tray
- func (t *Tray) Bounds() (Rect, bool)
- func (t *Tray) OnClick(fn func())
- func (t *Tray) OnDoubleClick(fn func())
- func (t *Tray) OnRightClick(fn func())
- func (t *Tray) Remove()
- func (t *Tray) SetIcon(png []byte, template bool)
- func (t *Tray) SetMacIcon(png []byte)
- func (t *Tray) SetMenu(m *menu.Menu) error
- func (t *Tray) SetMenuOn(m MenuTrigger)
- func (t *Tray) SetTitle(text string)
- func (t *Tray) SetTooltip(text string)
Constants ¶
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 )
Variables ¶
var ErrUnsupported = platform.ErrUnsupported
ErrUnsupported is platform.ErrUnsupported.
Functions ¶
This section is empty.
Types ¶
type MenuTrigger ¶
type MenuTrigger = platform.TrayMenuTrigger
MenuTrigger chooses which click opens the tray menu.
type Options ¶
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.
type Tray ¶
type Tray struct {
// contains filtered or unexported fields
}
Tray is one tray icon. Create it with New.
func New ¶
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 ¶
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 ¶
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 ¶
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 ¶
func (t *Tray) OnRightClick(fn func())
OnRightClick adds fn for a secondary click.
func (*Tray) Remove ¶
func (t *Tray) Remove()
Remove takes the icon away. The Tray is unusable afterwards.
func (*Tray) SetMacIcon ¶
SetMacIcon replaces the macOS image (Options.MacIcon); nil falls back to Icon.
func (*Tray) SetMenu ¶
SetMenu attaches m to the icon (nil removes the menu). Later changes to m are shown after m.Update().
func (*Tray) SetMenuOn ¶
func (t *Tray) SetMenuOn(m MenuTrigger)
SetMenuOn chooses which click opens the menu (Options.MenuOn).