Skip to content

Commit

Permalink
Add support for removing a menu item
Browse files Browse the repository at this point in the history
  • Loading branch information
bdwyertech committed Dec 19, 2022
1 parent 3cfcb36 commit 79fa785
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 0 deletions.
5 changes: 5 additions & 0 deletions systray.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ func (item *MenuItem) Hide() {
hideMenuItem(item)
}

// Remove removes a menu item
func (item *MenuItem) Remove() {
removeMenuItem(item)
}

// Show shows a previously hidden menu item
func (item *MenuItem) Show() {
showMenuItem(item)
Expand Down
1 change: 1 addition & 0 deletions systray.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ void setTooltip(char* tooltip);
void add_or_update_menu_item(int menuId, int parentMenuId, char* title, char* tooltip, short disabled, short checked, short isCheckable);
void add_separator(int menuId);
void hide_menu_item(int menuId);
void remove_menu_item(int menuId);
void show_menu_item(int menuId);
void reset_menu();
void quit();
6 changes: 6 additions & 0 deletions systray_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ func showMenuItem(item *MenuItem) {
)
}

func removeMenuItem(item *MenuItem) {
C.remove_menu_item(
C.int(item.id),
)
}

func resetMenu() {
C.reset_menu()
}
Expand Down
13 changes: 13 additions & 0 deletions systray_darwin.m
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ - (void) show_menu_item:(NSNumber*) menuId
}
}

- (void) remove_menu_item:(NSNumber*) menuId
{
NSMenuItem* menuItem = find_menu_item(menu, menuId);
if (menuItem != NULL) {
[menuItem.menu removeItem:menuItem];
}
}

- (void) reset_menu
{
[self->menu removeAllItems];
Expand Down Expand Up @@ -312,6 +320,11 @@ void hide_menu_item(int menuId) {
runInMainThread(@selector(hide_menu_item:), (id)mId);
}

void remove_menu_item(int menuId) {
NSNumber *mId = [NSNumber numberWithInt:menuId];
runInMainThread(@selector(remove_menu_item:), (id)mId);
}

void show_menu_item(int menuId) {
NSNumber *mId = [NSNumber numberWithInt:menuId];
runInMainThread(@selector(show_menu_item:), (id)mId);
Expand Down
25 changes: 25 additions & 0 deletions systray_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var (
pCreatePopupMenu = u32.NewProc("CreatePopupMenu")
pCreateWindowEx = u32.NewProc("CreateWindowExW")
pDefWindowProc = u32.NewProc("DefWindowProcW")
pDeleteMenu = u32.NewProc("DeleteMenu")
pRemoveMenu = u32.NewProc("RemoveMenu")
pDestroyWindow = u32.NewProc("DestroyWindow")
pDispatchMessage = u32.NewProc("DispatchMessageW")
Expand Down Expand Up @@ -673,6 +674,30 @@ func (t *winTray) addSeparatorMenuItem(menuItemId, parentId uint32) error {
return nil
}

func (t *winTray) removeMenuItem(menuItemId, parentId uint32) error {
if !wt.isReady() {
return ErrTrayNotReadyYet
}

const MF_BYCOMMAND = 0x00000000
const ERROR_SUCCESS syscall.Errno = 0

t.muMenus.RLock()
menu := uintptr(t.menus[parentId])
t.muMenus.RUnlock()
res, _, err := pDeleteMenu.Call(
menu,
uintptr(menuItemId),
MF_BYCOMMAND,
)
if res == 0 && err.(syscall.Errno) != ERROR_SUCCESS {
return err
}
t.delFromVisibleItems(parentId, menuItemId)

return nil
}

func (t *winTray) hideMenuItem(menuItemId, parentId uint32) error {
if !wt.isReady() {
return ErrTrayNotReadyYet
Expand Down

0 comments on commit 79fa785

Please sign in to comment.