问题描述
我有一个状态栏条目的应用程序,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:
这篇关于自定义NSStatusItem和NSView不可靠地接收NSTrackingEvents的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!