Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keep track of hidden items #74

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 41 additions & 17 deletions systray_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"sort"
"syscall"
"unsafe"

Expand Down Expand Up @@ -172,6 +173,8 @@ type winTray struct {

wmSystrayMessage,
wmTaskbarCreated uint32

visibleItems []uint32
}

// Loads an image from file and shows it in tray.
Expand Down Expand Up @@ -462,29 +465,25 @@ func (t *winTray) addOrUpdateMenuItem(menuId int32, title string, disabled, chec
}
mi.Size = uint32(unsafe.Sizeof(mi))

// 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 || int32(res) != menuId {
// We set the menu item info based on the menuID
res, _, err := pSetMenuItemInfo.Call(
uintptr(t.menu),
uintptr(menuId),
0,
uintptr(unsafe.Pointer(&mi)),
)

if res == 0 {
t.addToVisibleItems(menuId)
position := t.getVisibleItemIndex(menuId)
res, _, err = pInsertMenuItem.Call(
uintptr(t.menu),
uintptr(menuId),
uintptr(position),
1,
uintptr(unsafe.Pointer(&mi)),
)
if res == 0 {
return err
}
} else {
res, _, err = pSetMenuItemInfo.Call(
uintptr(t.menu),
uintptr(menuId),
0,
uintptr(unsafe.Pointer(&mi)),
)
if res == 0 {
t.delFromVisibleItems(menuId)
return err
}
}
Expand Down Expand Up @@ -535,6 +534,7 @@ func (t *winTray) hideMenuItem(menuId int32) error {
if res == 0 && err.(syscall.Errno) != ERROR_SUCCESS {
return err
}
t.delFromVisibleItems(menuId)

return nil
}
Expand Down Expand Up @@ -567,6 +567,30 @@ func (t *winTray) showMenu() error {
return nil
}

func (t *winTray) delFromVisibleItems(val int32) {
for i, itemval := range t.visibleItems {
if uint32(val) == itemval {
t.visibleItems = append(t.visibleItems[:i], t.visibleItems[i+1:]...)
break
}
}
}

func (t *winTray) addToVisibleItems(val int32) {
newvisible := append(t.visibleItems, uint32(val))
sort.Slice(newvisible, func(i, j int) bool { return newvisible[i] < newvisible[j] })
t.visibleItems = newvisible
}

func (t *winTray) getVisibleItemIndex(val int32) int {
for i, itemval := range t.visibleItems {
if uint32(val) == itemval {
return i
}
}
return -1
}

func nativeLoop() {
if err := wt.initInstance(); err != nil {
log.Errorf("Unable to init instance: %v", err)
Expand Down