transparent window can't click through in macos sonoma

186 Views Asked by At

i crate a fully transparent NSWindow like this:

NSWindowStyleMask styleMask = NSBorderlessWindowMask | NSFullSizeContentViewWindowMask; 
 
NSWinod *window = [[BrowserNSWindow alloc] initWithContentRect:NSMakeRect(0,0,500,600) 
                                                    styleMask:styleMask 
                                                      backing:NSBackingStoreBuffered 
                                                        defer:NO]; 
 
 
[window setBackgroundColor:[NSColor clearColor]]; 
[window setOpaque:NO]; 
[window setHasShadow:NO]; 
 
NSCustomView *view = [[NSCustomView alloc] initWithFrame:NSMakeRect(0,0,500,600)]; 
[view setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)]; 
[view setAutoresizesSubviews: true]; 
[window.contentView addSubview:view];

and override the NSView drawRect function:

- (void)drawRect:(NSRect)rect 
{ 
    [[NSColor clearColor] set]; 
    NSRectFill(rect); 
    NSRectFillUsingOperation(rect, NSCompositingOperationSourceOver); 
}

i call setNeedDisplay:YES to redraw the view. in macos sonoma, i found that when after multiple calls setNeedDisplay:YES, the transparent window can't click through. This feature run correctly in previous versions, like macos 13

1

There are 1 best solutions below

0
Petter Sjölund On

This might have something to do with the fact that the clipsToBounds property of NSViews are no longer set to true by default in Sonoma, so the rect passed to the drawRect: method might extend outside the view bounds.

Try either explicitly setting

[view setClipsToBounds: true];

or clipping the rect to the view bounds in drawRect:

rect = NSIntersectionRect(rect, self.bounds);