Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a transparent NSView subclass handle mouse events?

Tags:

cocoa

The problem

I have a transparent NSView on a transparent NSWindow. The view's drawRect: method draws some content (NSImages, NSBezierPaths and NSStrings) on the view but leaves parts of it transparent.

Clicking on the regions of the view that have been drawn on invokes the usual mouse event handling methods (mouseDown: and mouseUp:).

Clicking on the transparent areas gives focus to whatever window is behind my transparent window.

I would like to make parts of the transparent region clickable so that accidentally clicking between the elements drawn on my view does not cause the window to lose focus.


Solutions already attempted

  • Overriding the NSView's hitTest: method. Found that hitTest: was only called when clicking on a non-transparent area of the view.
  • Overriding the NSView's opaqueAncestor method. Found that this was not called when clicking on any part of the view.
  • Filling portions of the transparent area with [NSColor clearColor] in the drawRect: method, and with an almost-but-not-quite-transparent colour. This had no effect.
  • Experimented with the NSTrackingArea class. This appears to only add support for mouseEntered:, mouseExited:, mouseMoved:, and cursorUpdate: methods, not mouseUp: and mouseDown:.
like image 591
georgebrock Avatar asked Jan 12 '09 15:01

georgebrock


2 Answers

I had the same problem. It looks like [window setIgnoresMouseEvents:NO] will do it.

(On Lion, at least. See http://www.cocoabuilder.com/archive/cocoa/306910-lion-breaks-the-ability-to-click-through-transparent-window-areas-when-the-window-is-resizable.html)

like image 151
justin k. Avatar answered Oct 04 '22 20:10

justin k.


As far as I know, click events to transparent portions of windows aren't delivered to your application at all, so none of the normal event-chain overrides (i.e -hitTest:, -sendEvent:, etc) will work. The only way I can think of off the top of my head is to use Quartz Event Taps to capture all mouse clicks and then figure out if they're over a transparent area of your window manually. That, frankly, sounds like a huge PITA for not much gain.

like image 42
Boaz Stuller Avatar answered Oct 04 '22 18:10

Boaz Stuller