title: Animated progress bar in NSMenuItem
body: <p>I want to put an animated progress bar in an <code>NSMenuItem</code> custom view. This is demonstrated in Apple's <em>MenuItemView</em> sample, but it doesn't animate (at least not in 10.5, and the sample is apparently from 10.4).</p>
  
  <p>I have tried setting a timer that calls <code>setNeedsDisplay:YES</code>, scheduled as <code>NSEventTrackingRunLoopMode</code> like the docs say. This works, but only for a determinate progress bar if I change the value, and only the first time the menu opens. The second and successive times, the bar redraws twice and then remains frozen. For an indeterminate progress bar, the barber pole stripes never animate.</p>
  
  <p><hr/>
  Edit: code snippet. I just added the <code>itemChanged</code> call, which didn't seem to have any effect. Updating the text-only item works fine.</p>
  
  <pre><code>class AppDelegate(NSObject):
    barItem = None
    menuProgressBar = None
    progressItem = None
  
    def applicationDidFinishLaunching_(self, sender):
      statusbar = NSStatusBar.systemStatusBar()
      self.statusitem = statusbar.statusItemWithLength_(
          NSSquareStatusItemLength)
      self.statusitem.setHighlightMode_(True)
      image = NSImage.imageNamed_("menubar.png")
      self.statusitem.setImage_(image)
      self.statusitem.retain()
  
      menu = NSMenu.alloc().init()
  
      AppDelegate.barItem = NSMenuItem.alloc(). \
          initWithTitle_action_keyEquivalent_('progress', None, '')
      itemView = NSView.alloc().initWithFrame_(NSMakeRect(0, 0, 50, 20))
      itemView.setAutoresizingMask_(NSViewWidthSizable)
      AppDelegate.menuProgressBar = \
          NSProgressIndicator.alloc().initWithFrame_(NSMakeRect(16, 5, 22, 10))
      AppDelegate.menuProgressBar.setAutoresizingMask_(NSViewWidthSizable)
      AppDelegate.menuProgressBar.setControlSize_(NSSmallControlSize)
      AppDelegate.menuProgressBar.setUsesThreadedAnimation_(True)
      itemView.addSubview_(AppDelegate.menuProgressBar)
      AppDelegate.menuProgressBar.setIndeterminate_(False)
      AppDelegate.menuProgressBar.setMaxValue_(100)
      AppDelegate.menuProgressBar.startAnimation_(self)
      timer = NSTimer.timerWithTimeInterval_target_selector_userInfo_repeats_(
          0.1, self,
          objc.selector(self.animateProgress, signature='v@:'),
          None, True)
      NSRunLoop.currentRunLoop().addTimer_forMode_(
          timer, NSEventTrackingRunLoopMode)
      AppDelegate.barItem.setView_(itemView)
      menu.addItem_(AppDelegate.barItem)
  
      AppDelegate.progressItem = NSMenuItem.alloc(). \
          initWithTitle_action_keyEquivalent_('Progress', None, '')
      menu.addItem_(AppDelegate.progressItem)
  
      self.statusitem.setMenu_(menu)
  
    def animateProgress(self):
      time = NSDate.timeIntervalSinceReferenceDate()
      AppDelegate.menuProgressBar.setDoubleValue_(time%100)
      AppDelegate.menuProgressBar.display()
      AppDelegate.progressItem.setTitle_('Progress: %d'%(time%100))
      AppDelegate.barItem.menu().itemChanged_(AppDelegate.barItem)
  </code></pre>