mirror of
https://github.com/wailsapp/wails.git
synced 2026-03-14 14:45:49 +01:00
Support traymenu update
This commit is contained in:
parent
68bdc7c5eb
commit
abe83f2271
7 changed files with 57 additions and 14 deletions
|
|
@ -17,6 +17,7 @@ type SysTray interface {
|
|||
Close()
|
||||
SetMenu(menu *menu.Menu) error
|
||||
SetIcons(lightModeIcon, darkModeIcon *options.SystemTrayIcon) error
|
||||
Update()
|
||||
}
|
||||
|
||||
func NewSysTray() SysTray {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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) {}
|
||||
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue