【问题标题】:NSStatusItem right click menuNSStatusItem 右键菜单
【发布时间】:2011-07-25 03:20:26
【问题描述】:

我正在开发一个具有左右单击功能的状态栏应用程序。我已经按照其他帖子的提示开始了这项工作,但我不确定如何在右键单击时显示菜单。

我使用一个子类 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 在左键单击时一样?

【问题讨论】:

标签: objective-c cocoa nsstatusitem


【解决方案1】:

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

【讨论】:

  • 你的回答最棒的是它解决了我完全不相关的问题:) 谢谢。如果 NSStatusItem 具有菜单集,则不会在目标上调用该操作。因此,我无法设置 NSMenu,然后使用您的 popUpStatusItemMenu 方法显示菜单:)
【解决方案2】:

一种选择是假装鼠标左键向下:

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

【讨论】:

  • 非常棘手,但我认为这对我不起作用,因为我的左键单击会运行一个动作并且不显示菜单。
  • 是的,我对你的代码和描述有点困惑——你现在如何显示菜单?
  • 抱歉给您带来了困惑,经过更多的挖掘和实验后,我终于可以正常工作了。感谢您的 sn-p,我相信有一天会派上用场的 :)
  • 请回来(我认为系统会让您等待几个小时)并将您的解决方案发布给未来的读者!
  • 感谢您的提醒。我忘了你可以回答你自己的问题,我现在已经完成了。
【解决方案3】:

在视图中需要标题时添加了一些东西

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

}

【讨论】:

  • 您的意思是回答不同的问题吗?这个问题是关于在状态项被右键单击时显示一个弹出菜单。
  • 也许它应该作为评论(深夜硬币)。这是对已接受答案的一点补充。认为有一天它可能会对某人有所帮助。
【解决方案4】:
- (void)statusItemAction {
    NSEvent *event = NSApp.currentEvent;
    
    if (event.type == NSEventTypeRightMouseDown || (event.modifierFlags & NSEventModifierFlagControl)) {
        [self toggleMenu];
    } else {
        [self togglePopOver];
    }
}

【讨论】:

    猜你喜欢
    • 2011-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多