开发者

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, gener开发者_如何学Cally, 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?


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];


I tried: [[NSApp mainMenu] update] …

You're on the right track. This may be the one situation in which a parallel array in a Cocoa app is warranted.

  1. Keep a mutable array of menu items, parallel to your array of model objects that the menu items represent.
  2. When you receive numberOfItemsInMenu:, compare the number of model objects you have to the count of the menu-items array. If it's fewer, use the removeObjectsInRange: method to shorten the menu-items array. If it's more, pad the array out with NSNull objects. (You can't use nil here, since an NSArray can only contain objects, and nil is the absence of an object.)
  3. When you receive menu:updateItem:atIndex:shouldCancel:, replace the object in the array at that index with the new menu item before returning the new menu item.
  4. Conform to the NSMenuValidation protocol, as mentioned in the documentation for the update method. In your validation method, find the index of the menu item within the array, then get the model object at that index in the model objects array and update the menu item from it. If you're on Snow Leopard, you can send the menu item's menu a propertiesToUpdate message to determine what property values you need to confer from the model object.

The caveat is that this object, the delegate of the menu, must also be the target of the menu items. I'm assuming that it is. If it isn't, this will fail at step #4, as the validation messages are sent to the menu items' targets.

You may want to file an enhancement request asking for a better way.


In the delegate call for numberOfItemsInMenu: call removeAllItems... it's quite odd, but else the menu doesn't update, even it's calling for a delegate

- (NSInteger)numberOfItemsInMenu:(NSMenu*)menu {

    [menu removeAllItems];

    return [self.templateURLs count] + 2;

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜