diff --git a/example/main.go b/example/main.go index c67983b..dd793e4 100644 --- a/example/main.go +++ b/example/main.go @@ -61,12 +61,12 @@ func onReady() { shown := true toggle := func() { if shown { - subMenuBottom.Uncheck() + subMenuBottom.Check() mQuitOrig.Hide() mEnabled.Hide() shown = false } else { - subMenuBottom.Check() + subMenuBottom.Uncheck() mQuitOrig.Show() mEnabled.Show() shown = true diff --git a/systray.h b/systray.h index 4bf1dac..c064e4a 100644 --- a/systray.h +++ b/systray.h @@ -11,8 +11,7 @@ void setTitle(char* title); void setTooltip(char* tooltip); void configureAppWindow(char* title, int width, int height); void showAppWindow(char* url); -void add_or_update_menu_item(int menuId, char* title, char* tooltip, short disabled, short checked); -void add_or_update_submenu_item(int parent,int menuId, char* title, char* tooltip, short disabled, short checked); +void add_or_update_menu_item(int menuId, int parentMenuId, char* title, char* tooltip, short disabled, short checked); void add_separator(int menuId); void hide_menu_item(int menuId); void show_menu_item(int menuId); diff --git a/systray_darwin.m b/systray_darwin.m index dd5fd38..cf15282 100644 --- a/systray_darwin.m +++ b/systray_darwin.m @@ -17,12 +17,14 @@ { @public NSNumber* menuId; + NSNumber* parentMenuId; NSString* title; NSString* tooltip; short disabled; short checked; } -(id) initWithId: (int)theMenuId +withParentMenuId: (int)theParentMenuId withTitle: (const char*)theTitle withTooltip: (const char*)theTooltip withDisabled: (short)theDisabled @@ -30,12 +32,14 @@ @end @implementation MenuItem -(id) initWithId: (int)theMenuId + withParentMenuId: (int)theParentMenuId withTitle: (const char*)theTitle withTooltip: (const char*)theTooltip withDisabled: (short)theDisabled withChecked: (short)theChecked { menuId = [NSNumber numberWithInt:theMenuId]; + parentMenuId = [NSNumber numberWithInt:theParentMenuId]; title = [[NSString alloc] initWithCString:theTitle encoding:NSUTF8StringEncoding]; tooltip = [[NSString alloc] initWithCString:theTooltip @@ -48,7 +52,6 @@ @interface AppDelegate: NSObject - (void) add_or_update_menu_item:(MenuItem*) item; - - (void) add_or_update_submenu_item:(NSArray*) imageAndMenuId; - (IBAction)menuHandler:(id)sender; @property (assign) IBOutlet NSWindow *window; @end @@ -113,22 +116,31 @@ NSNumber* menuId = [sender representedObject]; systray_menu_item_selected(menuId.intValue); } + - (void)add_or_update_menu_item:(MenuItem *)item { + NSMenu *theMenu = self->menu; + NSMenuItem *parentItem; + if ([item->parentMenuId integerValue] > 0) { + parentItem = find_menu_item(menu, item->parentMenuId); + if (parentItem.hasSubmenu) { + theMenu = parentItem.submenu; + } else { + theMenu = [[NSMenu alloc] init]; + [parentItem setSubmenu:theMenu]; + } + } + NSMenuItem *menuItem; - menuItem = find_menu_with_parent(menu, item->menuId); + menuItem = find_menu_item(theMenu, item->menuId); if (menuItem == NULL) { - menuItem = [menu addItemWithTitle:item->title + menuItem = [theMenu addItemWithTitle:item->title action:@selector(menuHandler:) keyEquivalent:@""]; - [menuItem setTarget:self]; [menuItem setRepresentedObject:item->menuId]; - [menuItem setTag:[item->menuId integerValue]]; - - } else { - [menuItem setTitle:item->title]; - [menuItem setTag:[item->menuId integerValue]]; - [menuItem setTarget:self]; } + [menuItem setTitle:item->title]; + [menuItem setTag:[item->menuId integerValue]]; + [menuItem setTarget:self]; [menuItem setToolTip:item->tooltip]; if (item->disabled == 1) { menuItem.enabled = FALSE; @@ -142,94 +154,26 @@ } } -NSMenuItem *find_menu_with_parent(NSMenu *ourMenu, NSNumber *parent) { - NSMenuItem *foundItem = [ourMenu itemWithTag:[parent integerValue]]; - if (foundItem == NULL) { - NSArray *menu_items = ourMenu.itemArray; - int i; - for (i = 0; i < [menu_items count]; i++) { - NSMenuItem *i_item = [menu_items objectAtIndex:i]; - if (i_item.hasSubmenu) { - NSMenuItem *foundItem2 = find_menu_with_parent(i_item.submenu, parent); - if (foundItem2 == NULL) { - - } else { - foundItem = foundItem2; - break; - } +NSMenuItem *find_menu_item(NSMenu *ourMenu, NSNumber *menuId) { + NSMenuItem *foundItem = [ourMenu itemWithTag:[menuId integerValue]]; + if (foundItem != NULL) { + return foundItem; + } + NSArray *menu_items = ourMenu.itemArray; + int i; + for (i = 0; i < [menu_items count]; i++) { + NSMenuItem *i_item = [menu_items objectAtIndex:i]; + if (i_item.hasSubmenu) { + foundItem = find_menu_item(i_item.submenu, menuId); + if (foundItem != NULL) { + return foundItem; } } - return foundItem; - } else { - return foundItem; } + + return NULL; }; -- (void)add_or_update_submenu_item:(NSArray *)imageAndMenuId { - - NSNumber *parent = [imageAndMenuId objectAtIndex:0]; - MenuItem *newItem = [imageAndMenuId objectAtIndex:1]; - - NSMenuItem *foundItem = find_menu_with_parent(menu, parent); - - if (foundItem == NULL) { - NSLog(@"%s", ">>> foundItem == NULL - this should not occur!"); - } - - if (foundItem.hasSubmenu) { - NSMenu *oldMenu = foundItem.submenu; - - NSMenuItem *tempItem = [oldMenu addItemWithTitle:newItem->title - action:@selector(menuHandler:) - keyEquivalent:@""]; - tempItem.tag = [newItem->menuId integerValue]; - tempItem.title = newItem->title; - tempItem.action = @selector(menuHandler:); - tempItem.target = self; - tempItem.representedObject = newItem->menuId; - - //[oldMenu addItem:tempItem]; - if (newItem->disabled == 1) { - tempItem.enabled = FALSE; - } else { - tempItem.enabled = TRUE; - } - if (newItem->checked == 1) { - tempItem.state = NSControlStateValueOn; - } else { - tempItem.state = NSControlStateValueOff; - } - [foundItem setSubmenu:oldMenu]; - } else { - - NSMenu *newMenu = [[NSMenu alloc] init]; - NSMenuItem *tempItem = [newMenu addItemWithTitle:newItem->title - action:@selector(menuHandler:) - keyEquivalent:@""]; - - tempItem.tag = [newItem->menuId integerValue]; - tempItem.title = newItem->title; - tempItem.toolTip = newItem->tooltip; - tempItem.representedObject = newItem->menuId; - - [tempItem setTarget:self]; - - if (newItem->disabled == 1) { - tempItem.enabled = FALSE; - } else { - tempItem.enabled = TRUE; - } - if (newItem->checked == 1) { - tempItem.state = NSControlStateValueOn; - } else { - tempItem.state = NSControlStateValueOff; - } - [foundItem setSubmenu:newMenu]; - } -} - - - - (void) add_separator:(NSNumber*) menuId { [menu addItem: [NSMenuItem separatorItem]]; @@ -246,12 +190,12 @@ NSMenuItem *find_menu_with_parent(NSMenu *ourMenu, NSNumber *parent) { [menuItem setHidden:TRUE]; } -- (void)setMenuItemIcon:(NSArray*)imageAndMenuId { +- (void) setMenuItemIcon:(NSArray*)imageAndMenuId { NSImage* image = [imageAndMenuId objectAtIndex:0]; NSNumber* menuId = [imageAndMenuId objectAtIndex:1]; NSMenuItem* menuItem; - menuItem = find_menu_with_parent(menu, menuId); + menuItem = find_menu_item(menu, menuId); if (menuItem == NULL) { return; } @@ -324,26 +268,13 @@ void setTooltip(char* ctooltip) { runInMainThread(@selector(setTooltip:), (id)tooltip); } -void add_or_update_menu_item(int menuId, char* title, char* tooltip, short disabled, short checked) { - MenuItem* item = [[MenuItem alloc] initWithId: menuId withTitle: title withTooltip: tooltip withDisabled: disabled withChecked: checked]; +void add_or_update_menu_item(int menuId, int parentMenuId, char* title, char* tooltip, short disabled, short checked) { + MenuItem* item = [[MenuItem alloc] initWithId: menuId withParentMenuId: parentMenuId withTitle: title withTooltip: tooltip withDisabled: disabled withChecked: checked]; free(title); free(tooltip); runInMainThread(@selector(add_or_update_menu_item:), (id)item); } -void add_or_update_submenu_item(int parent, int menuId, char *title, - char *tooltip, short disabled, short checked) { - MenuItem *item = [[MenuItem alloc] initWithId:menuId - withTitle:title - withTooltip:tooltip - withDisabled:disabled - withChecked:checked]; - free(title); - free(tooltip); - NSNumber *parent2 = [NSNumber numberWithInt:parent]; - runInMainThread(@selector(add_or_update_submenu_item:), - @[ parent2, (id)item ]); -} void add_separator(int menuId) { NSNumber *mId = [NSNumber numberWithInt:menuId]; runInMainThread(@selector(add_separator:), (id)mId); diff --git a/systray_linux.c b/systray_linux.c index 025f7d8..50ff62f 100644 --- a/systray_linux.c +++ b/systray_linux.c @@ -189,7 +189,8 @@ void setTooltip(char* ctooltip) { void setMenuItemIcon(const char* iconBytes, int length, int menuId, bool template) { } -void add_or_update_menu_item(int menu_id, char* title, char* tooltip, short disabled, short checked) { +void add_or_update_menu_item(int menu_id, int parent_menu_id, char* title, char* tooltip, short disabled, short checked) { + // TODO: add support for sub-menus MenuItemInfo *mii = malloc(sizeof(MenuItemInfo)); mii->menu_id = menu_id; mii->title = title; @@ -199,11 +200,6 @@ void add_or_update_menu_item(int menu_id, char* title, char* tooltip, short disa g_idle_add(do_add_or_update_menu_item, mii); } -void add_or_update_submenu_item(int parent_id, int menu_id, char* title, char* tooltip, short disabled, short checked) { - // TODO: add support for sub-menus - add_or_update_menu_item(int menu_id, char* title, char* tooltip, short disabled, short checked) ; -} - void add_separator(int menu_id) { MenuItemInfo *mii = malloc(sizeof(MenuItemInfo)); mii->menu_id = menu_id; diff --git a/systray_nonwindows.go b/systray_nonwindows.go index 9e90e6b..b564879 100644 --- a/systray_nonwindows.go +++ b/systray_nonwindows.go @@ -58,24 +58,18 @@ func addOrUpdateMenuItem(item *MenuItem) { if item.checked { checked = 1 } - if item.parent == nil { - C.add_or_update_menu_item( - C.int(item.id), - C.CString(item.title), - C.CString(item.tooltip), - disabled, - checked, - ) - } else { - C.add_or_update_submenu_item( - C.int(item.parent.id), - C.int(item.id), - C.CString(item.title), - C.CString(item.tooltip), - disabled, - checked, - ) + var parentID int32 = 0 + if item.parent != nil { + parentID = item.parent.id } + C.add_or_update_menu_item( + C.int(item.id), + C.int(parentID), + C.CString(item.title), + C.CString(item.tooltip), + disabled, + checked, + ) } func addSeparator(id int32) {