Search code examples
objective-ccocoansviewnsmenuitemnsprogressindicator

Using NSProgressIndicator inside an NSMenuItem


I'm trying to use a NSProgressIndicator (indeterminate) inside of a statusbar-menu. I'm using an NSView-object as view for the menuitem, and then subviews the progress indicator to display it. But whenever i try to call the startAnimation: for the progress, nothing happens. When i try do the very same thing on a normal NSWindow it works perfectly, just not when inside a menuitem.

I'm new to both cocoa and objective-c so I might've overlooked something "obvious" but I've searched quite a bit for a workaround but without success. I found something about menuitems cant be updated while shown and that you need to use a bordeless window instead. But I have not been able to confirm this in any documentation.

Edit:

Ok, almost works now. When using the setUsesThreadedAnimation: and from a MenuDelegate's menuWillOpen and creating a new thread. This thread runs a local method:

-(void) doWork(NSProgressIndicator*) p{
     [p startAnimation:self];
}

This will start the progressindicator on a random(?) basis when opening the menu. If I call startAnimation: directly without going through doWork: (still using a new thread), it never works. Doesn't setUsesThreadedAnimation: make the progress-bar create it's own thread for the animation?


Solution

  • Solved it by using:

    [progressIndicator performSelector:@selector(startAnimation:)
                                withObject:self
                                afterDelay:0.0
                                   inModes:[NSArray 
                           arrayWithObject:NSEventTrackingRunLoopMode]];
    

    Inside the menuWillOpen:, the problem seems to have been calling startAnimation: before the progressbar was finished drawing itself.