-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathAMIndeterminateProgressIndicatorCell.m
175 lines (154 loc) · 4.52 KB
/
AMIndeterminateProgressIndicatorCell.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
//
// AMIndeterminateProgressIndicatorCell.m
// IPICellTest
//
// Created by Andreas on 23.01.07.
// Copyright 2007 Andreas Mayer. All rights reserved.
//
// 2007-03-10 Andreas Mayer
// - removed -keyEquivalent and -keyEquivalentModifierMask methods
// (I thought those were required by NSTableView/Column. They are not.
// Instead I was using NSButtons as a container for the cells in the demo project.
// Replacing those with plain NSControls did fix the problem.)
// 2007-03-24 Andreas Mayer
// - will now spin in the same direction in flipped and not flipped views
// 2008-09-03 Andreas Mayer
// - restore default settings for NSBezierPath after drawing
// - instead of the saturation, we now modify the lines' opacity; does look better on colored
// backgrounds
#import "AMIndeterminateProgressIndicatorCell.h"
#define ConvertAngle(a) (fmod((90.0-(a)), 360.0))
#define DEG2RAD 0.017453292519943295
@implementation AMIndeterminateProgressIndicatorCell
- (id)init
{
if (self = [super initImageCell:nil]) {
[self setAnimationDelay:5.0/60.0];
[self setDisplayedWhenStopped:YES];
[self setDoubleValue:0.0];
[self setColor:[NSColor blackColor]];
}
return self;
}
- (NSColor *)color
{
return color;
}
- (void)setColor:(NSColor *)value
{
CGFloat alphaComponent;
if (color != value) {
color = value;
[[color colorUsingColorSpaceName:@"NSCalibratedRGBColorSpace"] getRed:&redComponent green:&greenComponent blue:&blueComponent alpha:&alphaComponent];
//NSAssert((alphaComponent > 0.999), @"color must be opaque");
}
}
- (double)doubleValue
{
return doubleValue;
}
- (void)setDoubleValue:(double)value
{
if (doubleValue != value) {
doubleValue = value;
if (doubleValue > 1.0) {
doubleValue = 1.0;
} else if (doubleValue < 0.0) {
doubleValue = 0.0;
}
}
}
- (NSTimeInterval)animationDelay
{
return animationDelay;
}
- (void)setAnimationDelay:(NSTimeInterval)value
{
if (animationDelay != value) {
animationDelay = value;
}
}
- (BOOL)isDisplayedWhenStopped
{
return displayedWhenStopped;
}
- (void)setDisplayedWhenStopped:(BOOL)value
{
if (displayedWhenStopped != value) {
displayedWhenStopped = value;
}
}
- (BOOL)isSpinning
{
return spinning;
}
- (void)setSpinning:(BOOL)value
{
if (spinning != value) {
spinning = value;
}
}
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
// cell has no border
[self drawInteriorWithFrame:cellFrame inView:controlView];
}
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
//NSLog(@"cellFrame: %f %f %f %f", cellFrame.origin.x, cellFrame.origin.y, cellFrame.size.width, cellFrame.size.height);
if ([self isSpinning] || [self isDisplayedWhenStopped]) {
float flipFactor = ([controlView isFlipped] ? 1.0 : -1.0);
int step = round([self doubleValue]/(5.0/60.0));
float cellSize = MIN(cellFrame.size.width, cellFrame.size.height);
NSPoint center = cellFrame.origin;
center.x += cellSize/2.0;
center.y += cellFrame.size.height/2.0;
float outerRadius;
float innerRadius;
float strokeWidth = cellSize*0.08;
if (cellSize >= 32.0) {
outerRadius = cellSize*0.38;
innerRadius = cellSize*0.23;
} else {
outerRadius = cellSize*0.48;
innerRadius = cellSize*0.27;
}
float a; // angle
NSPoint inner;
NSPoint outer;
// remember defaults
NSLineCapStyle previousLineCapStyle = [NSBezierPath defaultLineCapStyle];
float previousLineWidth = [NSBezierPath defaultLineWidth];
// new defaults for our loop
[NSBezierPath setDefaultLineCapStyle:NSRoundLineCapStyle];
[NSBezierPath setDefaultLineWidth:strokeWidth];
if ([self isSpinning]) {
a = (270+(step* 30))*DEG2RAD;
} else {
a = 270*DEG2RAD;
}
a = flipFactor*a;
int i;
for (i = 0; i < 12; i++) {
// [[NSColor colorWithCalibratedWhite:MIN(sqrt(i)*0.25, 0.8) alpha:1.0] set];
// [[NSColor colorWithCalibratedWhite:0.0 alpha:1.0-sqrt(i)*0.25] set];
[[NSColor colorWithCalibratedRed:redComponent green:greenComponent blue:blueComponent alpha:1.0-sqrt(i)*0.25] set];
outer = NSMakePoint(center.x+cos(a)*outerRadius, center.y+sin(a)*outerRadius);
inner = NSMakePoint(center.x+cos(a)*innerRadius, center.y+sin(a)*innerRadius);
[NSBezierPath strokeLineFromPoint:inner toPoint:outer];
a -= flipFactor*30*DEG2RAD;
}
// restore previous defaults
[NSBezierPath setDefaultLineCapStyle:previousLineCapStyle];
[NSBezierPath setDefaultLineWidth:previousLineWidth];
}
}
- (void)setObjectValue:(id)value
{
if ([value respondsToSelector:@selector(boolValue)]) {
[self setSpinning:[value boolValue]];
} else {
[self setSpinning:NO];
}
}
@end