diff --git a/v2/internal/platform/systray/menu.go b/v2/internal/platform/systray/menu.go index f54c4bab2..1e15edbf5 100644 --- a/v2/internal/platform/systray/menu.go +++ b/v2/internal/platform/systray/menu.go @@ -7,14 +7,14 @@ import ( ) type PopupMenu struct { - menu win32.HMENU + menu win32.PopupMenu parent win32.HWND menuMapping map[int]*menu.MenuItem checkboxItems map[*menu.MenuItem][]int menuData *menu.Menu } -func (p *PopupMenu) buildMenu(parentMenu win32.HMENU, inputMenu *menu.Menu, startindex int) error { +func (p *PopupMenu) buildMenu(parentMenu win32.PopupMenu, inputMenu *menu.Menu, startindex int) error { for index, item := range inputMenu.Items { var ret bool itemID := index + startindex @@ -38,7 +38,7 @@ func (p *PopupMenu) buildMenu(parentMenu win32.HMENU, inputMenu *menu.Menu, star if err != nil { return err } - ret = win32.AppendMenu(parentMenu, uintptr(flags), uintptr(submenu), item.Label) + ret = parentMenu.Append(uintptr(flags), uintptr(submenu), item.Label) if ret == false { return errors.New("AppendMenu failed") } @@ -49,7 +49,7 @@ func (p *PopupMenu) buildMenu(parentMenu win32.HMENU, inputMenu *menu.Menu, star if item.IsCheckbox() { p.checkboxItems[item] = append(p.checkboxItems[item], itemID) } - ret = win32.AppendMenu(parentMenu, uintptr(flags), uintptr(itemID), item.Label) + ret = parentMenu.Append(uintptr(flags), uintptr(itemID), item.Label) if ret == false { return errors.New("AppendMenu failed") } @@ -83,7 +83,7 @@ func (p *PopupMenu) ShowAtCursor() error { return errors.New("SetForegroundWindow failed") } - if win32.TrackPopupMenu(p.menu, win32.TPM_LEFTALIGN, x, y-5, p.parent) == false { + if p.menu.Track(win32.TPM_LEFTALIGN, x, y-5, p.parent) == false { return errors.New("TrackPopupMenu failed") } @@ -99,12 +99,8 @@ func (p *PopupMenu) ProcessCommand(cmdMsgID int) { if item != nil { if item.Type == menu.CheckboxType { item.Checked = !item.Checked - var checkState uint = win32.MF_UNCHECKED - if item.Checked { - checkState = win32.MF_CHECKED - } for _, menuID := range p.checkboxItems[item] { - win32.CheckMenuItem(p.menu, int32(menuID), checkState) + p.menu.Check(uintptr(menuID), item.Checked) } // TODO: Check duplicate menu items } @@ -115,5 +111,5 @@ func (p *PopupMenu) ProcessCommand(cmdMsgID int) { } func (p *PopupMenu) Destroy() { - win32.DestroyMenu(p.menu) + p.menu.Destroy() } diff --git a/v2/internal/platform/win32/menu.go b/v2/internal/platform/win32/menu.go index 25a151dac..56fb97b01 100644 --- a/v2/internal/platform/win32/menu.go +++ b/v2/internal/platform/win32/menu.go @@ -1,18 +1,25 @@ package win32 -func CreatePopupMenu() HMENU { +type Menu HMENU +type PopupMenu Menu + +func CreatePopupMenu() PopupMenu { ret, _, _ := procCreatePopupMenu.Call(0, 0, 0, 0) - return HMENU(ret) + return PopupMenu(ret) } -func DestroyMenu(menu HMENU) bool { - ret, _, _ := procDestroyMenu.Call(uintptr(menu)) +func (m Menu) Destroy() bool { + ret, _, _ := procDestroyMenu.Call(uintptr(m)) return ret != 0 } -func TrackPopupMenu(menu HMENU, flags uint, x, y int, wnd HWND) bool { +func (p PopupMenu) Destroy() bool { + return Menu(p).Destroy() +} + +func (p PopupMenu) Track(flags uint, x, y int, wnd HWND) bool { ret, _, _ := procTrackPopupMenu.Call( - uintptr(menu), + uintptr(p), uintptr(flags), uintptr(x), uintptr(y), @@ -23,9 +30,13 @@ func TrackPopupMenu(menu HMENU, flags uint, x, y int, wnd HWND) bool { return ret != 0 } -func AppendMenu(menu HMENU, flags uintptr, id uintptr, text string) bool { +func (p PopupMenu) Append(flags uintptr, id uintptr, text string) bool { + return Menu(p).Append(flags, id, text) +} + +func (m Menu) Append(flags uintptr, id uintptr, text string) bool { ret, _, _ := procAppendMenuW.Call( - uintptr(menu), + uintptr(m), flags, id, MustStringToUTF16uintptr(text), @@ -33,10 +44,22 @@ func AppendMenu(menu HMENU, flags uintptr, id uintptr, text string) bool { return ret != 0 } -func CheckMenuItem(menu HMENU, id int32, flags uint) uint { +func (p PopupMenu) Check(id uintptr, checked bool) bool { + return Menu(p).Check(id, checked) +} + +func (m Menu) Check(id uintptr, check bool) bool { + var checkState uint = MF_UNCHECKED + if check { + checkState = MF_CHECKED + } + return CheckMenuItem(HMENU(m), id, checkState) != 0 +} + +func CheckMenuItem(menu HMENU, id uintptr, flags uint) uint { ret, _, _ := procCheckMenuItem.Call( uintptr(menu), - uintptr(id), + id, uintptr(flags), ) return uint(ret)