NSMenuItem 的视图(NSView 子类的实例)在悬停时不突出显示

发布于 2024-09-03 07:00:21 字数 255 浏览 5 评论 0原文

我需要使用自定义 NSView 子类来绘制一些内容,但当用户悬停时它不会突出显示,并且当用户悬停时它不会关闭 NSMenu点击它。有什么想法吗?

编辑

因此使用 -drawRect:[[self enleadingMenuItem] isHighlighted] 我能够判断是否需要将视图绘制为突出显示并有机会这样做。我需要弄清楚的是如何做到这一点。

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 dismiss 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独自唱情﹋歌 2024-09-10 07:00:21

也许你应该这样尝试:

#define menuItem ([self enclosingMenuItem])

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

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];
    }
}
两相知 2024-09-10 07:00:21

我不确定我是否理解你的问题。我认为您的意思是:菜单打开,所有绘图都停止绘制。我认为这是因为打开的 NSMenu 停止了 UI' NSRunLoop 的线程。两者之一。您应该尝试在其他线程中进行线程安全的绘图。

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.

心凉怎暖 2024-09-10 07:00:21

这对我有用。我使用 Stackview。但这也应该适用于视图。

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)
    } 
}
}

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)
    } 
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文