OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "spinner_view.h" | 5 #import "chrome/browser/ui/cocoa/spinner_view.h" |
6 | 6 |
7 #import <QuartzCore/QuartzCore.h> | 7 #import <QuartzCore/QuartzCore.h> |
8 | 8 |
9 #include "base/mac/scoped_cftyperef.h" | 9 #include "base/mac/scoped_cftyperef.h" |
10 #include "base/mac/scoped_nsobject.h" | |
11 #include "skia/ext/skia_utils_mac.h" | 10 #include "skia/ext/skia_utils_mac.h" |
12 | 11 |
13 namespace { | 12 namespace { |
14 const CGFloat k90_Degrees = (M_PI / 2); | 13 const CGFloat kDegrees90 = (M_PI / 2); |
15 const CGFloat k180_Degrees = (M_PI); | 14 const CGFloat kDegrees180 = (M_PI); |
16 const CGFloat k270_Degrees = (3 * M_PI / 2); | 15 const CGFloat kDegrees270 = (3 * M_PI / 2); |
17 const CGFloat k360_Degrees = (2 * M_PI); | 16 const CGFloat kDegrees360 = (2 * M_PI); |
18 const CGFloat kDesign_Width = 28.0; | 17 const CGFloat kDesignWidth = 28.0; |
19 const CGFloat kArc_Radius = 12.5; | 18 const CGFloat kArcRadius = 12.5; |
20 const CGFloat kArc_Length = 58.9; | 19 const CGFloat kArcLength = 58.9; |
21 const CGFloat kArc_Stroke_Width = 3.0; | 20 const CGFloat kArcStrokeWidth = 3.0; |
22 const CGFloat kArc_Animation_Time = 1.333; | 21 const CGFloat kArcAnimationTime = 1.333; |
23 const CGFloat kArc_Start_Angle = k180_Degrees; | 22 const CGFloat kArcStartAngle = kDegrees180; |
24 const CGFloat kArc_End_Angle = (kArc_Start_Angle + k270_Degrees); | 23 const CGFloat kArcEndAngle = (kArcStartAngle + kDegrees270); |
25 | 24 const SkColor kBlue = SkColorSetRGB(66.0, 133.0, 244.0); // #4285f4. |
26 const SkColor kBlue = SkColorSetRGB(66.0, 133.0, 244.0); // #4285f4. | |
27 const SkColor kRed = SkColorSetRGB(219.0, 68.0, 55.0); // #db4437. | |
28 const SkColor kYellow = SkColorSetRGB(244.0, 180.0, 0.0); // #f4b400. | |
29 const SkColor kGreen = SkColorSetRGB(15.0, 157.0, 88.0); // #0f9d58. | |
30 } | 25 } |
31 | 26 |
32 @interface SpinnerView() | 27 @interface SpinnerView () { |
33 { | 28 base::scoped_nsobject<CAAnimationGroup> spinnerAnimation_; |
34 base::scoped_nsobject<CAAnimationGroup> spinner_animation_; | 29 CAShapeLayer* shapeLayer_; // Weak. |
35 CAShapeLayer* shape_layer_; // Weak. | |
36 } | 30 } |
37 @end | 31 @end |
38 | 32 |
39 | 33 |
40 @implementation SpinnerView | 34 @implementation SpinnerView |
41 | 35 |
42 - (instancetype)initWithFrame:(NSRect)frame { | 36 - (instancetype)initWithFrame:(NSRect)frame { |
43 if (self = [super initWithFrame:frame]) { | 37 if (self = [super initWithFrame:frame]) { |
44 [self setWantsLayer:YES]; | 38 [self setWantsLayer:YES]; |
45 } | 39 } |
46 return self; | 40 return self; |
47 } | 41 } |
48 | 42 |
49 - (void)dealloc { | 43 - (void)dealloc { |
50 [[NSNotificationCenter defaultCenter] removeObserver:self]; | 44 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
51 [super dealloc]; | 45 [super dealloc]; |
52 } | 46 } |
53 | 47 |
54 // Return a custom CALayer for the view (called from setWantsLayer:). | 48 // Overridden to return a custom CALayer for the view (called from |
55 - (CALayer *)makeBackingLayer { | 49 // setWantsLayer:). |
50 - (CALayer*)makeBackingLayer { | |
56 CGRect bounds = [self bounds]; | 51 CGRect bounds = [self bounds]; |
57 // The spinner was designed to be |kDesign_Width| points wide. Compute the | 52 // The spinner was designed to be |kDesignWidth| points wide. Compute the |
58 // scale factor needed to scale design parameters like |RADIUS| so that the | 53 // scale factor needed to scale design parameters like |RADIUS| so that the |
59 // spinner scales to fit the view's bounds. | 54 // spinner scales to fit the view's bounds. |
60 CGFloat scale_factor = bounds.size.width / kDesign_Width; | 55 CGFloat scaleFactor = bounds.size.width / kDesignWidth; |
61 | 56 |
62 shape_layer_ = [CAShapeLayer layer]; | 57 shapeLayer_ = [CAShapeLayer layer]; |
63 [shape_layer_ setBounds:bounds]; | 58 [shapeLayer_ setBounds:bounds]; |
64 [shape_layer_ setLineWidth:kArc_Stroke_Width * scale_factor]; | 59 [shapeLayer_ setLineWidth:kArcStrokeWidth * scaleFactor]; |
65 [shape_layer_ setLineCap:kCALineCapSquare]; | 60 [shapeLayer_ setLineCap:kCALineCapRound]; |
66 [shape_layer_ setLineDashPattern:[NSArray arrayWithObject: | 61 [shapeLayer_ setLineDashPattern:@[ @(kArcLength * scaleFactor) ]]; |
67 [NSNumber numberWithFloat:kArc_Length * scale_factor]]]; | 62 [shapeLayer_ setFillColor:NULL]; |
68 [shape_layer_ setFillColor:NULL]; | 63 CGColorRef blueColor = gfx::CGColorCreateFromSkColor(kBlue); |
64 [shapeLayer_ setStrokeColor:blueColor]; | |
65 CGColorRelease(blueColor); | |
69 | 66 |
70 // Create the arc that, when stroked, creates the spinner. | 67 // Create the arc that, when stroked, creates the spinner. |
71 base::ScopedCFTypeRef<CGMutablePathRef> shape_path(CGPathCreateMutable()); | 68 base::ScopedCFTypeRef<CGMutablePathRef> shapePath(CGPathCreateMutable()); |
72 CGPathAddArc(shape_path, NULL, bounds.size.width / 2.0, | 69 CGPathAddArc(shapePath, NULL, bounds.size.width / 2.0, |
73 bounds.size.height / 2.0, kArc_Radius * scale_factor, | 70 bounds.size.height / 2.0, kArcRadius * scaleFactor, |
74 kArc_Start_Angle, kArc_End_Angle, 0); | 71 kArcStartAngle, kArcEndAngle, 0); |
75 [shape_layer_ setPath:shape_path]; | 72 [shapeLayer_ setPath:shapePath]; |
76 | 73 |
77 // Place |shape_layer_| in a parent layer so that it's easy to rotate | 74 // Place |shapeLayer_| in a parent layer so that it's easy to rotate |
78 // |shape_layer_| around the center of the view. | 75 // |shapeLayer_| around the center of the view. |
79 CALayer* parent_layer = [CALayer layer]; | 76 CALayer* parentLayer = [CALayer layer]; |
80 [parent_layer setBounds:bounds]; | 77 [parentLayer setBounds:bounds]; |
81 [parent_layer addSublayer:shape_layer_]; | 78 [parentLayer addSublayer:shapeLayer_]; |
82 [shape_layer_ setPosition:CGPointMake(bounds.size.width / 2.0, | 79 [shapeLayer_ setPosition:CGPointMake(bounds.size.width / 2.0, |
83 bounds.size.height / 2.0)]; | 80 bounds.size.height / 2.0)]; |
84 | 81 |
85 return parent_layer; | 82 return parentLayer; |
83 } | |
84 | |
85 // Overridden to start or stop the animation whenever the view is unhidden or | |
86 // hidden. | |
87 - (void)setHidden:(BOOL)flag { | |
88 [super setHidden:flag]; | |
89 [self updateAnimation:nil]; | |
90 } | |
91 | |
92 // Register/unregister for window miniaturization event notifications so that | |
93 // the spinner can stop animating if the window is minaturized | |
94 // (i.e. not visible). | |
95 - (void)viewWillMoveToWindow:(NSWindow*)newWindow { | |
96 if ([self window]) { | |
97 [[NSNotificationCenter defaultCenter] | |
98 removeObserver:self | |
99 name:NSWindowWillMiniaturizeNotification | |
100 object:[self window]]; | |
101 [[NSNotificationCenter defaultCenter] | |
102 removeObserver:self | |
103 name:NSWindowDidDeminiaturizeNotification | |
104 object:[self window]]; | |
105 } | |
106 | |
107 if (newWindow) { | |
108 [[NSNotificationCenter defaultCenter] | |
109 addObserver:self | |
110 selector:@selector(updateAnimation:) | |
111 name:NSWindowWillMiniaturizeNotification | |
112 object:newWindow]; | |
113 [[NSNotificationCenter defaultCenter] | |
114 addObserver:self | |
115 selector:@selector(updateAnimation:) | |
116 name:NSWindowDidDeminiaturizeNotification | |
117 object:newWindow]; | |
118 } | |
119 } | |
120 | |
121 // Start or stop the animation whenever the view is added to or removed from a | |
122 // window. | |
123 - (void)viewDidMoveToWindow { | |
124 [self updateAnimation:nil]; | |
86 } | 125 } |
87 | 126 |
88 // The spinner animation consists of four cycles that it continuously repeats. | 127 // The spinner animation consists of four cycles that it continuously repeats. |
89 // Each cycle consists of one complete rotation of the spinner's arc drawn in | 128 // Each cycle consists of one complete rotation of the spinner's arc plus a |
90 // blue, red, yellow, or green. The arc's length also grows and shrinks over the | 129 // rotation adjustment at the end of each cycle (see rotation animation comment |
91 // course of each cycle, which the spinner achieves by drawing the arc using | 130 // below for the reason for the rotation adjustment and four-cycle length of |
92 // a (solid) dashed line pattern and animating the "lineDashPhase" property. | 131 // the full animation). The arc's length also grows and shrinks over the course |
132 // of each cycle, which the spinner achieves by drawing the arc using a (solid) | |
133 // dashed line pattern and animating the "lineDashPhase" property. | |
93 - (void)initializeAnimation { | 134 - (void)initializeAnimation { |
94 CGRect bounds = [self bounds]; | 135 CGRect bounds = [self bounds]; |
95 CGFloat scale_factor = bounds.size.width / kDesign_Width; | 136 CGFloat scaleFactor = bounds.size.width / kDesignWidth; |
96 | 137 |
97 // Create the first half of the arc animation, where it grows from a short | 138 // Create the first half of the arc animation, where it grows from a short |
98 // block to its full length. | 139 // block to its full length. |
99 base::scoped_nsobject<CAMediaTimingFunction> timing_function( | 140 base::scoped_nsobject<CAMediaTimingFunction> timingFunction( |
100 [[CAMediaTimingFunction alloc] initWithControlPoints:0.4 :0.0 :0.2 :1]); | 141 [[CAMediaTimingFunction alloc] initWithControlPoints:0.4 :0.0 :0.2 :1]); |
101 base::scoped_nsobject<CAKeyframeAnimation> first_half_animation( | 142 base::scoped_nsobject<CAKeyframeAnimation> firstHalfAnimation( |
102 [[CAKeyframeAnimation alloc] init]); | 143 [[CAKeyframeAnimation alloc] init]); |
103 [first_half_animation setTimingFunction:timing_function]; | 144 [firstHalfAnimation setTimingFunction:timingFunction]; |
104 [first_half_animation setKeyPath:@"lineDashPhase"]; | 145 [firstHalfAnimation setKeyPath:@"lineDashPhase"]; |
105 NSMutableArray* animation_values = [NSMutableArray array]; | 146 NSMutableArray* animationValues = [NSMutableArray array]; |
106 // Begin the lineDashPhase animation just short of the full arc length, | 147 // Begin the lineDashPhase animation just short of the full arc length, |
107 // otherwise the arc will be zero length at start. | 148 // otherwise the arc will be zero length at start. |
108 [animation_values addObject: | 149 [animationValues addObject:@(-(kArcLength - 0.4) * scaleFactor)]; |
Robert Sesek
2015/04/03 21:00:05
It looks like animationValues need not be mutable
shrike
2015/04/03 21:11:33
Done.
| |
109 [NSNumber numberWithFloat:-(kArc_Length - 0.2) * scale_factor]]; | 150 [animationValues addObject:@(0.0)]; |
110 [animation_values addObject:[NSNumber numberWithFloat:0.0]]; | 151 [firstHalfAnimation setValues:animationValues]; |
111 [first_half_animation setValues:animation_values]; | 152 NSArray* keyTimes = @[ @(0.0), @(1.0) ]; |
112 NSMutableArray* key_times = [NSMutableArray array]; | 153 [firstHalfAnimation setKeyTimes:keyTimes]; |
113 [key_times addObject:[NSNumber numberWithFloat:0.0]]; | 154 [firstHalfAnimation setDuration:kArcAnimationTime / 2.0]; |
114 [key_times addObject:[NSNumber numberWithFloat:1.0]]; | 155 [firstHalfAnimation setRemovedOnCompletion:NO]; |
115 [first_half_animation setKeyTimes:key_times]; | 156 [firstHalfAnimation setFillMode:kCAFillModeForwards]; |
116 [first_half_animation setDuration:kArc_Animation_Time / 2.0]; | |
117 [first_half_animation setRemovedOnCompletion:NO]; | |
118 [first_half_animation setFillMode:kCAFillModeForwards]; | |
119 | 157 |
120 // Create the second half of the arc animation, where it shrinks from full | 158 // Create the second half of the arc animation, where it shrinks from full |
121 // length back to a short block. | 159 // length back to a short block. |
122 base::scoped_nsobject<CAKeyframeAnimation> second_half_animation( | 160 base::scoped_nsobject<CAKeyframeAnimation> secondHalfAnimation( |
123 [[CAKeyframeAnimation alloc] init]); | 161 [[CAKeyframeAnimation alloc] init]); |
124 [second_half_animation setTimingFunction:timing_function]; | 162 [secondHalfAnimation setTimingFunction:timingFunction]; |
125 [second_half_animation setKeyPath:@"lineDashPhase"]; | 163 [secondHalfAnimation setKeyPath:@"lineDashPhase"]; |
126 animation_values = [NSMutableArray array]; | 164 animationValues = [NSMutableArray array]; |
127 [animation_values addObject:[NSNumber numberWithFloat:0.0]]; | 165 [animationValues addObject:@(0.0)]; |
Robert Sesek
2015/04/03 21:00:05
Same, animationValues = @[ @(0.0) ];
shrike
2015/04/03 21:11:33
Done.
| |
128 // Stop the lineDashPhase animation just before it reaches the full arc | 166 // Stop the lineDashPhase animation just before it reaches the full arc |
129 // length, otherwise the arc will be zero length at the end. | 167 // length, otherwise the arc will be zero length at the end. |
130 [animation_values addObject: | 168 [animationValues addObject:@((kArcLength - 0.3) * scaleFactor)]; |
131 [NSNumber numberWithFloat:(kArc_Length - 0.3) * scale_factor]]; | 169 [secondHalfAnimation setValues:animationValues]; |
132 [second_half_animation setValues:animation_values]; | 170 [secondHalfAnimation setKeyTimes:keyTimes]; |
133 [second_half_animation setKeyTimes:key_times]; | 171 [secondHalfAnimation setDuration:kArcAnimationTime / 2.0]; |
134 [second_half_animation setDuration:kArc_Animation_Time / 2.0]; | 172 [secondHalfAnimation setRemovedOnCompletion:NO]; |
135 [second_half_animation setRemovedOnCompletion:NO]; | 173 [secondHalfAnimation setFillMode:kCAFillModeForwards]; |
136 [second_half_animation setFillMode:kCAFillModeForwards]; | |
137 | 174 |
138 // Make four copies of the arc animations, to cover the four complete cycles | 175 // Make four copies of the arc animations, to cover the four complete cycles |
139 // of the full animation. | 176 // of the full animation. |
140 NSMutableArray* animations = [NSMutableArray array]; | 177 NSMutableArray* animations = [NSMutableArray array]; |
141 NSUInteger i; | 178 CGFloat beginTime = 0; |
142 CGFloat begin_time = 0; | 179 for (NSUInteger i = 0; i < 4; i++, beginTime += kArcAnimationTime) { |
143 for (i = 0; i < 4; i++, begin_time += kArc_Animation_Time) { | 180 [firstHalfAnimation setBeginTime:beginTime]; |
144 [first_half_animation setBeginTime:begin_time]; | 181 [secondHalfAnimation setBeginTime:beginTime + kArcAnimationTime / 2.0]; |
145 [second_half_animation setBeginTime:begin_time + kArc_Animation_Time / 2.0]; | 182 [animations addObject:firstHalfAnimation]; |
146 [animations addObject:first_half_animation]; | 183 [animations addObject:secondHalfAnimation]; |
147 [animations addObject:second_half_animation]; | 184 firstHalfAnimation.reset([firstHalfAnimation copy]); |
148 first_half_animation.reset([first_half_animation copy]); | 185 secondHalfAnimation.reset([secondHalfAnimation copy]); |
149 second_half_animation.reset([second_half_animation copy]); | |
150 } | 186 } |
151 | 187 |
152 // Create the rotation animation, which rotates the arc 360 degrees on each | 188 // Create the rotation animation, which rotates the arc 360 degrees on each |
153 // cycle. The animation also includes a separate 90 degree rotation in the | 189 // cycle. The animation also includes a separate 90 degree rotation in the |
154 // opposite direction at the very end of each cycle. Ignoring the 360 degree | 190 // opposite direction at the very end of each cycle. Ignoring the 360 degree |
155 // rotation, each arc starts as a short block at degree 0 and ends as a | 191 // rotation, each arc starts as a short block at degree 0 and ends as a short |
156 // short block at degree 270. Without a 90 degree rotation at the end of each | 192 // block at degree 270. Without a 90 degree rotation at the end of each cycle, |
157 // cycle, the short block would appear to suddenly jump from 270 degrees to | 193 // the short block would appear to suddenly jump from 270 degrees to 360 |
158 // 360 degrees. | 194 // degrees. The full animation has to contain four of these -90 degree |
159 CAKeyframeAnimation *rotation_animation = [CAKeyframeAnimation animation]; | 195 // adjustments in order for the arc to return to its starting point, at which |
160 [rotation_animation setTimingFunction: | 196 // point the full animation can smoothly repeat. |
197 CAKeyframeAnimation* rotationAnimation = [CAKeyframeAnimation animation]; | |
198 [rotationAnimation setTimingFunction: | |
161 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]]; | 199 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]]; |
162 [rotation_animation setKeyPath:@"transform.rotation"]; | 200 [rotationAnimation setKeyPath:@"transform.rotation"]; |
163 animation_values = [NSMutableArray array]; | 201 animationValues = [NSMutableArray array]; |
164 // Use a key frame animation to rotate 360 degrees on each cycle, and then | 202 // Use a key frame animation to rotate 360 degrees on each cycle, and then |
165 // jump back 90 degrees at the end of each cycle. | 203 // jump back 90 degrees at the end of each cycle. |
166 [animation_values addObject:[NSNumber numberWithFloat:0.0]]; | 204 [animationValues addObject:@(0.0)]; |
Robert Sesek
2015/04/03 21:00:05
Again, use @[]
shrike
2015/04/03 21:11:33
Done.
| |
167 [animation_values addObject:[NSNumber numberWithFloat:-1 * k360_Degrees]]; | 205 [animationValues addObject:@(-1 * kDegrees360)]; |
168 [animation_values addObject: | 206 [animationValues addObject:@(-1.0 * kDegrees360 + kDegrees90)]; |
169 [NSNumber numberWithFloat:-1 * k360_Degrees + k90_Degrees]]; | 207 [animationValues addObject:@(-2.0 * kDegrees360 + kDegrees90)]; |
170 [animation_values addObject: | 208 [animationValues addObject:@(-2.0 * kDegrees360 + kDegrees180)]; |
171 [NSNumber numberWithFloat:-2 * k360_Degrees + k90_Degrees]]; | 209 [animationValues addObject:@(-3.0 * kDegrees360 + kDegrees180)]; |
172 [animation_values addObject: | 210 [animationValues addObject:@(-3.0 * kDegrees360 + kDegrees270)]; |
173 [NSNumber numberWithFloat:-2 * k360_Degrees + k180_Degrees]]; | 211 [animationValues addObject:@(-4.0 * kDegrees360 + kDegrees270)]; |
174 [animation_values addObject: | 212 [rotationAnimation setValues:animationValues]; |
175 [NSNumber numberWithFloat:-3 * k360_Degrees + k180_Degrees]]; | 213 keyTimes = @[ @(0.0), @(0.25), @(0.25), @(0.5), @(0.5), @(0.75), @(0.75), |
176 [animation_values addObject: | 214 @(1.0)]; |
177 [NSNumber numberWithFloat:-3 * k360_Degrees + k270_Degrees]]; | 215 [rotationAnimation setKeyTimes:keyTimes]; |
178 [animation_values addObject: | 216 [rotationAnimation setDuration:kArcAnimationTime * 4.0]; |
179 [NSNumber numberWithFloat:-4 * k360_Degrees + k270_Degrees]]; | 217 [rotationAnimation setRemovedOnCompletion:NO]; |
180 [rotation_animation setValues:animation_values]; | 218 [rotationAnimation setFillMode:kCAFillModeForwards]; |
181 key_times = [NSMutableArray array]; | 219 [rotationAnimation setRepeatCount:HUGE_VALF]; |
182 [key_times addObject:[NSNumber numberWithFloat:0.0]]; | 220 [animations addObject:rotationAnimation]; |
183 [key_times addObject:[NSNumber numberWithFloat:0.25]]; | |
184 [key_times addObject:[NSNumber numberWithFloat:0.25]]; | |
185 [key_times addObject:[NSNumber numberWithFloat:0.5]]; | |
186 [key_times addObject:[NSNumber numberWithFloat:0.5]]; | |
187 [key_times addObject:[NSNumber numberWithFloat:0.75]]; | |
188 [key_times addObject:[NSNumber numberWithFloat:0.75]]; | |
189 [key_times addObject:[NSNumber numberWithFloat:1.0]]; | |
190 [rotation_animation setKeyTimes:key_times]; | |
191 [rotation_animation setDuration:kArc_Animation_Time * 4.0]; | |
192 [rotation_animation setRemovedOnCompletion:NO]; | |
193 [rotation_animation setFillMode:kCAFillModeForwards]; | |
194 [rotation_animation setRepeatCount:HUGE_VALF]; | |
195 [animations addObject:rotation_animation]; | |
196 | |
197 // Create a four-cycle-long key frame animation to transition between | |
198 // successive colors at the end of each cycle. | |
199 CAKeyframeAnimation *color_animation = [CAKeyframeAnimation animation]; | |
200 color_animation.timingFunction = | |
201 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; | |
202 color_animation.keyPath = @"strokeColor"; | |
203 CGColorRef blueColor = gfx::CGColorCreateFromSkColor(kBlue); | |
204 CGColorRef redColor = gfx::CGColorCreateFromSkColor(kRed); | |
205 CGColorRef yellowColor = gfx::CGColorCreateFromSkColor(kYellow); | |
206 CGColorRef greenColor = gfx::CGColorCreateFromSkColor(kGreen); | |
207 animation_values = [NSMutableArray array]; | |
208 [animation_values addObject:(id)blueColor]; | |
209 [animation_values addObject:(id)blueColor]; | |
210 [animation_values addObject:(id)redColor]; | |
211 [animation_values addObject:(id)redColor]; | |
212 [animation_values addObject:(id)yellowColor]; | |
213 [animation_values addObject:(id)yellowColor]; | |
214 [animation_values addObject:(id)greenColor]; | |
215 [animation_values addObject:(id)greenColor]; | |
216 [animation_values addObject:(id)blueColor]; | |
217 [color_animation setValues:animation_values]; | |
218 CGColorRelease(blueColor); | |
219 CGColorRelease(redColor); | |
220 CGColorRelease(yellowColor); | |
221 CGColorRelease(greenColor); | |
222 key_times = [NSMutableArray array]; | |
223 // Begin the transition bewtween colors at T - 10% of the cycle. | |
224 const CGFloat transition_offset = 0.1 * 0.25; | |
225 [key_times addObject:[NSNumber numberWithFloat:0.0]]; | |
226 [key_times addObject:[NSNumber numberWithFloat:0.25 - transition_offset]]; | |
227 [key_times addObject:[NSNumber numberWithFloat:0.25]]; | |
228 [key_times addObject:[NSNumber numberWithFloat:0.50 - transition_offset]]; | |
229 [key_times addObject:[NSNumber numberWithFloat:0.5]]; | |
230 [key_times addObject:[NSNumber numberWithFloat:0.75 - transition_offset]]; | |
231 [key_times addObject:[NSNumber numberWithFloat:0.75]]; | |
232 [key_times addObject:[NSNumber numberWithFloat:0.999 - transition_offset]]; | |
233 [key_times addObject:[NSNumber numberWithFloat:0.999]]; | |
234 [color_animation setKeyTimes:key_times]; | |
235 [color_animation setDuration:kArc_Animation_Time * 4.0]; | |
236 [color_animation setRemovedOnCompletion:NO]; | |
237 [color_animation setFillMode:kCAFillModeForwards]; | |
238 [color_animation setRepeatCount:HUGE_VALF]; | |
239 [animations addObject:color_animation]; | |
240 | 221 |
241 // Use an animation group so that the animations are easier to manage, and to | 222 // Use an animation group so that the animations are easier to manage, and to |
242 // give them the best chance of firing synchronously. | 223 // give them the best chance of firing synchronously. |
243 CAAnimationGroup* group = [CAAnimationGroup animation]; | 224 CAAnimationGroup* group = [CAAnimationGroup animation]; |
244 [group setDuration:kArc_Animation_Time * 4]; | 225 [group setDuration:kArcAnimationTime * 4]; |
245 [group setRepeatCount:HUGE_VALF]; | 226 [group setRepeatCount:HUGE_VALF]; |
246 [group setFillMode:kCAFillModeForwards]; | 227 [group setFillMode:kCAFillModeForwards]; |
247 [group setRemovedOnCompletion:NO]; | 228 [group setRemovedOnCompletion:NO]; |
248 [group setAnimations:animations]; | 229 [group setAnimations:animations]; |
249 | 230 |
250 spinner_animation_.reset([group retain]); | 231 spinnerAnimation_.reset([group retain]); |
251 } | 232 } |
252 | 233 |
253 - (void)updateAnimation:(NSNotification*)notification { | 234 - (void)updateAnimation:(NSNotification*)notification { |
254 // Only animate the spinner if it's within a window, and that window is not | 235 // Only animate the spinner if it's within a window, and that window is not |
255 // currently minimized or being minimized. | 236 // currently minimized or being minimized. |
256 if ([self window] && ![[self window] isMiniaturized] && ![self isHidden] && | 237 if ([self window] && ![[self window] isMiniaturized] && ![self isHidden] && |
257 ![[notification name] isEqualToString: | 238 ![[notification name] isEqualToString: |
258 NSWindowWillMiniaturizeNotification]) { | 239 NSWindowWillMiniaturizeNotification]) { |
259 if (spinner_animation_.get() == nil) { | 240 if (spinnerAnimation_.get() == nil) { |
260 [self initializeAnimation]; | 241 [self initializeAnimation]; |
242 } | |
243 // The spinner should never be animating at this point. | |
244 DCHECK(!isAnimating_); | |
245 if (!isAnimating_) { | |
246 [shapeLayer_ addAnimation:spinnerAnimation_.get() forKey:nil]; | |
247 isAnimating_ = true; | |
248 } | |
249 } else { | |
250 [shapeLayer_ removeAllAnimations]; | |
251 isAnimating_ = false; | |
261 } | 252 } |
262 // The spinner should never be animating at this point | |
263 DCHECK(!is_animating_); | |
264 if (!is_animating_) { | |
265 [shape_layer_ addAnimation:spinner_animation_.get() forKey:nil]; | |
266 is_animating_ = true; | |
267 } | |
268 } else { | |
269 [shape_layer_ removeAllAnimations]; | |
270 is_animating_ = false; | |
271 } | |
272 } | 253 } |
273 | 254 |
274 // Register/unregister for window miniaturization event notifications so that | |
275 // the spinner can stop animating if the window is minaturized | |
276 // (i.e. not visible). | |
277 - (void)viewWillMoveToWindow:(NSWindow*)newWindow { | |
278 if ([self window]) { | |
279 [[NSNotificationCenter defaultCenter] | |
280 removeObserver:self | |
281 name:NSWindowWillMiniaturizeNotification | |
282 object:[self window]]; | |
283 [[NSNotificationCenter defaultCenter] | |
284 removeObserver:self | |
285 name:NSWindowDidDeminiaturizeNotification | |
286 object:[self window]]; | |
287 } | |
288 | |
289 if (newWindow) { | |
290 [[NSNotificationCenter defaultCenter] | |
291 addObserver:self | |
292 selector:@selector(updateAnimation:) | |
293 name:NSWindowWillMiniaturizeNotification | |
294 object:newWindow]; | |
295 [[NSNotificationCenter defaultCenter] | |
296 addObserver:self | |
297 selector:@selector(updateAnimation:) | |
298 name:NSWindowDidDeminiaturizeNotification | |
299 object:newWindow]; | |
300 } | |
301 } | |
302 | |
303 // Start or stop the animation whenever the view is added to or removed from a | |
304 // window. | |
305 - (void)viewDidMoveToWindow { | |
306 [self updateAnimation:nil]; | |
307 } | |
308 | |
309 // Start or stop the animation whenever the view is unhidden or hidden. | |
310 - (void)setHidden:(BOOL)flag | |
311 { | |
312 [super setHidden:flag]; | |
313 [self updateAnimation:nil]; | |
314 } | |
315 | |
316 | |
317 @end | 255 @end |
OLD | NEW |