-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMVTabView.m
369 lines (312 loc) · 10.3 KB
/
MVTabView.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#import "MVTabView.h"
#import "MVGraphicsFunctions.h"
#import "NSObject+PerformBlockAfterDelay.h"
#define kMVTabViewGlowingDuration 0.7
@interface MVTabView ()
@property (readwrite, getter = isHighlighted) BOOL highlighted;
@property (strong, readwrite) TUIView *sortingBackground;
@property (strong, readwrite) TUIView *glowingView;
@property (readwrite) BOOL mouseDown;
@property (readwrite) BOOL mouseDragged;
- (void)updateGlowingAnimation;
@end
void MVTabDraw(MVTabView *view, CGRect rect, BOOL forceGlowing, int glowingPhase);
void MVTabDraw(MVTabView *view, CGRect rect, BOOL forceGlowing, int glowingPhase)
{
CGContextRef ctx = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
[[NSGraphicsContext currentContext] saveGraphicsState];
CGRect bounds = view.bounds;
if(view.previousTab != nil) {
[[NSColor colorWithDeviceRed:0.8314 green:0.8510 blue:0.8745 alpha:1.0000] set];
[NSBezierPath fillRect:CGRectMake(0, 7, 0.5, 9.5)];
[[NSColor whiteColor] set];
[NSBezierPath fillRect:CGRectMake(0, 6.5, 0.5, 0.5)];
}
float marginX = 7;
// label
NSColor *fontColor;
NSColor *shadowColor = [NSColor colorWithDeviceWhite:1 alpha:1];
CGSize shadowOffset = NSMakeSize(0, -1);
if(view.selected)
{
fontColor = [NSColor colorWithDeviceRed:0.1255 green:0.5137 blue:0.9686 alpha:1.0000];
}
else if(view.online)
{
fontColor = [NSColor colorWithDeviceRed:0.2000 green:0.2000 blue:0.2000 alpha:1.0000];
}
else
{
fontColor = [NSColor colorWithDeviceRed:0.6078 green:0.6510 blue:0.7059 alpha:1.0000];
}
CGSize size = MVSizeOfString(view.name, 11, kMVStringTypeBold);
CGRect stringRect = CGRectMake(marginX, 1, bounds.size.width - marginX - 2, 19);
if(stringRect.size.width - 3 < size.width)
{
// mask
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
CGContextRef maskContext =
CGBitmapContextCreate(NULL,
bounds.size.width,
bounds.size.height,
8,
bounds.size.width,
colorspace,
0);
CGColorSpaceRelease(colorspace);
NSGraphicsContext *maskGraphicsContext =
[NSGraphicsContext graphicsContextWithGraphicsPort:maskContext flipped:NO];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:maskGraphicsContext];
[[NSColor whiteColor] set];
CGContextFillRect(maskContext, rect);
NSColor *startColor = [NSColor colorWithDeviceWhite:0 alpha:1];
NSColor *endColor = [NSColor colorWithDeviceWhite:0 alpha:0];
NSGradient *gradient = [[NSGradient alloc] initWithStartingColor:startColor endingColor:endColor];
[gradient drawInRect:CGRectMake(bounds.size.width - 8, 0,
4, bounds.size.height) angle:180];
[[NSColor blackColor] set];
CGContextFillRect(maskContext, CGRectMake(bounds.size.width - 4, 0, 4, bounds.size.height));
[NSGraphicsContext restoreGraphicsState];
CGImageRef alphaMask = CGBitmapContextCreateImage(maskContext);
// use mask
CGContextSaveGState(ctx);
CGContextClipToMask(ctx, bounds, alphaMask);
}
// draw text
int fontStyle = view.isSelected ? kMVStringTypeMedium : kMVStringTypeNormal;
if(view.isSelected)
stringRect.origin.y = stringRect.origin.y + 2;
MVDrawStringAlignLineBreakMode(view.name, stringRect, fontColor, 11, fontStyle,
shadowColor, shadowOffset, 1,
0,
NSLineBreakByClipping);
if(stringRect.size.width < size.width)
CGContextRestoreGState(ctx);
[[NSGraphicsContext currentContext] restoreGraphicsState];
};
@implementation MVTabView
@synthesize name = name_,
identifier = identifier,
sortable = sortable_,
showed = showed_,
selected = selected_,
sorting = sorting_,
glowing = glowing_,
online = online_,
nextTab = nextTab_,
previousTab = previousTab_,
glowingView = glowingView_,
mouseDown = mouseDown_,
mouseDragged = mouseDragged_,
delegate = delegate_,
highlighted = highlighted_,
sortingBackground = sortingBackground_;
#pragma mark -
#pragma mark Public Methods
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self) {
self.opaque = NO;
self.backgroundColor = [TUIColor clearColor];
self.shouldDisplayWhenWindowChangesFocus = YES;
highlighted_ = NO;
sorting_ = NO;
glowing_ = NO;
online_ = NO;
nextTab_ = nil;
previousTab_ = nil;
mouseDown_ = NO;
mouseDragged_ = NO;
glowingView_ = [[TUIView alloc] initWithFrame:CGRectZero];
glowingView_.userInteractionEnabled = NO;
glowingView_.opaque = NO;
glowingView_.backgroundColor = [TUIColor clearColor];
glowingView_.drawRect = ^(TUIView *view, CGRect rect)
{
MVTabDraw((MVTabView*)(view.superview), rect, YES, 1);
};
glowingView_.layout = ^(TUIView *view)
{
return view.superview.bounds;
};
sortingBackground_ = nil;
[self addObserver:self forKeyPath:@"previousTab" options:0 context:NULL];
[self addObserver:self forKeyPath:@"previousTab.selected" options:0 context:NULL];
[self addObserver:self forKeyPath:@"previousTab.glowing" options:0 context:NULL];
}
return self;
}
- (void)dealloc
{
[self removeObserver:self forKeyPath:@"previousTab"];
[self removeObserver:self forKeyPath:@"previousTab.selected"];
[self removeObserver:self forKeyPath:@"previousTab.glowing"];
}
- (float)expectedWidth
{
CGSize size = MVSizeOfString(self.name, 11, kMVStringTypeBold);
float width = ceil(size.width) + 5 + 5 + 1 + 1 + 2;
return width;
}
#pragma mark -
#pragma mark KVO
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if(object == self)
[self setNeedsDisplay];
else
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
#pragma mark -
#pragma mark Properties
- (void)setSorting:(BOOL)sorting
{
if(sorting == sorting_)
return;
sorting_ = sorting;
if(sorting_) {
CGRect rect = CGRectMake(-5, 0, self.frame.size.width + 9, self.frame.size.height);
if(!self.sortingBackground) {
self.sortingBackground = [[TUIView alloc] initWithFrame:rect];
self.sortingBackground.userInteractionEnabled = NO;
self.sortingBackground.opaque = NO;
self.sortingBackground.backgroundColor = [TUIColor clearColor];
self.sortingBackground.drawRect = ^(TUIView *view, CGRect rect) {
NSColor *startColor;
NSColor *endColor;
NSGradient *gradient;
// left & right shadow
startColor = [NSColor colorWithDeviceWhite:0 alpha:0.10];
endColor = [NSColor colorWithDeviceWhite:0 alpha:0.0];
gradient = [[NSGradient alloc] initWithStartingColor:startColor endingColor:endColor];
[gradient drawInRect:CGRectMake(0, 1,
5, view.bounds.size.height) angle:180];
[gradient drawInRect:CGRectMake(view.bounds.size.width - 5, 1,
5, view.bounds.size.height) angle:0];
};
self.sortingBackground.layer.opacity = 0.0;
[self addSubview:self.sortingBackground];
}
else {
[self.sortingBackground setFrame:rect];
}
[TUIView animateWithDuration:0.2 animations:^{
self.sortingBackground.layer.opacity = 1.0;
}];
}
else {
if(self.sortingBackground) {
[TUIView animateWithDuration:0.2 animations:^{
self.sortingBackground.layer.opacity = 0.0;
}];
[self mv_performBlock:^{
[self.sortingBackground removeFromSuperview];
self.sortingBackground = nil;
} afterDelay:0.2];
}
}
[self setNeedsDisplay];
}
- (void)setGlowing:(BOOL)glowing
{
if(glowing == glowing_)
return;
glowing_ = glowing;
[self updateGlowingAnimation];
}
- (void)setSelected:(BOOL)selected
{
if(selected == selected_)
return;
selected_ = selected;
[self updateGlowingAnimation];
}
- (void)setOnline:(BOOL)online
{
if(online == online_)
return;
online_ = online;
}
- (void)setNextTab:(MVTabView *)nextTab
{
if(nextTab == nextTab_)
return;
nextTab_ = nextTab;
[self setNeedsDisplay];
}
- (void)setPreviousTab:(MVTabView *)previousTab
{
if(previousTab == previousTab_)
return;
previousTab_ = previousTab;
[self setNeedsDisplay];
}
#pragma mark -
#pragma mark Event Handling
- (void)mouseDown:(NSEvent *)theEvent
{
[super mouseDown:theEvent];
self.mouseDown = YES;
[TUIView setAnimationsEnabled:NO block:^{
[self updateGlowingAnimation];
}];
[self.delegate tabViewShouldBeSelect:self];
}
- (void)mouseDragged:(NSEvent *)theEvent
{
[super mouseDragged:theEvent];
self.mouseDragged = YES;
}
- (void)mouseUp:(NSEvent *)theEvent
{
[super mouseUp:theEvent];
self.mouseDragged = NO;
self.mouseDown = NO;
[TUIView setAnimationsEnabled:NO block:^{
[self updateGlowingAnimation];
}];
}
- (BOOL)avoidDisplayWhenWindowChangesFocus
{
return !self.isSelected;
}
#pragma mark -
#pragma mark Drawing Methods
- (void)drawRect:(CGRect)rect
{
MVTabDraw(self, rect, NO, 0);
}
#pragma mark -
#pragma mark Private Methods
- (void)updateGlowingAnimation
{
if(self.isGlowing && !self.isSelected && !self.mouseDown)
{
if(!self.glowingView.superview)
[self addSubview:self.glowingView];
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
animation.values = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0],
[NSNumber numberWithFloat:1.0],
[NSNumber numberWithFloat:0.0],
nil];
animation.repeatCount = INT_MAX;
animation.duration = 1;
[self.glowingView.layer addAnimation:animation
forKey:@"opacity"];
}
else
{
[self.glowingView removeFromSuperview];
[self.glowingView removeAllAnimations];
}
[TUIView animateWithDuration:0.5 animations:^{
[self redraw];
}];
}
@end