简体   繁体   English

NSMenuItem中自定义视图上的NSTackingRect并不总是会触发mouseExited事件

[英]NSTackingRect on a custom View in a NSMenuItem does not always fire a mouseExited event

I have subclassed NSView and create an NSTrackingArea using the following: 我已经继承了NSView并使用以下代码创建了NSTrackingArea:

-(void)setUpTrackingArea
{
    if(trackingArea != nil)
    {
        [self removeTrackingArea:trackingArea];
    }

    int opts = (NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingEnabledDuringMouseDrag);
    trackingArea = [ [NSTrackingArea alloc] initWithRect:[self bounds] options:opts owner:self userInfo:nil];
    [self addTrackingArea:trackingArea];

    NSLog(@"update tracking area %@", trackingArea);

    NSPoint mouseLocation = [[self window] mouseLocationOutsideOfEventStream];
    mouseLocation = [self convertPoint: mouseLocation   fromView: nil];

    if (NSPointInRect(mouseLocation, [self bounds]))
    {
        [self mouseEntered: nil];
    }
    else
    {
        [self mouseExited: nil];
    }

}

I am also overriding: 我也要重写:

- (void)mouseEntered:(NSEvent *)theEvent
- (void)mouseExited:(NSEvent *)theEvent

to set a highlight property which then calls 设置一个高亮属性,然后调用

[self setNeedsDisplay:YES];

which calls drawrect to highlight the menu view as you would expect a menu to. 它调用drawrect突出显示菜单视图,就像您期望的菜单一样。

The problem is the mouse exited event does not always seem to fire leaving some custom views highlighted after the mouse has moved away. 问题在于鼠标退出事件并不总是会触发,而是在鼠标移开后留下了一些自定义视图。

Any ideas what I am doing wrong? 有什么想法我做错了吗?

I have created a demo project which presents this issue. 我创建了一个演示项目,介绍了这个问题。

see https://github.com/antokne/APGCustomMenuItemView 参见https://github.com/antokne/APGCustomMenuItemView

Thants. Thants。

I had the same problem some time ago; 我前段时间有同样的问题; the reason was that as soon as you have a tracking area with both the "enter/exit" AND "always" options being set, it stops working reliably. 原因是,只要您在跟踪区域中同时设置了“输入/退出”和“始终”选项,它就会可靠地停止工作。 My - admittedly very crude - solution was to create two tracking areas on top of each other like so: 我的-当然是很粗糙的-解决方案是在彼此之上创建两个跟踪区域,如下所示:

NSTrackingArea *mouseOverTracker = [[NSTrackingArea alloc] initWithRect:self.view.bounds options:(NSTrackingActiveAlways|NSTrackingMouseMoved) owner:self userInfo:nil];
NSTrackingArea *mouseOverTracker2 = [[NSTrackingArea alloc] initWithRect:self.view.bounds options:(NSTrackingMouseEnteredAndExited|NSTrackingActiveAlways) owner:self userInfo:nil];
[self.view addTrackingArea:mouseOverTracker];
[self.view addTrackingArea:mouseOverTracker2];

That worked for me. 那对我有用。

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM