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 ErrUnsupported = errors.New("tray: not supported on this platform")
ErrUnsupported is returned by Run on a platform with no tray backend.
Functions ¶
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.
Items []Item
}
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.