forked from pieter/gitx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPBSourceViewBadge.m
121 lines (85 loc) · 3.1 KB
/
PBSourceViewBadge.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
//
// PBSourceViewBadge.m
// GitX
//
// Created by Nathan Kinsinger on 2/13/10.
// Copyright 2010 Nathan Kinsinger. All rights reserved.
//
#import "PBSourceViewBadge.h"
#import "PBSourceViewCell.h"
@implementation PBSourceViewBadge
+ (NSColor *) badgeHighlightColor
{
return [NSColor colorWithCalibratedHue:0.612 saturation:0.275 brightness:0.735 alpha:1.000];
}
+ (NSColor *) badgeBackgroundColor
{
return [NSColor colorWithCalibratedWhite:0.6 alpha:1.00];
}
+ (NSColor *) badgeColorForCell:(NSTextFieldCell *)cell
{
if ([cell isHighlighted])
return [NSColor whiteColor];
if ([[[cell controlView] window] isMainWindow])
return [self badgeHighlightColor];
return [self badgeBackgroundColor];
}
+ (NSColor *) badgeTextColorForCell:(NSTextFieldCell *)cell
{
if (![cell isHighlighted])
return [NSColor whiteColor];
if (![[[cell controlView] window] isKeyWindow])
if ([[[cell controlView] window] isMainWindow])
return [self badgeHighlightColor];
else
return [self badgeBackgroundColor];
if ([[[cell controlView] window] firstResponder] == [cell controlView])
return [self badgeHighlightColor];
return [self badgeBackgroundColor];
}
+ (NSMutableDictionary *) badgeTextAttributes
{
NSMutableDictionary *badgeTextAttributes = nil;
if (!badgeTextAttributes) {
NSMutableParagraphStyle *centerStyle = [[NSMutableParagraphStyle alloc] init];
[centerStyle setAlignment:NSCenterTextAlignment];
badgeTextAttributes = [NSMutableDictionary dictionary];
[badgeTextAttributes setObject:[NSFont fontWithName:@"Helvetica-Bold" size:[NSFont systemFontSize] - 2] forKey:NSFontAttributeName];
[badgeTextAttributes setObject:centerStyle forKey:NSParagraphStyleAttributeName];
}
return badgeTextAttributes;
}
#pragma mark -
#pragma mark badges
+ (NSImage *) badge:(NSString *)badge forCell:(NSTextFieldCell *)cell
{
NSColor *badgeColor = [self badgeColorForCell:cell];
NSColor *textColor = [self badgeTextColorForCell:cell];
NSMutableDictionary *badgeTextAttributes = [self badgeTextAttributes];
[badgeTextAttributes setObject:textColor forKey:NSForegroundColorAttributeName];
NSAttributedString *badgeString = [[NSAttributedString alloc] initWithString:badge attributes:badgeTextAttributes];
float imageHeight = ceilf([badgeString size].height);
float radius = ceilf(imageHeight / 4) * 2;
float minWidth = ceilf(radius * 2.5);
float imageWidth = ceilf([badgeString size].width + radius);
if (imageWidth < minWidth)
imageWidth = minWidth;
NSRect badgeRect = NSMakeRect(0, 0, imageWidth, imageHeight);
NSBezierPath *badgePath = [NSBezierPath bezierPathWithRoundedRect:badgeRect xRadius:radius yRadius:radius];
NSImage *badgeImage = [[NSImage alloc] initWithSize:badgeRect.size];
[badgeImage lockFocus];
[badgeColor set];
[badgePath fill];
[badgeString drawInRect:badgeRect];
[badgeImage unlockFocus];
return badgeImage;
}
+ (NSImage *) checkedOutBadgeForCell:(NSTextFieldCell *)cell
{
return [self badge:@"✔" forCell:cell];
}
+ (NSImage *) numericBadge:(NSInteger)number forCell:(NSTextFieldCell *)cell
{
return [self badge:[NSString stringWithFormat:@"%d", number] forCell:cell];
}
@end