Documentation
¶
Overview ¶
Package tray puts an icon with a menu in the system tray / menu bar, cgo-free.
Build a Config, hand it to Run, and Run blocks driving the OS event loop until Stop is called. Each backend binds what the OS already ships — NSStatusItem on macOS, Shell_NotifyIcon on Windows — with no cgo and no bundled libraries.
err := tray.Run(tray.Config{
Title: "myapp",
Items: []tray.Item{
{Title: "Open", OnClick: openUI},
{Separator: true},
{Title: "Quit", OnClick: tray.Stop},
},
})
Threading: Run owns the process's UI event loop, so it must be called from the main goroutine, locked to the main OS thread:
func main() {
runtime.LockOSThread()
tray.Run(cfg)
}
A menu item's OnClick runs on that UI thread; keep it short or hand work to another goroutine. Stop, by contrast, is safe to call from any goroutine.
Backends: macOS and Windows are implemented. Linux is not — a StatusNotifierItem tray means a D-Bus dependency this module avoids and it is fragmented across desktops, so Run returns ErrUnsupported there.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrAlreadyRunning = errors.New("tray: already running")
ErrAlreadyRunning is returned by Run when a tray is already active in this process; only one tray may run at a time.
var ErrNotRunning = errors.New("tray: not running")
ErrNotRunning is returned by SetItems when no tray is active, so a menu update that went nowhere is visible instead of silently dropped.
var ErrUnsupported = fmt.Errorf("tray: not supported on this platform: %w", errors.ErrUnsupported)
ErrUnsupported is returned by Run on a platform with no tray backend.
Functions ¶
func Run ¶
Run shows the tray and drives the OS event loop until Stop is called. It blocks and must be called from the main goroutine (see the package doc on threading). It returns ErrUnsupported on platforms with no backend and ErrAlreadyRunning if a tray is already active.
func SetItems ¶
SetItems replaces the whole menu while the tray is running, which is how an item is added, retitled, greyed out, or removed: pass the menu you want now. It is safe to call from any goroutine; the rebuild happens on the UI thread, so it may not have landed yet when SetItems returns. It reports ErrNotRunning if no tray is active and ErrUnsupported on a platform with no backend.
Types ¶
type Config ¶
type Config struct {
// Title is a short text label. macOS shows it in the menu bar (next to the
// icon, or alone when Icon is empty). Windows ignores it.
Title string
// Tooltip is shown on hover.
Tooltip string
// Icon is a PNG image for the menu bar / tray. macOS renders it, scaled to
// fit; with no icon it falls back to Title. Windows ignores this field and
// shows the application's default icon (honoring a custom PNG there is a
// follow-up). Linux has no backend. It is always safe to set.
Icon []byte
// Items are the menu entries, top to bottom. SetItems replaces them while
// the tray runs.
Items []Item
// OnReady is called once on the UI thread after the tray is visible and the
// event loop is turning. It exists so a caller can start work that needs a
// live tray without guessing at a delay. Like OnClick it runs on the UI
// thread, so keep it short or hand the work to another goroutine.
OnReady func()
}
Config describes the tray icon and its menu. It is read once by Run.
type Item ¶
type Item struct {
// Title is the menu text. Ignored when Separator is true.
Title string
// Disabled greys the item out and suppresses OnClick.
Disabled bool
// Separator makes this a divider line instead of a clickable item; all
// other fields are ignored.
Separator bool
// OnClick is called on the UI thread when the item is chosen.
OnClick func()
}
Item is one entry in the tray menu.