开发者

Animated progress bar in NSMenuItem

I want to put an animated progress bar in an NSMenuItem custom view. This is demonstrated in Apple's MenuItemView sample, but it doesn't animate (at least not in 10.5, and the sample is apparently from 10.4).

I have tried setting a timer that calls setNeedsDisplay:YES, scheduled as NSEventTrackingRunLoopMode 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.


Edit: code snippet. I just added the itemChanged call, which didn't seem to have any effect. Updating the text-only item works fine.

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_(
       开发者_C百科 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)


The timer approach should be completely unnecessary. Have you told the NSProgressIndicator to -setUsesThreadedAnimation:YES then told it to -startAnimation:?


For an indeterminate indicator I send the startAnimation: message in the menu's menuWillOpen: delegate method using performSelector:... to send it in the NSEventTrackingRunLoopMode mode.

- (void)menuWillOpen:(NSMenu *)menu
{
  [[progressIndicator performSelector:@selector(startAnimation:)
                           withObject:self
                           afterDelay:0.0
                              inModes:[NSArray arrayWithObject:NSEventTrackingRunLoopMode]];
}

For the determinate indicator I tried your code (but in objective-C) and it worked. I don't know python or PyObjC but looking at the code for the time variable I think you are sending the timeIntervalSinceReferenceDate() call to the NSDate class. So maybe time is always zero? Based on the alloc() calls I wonder if it shouldn't be ...

time = NSDate.date().timeIntervalSinceReferenceDate()


Update: for the record here is the code I used for testing the determinate progress indicator. Just obj-c version of the OP's code. The indicator updates properly.

- (void)menuWillOpen:(NSMenu *)menu
{
    NSTimer *timer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(animateProgress:) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSEventTrackingRunLoopMode];
}

- (void)animateProgress:(NSTimer *)timer
{
    NSTimeInterval time = [[NSDate date] timeIntervalSinceReferenceDate];
    [progressIndicator setDoubleValue:fmod(time, 100)];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜