5

My inspiration for this project is the Droplr and CloudApp mac menubar applications. I have been attempting to implement the code explained here.

However, when I implement the code, the menubar images disappear. Here is my code to create the status item:

- (id)init {
self = [super init];
if (self != nil) {
  // Install status item into the menu bar
  NSStatusItem *statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:STATUS_ITEM_VIEW_WIDTH];
  _statusItemView = [[StatusItemView alloc] initWithStatusItem:statusItem];
  _statusItemView.image = [NSImage imageNamed:@"Status"];
  _statusItemView.alternateImage = [NSImage imageNamed:@"StatusHighlighted"];
  _statusItemView.action = @selector(togglePanel:);
  StatusItemView* dragView = [[StatusItemView alloc] initWithFrame:NSMakeRect(0, 0, 24, 24)];
  [statusItem setView:dragView];
  [dragView release];
}
return self;
}

This is my view file:

#import "StatusItemView.h"
@implementation StatusItemView
@synthesize statusItem = _statusItem;
@synthesize image = _image;
@synthesize alternateImage = _alternateImage;
@synthesize isHighlighted = _isHighlighted;
@synthesize action = _action;
@synthesize target = _target;
#pragma mark -
- (id)initWithStatusItem:(NSStatusItem *)statusItem {
  CGFloat itemWidth = [statusItem length];
  CGFloat itemHeight = [[NSStatusBar systemStatusBar] thickness];
  NSRect itemRect = NSMakeRect(0.0, 0.0, itemWidth, itemHeight);
  self = [super initWithFrame:itemRect];
  if (self != nil) {
    _statusItem = statusItem;
    _statusItem.view = self;
  }
return self;
}

#pragma mark -
- (id)initWithFrame:(NSRect)frame {
  self = [super initWithFrame:frame];
  if (self) {
    //register for drags
    [self registerForDraggedTypes:[NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
  }
  return self;
}
//we want to copy the files
- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender {
  return NSDragOperationCopy;
}

perform the drag and log the files that are dropped
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender {
  NSPasteboard *pboard;
  NSDragOperation sourceDragMask;
  sourceDragMask = [sender draggingSourceOperationMask];
  pboard = [sender draggingPasteboard];
  if( [[pboard types] containsObject:NSFilenamesPboardType] ) {
    NSArray *files = [pboard propertyListForType:NSFilenamesPboardType];
    NSLog(@"Files: %@",files);
  }
  return YES;
}

#pragma mark -

- (void)drawRect:(NSRect)dirtyRect {
  [self.statusItem drawStatusBarBackgroundInRect:dirtyRect withHighlight:self.isHighlighted];
  NSImage *icon = self.isHighlighted ? self.alternateImage : self.image;
  NSSize iconSize = [icon size];
  NSRect bounds = self.bounds;
  CGFloat iconX = roundf((NSWidth(bounds) - iconSize.width) / 2);
  CGFloat iconY = roundf((NSHeight(bounds) - iconSize.height) / 2);
  NSPoint iconPoint = NSMakePoint(iconX, iconY);
  [icon compositeToPoint:iconPoint operation:NSCompositeSourceOver];
}

#pragma mark -
#pragma mark Mouse tracking

- (void)mouseDown:(NSEvent *)theEvent {
  [NSApp sendAction:self.action to:self.target from:self];
}

#pragma mark -
#pragma mark Accessors
- (void)setHighlighted:(BOOL)newFlag {
  if (_isHighlighted == newFlag) return;
  _isHighlighted = newFlag;
  [self setNeedsDisplay:YES];
}

#pragma mark -
- (void)setImage:(NSImage *)newImage {
  if (_image != newImage) {
    _image = newImage;
    [self setNeedsDisplay:YES];
  }
}

- (void)setAlternateImage:(NSImage *)newImage {
  if (_alternateImage != newImage) {
    _alternateImage = newImage;
    if (self.isHighlighted) {
      [self setNeedsDisplay:YES];
    }
  }
}

#pragma mark -
- (NSRect)globalRect {
  NSRect frame = [self frame];
  frame.origin = [self.window convertBaseToScreen:frame.origin];
  return frame;
}

@end

Thanks everyone!

Community
  • 1
  • 1

1 Answers1

0

I know this is an old question but perhaps this might help:

Try setting the NSStatusItem *statusItem as a @propery of the class. If you have ARC, the menubar might be getting destroyed right after the init function finishes.

dAngelov
  • 830
  • 6
  • 10