6

I'm working on a status bar app that has a left and right click. I've got the start of this working by following the tips from other posts but I'm not sure how to go about showing a menu on right click.

I use a subclassed NSView as the custom view of my NSStatusItem and have the right and left clicks executing different functions:

- (void)mouseDown:(NSEvent *)theEvent{
    [super mouseDown:theEvent];
    if ([theEvent modifierFlags] & NSCommandKeyMask){
        [self.target performSelectorOnMainThread:self.rightAction withObject:nil waitUntilDone:NO];
    }else{
        [self.target performSelectorOnMainThread:self.action withObject:nil waitUntilDone:NO];
    }
}

- (void)rightMouseDown:(NSEvent *)theEvent{
    [super rightMouseDown:theEvent];
    [self.target performSelectorOnMainThread:self.rightAction withObject:nil waitUntilDone:NO];
}

How can I show a menu on right click, the same way the standard NSStatusItem does on left click?

keegan3d
  • 10,357
  • 9
  • 53
  • 77
  • Possible duplicate of [Cocoa: Right Click NSStatusItem](https://stackoverflow.com/questions/4565820/cocoa-right-click-nsstatusitem) – BB9z Jan 03 '19 at 13:13

4 Answers4

22

NSStatusItem popUpStatusItemMenu: did the trick. I am calling it from my right click action and passing in the menu I want to show and it's showing it! This is not what I would have expected this function to do, but it's working.

Here's the important parts of what my code looks like:

- (void)showMenu{
    // check if we are showing the highlighted state of the custom status item view
    if(self.statusItemView.clicked){
        // show the right click menu
        [self.statusItem popUpStatusItemMenu:self.rightClickMenu];
    }
}

// menu delegate method to unhighlight the custom status bar item view
- (void)menuDidClose:(NSMenu *)menu{ 
    [self.statusItemView setHighlightState:NO];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
    // setup custom view that implements mouseDown: and rightMouseDown:
    self.statusItemView = [[ISStatusItemView alloc] init];
    self.statusItemView.image = [NSImage imageNamed:@"menu.png"];
    self.statusItemView.alternateImage = [NSImage imageNamed:@"menu_alt.png"];    
    self.statusItemView.target = self;
    self.statusItemView.action = @selector(mainAction);
    self.statusItemView.rightAction = @selector(showMenu);

    // set menu delegate
    [self.rightClickMenu setDelegate:self];

    // use the custom view in the status bar item
    self.statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
    [self.statusItem setView:self.statusItemView];
}

Here is the implementation for the custom view:

@implementation ISStatusItemView

@synthesize image = _image;
@synthesize alternateImage = _alternateImage;
@synthesize clicked = _clicked;
@synthesize action = _action;
@synthesize rightAction = _rightAction;
@synthesize target = _target;

- (void)setHighlightState:(BOOL)state{
    if(self.clicked != state){
        self.clicked = state;
        [self setNeedsDisplay:YES];
    }
}

- (void)drawImage:(NSImage *)aImage centeredInRect:(NSRect)aRect{
    NSRect imageRect = NSMakeRect((CGFloat)round(aRect.size.width*0.5f-aImage.size.width*0.5f),
                                  (CGFloat)round(aRect.size.height*0.5f-aImage.size.height*0.5f),
                                  aImage.size.width, 
                                  aImage.size.height);
    [aImage drawInRect:imageRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0f];
}

- (void)drawRect:(NSRect)rect{
    if(self.clicked){
        [[NSColor selectedMenuItemColor] set];
        NSRectFill(rect);        
        if(self.alternateImage){
            [self drawImage:self.alternateImage centeredInRect:rect];
        }else if(self.image){
            [self drawImage:self.image centeredInRect:rect];
        }
    }else if(self.image){
        [self drawImage:self.image centeredInRect:rect];
    }
}

- (void)mouseDown:(NSEvent *)theEvent{
    [super mouseDown:theEvent];
    [self setHighlightState:!self.clicked];
    if ([theEvent modifierFlags] & NSCommandKeyMask){
        [self.target performSelectorOnMainThread:self.rightAction withObject:nil waitUntilDone:NO];
    }else{
        [self.target performSelectorOnMainThread:self.action withObject:nil waitUntilDone:NO];
    }
}

- (void)rightMouseDown:(NSEvent *)theEvent{
    [super rightMouseDown:theEvent];
    [self setHighlightState:!self.clicked];
    [self.target performSelectorOnMainThread:self.rightAction withObject:nil waitUntilDone:NO];
}

- (void)dealloc{
    self.target = nil;
    self.action = nil;
    self.rightAction = nil;
    [super dealloc];
}

@end
keegan3d
  • 10,357
  • 9
  • 53
  • 77
  • 2
    The great thing about your answer is that it solved my totally irrelevant question :) Thanks. If an NSStatusItem has a menu set, the action will not be invoked on the target. Thus, I was able to NOT set an NSMenu, then use your popUpStatusItemMenu method to show the menu :) – Mazyod Feb 11 '12 at 03:27
3

One option is to just fake the left mouse down:

- (void)rightMouseDown: (NSEvent *)event {
    NSEvent * newEvent;
    newEvent = [NSEvent mouseEventWithType:NSLeftMouseDown
                                  location:[event locationInWindow]
                             modifierFlags:[event modifierFlags]
                                 timestamp:CFAbsoluteTimeGetCurrent()
                              windowNumber:[event windowNumber]
                                   context:[event context]
                               eventNumber:[event eventNumber]
                                clickCount:[event clickCount]
                                  pressure:[event pressure]];
    [self mouseDown:newEvent];
}
jscs
  • 63,694
  • 13
  • 151
  • 195
  • Very tricksy, but I don't think this will work for me because my left click runs an action and doesn't show a menu. – keegan3d Jul 25 '11 at 07:46
  • Yes, I was a bit confused by your code and description -- how are you displaying the menu now? – jscs Jul 25 '11 at 07:47
  • Sorry for the confusion, I got it working after a lot more digging and experimenting. Thanks for your snippet, I'm sure that will come in useful one day :) – keegan3d Jul 25 '11 at 08:47
  • Please come back (I think the system makes you wait a few hours) and post your solution for future readers! – jscs Jul 25 '11 at 08:49
  • Thanks for reminding me. I forgot you can answer your own questions, which I've done now. – keegan3d Jul 25 '11 at 09:22
0

Added little something for when you need title in your view

- (void)drawRect:(NSRect)rect{
    if(self.clicked){
        [[NSColor selectedMenuItemColor] set];
        NSRectFill(rect);
        if(self.alternateImage){
            [self drawImage:self.alternateImage centeredInRect:rect];
        }else if(self.image){
            [self drawImage:self.image centeredInRect:rect];
        } else {
            [self drawTitleInRect:rect];
        }
    } else if(self.image){
        [self drawImage:self.image centeredInRect:rect];
    } else {
        [self drawTitleInRect:rect];
    }

}

-(void)drawTitleInRect:(CGRect)rect
{
    CGSize size = [_title sizeWithAttributes:nil];

    CGRect newRect = CGRectMake(MAX((rect.size.width - size.width)/2.f,0.f),
                                MAX((rect.size.height - size.height)/2.f,0.f),
                                size.width,
                                size.height);

    NSDictionary *attributes = @{NSForegroundColorAttributeName : self.clicked?[NSColor highlightColor]:[NSColor textColor]
                                 };
    [_title drawInRect:newRect withAttributes:attributes];

}
Szymon Kuczur
  • 5,783
  • 3
  • 16
  • 14
  • Did you mean to answer a different question? This question is about showing a pop-up menu when the status item is right-clicked. – Peter Hosey Feb 08 '14 at 07:10
  • Maybe it should be as a comment (late night coindg). It is a little addition to the accepted answer. Thought it may help someone, someday. – Szymon Kuczur Feb 10 '14 at 23:13
0
- (void)statusItemAction {
    NSEvent *event = NSApp.currentEvent;
    
    if (event.type == NSEventTypeRightMouseDown || (event.modifierFlags & NSEventModifierFlagControl)) {
        [self toggleMenu];
    } else {
        [self togglePopOver];
    }
}
0oneo
  • 695
  • 7
  • 10