objective-c – NSStatusItem右键菜单

我正在开发一个左右键单击的状态栏应用程序.我通过关注其他帖子的提示开始了这项工作,但我不确定如何在右键单击显示菜单.

我使用子类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在左键单击时的操作方式相同?

解决方法

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

相关文章

我正在用TitaniumDeveloper编写一个应用程序,它允许我使用Ja...
我的问题是当我尝试从UIWebView中调用我的AngularJS应用程序...
我想获取在我的Mac上运行的所有前台应用程序的应用程序图标....
我是一名PHP开发人员,我使用MVC模式和面向对象的代码.我真的...
OSX中的SetTimer在Windows中是否有任何等效功能?我正在使用...
我不确定引擎盖下到底发生了什么,但这是我的设置,示例代码和...