Support traymenu update

This commit is contained in:
Lea Anthony 2022-10-06 08:21:30 +11:00
commit abe83f2271
No known key found for this signature in database
GPG key ID: 33DAF7BB90A58405
7 changed files with 57 additions and 14 deletions

View file

@ -17,6 +17,7 @@ type SysTray interface {
Close()
SetMenu(menu *menu.Menu) error
SetIcons(lightModeIcon, darkModeIcon *options.SystemTrayIcon) error
Update()
}
func NewSysTray() SysTray {

View file

@ -10,6 +10,7 @@ type PopupMenu struct {
menu win32.HMENU
parent win32.HWND
menuMapping map[int]*menu.MenuItem
menuData *menu.Menu
}
func buildMenu(parentMenu win32.HMENU, inputMenu *menu.Menu) (map[int]*menu.MenuItem, error) {
@ -40,17 +41,20 @@ func buildMenu(parentMenu win32.HMENU, inputMenu *menu.Menu) (map[int]*menu.Menu
return menuMapping, nil
}
func (p *PopupMenu) Update() error {
var err error
p.menu = win32.CreatePopupMenu()
p.menuMapping, err = buildMenu(p.menu, p.menuData)
return err
}
func NewPopupMenu(parent win32.HWND, inputMenu *menu.Menu) (*PopupMenu, error) {
popupMenu := win32.CreatePopupMenu()
mappings, err := buildMenu(popupMenu, inputMenu)
if err != nil {
return nil, err
result := &PopupMenu{
parent: parent,
menuData: inputMenu,
}
return &PopupMenu{
parent: parent,
menu: popupMenu,
menuMapping: mappings,
}, nil
err := result.Update()
return result, err
}
func (p *PopupMenu) ShowAtCursor() error {
@ -80,3 +84,7 @@ func (p *PopupMenu) ProcessCommand(cmdMsgID int) {
item.Click(&menu.CallbackData{MenuItem: item})
}
}
func (p *PopupMenu) Destroy() {
win32.DestroyMenu(p.menu)
}

View file

@ -49,6 +49,16 @@ func (p *Systray) Close() {
}
}
func (p *Systray) Update() {
// Delete old menu
if p.menu != nil {
p.menu.Destroy()
}
p.menu.Update()
}
// SetTitle is unused on Windows
func (p *Systray) SetTitle(_ string) {}

View file

@ -19,6 +19,7 @@ var (
procSetForegroundWindow = moduser32.NewProc("SetForegroundWindow")
procCreatePopupMenu = moduser32.NewProc("CreatePopupMenu")
procTrackPopupMenu = moduser32.NewProc("TrackPopupMenu")
procDestroyMenu = moduser32.NewProc("DestroyMenu")
procAppendMenuW = moduser32.NewProc("AppendMenuW")
procCreateIconFromResourceEx = moduser32.NewProc("CreateIconFromResourceEx")
procGetMessageW = moduser32.NewProc("GetMessageW")

View file

@ -5,6 +5,11 @@ func CreatePopupMenu() HMENU {
return HMENU(ret)
}
func DestroyMenu(menu HMENU) bool {
ret, _, _ := procDestroyMenu.Call(uintptr(menu))
return ret != 0
}
func TrackPopupMenu(menu HMENU, flags uint, x, y int, wnd HWND) bool {
ret, _, _ := procTrackPopupMenu.Call(
uintptr(menu),

View file

@ -111,6 +111,5 @@ func (a *Application) On(eventType EventType, callback func()) {
func (a *Application) NewSystemTray(options *options.SystemTray) *SystemTray {
systemTray := newSystemTray(options)
a.systemTrays = append(a.systemTrays, systemTray)
println("created systemTray")
return systemTray
}

View file

@ -22,7 +22,6 @@ type SystemTray struct {
func newSystemTray(options *options.SystemTray) *SystemTray {
return &SystemTray{
impl: platform.NewSysTray(),
title: options.Title,
lightModeIcon: options.LightModeIcon,
darkModeIcon: options.DarkModeIcon,
@ -33,6 +32,7 @@ func newSystemTray(options *options.SystemTray) *SystemTray {
}
func (t *SystemTray) run() {
t.impl = platform.NewSysTray()
t.impl.SetTitle(t.title)
t.impl.SetIcons(t.lightModeIcon, t.darkModeIcon)
t.impl.SetTooltip(t.tooltip)
@ -44,8 +44,11 @@ func (t *SystemTray) run() {
}
func (t *SystemTray) SetTitle(title string) {
t.title = title
t.impl.SetTitle(title)
if t.impl != nil {
t.impl.SetTitle(title)
} else {
t.title = title
}
}
func (t *SystemTray) Run() error {
@ -54,5 +57,21 @@ func (t *SystemTray) Run() error {
}
func (t *SystemTray) Close() {
t.impl.Close()
if t.impl != nil {
t.impl.Close()
}
}
func (t *SystemTray) SetMenu(items *menu.Menu) {
if t.impl != nil {
t.impl.SetMenu(t.menu)
} else {
t.menu = items
}
}
func (t *SystemTray) Update() {
if t.impl != nil {
t.impl.Update()
}
}