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‘shitTest:method. Found thathitTest:was only called when clicking on a non-transparent area of the view. - Overriding the
NSView‘sopaqueAncestormethod. Found that this was not called when clicking on any part of the view. - Filling portions of the transparent area with
[NSColor clearColor]in thedrawRect:method, and with an almost-but-not-quite-transparent colour. This had no effect. - Experimented with the
NSTrackingAreaclass. This appears to only add support formouseEntered:,mouseExited:,mouseMoved:, andcursorUpdate:methods, notmouseUp:andmouseDown:.
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.