Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update NSMenu while it's open?

I have a NSMenu with dynamically added NSMenuItems. The NSMenu is not refreshing properly while it's kept open. I am calling NSMenu update method in NSEventTrackingRunLoopModes.

I have implemented following methods to update NSMenu.

- (void)menuNeedsUpdate:(NSMenu *)menu {
for (NSInteger index = 0; index < count; index++)
    [self menu:menu updateItem:[menu itemAtIndex:index] 
                       atIndex:index 
                  shouldCancel:NO];
}

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

- (NSInteger)numberOfItemsInMenu:(NSMenu *)menu
like image 730
Vinpai Avatar asked Apr 13 '12 07:04

Vinpai


2 Answers

Updating the menu items in NSEventTrackingRunLoopMode solved this issue.

like image 102
Vinpai Avatar answered Nov 07 '22 22:11

Vinpai


I am dynamically populating menu items in a timer and NSMenu is not updating while it's open.

Make sure to have the timer fire on the respective run mode:

[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

You might only have it fire on NSDefaultRunLoopMode right now.

like image 5
soryu2 Avatar answered Nov 07 '22 23:11

soryu2