-
Notifications
You must be signed in to change notification settings - Fork 11
/
TSModeCell.m
134 lines (104 loc) · 5.59 KB
/
TSModeCell.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
//
// TSModeTabView.m
// ThisService
//
// Created by Jesper on 2007-08-03.
// Copyright 2007-2012 waffle software. All rights reserved.
// BSD licensed - see license.txt for more information.
//
#import "TSModeCell.h"
#import "NSBezierPath+AttributedStringExtensions.h"
@implementation TSModeCell
- (NSRect)drawingRectForBounds:(NSRect)theRect {
return theRect;
}
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
// NSLog(@"Told to draw with frame: %@ in view %@", NSStringFromRect(cellFrame), controlView);
BOOL flipped = [controlView isFlipped];
NSRect insideRect; NSRect borderRect;
NSDivideRect(cellFrame,&borderRect,&insideRect,1.0,(flipped ? NSMaxYEdge : NSMinYEdge));
[self drawInteriorWithFrame:insideRect inView:controlView];
[[[[NSColor shadowColor] highlightWithLevel:0.55] colorWithAlphaComponent:0.5] set];
NSRectFill(borderRect);
}
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
static NSGradient *unselectedGradient = nil;
static NSGradient *unselectedTextGradient = nil;
static NSShadow *unselectedTextShadow = nil;
static NSGradient *selectedGradient = nil;
static NSGradient *selectedTextGradient = nil;
static NSShadow *selectedTextShadow = nil;
static NSGradient *selectedSpotlightGradient = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
unselectedGradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedWhite:0.733 alpha:1.000]
endingColor:[NSColor colorWithCalibratedWhite:0.529 alpha:1.000]];
unselectedTextGradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedWhite:0.270 alpha:1.000]
endingColor:[NSColor colorWithCalibratedWhite:0.127 alpha:1.000]];
unselectedTextShadow = [[NSShadow alloc] init];
[unselectedTextShadow setShadowBlurRadius:0];
[unselectedTextShadow setShadowOffset:NSMakeSize(0, -0.75)];
[unselectedTextShadow setShadowColor:[NSColor colorWithCalibratedWhite:0.8 alpha:1]];
selectedGradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedWhite:0.478 alpha:1.000]
endingColor:[NSColor colorWithCalibratedWhite:0.320 alpha:1.000]];
selectedTextGradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedWhite:0.794 alpha:1.000]
endingColor:[NSColor colorWithCalibratedWhite:0.957 alpha:1.000]];
selectedTextShadow = [[NSShadow alloc] init];
[selectedTextShadow setShadowBlurRadius:3.5];
[selectedTextShadow setShadowOffset:NSMakeSize(0, -0.5)];
[selectedTextShadow setShadowColor:[NSColor colorWithCalibratedWhite:0 alpha:0.6]];
selectedSpotlightGradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedWhite:1 alpha:0.03] endingColor:[NSColor colorWithCalibratedWhite:1 alpha:0]];
});
NSGradient *backgroundGradient = unselectedGradient;
NSGradient *textGradient = unselectedTextGradient;
NSShadow *textShadow = unselectedTextShadow;
NSGradient *backgroundSpotlightGradient = nil;
BOOL flipped = [controlView isFlipped];
BOOL selected = [self state] == NSOnState;
BOOL highlighted = [self isHighlighted];
if (selected || highlighted) {
backgroundGradient = selectedGradient;
textGradient = selectedTextGradient;
textShadow = selectedTextShadow;
if (selected) {
backgroundSpotlightGradient = selectedSpotlightGradient;
}
}
int angle = (flipped ? (selected ? 270 : 90) : (selected ? 90 : 270));
[backgroundGradient drawInRect:cellFrame angle:angle];
if (backgroundSpotlightGradient) {
[backgroundSpotlightGradient drawInRect:cellFrame relativeCenterPosition:NSMakePoint(0, 0.5)];
}
NSString *title = [self title];
NSFont *font = [self font];
NSRect attrRect = [self titleRectForBounds:cellFrame];
NSAffineTransform *unflip = [NSAffineTransform transform];
if (flipped) {
[unflip translateXBy:0.0 yBy:cellFrame.size.height];
[unflip scaleXBy:1 yBy:-1];
}
CGFloat yNudge = 3;
NSAffineTransform *moveToPosition = [NSAffineTransform transform];
[moveToPosition translateXBy:attrRect.origin.x yBy:(flipped ? attrRect.size.height - (attrRect.origin.y - yNudge) : attrRect.origin.y - yNudge)];
NSBezierPath *bezierPathOfGlyphs = [NSBezierPath bezierPathWithString:title inFont:font];
[bezierPathOfGlyphs transformUsingAffineTransform:moveToPosition];
[bezierPathOfGlyphs transformUsingAffineTransform:unflip];
[[NSGraphicsContext currentContext] saveGraphicsState];
[textShadow set];
[[textGradient interpolatedColorAtLocation:0.5] setFill];
[bezierPathOfGlyphs fill];
[[NSGraphicsContext currentContext] restoreGraphicsState];
[textGradient drawInBezierPath:bezierPathOfGlyphs angle:-90];
if (![self isEnabled]) {
[[[NSColor grayColor] colorWithAlphaComponent:0.25] setFill];
[NSBezierPath fillRect:cellFrame];
}
}
- (NSRect)titleRectForBounds:(NSRect)bounds {
NSAttributedString *attr = [self attributedTitle];
NSSize size = [attr size];
NSRect rect = NSMakeRect(NSMinX(bounds)+(bounds.size.width-size.width)/2.0,NSMinY(bounds)+(bounds.size.height-size.height)/2.0,size.width,size.height);
rect = NSOffsetRect(rect,0.0,(size.height*0.05));
return rect;
}
@end