From 86732fc4655165162fa5156b4142eaffda9a9709 Mon Sep 17 00:00:00 2001 From: Tong Guo Date: Mon, 18 Aug 2014 22:27:01 +0800 Subject: [PATCH] IMPROVED the performance in drawing the background view of the panel. --- Popup/BackgroundView.h | 3 +++ Popup/BackgroundView.m | 40 ++++++++++++++++++++++------------------ 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/Popup/BackgroundView.h b/Popup/BackgroundView.h index c93aedf..215eca8 100644 --- a/Popup/BackgroundView.h +++ b/Popup/BackgroundView.h @@ -3,9 +3,12 @@ @interface BackgroundView : NSView { +@private NSInteger _arrowX; + NSBezierPath* _path; } @property (nonatomic, assign) NSInteger arrowX; +@property (nonatomic, retain) NSBezierPath* path; @end diff --git a/Popup/BackgroundView.m b/Popup/BackgroundView.m index f7e630a..2fcc2c3 100644 --- a/Popup/BackgroundView.m +++ b/Popup/BackgroundView.m @@ -13,54 +13,58 @@ @implementation BackgroundView @synthesize arrowX = _arrowX; +@synthesize path = _path; #pragma mark - - (void)drawRect:(NSRect)dirtyRect { NSRect contentRect = NSInsetRect([self bounds], LINE_THICKNESS, LINE_THICKNESS); - NSBezierPath *path = [NSBezierPath bezierPath]; - - [path moveToPoint:NSMakePoint(_arrowX, NSMaxY(contentRect))]; - [path lineToPoint:NSMakePoint(_arrowX + ARROW_WIDTH / 2, NSMaxY(contentRect) - ARROW_HEIGHT)]; - [path lineToPoint:NSMakePoint(NSMaxX(contentRect) - CORNER_RADIUS, NSMaxY(contentRect) - ARROW_HEIGHT)]; + if ( !self.path ) + self.path = [NSBezierPath bezierPath]; + + [ self.path removeAllPoints ]; + + [self.path moveToPoint:NSMakePoint(_arrowX, NSMaxY(contentRect))]; + [self.path lineToPoint:NSMakePoint(_arrowX + ARROW_WIDTH / 2, NSMaxY(contentRect) - ARROW_HEIGHT)]; + [self.path lineToPoint:NSMakePoint(NSMaxX(contentRect) - CORNER_RADIUS, NSMaxY(contentRect) - ARROW_HEIGHT)]; NSPoint topRightCorner = NSMakePoint(NSMaxX(contentRect), NSMaxY(contentRect) - ARROW_HEIGHT); - [path curveToPoint:NSMakePoint(NSMaxX(contentRect), NSMaxY(contentRect) - ARROW_HEIGHT - CORNER_RADIUS) + [self.path curveToPoint:NSMakePoint(NSMaxX(contentRect), NSMaxY(contentRect) - ARROW_HEIGHT - CORNER_RADIUS) controlPoint1:topRightCorner controlPoint2:topRightCorner]; - [path lineToPoint:NSMakePoint(NSMaxX(contentRect), NSMinY(contentRect) + CORNER_RADIUS)]; + [self.path lineToPoint:NSMakePoint(NSMaxX(contentRect), NSMinY(contentRect) + CORNER_RADIUS)]; NSPoint bottomRightCorner = NSMakePoint(NSMaxX(contentRect), NSMinY(contentRect)); - [path curveToPoint:NSMakePoint(NSMaxX(contentRect) - CORNER_RADIUS, NSMinY(contentRect)) + [self.path curveToPoint:NSMakePoint(NSMaxX(contentRect) - CORNER_RADIUS, NSMinY(contentRect)) controlPoint1:bottomRightCorner controlPoint2:bottomRightCorner]; - [path lineToPoint:NSMakePoint(NSMinX(contentRect) + CORNER_RADIUS, NSMinY(contentRect))]; + [self.path lineToPoint:NSMakePoint(NSMinX(contentRect) + CORNER_RADIUS, NSMinY(contentRect))]; - [path curveToPoint:NSMakePoint(NSMinX(contentRect), NSMinY(contentRect) + CORNER_RADIUS) + [self.path curveToPoint:NSMakePoint(NSMinX(contentRect), NSMinY(contentRect) + CORNER_RADIUS) controlPoint1:contentRect.origin controlPoint2:contentRect.origin]; - [path lineToPoint:NSMakePoint(NSMinX(contentRect), NSMaxY(contentRect) - ARROW_HEIGHT - CORNER_RADIUS)]; + [self.path lineToPoint:NSMakePoint(NSMinX(contentRect), NSMaxY(contentRect) - ARROW_HEIGHT - CORNER_RADIUS)]; NSPoint topLeftCorner = NSMakePoint(NSMinX(contentRect), NSMaxY(contentRect) - ARROW_HEIGHT); - [path curveToPoint:NSMakePoint(NSMinX(contentRect) + CORNER_RADIUS, NSMaxY(contentRect) - ARROW_HEIGHT) + [self.path curveToPoint:NSMakePoint(NSMinX(contentRect) + CORNER_RADIUS, NSMaxY(contentRect) - ARROW_HEIGHT) controlPoint1:topLeftCorner controlPoint2:topLeftCorner]; - [path lineToPoint:NSMakePoint(_arrowX - ARROW_WIDTH / 2, NSMaxY(contentRect) - ARROW_HEIGHT)]; - [path closePath]; + [self.path lineToPoint:NSMakePoint(_arrowX - ARROW_WIDTH / 2, NSMaxY(contentRect) - ARROW_HEIGHT)]; + [self.path closePath]; [[NSColor colorWithDeviceWhite:1 alpha:FILL_OPACITY] setFill]; - [path fill]; + [self.path fill]; [NSGraphicsContext saveGraphicsState]; NSBezierPath *clip = [NSBezierPath bezierPathWithRect:[self bounds]]; - [clip appendBezierPath:path]; + [clip appendBezierPath:self.path]; [clip addClip]; - [path setLineWidth:LINE_THICKNESS * 2]; + [self.path setLineWidth:LINE_THICKNESS * 2]; [[NSColor whiteColor] setStroke]; - [path stroke]; + [self.path stroke]; [NSGraphicsContext restoreGraphicsState]; }