6

I've got a problem here. I'm creating a NSTrackingArea like this:

NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:[self frame] options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways owner:self userInfo:nil];
[self addTrackingArea:area];
[area release];

This works quite fine. However, here's a problem. I have it set up like this:

-(void)mouseEntered:(NSEvent *)event {
    [self toggleDetail];
}
-(void)mouseExited:(NSEvent *)event {
    [self toggleDetail];
}

And toggleDetail is basically like this:

- (void)toggleDetail {
if (!attachedWindow) {
    NSPoint buttonPoint = NSMakePoint(NSMidX([conditionImage frame]),
                                      NSMidY([conditionImage frame]));
    attachedWindow = [[MAAttachedWindow alloc] initWithView:view 
                                            attachedToPoint:buttonPoint 
                                                   inWindow:[self window] 
                                                     onSide:12
                                                 atDistance:10.0];
    //config removed because of irrelevance
    [[self window] addChildWindow:attachedWindow ordered:NSWindowAbove];
} else {
    [[self window] removeChildWindow:attachedWindow];
    [attachedWindow orderOut:self];
    [attachedWindow release];
    attachedWindow = nil;
}

}

Now here's my problem. When my MAAttachedWindow is closed, and I move my mouse over the window, it opens. Dandy. However, it's only good when I keep my mouse away from the MAAttachedWindow. As soon as I move my mouse over it (while still over the main window) it starts to violently open and close the MAAttachedWindow.

Here's why: As soon as the window opens, the tracking area believes that my mouse isn't over the window anymore because the popup is in front of it. However, as soon as it removes the popup, then it thinks that my mouse is over it again, thus creating and showing the popup once more. Thus it's an endless loop.

My question is if there's a way around this, where it can not close the window unless my mouse is outside both the window and the popup or something similar. Is there a way to do this?

sudo rm -rf
  • 29,408
  • 19
  • 102
  • 161
  • Why would you show a window only when your mouse is in certain position (tracking area rect)? I'd say toggle the window presence each time mouse _enters_ the tracking area. – Eimantas Feb 25 '11 at 05:42
  • @Eimantas Because the popup is more of a hover and get info popup, rather than something that is to be interacted with. – sudo rm -rf Feb 25 '11 at 05:58
  • Then I suggest showing it somewhere sideways the tracking area. Not on top of it cause otherwise the cursor would block some info on the info-window. – Eimantas Feb 25 '11 at 06:14
  • @Eimantas: Yeah I could, but that's not the solution I was hoping for. – sudo rm -rf Feb 25 '11 at 14:53
  • So basically is it possible to determine which window the event came from? – sudo rm -rf Feb 25 '11 at 16:43

1 Answers1

3

See -[NSWindow setIgnoresMouseEvents:].

By the way, be very careful with overlay windows. The difficulty with them is that you have to give it absolute coordinates when you create it and there’s a small race—the parent window can be moved between the time that you get its frame, and the time that you create the child window. Window moving is done by the Window Server and can be done independently of the application (that's why you can move a window when the application is beach balling). Now it’s very rare that this would be an issue, but it is possible and quite hard to fix it properly. It’s more of a problem if you’re trying to resize or move a child window when the parent window resizes.

Now I realise that none of this might apply to you, but if it does, and you can think of an alternative to using child windows, I would advise you to go with it.

Chris Suter
  • 2,897
  • 2
  • 19
  • 10
  • Haven't tried it yet, but that looks perfect! Thank you also for the warning. I'll keep it in mind. – sudo rm -rf Mar 06 '11 at 15:37
  • Ok, got to try it out and it works quite nicely except for one thing. If I move my mouse over the window and the popup comes up, then I move my mouse over the popup, it doesn't do that weird twitching thing any more. If I move my mouse over the window in the area where the popup will come up, however, it still has the same problem. Any ideas why it would make a difference where the mouse enters the window? Thanks for your help! – sudo rm -rf Mar 06 '11 at 20:33
  • When do you do the -[NSWindow setIgnoresMouseEvents:] call? Make sure it's before the Window becomes visible. You might find that you have to wait until the Window has been created by the Window Server before making it visible (which can be slightly awkward IIRC). – Chris Suter Mar 07 '11 at 03:09