Displaying NSProgressIndicatorSpinningStyle in NSStatusItem and then hiding it

1.1k views Asked by At

I'm working on NSStatusItem. I've managed to use the setImage and setAlternateImage to work. When user selects something, it takes a while for it to accomplish whatever it is doing. While it is doing something, I tried changing from the usual Image to a spinner. The way that I am doing it now is that I created a view, set the NSProgressIndicator to it, and then use

[statusItem setView: viewWithSpinner];

It seems to work until I try to remove it and display the original image. The only way I can hide it is to do

[statusItem setView: nil];

but that breaks everything, the original images don't come back. I guess cause there's no more view. I can't seem to save the original view before setting the viewWithSpinner.

Can someone advise me of a way of accomplishing this?

So...

NSStatusItem *myStatusItem;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[myStatusItem setImage:statusImage];
[myStatusItem setHighlightMode:YES];
[myStatusItem setAlternateImage:statusImageSel];
[myStatusItem setMenu:myStatusMenu];
etc...
[self createSpinner];
}

-(void)createSpinner
{
//to overcome the white border problem

NSView *progressIndicatorHolder = [[NSView alloc] init];

NSProgressIndicator *progressIndicator = [[NSProgressIndicator alloc] init];

[progressIndicator setBezeled: NO];
[progressIndicator setStyle: NSProgressIndicatorSpinningStyle];
[progressIndicator setControlSize: NSSmallControlSize];
[progressIndicator sizeToFit];
[progressIndicator setUsesThreadedAnimation:YES];

[progressIndicatorHolder addSubview:progressIndicator];

[progressIndicator startAnimation:self];

//for testing purposes
[[myStatusItem view] addSubview:progressIndicatorHolder];

spinnerView = progressIndicatorHolder;
}
2

There are 2 answers

0
James Boutcher On

If you're looking to just hide the NSStatusItem view, just call [yourStatusItem setLength:0].

0
Andy On

I suggest save old view using [statusItem view] before setting to any other view. Before coming back to normal menu, set old saved view to statusItem using setView method.