Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When exactly does an NSWindow get rounded corners?

(I found several similar questions, but nothing quite the same. Some were older than the OS in question. Some were doing crazy things, like completely custom windows. Nobody I found has instructions for how to make a perfectly ordinary window work correctly.)

Starting in OS X Lion, standard windows have had rounded corners. Unfortunately, I'm having trouble replicating this in my application. When an NSView is pushed against the corner of an NSWindow, sometimes it stays clipped to the round NSWindow, and sometimes it escapes and makes a square corner.

I haven't been able to figure out a pattern yet.

  • Even NSViews that "draw their background" sometimes force square corners, so it's not that.
  • My custom NSViews make square corners (am I responsible for checking my position in the NSWindow, and clipping to that, if I'm near the edge?), but some standard Cocoa controls do this, too.

For example, make a new Cocoa project, put a (scrolling) NSTableView or NSTextView in the main NSWindow, and add layout constraints so it follows the edges of the window. The bottom corners are square! The Finder, in contrast, has (what looks like) an NSTableView, right up against the bottom corner of the window, and it's rounded.

How do I make an ordinary NSWindow, with the proper round corners on the bottom?

like image 600
J. Cocoe Avatar asked Aug 16 '16 23:08

J. Cocoe


1 Answers

The flag to set, in order to get NSWindow to clip its contents to its actual window border (why the heck isn't this the default?) is NSView's wantsLayer.

  • In code: contentView!.wantsLayer = true, when the window is loaded.

  • Or in IB: select the root View of your Window, and then in "View Effects inspector", check the box next to it in "Core Animation Layer" (even if you're not using Core Animation).

Meta: the documentation for wantsLayer talks about a lot of complex things that don't seem at all related to window shape or clipping, and it's not even a property of NSWindow, while NSWindow has some flags that claim to be about window corner rounding but which don't work any more, so I'm not sure how anybody is supposed to discover this, except by spending 2 days on trial-and-error. I hope this answer helps somebody else!

like image 100
J. Cocoe Avatar answered Oct 07 '22 11:10

J. Cocoe