开发者

An NSMenuItem's view (instance of an NSView subclass) isn't highlighting on hover

I need to use a custom NSView subclass to draw some content, but it isn't drawing as highlighted when the user hovers and it doesn't dismi开发者_StackOverflowss the NSMenu when the user clicks on it. Any ideas?

Edit

So using -drawRect: and [[self enclosingMenuItem] isHighlighted] I'm able to tell whether or not I need to draw the view as highlighted and am given the chance to do so. All I have to figure out is how to do that.


Maybe you should try it this way:

#define menuItem ([self enclosingMenuItem])

- (void) drawRect: (NSRect) rect {
    BOOL isHighlighted = [menuItem isHighlighted];
    if (isHighlighted) {
        [[NSColor selectedMenuItemColor] set];
        [NSBezierPath fillRect:rect];
    } else {
        [super drawRect: rect];
    }
}


I'm not sure if I understood your question. I think you mean the following: The Menu opened and all your drawings stopped drawing. I think this is because the opened NSMenu stopps the UI' NSRunLoop its thread. One of both. You should try to do your drawing thread-safe in an other thread.


This worked for me. I use a Stackview. But this should work for views as well.

class MenuStackView: NSStackView {

override init(frame frameRect: NSRect) {
    super.init(frame: frameRect)
}

required init?(coder aDecoder: NSCoder) {
   super.init(coder: aDecoder)
}

override func draw(_ dirtyRect: NSRect) {
    super.draw(dirtyRect)
    let menuItem = self.enclosingMenuItem
    let isHighlighted = menuItem?.isHighlighted ?? false
    if isHighlighted {
        NSColor.selectedMenuItemColor.set()
        NSBezierPath.fill(dirtyRect)
    } 
}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜