From 5fb0feca3c0677e9fa31e579ff69631f49a379a2 Mon Sep 17 00:00:00 2001 From: Kali Kaneko Date: Thu, 18 Oct 2018 11:42:25 +0200 Subject: make addOrUpdateMenuItem insert item if it was deleted Currently, a menu item that was previously hidden (ie, deleted) will not return -1, and so when we're in the Show() code execution path we will never insert it in the previous position: attempts at Show() are failing because it tries to update a non-existing item. - Resolves: #67 --- systray_windows.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'systray_windows.go') diff --git a/systray_windows.go b/systray_windows.go index 7a9d7a1..3624214 100644 --- a/systray_windows.go +++ b/systray_windows.go @@ -463,8 +463,10 @@ func (t *winTray) addOrUpdateMenuItem(menuId int32, title string, disabled, chec // The return value is the identifier of the specified menu item. // If the menu item identifier is NULL or if the specified item opens a submenu, the return value is -1. + // If the given menu identifier is not found (becase we deleted the menu item when hiding it), + // the call will return the next integer that is available as an existing menu item. res, _, err := pGetMenuItemID.Call(uintptr(t.menu), uintptr(menuId)) - if int32(res) == -1 { + if int32(res) == -1 || int32(res) != menuId { res, _, err = pInsertMenuItem.Call( uintptr(t.menu), uintptr(menuId), -- cgit v1.2.3 From ac73430500283a19a3602cc33eb49a440e38554b Mon Sep 17 00:00:00 2001 From: Kali Kaneko Date: Thu, 18 Oct 2018 11:46:49 +0200 Subject: get rid of confusing error For some reason, at least in Windows 10, the return value is 0, but the error message states that the operation completed successfully. --- systray_windows.go | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'systray_windows.go') diff --git a/systray_windows.go b/systray_windows.go index 3624214..040cdbd 100644 --- a/systray_windows.go +++ b/systray_windows.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "os" "path/filepath" + "syscall" "unsafe" "golang.org/x/sys/windows" @@ -524,6 +525,7 @@ func (t *winTray) addSeparatorMenuItem(menuId int32) error { func (t *winTray) hideMenuItem(menuId int32) error { // https://msdn.microsoft.com/en-us/library/windows/desktop/ms647629(v=vs.85).aspx const MF_BYCOMMAND = 0x00000000 + const ERROR_SUCCESS = 0 res, _, err := pDeleteMenu.Call( uintptr(t.menu), @@ -531,6 +533,9 @@ func (t *winTray) hideMenuItem(menuId int32) error { MF_BYCOMMAND, ) if res == 0 { + if int(err.(syscall.Errno)) == ERROR_SUCCESS { + return nil + } return err } -- cgit v1.2.3