Guest User

Untitled

a guest
May 22nd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. @implementation StatusItemController
  2. @synthesize theMenu;
  3. @synthesize statusItem;
  4.  
  5. - (id)init {
  6. if ((self = [super init])) {
  7. secondsPassed = 0;
  8.  
  9. NSStatusBar *bar = [NSStatusBar systemStatusBar];
  10. statusItem = [bar statusItemWithLength:NSVariableStatusItemLength];
  11. // Not sure if this retain is necessary...hmmm
  12. [statusItem retain];
  13. [statusItem setTitle:[NSString stringWithFormat:@"%i",secondsPassed]];
  14. [statusItem setHighlightMode:YES];
  15.  
  16. // Create your menu and add buttons/dividers to it. Adding submenus also works.
  17. theMenu = [[NSMenu alloc] init];
  18. // Had to disable this to get it to work properly;
  19. // there's probably a good reason why I couldn't get it working with it enabled.
  20. [theMenu setAutoenablesItems:NO];
  21.  
  22. // It's always good form to include a "Quit" button
  23. NSMenuItem *quitItem = [[NSMenuItem alloc] initWithTitle:@"Quit" action:@selector(quit:) keyEquivalent:@""];
  24. [quitItem setTarget:self];
  25. [quitItem setEnabled:YES];
  26. [theMenu addItem:quitItem];
  27. [quitItem release];
  28.  
  29. [statusItem setMenu:theMenu];
  30.  
  31. // And here is where you would start your background process...
  32. // EXAMPLE:
  33. NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 1.0
  34. target: self
  35. selector: @selector(updateLabel:)
  36. userInfo: nil
  37. repeats: YES];
  38. [timer fire];
  39. }
  40. return self;
  41. }
  42.  
  43. - (void)updateLabel:(id)sender {
  44. // Change the status bar thing.
  45. secondsPassed += 1;
  46. [statusItem setTitle:[NSString stringWithFormat:@"%i",secondsPassed]];
  47. }
  48.  
  49. - (void)dealloc {
  50. [theMenu release];
  51. [statusItem release];
  52. [super dealloc];
  53. }
  54.  
  55. - (void)quit:(id)sender {
  56. [[NSApplication sharedApplication] terminate:self];
  57. }
Add Comment
Please, Sign In to add comment