forked from indragiek/INPopoverController
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathINPopoverWindowFrame.m
More file actions
139 lines (124 loc) · 5.48 KB
/
INPopoverWindowFrame.m
File metadata and controls
139 lines (124 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//
// INPopoverWindowFrame.m
// Copyright 2011 Indragie Karunaratne. All rights reserved.
//
// Licensed under the BSD License <http://www.opensource.org/licenses/bsd-license>
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#import "INPopoverWindowFrame.h"
@interface INPopoverWindowFrame ()
- (NSBezierPath*)_popoverBezierPathWithRect:(NSRect)aRect;
@end
@implementation INPopoverWindowFrame
@synthesize color = _color, borderColor = _borderColor;
@synthesize borderWidth = _borderWidth;
@synthesize arrowDirection = _arrowDirection;
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Set some default values
self.arrowDirection = INPopoverArrowDirectionLeft;
self.color = [NSColor colorWithCalibratedWhite:0.0 alpha:0.8];
}
return self;
}
- (void)dealloc
{
[_color release];
[_borderColor release];
[super dealloc];
}
- (void)drawRect:(NSRect)dirtyRect
{
NSBezierPath *path = [self _popoverBezierPathWithRect:[self bounds]];
[self.color set];
[path fill];
[path setLineWidth:self.borderWidth];
[self.borderColor set];
[path stroke];
}
#pragma mark -
#pragma mark Private
- (NSBezierPath*)_popoverBezierPathWithRect:(NSRect)aRect
{
NSBezierPath *path = [NSBezierPath bezierPath];
CGFloat radius = INPOPOVER_CORNER_RADIUS;
CGFloat inset = radius + INPOPOVER_ARROW_HEIGHT;
NSRect drawingRect = NSInsetRect(aRect, inset, inset);
CGFloat minX = NSMinX(drawingRect);
CGFloat maxX = NSMaxX(drawingRect);
CGFloat minY = NSMinY(drawingRect);
CGFloat maxY = NSMaxY(drawingRect);
// Bottom left corner
[path appendBezierPathWithArcWithCenter:NSMakePoint(minX, minY) radius:radius startAngle:180.0 endAngle:270.0];
if (self.arrowDirection == INPopoverArrowDirectionDown) {
CGFloat midX = NSMidX(drawingRect);
NSPoint points[3];
points[0] = NSMakePoint(floor(midX - (INPOPOVER_ARROW_WIDTH / 2.0)), minY - radius); // Starting point
points[1] = NSMakePoint(floor(midX), points[0].y - INPOPOVER_ARROW_HEIGHT); // Arrow tip
points[2] = NSMakePoint(floor(midX + (INPOPOVER_ARROW_WIDTH / 2.0)), points[0].y); // Ending point
[path appendBezierPathWithPoints:points count:3];
}
// Bottom right corner
[path appendBezierPathWithArcWithCenter:NSMakePoint(maxX, minY) radius:radius startAngle:270.0 endAngle:360.0];
if (self.arrowDirection == INPopoverArrowDirectionRight) {
CGFloat midY = NSMidY(drawingRect);
NSPoint points[3];
points[0] = NSMakePoint(maxX + radius, floor(midY - (INPOPOVER_ARROW_WIDTH / 2.0)));
points[1] = NSMakePoint(points[0].x + INPOPOVER_ARROW_HEIGHT, floor(midY));
points[2] = NSMakePoint(points[0].x, floor(midY + (INPOPOVER_ARROW_WIDTH / 2.0)));
[path appendBezierPathWithPoints:points count:3];
}
// Top right corner
[path appendBezierPathWithArcWithCenter:NSMakePoint(maxX, maxY) radius:radius startAngle:0.0 endAngle:90.0];
if (self.arrowDirection == INPopoverArrowDirectionUp) {
CGFloat midX = NSMidX(drawingRect);
NSPoint points[3];
points[0] = NSMakePoint(floor(midX + (INPOPOVER_ARROW_WIDTH / 2.0)), maxY + radius);
points[1] = NSMakePoint(floor(midX), points[0].y + INPOPOVER_ARROW_HEIGHT);
points[2] = NSMakePoint(floor(midX - (INPOPOVER_ARROW_WIDTH / 2.0)), points[0].y);
[path appendBezierPathWithPoints:points count:3];
}
// Top left corner
[path appendBezierPathWithArcWithCenter:NSMakePoint(minX, maxY) radius:radius startAngle:90.0 endAngle:180.0];
if (self.arrowDirection == INPopoverArrowDirectionLeft) {
CGFloat midY = NSMidY(drawingRect);
NSPoint points[3];
points[0] = NSMakePoint(minX - radius, floor(midY + (INPOPOVER_ARROW_WIDTH / 2.0)));
points[1] = NSMakePoint(points[0].x - INPOPOVER_ARROW_HEIGHT, floor(midY));
points[2] = NSMakePoint(points[0].x, floor(midY - (INPOPOVER_ARROW_WIDTH / 2.0)));
[path appendBezierPathWithPoints:points count:3];
}
[path closePath];
return path;
}
#pragma mark -
#pragma mark Accessors
// Redraw the frame every time a property is changed
- (void)setColor:(NSColor *)newColor
{
if (_color != newColor) {
[_color release];
_color = [newColor retain];
[self setNeedsDisplay:YES];
}
}
- (void)setBorderColor:(NSColor *)newBorderColor
{
if (_borderColor != newBorderColor) {
[_borderColor release];
_borderColor = [newBorderColor retain];
[self setNeedsDisplay:YES];
}
}
- (void)setArrowDirection:(INPopoverArrowDirection)newArrowDirection
{
_arrowDirection = newArrowDirection;
[self setNeedsDisplay:YES];
}
- (void)setBorderWidth:(CGFloat)newBorderWidth
{
_borderWidth = newBorderWidth;
[self setNeedsDisplay:YES];
}
@end