title: Rounded corner gradient background of window in Cocoa
body: <p>I have a window with text field and bezel button. It's borderless and transparent, but the issue reproduces on any window.</p>
  
  <p>The content view of that window is set in IB to a custom class that draws window's background.</p>
  
  <p>Here's the code:</p>
  
  <pre><code>- (void)drawRect:(NSRect)dirtyRect
  {
      [NSGraphicsContext saveGraphicsState];
  
      float cornerRadius = 10;
      NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:self.bounds xRadius:cornerRadius yRadius:cornerRadius];
  
      [path setClip];
  
      NSGradient *gradient = [[NSGradient alloc] initWithColorsAndLocations:
                              [NSColor colorWithCalibratedRed:0.96f green:0.96f blue:0.96f alpha:1.00f], 0.0f,
                              [NSColor colorWithCalibratedRed:0.84f green:0.84f blue:0.84f alpha:1.00f], 1.0f,
                              nil];
  
      [gradient drawInRect:self.bounds angle:270];
  
      [NSGraphicsContext restoreGraphicsState];
  }
  </code></pre>
  
  <p>It causes some really weird artifacts, like disappearing objects or the text field's background changing to window's:</p>
  
  <p><img src="https://i.stack.imgur.com/AJg2t.png" alt="enter image description here">
  <img src="https://i.stack.imgur.com/DdG94.png" alt="enter image description here"></p>
  
  <p>What's going on? I've tried to isolate it, I've been playing around with this "graphics context state saving" thing (which I'm not sure if I understand correctly), but the problem persists.</p>
  
  <p>I have XCode 4.4, SDK is 10.7 (so is my OS), and the deployment target is 10.6. It probably doesn't matter, but I've been doing something similar in the past and I've never had such strange issues.</p>