Search code examples
cocoaappkitnsmenu

Force NSMenu (nested submenu) update for Main Menu of Cocoa App


  1. I have some submenu inserted as Window item submenu of Main Menu
  2. I have an instance of my object (let's assume its class name is MenuController) inherited from NSObject and supports 2 from NSMenuDelegate methods: – numberOfItemsInMenu: – menu:updateItem:atIndex:shouldCancel:
  3. This instance added as Blue-Object into NIB for awaking at runtime
  4. Object from steps 2-3 configured as delegate for submenu (step 1)

Now, I can provide submenu content in runtime.

Next, I do following: I can add new items or remove old from an array (inside MenuController which contains menu titles) that mapped to real submenu via protocol and delegate. All works just fine. Except one thing: I like assign shortcuts to my dynamic menu items. CMD-1, CMD-2, CMD-3, etc

Window / MySubmenu / MyItem1 CMD-1, MyItem2 CMD-2, ...

So, for call some items I don't wanna go to Window / MySubmenu / MyItem to click it by mouse, I wanna press just one shortcut, like CMD-3 to call the item.

Ok, periodically it works as expected. But, generally, I have no way to inform Main Menu about my nested submenu changes, except open the Window / MySubmenu to reload its content. One stable way to reproduce the issue - just try to remove some item and press its old shortcut assigned to it, after you create new item as replace for deleted - bingo - shortcut wont work before you navigate to Window / MySubmenu to see current submenu content.

I don't know a way to force main menu to rebuild its submenus... I tried: [[NSApp mainMenu] update] and games with NSNotificationCenter for send NSMenuDidAddItemNotification, NSMenuDidRemoveItemNotification, NSMenuDidChangeItemNotification

I tried outlet to my submenu and call to update method explicitly - there is no way... Some times AppKit calls my delegate methods - and I see that, sometimes it doesn't want to call anything. Looks like a random strategy.

How can I make sure that after "some call" my submenu will be in actual state after internal array modifications?


Solution

  • To implement 1:1 mapping, implement in delegate these 3 methods:

    - (BOOL)menu:(NSMenu *)menu
    updateItem:(NSMenuItem *)item 
    atIndex:(NSInteger)index shouldCancel:(BOOL)shouldCancel
    

    and

    - (NSInteger)numberOfItemsInMenu:(NSMenu *)menu
    

    and

    - (void)menuNeedsUpdate:(NSMenu *)menu
    {
        if (!attachedMenu)
            attachedMenu = menu;
        if (!menu)
            menu = attachedMenu;
        NSInteger count = [self numberOfItemsInMenu:menu];
        while ([menu numberOfItems] < count)
            [menu insertItem:[[NSMenuItem new] autorelease] atIndex:0];
        while ([menu numberOfItems] > count)
            [menu removeItemAtIndex:0];
        for (NSInteger index = 0; index < count; index++)
            [self menu:menu updateItem:[menu itemAtIndex:index] atIndex:index shouldCancel:NO];
    }
    

    attachedMenu - is internal var of type NSMenu*

    Next, when you wanna force refresh the submenu, anytime - just call

    [self menuNeedsUpdate:nil];