Display NSMenuItem When Changed?

Discussion in 'Mac Programming' started by Darkroom, Dec 12, 2008.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    i've noticed in some menu bar apps like Google's GMail Notifier, the time since last email check will display when changed while the menu is visible. how can i emulate this behavior in my own NSMenu with a timer that changes?

    Code:
    [MainMenu_Item3 setNeedsDisplay:YES];
    
    doesn't work...
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    I tried to do this a while ago but didn't have much luck, and didn't look into it any further. I have a feeling it's new with Leopard since the AirPort menu does this too. You could try posting the question on cocoadev.com/forums
     
  3. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #3
    FWIW, for those who come across this in the future:

    The main way I found to do this is to use an NSTimer and add it to the run loop with the mode set to NSEventTrackingRunLoopMode. When that timer fires you can update the menu like usual. However, I ran into problems with code that was using the main run loop (e.g. NSURLConnection). It never ran when the menu was active, so instead I created a thread with its own run loop and ran the NSURLConnection there so it would use the thread's run loop. Making sure to rebuild the menu items on the main thread, everything works nicely.
     

Share This Page