• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Updating NSMenu while it's open
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Updating NSMenu while it's open


  • Subject: Re: Updating NSMenu while it's open
  • From: Dave DeLong <email@hidden>
  • Date: Tue, 24 Apr 2012 16:51:20 -0700

On Apr 21, 2012, at 3:14 AM, Ken Thomases wrote:

> On Apr 20, 2012, at 1:32 PM, vinayak pai wrote:
>
>> Yes. I am facing the same problem. I am updating the menu in
>> performSelectorOnMainThread. If the menu is kept open without any
>> event then there is menu refreshing issue ie. sometimes some items
>> will be shown twice. When I move the mouse over items then it will
>> update. But, when the menu is open and mouse is still then  the menu
>> won't update. Is there a way to update the menu while it's open
>> without any mouse movement like System WiFi menu?
>
> I'm not sure.  If menus are shown twice, then I suspect a synchronization bug in your code.
>
> You can try posting an application-defined event (NSEvent of type NSApplicationDefined) to the event queue after you've changed the menus.

(Resending this, since it didn't appear to go through before)

Yes.  As I answered before, you have to get things scheduled in the right runloop mode.  But once you do, it's quite easy:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    NSStatusItem *item = [[[NSStatusBar systemStatusBar] statusItemWithLength:24] retain];
    [item setImage:[NSImage imageNamed:NSImageNameApplicationIcon]];
    [item setEnabled:YES];

    NSMenu *m = [[NSMenu alloc] init];
    [item setMenu:m];

    NSTimer *t = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateMenu:) userInfo:m repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:t forMode:NSDefaultRunLoopMode];
    [[NSRunLoop currentRunLoop] addTimer:t forMode:NSEventTrackingRunLoopMode];
}

- (void)updateMenu:(NSTimer *)t {
    NSMenu *m = [t userInfo];

    NSString *s = [[NSDate date] description];
    NSMenuItem *i = [[NSMenuItem alloc] initWithTitle:s action:nil keyEquivalent:@""];
    [m addItem:i];
    [m update];
}

Run that, open the poorly-iconed status item, and don't touch your mouse.

Yes, this code is terrible and leaks and all that.  It's just to illustrate the point.

Dave
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Updating NSMenu while it's open (From: vinayak pai <email@hidden>)
 >Re: Updating NSMenu while it's open (From: Ken Thomases <email@hidden>)
 >Re: Updating NSMenu while it's open (From: vinayak pai <email@hidden>)
 >Re: Updating NSMenu while it's open (From: Ken Thomases <email@hidden>)

  • Prev by Date: Re: Updating NSMenu while it's open
  • Next by Date: Re: addsubview
  • Previous by thread: Re: Updating NSMenu while it's open
  • Next by thread: Re: Updating NSMenu while it's open
  • Index(es):
    • Date
    • Thread