-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathOEXTokenAttachmentCell.m
225 lines (185 loc) · 7.19 KB
/
OEXTokenAttachmentCell.m
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//
// OEXTokenAttachmentCell.m
// OEXTokenField
//
// Created by Nicolas BACHSCHMIDT on 16/03/2013.
// Copyright (c) 2013 Octiplex. All rights reserved.
//
#import "OEXTokenAttachmentCell.h"
static CGFloat const kOEXTokenAttachmentTitleMargin = 11;
static CGFloat const kOEXTokenAttachmentTokenMargin = 3;
@implementation OEXTokenAttachmentCell
{
// Theses ivars are set at the begining of the draw
OEXTokenDrawingMode _drawingMode;
OEXTokenJoinStyle _joinStyle;
}
#pragma mark - Geometry
- (NSPoint)cellBaselineOffset
{
return NSMakePoint(0, self.font.descender);
}
- (NSSize)cellSize
{
NSSize titleSize = [self.stringValue sizeWithAttributes:@{NSFontAttributeName:self.font}];
return [self cellSizeForTitleSize:titleSize];
}
- (NSSize)cellSizeForTitleSize:(NSSize)titleSize
{
NSSize size = titleSize;
// Add margins + height for the token rounded edges
size.width += size.height + kOEXTokenAttachmentTitleMargin * 2;
NSRect rect = {NSZeroPoint, size};
return NSIntegralRect(rect).size;
}
- (NSRect)titleRectForBounds:(NSRect)bounds
{
bounds.size.width = MAX(bounds.size.width, kOEXTokenAttachmentTitleMargin * 2 + bounds.size.height);
return NSInsetRect(bounds, kOEXTokenAttachmentTitleMargin + bounds.size.height / 2, 0);
}
#pragma mark - Drawing
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView characterIndex:(NSUInteger)charIndex layoutManager:(NSLayoutManager *)layoutManager
{
_drawingMode = self.isHighlighted && controlView.window.isKeyWindow ? OEXTokenDrawingModeHighlighted : OEXTokenDrawingModeDefault;
_joinStyle = OEXTokenJoinStyleNone;
if ( [controlView respondsToSelector:@selector(selectedRanges)] )
{
for ( NSValue *rangeValue in [(id) controlView selectedRanges] )
{
NSRange range = rangeValue.rangeValue;
if ( ! NSLocationInRange(charIndex, range) )
continue;
if ( controlView.window.isKeyWindow )
_drawingMode = OEXTokenDrawingModeSelected;
// TODO: RTL is not supported yet
if ( range.location < charIndex )
_joinStyle |= OEXTokenJoinStyleLeft;
if ( NSMaxRange(range) > charIndex + 1 )
_joinStyle |= OEXTokenJoinStyleRight;
}
}
[self drawTokenWithFrame:cellFrame inView:controlView];
}
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
[self drawWithFrame:cellFrame inView:controlView characterIndex:NSNotFound layoutManager:nil];
}
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
[self drawWithFrame:cellFrame inView:controlView characterIndex:NSNotFound layoutManager:nil];
}
- (void)drawTokenWithFrame:(NSRect)rect inView:(NSView *)controlView
{
[[NSGraphicsContext currentContext] saveGraphicsState];
NSColor *fillColor = [self tokenFillColorForDrawingMode:self.tokenDrawingMode];
NSColor *strokeColor = [self tokenStrokeColorForDrawingMode:self.tokenDrawingMode];
NSBezierPath *path = [self tokenPathForBounds:rect joinStyle:self.tokenJoinStyle];
[path addClip];
if ( fillColor ) {
[fillColor setFill];
[path fill];
}
if ( strokeColor ) {
[strokeColor setStroke];
[path stroke];
}
[self drawTitleWithFrame:[self titleRectForBounds:rect] inView:controlView];
[[NSGraphicsContext currentContext] restoreGraphicsState];
}
- (void)drawTitleWithFrame:(NSRect)rect inView:(NSView *)controlView;
{
NSColor *textColor = [self tokenTitleColorForDrawingMode:self.tokenDrawingMode];
NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
style.lineBreakMode = NSLineBreakByTruncatingTail;
[self.stringValue drawInRect:rect withAttributes:@{NSFontAttributeName:self.font, NSForegroundColorAttributeName:textColor, NSParagraphStyleAttributeName:style}];
}
#pragma mark - State
- (OEXTokenDrawingMode)tokenDrawingMode
{
return _drawingMode;
}
- (OEXTokenJoinStyle)tokenJoinStyle
{
return _joinStyle;
}
- (NSColor *)tokenFillColorForDrawingMode:(OEXTokenDrawingMode)drawingMode
{
// Those colors are Stolen From Apple
switch ( drawingMode )
{
case OEXTokenDrawingModeDefault:
return [NSColor colorWithDeviceRed:0.8706 green:0.9059 blue:0.9725 alpha:1];
case OEXTokenDrawingModeHighlighted:
return [NSColor colorWithDeviceRed:0.7330 green:0.8078 blue:0.9451 alpha:1];
case OEXTokenDrawingModeSelected:
return [NSColor colorWithDeviceRed:0.3490 green:0.5451 blue:0.9255 alpha:1];
}
}
- (NSColor *)tokenStrokeColorForDrawingMode:(OEXTokenDrawingMode)drawingMode
{
// Those colors are Stolen From Apple
switch ( drawingMode )
{
case OEXTokenDrawingModeDefault:
return [NSColor colorWithDeviceRed:0.6431 green:0.7412 blue:0.9255 alpha:1];
case OEXTokenDrawingModeHighlighted:
return [NSColor colorWithDeviceRed:0.4275 green:0.5843 blue:0.8784 alpha:1];
case OEXTokenDrawingModeSelected:
return [NSColor colorWithDeviceRed:0.3490 green:0.5451 blue:0.9255 alpha:1];
}
}
- (NSColor *)tokenTitleColorForDrawingMode:(OEXTokenDrawingMode)drawingMode
{
switch ( drawingMode )
{
case OEXTokenDrawingModeDefault:
case OEXTokenDrawingModeHighlighted:
return [NSColor controlTextColor];
case OEXTokenDrawingModeSelected:
return [NSColor alternateSelectedControlTextColor];
}
}
#pragma mark - Geometry
- (NSBezierPath *)tokenPathForBounds:(NSRect)bounds joinStyle:(OEXTokenJoinStyle)jointStyle
{
bounds.size.width = MAX(bounds.size.width, kOEXTokenAttachmentTokenMargin * 2 + bounds.size.height);
CGFloat radius = bounds.size.height / 2;
CGRect innerRect = NSInsetRect(bounds, kOEXTokenAttachmentTokenMargin + radius, 0);
CGFloat x0 = NSMinX(bounds);
CGFloat x1 = NSMinX(innerRect);
CGFloat x2 = NSMaxX(innerRect);
CGFloat x3 = NSMaxX(bounds);
CGFloat minY = NSMinY(bounds);
CGFloat maxY = NSMaxY(bounds);
CGFloat midY = NSMidY(bounds);
NSBezierPath *path = [NSBezierPath new];
[path moveToPoint:NSMakePoint(x1, minY)];
// Left edge
if ( jointStyle & OEXTokenJoinStyleLeft ) {
[path lineToPoint:NSMakePoint(x0, minY)];
[path lineToPoint:NSMakePoint(x0, maxY)];
[path lineToPoint:NSMakePoint(x1, maxY)];
}
else {
// Degrees?! O_o
[path appendBezierPathWithArcWithCenter:NSMakePoint(x1, midY) radius:radius startAngle:-90 endAngle:90 clockwise:YES];
}
// Top edge
[path lineToPoint:NSMakePoint(x2, maxY)];
// Right edge
if ( jointStyle & OEXTokenJoinStyleRight ) {
[path lineToPoint:NSMakePoint(x3, maxY)];
[path lineToPoint:NSMakePoint(x3, minY)];
[path lineToPoint:NSMakePoint(x2, minY)];
}
else {
[path appendBezierPathWithArcWithCenter:NSMakePoint(x2, midY) radius:radius startAngle:90 endAngle:-90 clockwise:YES];
}
// Bottom edge
[path lineToPoint:NSMakePoint(x1, minY)];
[path closePath];
// As we'll clip to the path, let's double the desired line width (1)
[path setLineWidth:2];
return path;
}
@end