Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mouse Events Bleeding Through NSView

I have an NSView which covers its parent window's content view. This view has a click event handler which removes it from the content view. Inside this view, I have another view. When I drag the mouse in this inner view, the mouse events are applied not only to the view in the front, but also to the views behind. Additionally, the cursors from the views behind are showing up as well. This is the same problem occurring here: NSView overlay passes mouse events to underlying subviews? but the answer there won't work for my project because I can't open another window.

like image 1000
Maz Avatar asked Jul 06 '11 01:07

Maz


People also ask

What is the use of NSView?

The infrastructure for drawing, printing, and handling events in an app. You typically don’t use NSView objects directly. Instead, you use objects whose classes descend from NSView or you subclass NSView yourself and override its methods to implement the behavior you need.

Why do I need to call Super when implementing NSView?

Because NSView changes the default behavior of the rightMouseDown: method, you should call super when implementing that method in your custom subclasses. If your view descends from a class other than NSView, call super to let the parent view handle any events that you do not.

When do I need to update the nsopenglcontext of an NSView?

Posted whenever an NSView object recalculates its tracking areas. Posted whenever an NSView object that has attached surfaces (that is, NSOpenGLContext objects) moves to a different screen, or other cases where the NSOpenGLContext object needs to be updated. var candidateListTouchBarItem: NSCandidateListTouchBarItem<AnyObject>?

What is the difference between an NSWindow and an view?

Views handle the presentation and interaction with your app’s visible content. You arrange one or more views inside an NSWindow object, which acts as a wrapper for your content. A view object defines a rectangular region for drawing and receiving mouse events.


2 Answers

Without seeing your event-handling code it's difficult to know what's happening, but I suspect you might be calling super's implementation of the various event-handling methods in your implementations.

NSView is a subclass of NSResponder, so by default un-handled events are passed up the responder chain. The superview of a view is the next object in the responder chain, so if you call, for example, [super mouseDown:event] in your implementation of ‑mouseDown:, the event will be passed to the superview.

The fix is to ensure you don't call super's implementation in your event handlers.

This is incorrect:

- (void)mouseDown:(NSEvent*)anEvent
{
    //do something
    [super mouseDown:event];
}

This is correct:

- (void)mouseDown:(NSEvent*)anEvent
{
    //do something
}
like image 146
Rob Keniger Avatar answered Oct 19 '22 09:10

Rob Keniger


Rob's answer and Maz's comment on that answer solve this issue, but just to make it absolutely explicit. In order to prevent a NSView from bleeding it's mouse events to the parent, one must implement the empty methods.

// NSResponder =========================================
- (void) mouseDown:(NSEvent*)event {}
- (void) mouseDragged:(NSEvent*)event {}
- (void) mouseUp:(NSEvent*)event {}
like image 23
aepryus Avatar answered Oct 19 '22 11:10

aepryus