forked from bububa/MongoHub-Mac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SidebarBadgeCell.m
147 lines (121 loc) · 4.76 KB
/
SidebarBadgeCell.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
//
// TSBadgeCell.m
// Tahsis
//
// Created by Matteo Bertozzi on 11/29/08.
// Copyright 2008 Matteo Bertozzi. All rights reserved.
//
#import "SidebarBadgeCell.h"
@interface SidebarBadgeCell (Private)
- (CGFloat)drawBadge:(NSRect)cellFrame;
@end
@implementation SidebarBadgeCell
#define TSBADGECELL_BUFFER_LEFT_SMALL 2
#define TSBADGECELL_BUFFER_LEFT 4
#define TSBADGECELL_BUFFER_SIDE 3
#define TSBADGECELL_BUFFER_TOP 3
#define TSBADGECELL_PADDING 6
#define TSBADGECELL_CIRCLE_BUFFER_RIGHT 5
#define TSBADGECELL_RADIUS_X 7
#define TSBADGECELL_RADIUS_Y 8
#define TSBADGECELL_TEXT_HEIGHT 14
#define TSBADGECELL_TEXT_MINI 8
#define TSBADGECELL_TEXT_SMALL 20
#define TSBADGECELL_ICON_SIZE 16
#define TSBADGECELL_ICON_HEIGHT_OFFSET 2
@synthesize badgeCount = _badgeCount;
@synthesize hasBadge = _hasBadge;
@synthesize icon = _icon;
- (void)dealloc {
[_icon release];
[super dealloc];
}
- (void)awakeFromNib {
[self setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
_badgeCount = 0;
_hasBadge = NO;
_icon = nil;
}
- (id)copyWithZone:(NSZone*)zone {
SidebarBadgeCell *cell = [super copyWithZone:zone];
cell->_icon = [_icon retain];
return cell;
}
- (void)setIcon:(NSImage *)icon {
if (_icon != icon) {
[_icon release];
_icon = [icon retain];
[_icon setFlipped:YES];
[_icon setSize:NSMakeSize(TSBADGECELL_ICON_SIZE, TSBADGECELL_ICON_SIZE)];
}
}
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
bool drawBadge = (_hasBadge && cellFrame.size.width > TSBADGECELL_TEXT_SMALL * 3);
CGFloat badgeWidth = (drawBadge ? [self drawBadge:cellFrame] : 0);
if (_icon != nil) {
// Draw Icon
NSRect iconRect = cellFrame;
iconRect.origin.y += TSBADGECELL_ICON_HEIGHT_OFFSET;
iconRect.size.height = TSBADGECELL_ICON_SIZE;
iconRect.size.width = TSBADGECELL_ICON_SIZE;
[_icon drawInRect:iconRect fromRect:NSZeroRect
operation:NSCompositeSourceOver fraction:1.0];
// Draw Rect
NSRect labelRect = cellFrame;
labelRect.origin.x += TSBADGECELL_ICON_SIZE + TSBADGECELL_BUFFER_LEFT;
labelRect.size.width -= (badgeWidth + TSBADGECELL_ICON_SIZE + TSBADGECELL_BUFFER_LEFT);
[super drawInteriorWithFrame:labelRect inView:controlView];
} else {
NSRect labelRect = cellFrame;
labelRect.size.width -= badgeWidth;
[super drawInteriorWithFrame:labelRect inView:controlView];
}
}
- (CGFloat)drawBadge:(NSRect)cellFrame {
// Setup Badge String and Size
NSString *badge = [[NSString alloc] initWithFormat:@"%d", _badgeCount];
NSSize badgeNumSize = [badge sizeWithAttributes:nil];
NSFont *badgeFont = [self font];
// Calculate the Badge's coordinate
CGFloat badgeWidth = badgeNumSize.width + TSBADGECELL_BUFFER_SIDE * 2;
if (badgeNumSize.width < TSBADGECELL_TEXT_MINI)
badgeWidth = TSBADGECELL_TEXT_SMALL;
CGFloat badgeY = cellFrame.origin.y + TSBADGECELL_BUFFER_TOP;
CGFloat badgeX = cellFrame.origin.x + cellFrame.size.width -
TSBADGECELL_CIRCLE_BUFFER_RIGHT - badgeWidth;
CGFloat badgeNumX = badgeX + TSBADGECELL_BUFFER_LEFT;
if (badgeNumSize.width < TSBADGECELL_TEXT_MINI)
badgeNumX += TSBADGECELL_BUFFER_LEFT_SMALL;
// Draw the badge and number
NSRect badgeRect = NSMakeRect(badgeX, badgeY, badgeWidth, TSBADGECELL_TEXT_HEIGHT);
NSBezierPath *badgePath = [NSBezierPath bezierPathWithRoundedRect:badgeRect
xRadius:TSBADGECELL_RADIUS_X
yRadius:TSBADGECELL_RADIUS_Y];
BOOL isWindowFront = [[NSApp mainWindow] isVisible];
BOOL isViewInFocus = [[[[self controlView] window] firstResponder] isEqual:[self controlView]];
BOOL isCellHighlighted = [self isHighlighted];
NSDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:badgeFont forKey:NSFontAttributeName];
if (isWindowFront && isViewInFocus && isCellHighlighted) {
[[NSColor whiteColor] set];
[dict setValue:[NSColor alternateSelectedControlColor] forKey:NSForegroundColorAttributeName];
} else if (isWindowFront && isViewInFocus && !isCellHighlighted) {
[[NSColor colorWithCalibratedRed:0.53 green:0.60 blue:0.74 alpha:1.0] set];
[dict setValue:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];
} else if (isWindowFront && isCellHighlighted) {
[[NSColor whiteColor] set];
[dict setValue:[NSColor colorWithCalibratedRed:0.51 green:0.58 blue:0.72 alpha:1.0] forKey:NSForegroundColorAttributeName];
} else if (!isWindowFront && isCellHighlighted) {
[[NSColor whiteColor] set];
[dict setValue:[NSColor disabledControlTextColor] forKey:NSForegroundColorAttributeName];
} else {
[[NSColor disabledControlTextColor] set];
[dict setValue:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];
}
[badgePath fill];
[badge drawAtPoint:NSMakePoint(badgeNumX, badgeY) withAttributes:dict];
[badge release];
[dict release];
return badgeWidth + TSBADGECELL_PADDING;
}
@end