NSStatusItem右键菜单

6

我在开发一个状态栏应用,有左键和右键点击功能。我已经通过其他帖子中的提示开始了这个工作,但我不知道如何在右键单击时显示菜单。

我使用一个子类化的NSView作为我的NSStatusItem的自定义视图,并且左右点击执行不同的函数:

- (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];
}

我该如何在右键单击时显示菜单,就像标准的NSStatusItem在左键单击时所做的那样?

可能是Cocoa:右键单击NSStatusItem的重复问题。 - BB9z
4个回答

22

NSStatusItem popUpStatusItemMenu:这个方法解决了我的问题。我从右键动作中调用它,并传入要显示的菜单,它就会显示出来!这不是我本来期望这个函数会做什么,但它起作用了。

以下是我代码的重要部分:

- (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];
}

这是自定义视图的实现代码:

@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

2
你的回答非常好,解决了我的完全不相关的问题 :) 谢谢。 如果 NSStatusItem 设置了菜单,则不会在目标上调用操作。因此,我能够不设置 NSMenu,然后使用您的 popUpStatusItemMenu 方法来显示菜单 :) - Mazyod

3

一种选项是直接模拟左键按下:

- (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];
}

非常棘手,但我认为这对我不起作用,因为我的左键运行一个操作而不显示菜单。 - keegan3d
是的,我对你的代码和描述有点困惑——你现在是如何显示菜单的? - jscs
抱歉造成困惑,经过更多的挖掘和实验,我已经解决了问题。感谢你的代码片段,我相信它将在未来某一天派上用场 :) - keegan3d
请稍后回来(我认为系统会让您等待几个小时),并发布您的解决方案,以供未来读者参考! - jscs
谢谢提醒。我忘记了你可以回答自己的问题,现在我已经回答了。 - keegan3d

0

在你的视图中需要标题时,添加一些小东西

- (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];

}

你是不是想回答另一个问题?这个问题是关于在右键单击状态栏项时显示弹出菜单的。 - Peter Hosey
也许应该作为注释(深夜编程)。这是对已接受答案的小补充。希望它能在某一天帮助到某个人。 - Szymon Kuczur

0
- (void)statusItemAction {
    NSEvent *event = NSApp.currentEvent;
    
    if (event.type == NSEventTypeRightMouseDown || (event.modifierFlags & NSEventModifierFlagControl)) {
        [self toggleMenu];
    } else {
        [self togglePopOver];
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接