Skip to content

Instantly share code, notes, and snippets.

@clayallsopp
Created July 2, 2010 21:48
Show Gist options
  • Save clayallsopp/461954 to your computer and use it in GitHub Desktop.
Save clayallsopp/461954 to your computer and use it in GitHub Desktop.
@implementation StatusItemController
@synthesize theMenu;
@synthesize statusItem;
- (id)init {
if ((self = [super init])) {
secondsPassed = 0;
NSStatusBar *bar = [NSStatusBar systemStatusBar];
statusItem = [bar statusItemWithLength:NSVariableStatusItemLength];
// Not sure if this retain is necessary...hmmm
[statusItem retain];
[statusItem setTitle:[NSString stringWithFormat:@"%i",secondsPassed]];
[statusItem setHighlightMode:YES];
// Create your menu and add buttons/dividers to it. Adding submenus also works.
theMenu = [[NSMenu alloc] init];
// Had to disable this to get it to work properly;
// there's probably a good reason why I couldn't get it working with it enabled.
[theMenu setAutoenablesItems:NO];
// It's always good form to include a "Quit" button
NSMenuItem *quitItem = [[NSMenuItem alloc] initWithTitle:@"Quit" action:@selector(quit:) keyEquivalent:@""];
[quitItem setTarget:self];
[quitItem setEnabled:YES];
[theMenu addItem:quitItem];
[quitItem release];
[statusItem setMenu:theMenu];
// And here is where you would start your background process...
// EXAMPLE:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 1.0
target: self
selector: @selector(updateLabel:)
userInfo: nil
repeats: YES];
[timer fire];
}
return self;
}
- (void)updateLabel:(id)sender {
// Change the status bar thing.
secondsPassed += 1;
[statusItem setTitle:[NSString stringWithFormat:@"%i",secondsPassed]];
}
- (void)dealloc {
[theMenu release];
[statusItem release];
[super dealloc];
}
- (void)quit:(id)sender {
[[NSApplication sharedApplication] terminate:self];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment