Documentation
¶
Overview ¶
Package tray is a cross-platform system-tray (menu-bar) widget for go-widgets.
A tray icon is OS-integration, not a pixel-blitted widget, so it cannot live in the pure-blitting toolkit. This package models the tray, its menu and menu items in a platform-agnostic core, and drives them through a small Backend interface implemented per-OS:
- darwin: NSStatusItem + NSMenu via purego + the Objective-C runtime
- windows: Shell_NotifyIcon + TrackPopupMenu via x/sys/windows
- linux: StatusNotifierItem + com.canonical.dbusmenu over DBus
All CGO_ENABLED=0. A headless backend backs tests and display-less CI.
Index ¶
- Variables
- type Backend
- type Headless
- type Menu
- type MenuItem
- type Tray
- func (t *Tray) Attach() error
- func (t *Tray) Icon() []byte
- func (t *Tray) Menu() *Menu
- func (t *Tray) OnReady(fn func()) *Tray
- func (t *Tray) Quit()
- func (t *Tray) Run() error
- func (t *Tray) SetIcon(iconPNG []byte) *Tray
- func (t *Tray) SetMenu(m *Menu) *Tray
- func (t *Tray) SetTooltip(s string) *Tray
- func (t *Tray) Tooltip() string
- func (t *Tray) WithBackend(b Backend) *Tray
Constants ¶
This section is empty.
Variables ¶
var ErrNoBackend = errors.New("tray: no backend for this platform")
ErrNoBackend is returned by Run when no platform backend has been set (and none was selected for the current OS).
Functions ¶
This section is empty.
Types ¶
type Backend ¶
type Backend interface {
// Run shows the tray and blocks on the platform event loop until Quit.
Run(t *Tray) error
// Refresh re-applies the tray's icon, tooltip and menu after a change.
Refresh(t *Tray)
// Quit stops the event loop started by Run.
Quit()
}
Backend drives a Tray on a specific platform.
type Headless ¶
type Headless struct {
Started bool
Refreshes int
LastIcon []byte
LastTip string
LastMenu *Menu
// contains filtered or unexported fields
}
Headless is a display-less Backend for tests and CI. It records the tray state applied to it and blocks Run until Quit, so a tray can be exercised end-to-end without a real desktop session.
type Menu ¶
type Menu struct {
Items []*MenuItem
}
Menu is an ordered list of items.
type MenuItem ¶
type MenuItem struct {
Label string
Tooltip string
Checked bool
Disabled bool
Separator bool
// OnClick is invoked when the item is activated. For a checkbox item the
// Checked field is toggled before OnClick runs.
OnClick func()
// then ignored).
Submenu *Menu
// contains filtered or unexported fields
}
MenuItem is one entry in a tray menu.
type Tray ¶
type Tray struct {
// contains filtered or unexported fields
}
Tray is a system-tray icon with a tooltip and a menu.
func New ¶
New creates a tray showing iconPNG (PNG-encoded bytes). The platform backend is selected automatically; use WithBackend to override (eg. for tests).
func (*Tray) Attach ¶
Attach shows the tray inside a host-owned event loop and returns immediately, instead of Run's block-until-Quit. Use it from an application that already drives the platform's main run loop (its own window): Run would try to start a second loop, whereas Attach just registers the tray with the running one. It must be called on the platform's main/UI thread. Returns ErrNoBackend when the active backend does not support attaching.
func (*Tray) SetTooltip ¶
SetTooltip sets the hover tooltip and refreshes if running.
func (*Tray) WithBackend ¶
WithBackend overrides the platform backend and returns the tray.