Search code examples
cocoaappkitgraphicscontext

How could the existence of `-saveGraphicsContext` and `-restoreGraphicsContext` be causing these issues in 10.10?


We have a custom NSPopUpButtonCell, and overriding this method:

- (void)drawBorderAndBackgroundWithFrame:(NSRect)cellFrame inView:(NSView*)controlView
{
   [[NSGraphicsContext currentContext] saveGraphicsState];
   CGFloat strokeWidth = 1;
   cellFrame = NSInsetRect(cellFrame, strokeWidth/2.0, strokeWidth/2.0);

   NSBezierPath* rectanglePath = [NSBezierPath bezierPathWithRoundedRect:cellFrame xRadius:2 yRadius:2];
   rectanglePath.lineWidth = strokeWidth;

   if (self.horizontalImageOffset != 0)
   {
      CGRect stateImageRect = [self stateImageRectForBounds:cellFrame];
      CGFloat verticalSeparatorX = NSMaxX(stateImageRect);

      NSBezierPath* verticalSeparator = [NSBezierPath bezierPath];
      [verticalSeparator moveToPoint:NSMakePoint(verticalSeparatorX, NSHeight(cellFrame) - strokeWidth)];
      [verticalSeparator lineToPoint:NSMakePoint(verticalSeparatorX, strokeWidth + 1)];
      verticalSeparator.lineWidth = strokeWidth;

      [verticalSeparator stroke];
   }

   [NSGraphicsContext restoreGraphicsState];
}

The issue is that when I bring this class into my project, it makes another one of my (unrelated) views disappear-- as in, won't draw properly

enter image description here

If I comment out the -saveGraphicsState and -restoreGraphicsState it works and draws my other view properly

enter image description here

What could be going wrong here?

EDIT: Kicker is that teammates on 10.11 and 10.12 don't see this issue


Solution

  • I think you should call

    [NSGraphicsContext saveGraphicsState];
    

    not

    [[NSGraphicsContext currentContext] saveGraphicsState];