自定义NSStatusItem和NSView不可靠地接收NSTrackingEvents [英] Custom NSStatusItem and NSView not reliably receiving NSTrackingEvents

查看:247
本文介绍了自定义NSStatusItem和NSView不可靠地接收NSTrackingEvents的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个状态栏条目的应用程序,Iam试图显示一个面板mouseOver。我有自定义状态项(和相关联的视图)挂接和工作,但跟踪rect只是接收事件每打十几个发射。这使我相信有一个竞争条件发生在某个地方,但我找不到它。在我的自定义状态栏项目视图中:

I have a Status-Bar item only app that Iam trying to get to show a panel on mouseOver. I have the custom status item (and associated view) hooked up and working, but the tracking rect is only receiving events on every dozen or so launches. This leads me to believe there is a race condition happening somewhere, but I can't find it. In my custom status bar item view:

- (id)initWithStatusItem:(NSStatusItem *)statusItem {
    CGFloat itemWidth = [statusItem length];
    CGFloat itemHeight = [[NSStatusBar systemStatusBar] thickness];
    NSRect itemRect = NSMakeRect(0.0, 0.0, itemWidth, itemHeight);
    NSLog(@"itemRect: %@", NSStringFromRect(itemRect));

    if ((self = [super initWithFrame:itemRect])) {
        _statusItem = statusItem;
        _statusItem.view = self;

        NSTrackingAreaOptions options = NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveAlways;
        NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:itemRect
                                                                    options:options
                                                                      owner:self
                                                                   userInfo:nil];
        [self addTrackingArea:trackingArea];

        [self.window setIgnoresMouseEvents:NO];
        [self.window setAcceptsMouseMovedEvents:YES];

        self.wantsLayer = YES;
    }
    return self;
}

- (void)mouseEntered:(NSEvent *)theEvent {
    [[NSNotificationCenter defaultCenter] postNotificationName:UAStatusItemMouseEnteredNotification object:nil];
}

- (void)mouseExited:(NSEvent *)theEvent {
    [[NSNotificationCenter defaultCenter] postNotificationName:UAStatusItemMouseExitedNotification object:nil];
}

在大多数启动时,应用程序不响应跟踪鼠标事件,每隔一段时间, mouseEntered: mouseExited:方法被正确调用,这里发生了什么,我做错了什么?

On most launches, the app does not respond to the tracking mouse events, but every so often, the mouseEntered: and mouseExited: methods are being called properly, completely confusing me. What is going on here and what am I doing wrong?







EDIT 07/17/2012


我根据@ Streams的回答更改了代码,但发现同样的问题:




EDIT 07/17/2012
I altered the code based on @Streams's answer, but an seeing the same problem:

- (id)initWithStatusItem:(NSStatusItem *)statusItem {
    CGFloat itemWidth = [statusItem length];
    CGFloat itemHeight = [[NSStatusBar systemStatusBar] thickness];
    NSRect itemRect = NSMakeRect(0.0, 0.0, itemWidth, itemHeight);
    NSLog(@"itemRect: %@", NSStringFromRect(itemRect));

    if ((self = [super initWithFrame:itemRect])) {
        _statusItem = statusItem;
        _statusItem.view = self;            

        [self updateTrackingAreas];

        [self.window setIgnoresMouseEvents:NO];
        [self.window setAcceptsMouseMovedEvents:YES];

        self.wantsLayer = YES;
    }
    return self;
}

- (void)updateTrackingAreas {

    if (self.trackingArea)
        [self removeTrackingArea:self.trackingArea];

    [super updateTrackingAreas];

    self.trackingArea = [[NSTrackingArea alloc] initWithRect:CGRectZero
                                                     options:NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingInVisibleRect | NSTrackingActiveAlways
                                                       owner:self
                                                    userInfo:nil];
    [self addTrackingArea:self.trackingArea];
}







EDIT 07/18/2012


这里是一个准系统示例项目,它使用一个知名的github项目(由@Stream编写)来显示问题。




EDIT 07/18/2012
Here is a barebones sample project that uses a well know github project (written by @Stream) to show the problem. It cannot receive the mouseover events reliably, if at all.

推荐答案

我打开了一个DTS请求,让苹果看看这个。这是响应:

I opened a DTS request to have Apple take a look at this. Here is the response:


...你在启动应用程序时在Xcode中使用全屏。 I
不是这样做的,但我现在可以重现这个问题。从我可以
告诉它只有当你的应用程序是从全屏模式启动
Xcode。您的用户不会以这种方式启动应用程序。这是一个
问题与AppKit的全屏模式,不一定与您的
代码。

...you are using full screen in Xcode when starting your app. I wasn't doing this [before], but I now can reproduce the issue. From what I can tell it only happens when your app is started from full screen mode in Xcode. Your users won't be starting the app this way. This is a problem with AppKit's fullScreen mode, and not necessarily with your code.

这篇关于自定义NSStatusItem和NSView不可靠地接收NSTrackingEvents的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆