5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-04 05:50:54 +08:00
wails/v2/internal/runtime/tray.go
Lea Anthony f0c932713b
Support changing tray icon at runtime
Support both icon & text in tray
2020-12-24 10:05:55 +11:00

58 lines
1.3 KiB
Go

package runtime
import (
"github.com/wailsapp/wails/v2/internal/messagedispatcher/message"
"github.com/wailsapp/wails/v2/internal/servicebus"
"github.com/wailsapp/wails/v2/pkg/menu"
)
// Tray defines all Tray related operations
type Tray interface {
On(menuID string, callback func(*menu.MenuItem))
Update()
GetByID(menuID string) *menu.MenuItem
RemoveByID(id string) bool
SetLabel(label string)
SetIcon(name string)
}
type trayRuntime struct {
bus *servicebus.ServiceBus
trayMenu *menu.TrayOptions
}
// newTray creates a new Menu struct
func newTray(bus *servicebus.ServiceBus, menu *menu.TrayOptions) Tray {
return &trayRuntime{
bus: bus,
trayMenu: menu,
}
}
// On registers a listener for a particular event
func (t *trayRuntime) On(menuID string, callback func(*menu.MenuItem)) {
t.bus.Publish("tray:on", &message.TrayOnMessage{
MenuID: menuID,
Callback: callback,
})
}
func (t *trayRuntime) Update() {
t.bus.Publish("tray:update", t.trayMenu)
}
func (t *trayRuntime) SetLabel(label string) {
t.bus.Publish("tray:setlabel", label)
}
func (t *trayRuntime) SetIcon(name string) {
t.bus.Publish("tray:seticon", name)
}
func (t *trayRuntime) GetByID(menuID string) *menu.MenuItem {
return t.trayMenu.Menu.GetByID(menuID)
}
func (t *trayRuntime) RemoveByID(id string) bool {
return t.trayMenu.Menu.RemoveByID(id)
}