Skip to content
This repository has been archived by the owner on Mar 5, 2023. It is now read-only.

IMPROVED the performance in drawing the background view of the panel. #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Popup/BackgroundView.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@

@interface BackgroundView : NSView
{
@private
NSInteger _arrowX;
NSBezierPath* _path;
}

@property (nonatomic, assign) NSInteger arrowX;
@property (nonatomic, retain) NSBezierPath* path;

@end
40 changes: 22 additions & 18 deletions Popup/BackgroundView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down