0
votes

I created a custom view class because I wanted to have a status item you could drag items to.

Here's the definition of the view:

@interface DragStatusView : NSImageView <NSMenuDelegate>{
    BOOL highlight;
}
@end

In my ApplicationDelegate.m I instantiate a NSStatusItem, and an instance of my DragStatusView. I set the image on the DragStatusView, and also set its menu to an instance of NSMenu containing a few NSMenuItems.

- (void)applicationDidFinishLaunching:(NSNotification *)notification
{
    // Install icon into the menu bar
    statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];

    NSImage *statusImage = [NSImage imageNamed:@"Status"];
    [statusItem setImage:statusImage];




    [menuItem setTitle:NSLocalizedString(@"Special Status", @"imgur menu item text")];
    CGFloat itemHeight = [[NSStatusBar systemStatusBar] thickness];
    NSRect itemRect = NSMakeRect(0.0, 0.0, NSSquareStatusItemLength, itemHeight);

    DragStatusView* dragView = [[DragStatusView alloc] initWithFrame:itemRect];
    [dragView retain];

    [dragView setImage:statusImage]; 
    [dragView setMenu:menu];

    [statusItem setHighlightMode:YES];
    [statusItem setView:dragView];
}

Here is the method in the DragStatusView controller that triggers the menu to pop up:

- (void)mouseDown:(NSEvent *)event {
    [[[NSApp delegate] statusItem] popUpStatusItemMenu:[self menu]]; // or another method that returns a menu
}

This mostly works, however the menu appears too high when you click on the status item.

How it looks before clicking: http://imgur.com/fpJcd,quS3c#1

How it looks after clicking: http://imgur.com/fpJcd,quS3c#0 (the menu appears at the top of the screen -- ahh!)

How can I make the menu appear at the bottom of the status bar?

Thanks!

2

2 Answers

1
votes

How are you popping up the menu? What you want to do is something like this:

- (void)mouseDown:(NSEvent *)event {
    [statusItem popUpStatusItemMenu:[self menu]];
}

Of course, your view will need a reference to the status item, then.

1
votes

Finally got it working.

Code can be seen here:

https://github.com/zbuc/imgurBar/blob/master/imgur/ApplicationDelegate.m https://github.com/zbuc/imgurBar/blob/master/imgur/StatusItemView.m

I'm not quite sure why this code works though, which makes me uncomfortable. It does things with the bounding rects slightly differently but I'm not seeing what exactly makes this work now.