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
3 // found in the LICENSE file.
5 #import "chrome/browser/ui/cocoa/spinner_view.h"
7 #import <QuartzCore/QuartzCore.h>
9 #include "base/mac/scoped_cftyperef.h"
10 #include "skia/ext/skia_utils_mac.h"
13 const CGFloat kDegrees90 = (M_PI / 2);
14 const CGFloat kDegrees180 = (M_PI);
15 const CGFloat kDegrees270 = (3 * M_PI / 2);
16 const CGFloat kDegrees360 = (2 * M_PI);
17 const CGFloat kDesignWidth = 28.0;
18 const CGFloat kArcRadius = 12.5;
19 const CGFloat kArcLength = 58.9;
20 const CGFloat kArcStrokeWidth = 3.0;
21 const CGFloat kArcAnimationTime = 1.333;
22 const CGFloat kArcStartAngle = kDegrees180;
23 const CGFloat kArcEndAngle = (kArcStartAngle + kDegrees270);
24 const SkColor kBlue = SkColorSetRGB(66.0, 133.0, 244.0); // #4285f4.
27 @interface SpinnerView () {
28 base::scoped_nsobject<CAAnimationGroup> spinnerAnimation_;
29 CAShapeLayer* shapeLayer_; // Weak.
34 @implementation SpinnerView
36 - (instancetype)initWithFrame:(NSRect)frame {
37 if (self = [super initWithFrame:frame]) {
38 [self setWantsLayer:YES];
44 [[NSNotificationCenter defaultCenter] removeObserver:self];
48 // Overridden to return a custom CALayer for the view (called from
50 - (CALayer*)makeBackingLayer {
51 CGRect bounds = [self bounds];
52 // The spinner was designed to be |kDesignWidth| points wide. Compute the
53 // scale factor needed to scale design parameters like |RADIUS| so that the
54 // spinner scales to fit the view's bounds.
55 CGFloat scaleFactor = bounds.size.width / kDesignWidth;
57 shapeLayer_ = [CAShapeLayer layer];
58 [shapeLayer_ setBounds:bounds];
59 [shapeLayer_ setLineWidth:kArcStrokeWidth * scaleFactor];
60 [shapeLayer_ setLineCap:kCALineCapRound];
61 [shapeLayer_ setLineDashPattern:@[ @(kArcLength * scaleFactor) ]];
62 [shapeLayer_ setFillColor:NULL];
63 CGColorRef blueColor = gfx::CGColorCreateFromSkColor(kBlue);
64 [shapeLayer_ setStrokeColor:blueColor];
65 CGColorRelease(blueColor);
67 // Create the arc that, when stroked, creates the spinner.
68 base::ScopedCFTypeRef<CGMutablePathRef> shapePath(CGPathCreateMutable());
69 CGPathAddArc(shapePath, NULL, bounds.size.width / 2.0,
70 bounds.size.height / 2.0, kArcRadius * scaleFactor,
71 kArcStartAngle, kArcEndAngle, 0);
72 [shapeLayer_ setPath:shapePath];
74 // Place |shapeLayer_| in a parent layer so that it's easy to rotate
75 // |shapeLayer_| around the center of the view.
76 CALayer* parentLayer = [CALayer layer];
77 [parentLayer setBounds:bounds];
78 [parentLayer addSublayer:shapeLayer_];
79 [shapeLayer_ setPosition:CGPointMake(bounds.size.width / 2.0,
80 bounds.size.height / 2.0)];
85 // Overridden to start or stop the animation whenever the view is unhidden or
87 - (void)setHidden:(BOOL)flag {
88 [super setHidden:flag];
89 [self updateAnimation:nil];
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 {
97 [[NSNotificationCenter defaultCenter]
99 name:NSWindowWillMiniaturizeNotification
100 object:[self window]];
101 [[NSNotificationCenter defaultCenter]
103 name:NSWindowDidDeminiaturizeNotification
104 object:[self window]];
108 [[NSNotificationCenter defaultCenter]
110 selector:@selector(updateAnimation:)
111 name:NSWindowWillMiniaturizeNotification
113 [[NSNotificationCenter defaultCenter]
115 selector:@selector(updateAnimation:)
116 name:NSWindowDidDeminiaturizeNotification
121 // Start or stop the animation whenever the view is added to or removed from a
123 - (void)viewDidMoveToWindow {
124 [self updateAnimation:nil];
127 // The spinner animation consists of four cycles that it continuously repeats.
128 // Each cycle consists of one complete rotation of the spinner's arc plus a
129 // rotation adjustment at the end of each cycle (see rotation animation comment
130 // below for the reason for the rotation adjustment and four-cycle length of
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.
134 - (void)initializeAnimation {
135 CGRect bounds = [self bounds];
136 CGFloat scaleFactor = bounds.size.width / kDesignWidth;
138 // Create the first half of the arc animation, where it grows from a short
139 // block to its full length.
140 base::scoped_nsobject<CAMediaTimingFunction> timingFunction(
141 [[CAMediaTimingFunction alloc] initWithControlPoints:0.4 :0.0 :0.2 :1]);
142 base::scoped_nsobject<CAKeyframeAnimation> firstHalfAnimation(
143 [[CAKeyframeAnimation alloc] init]);
144 [firstHalfAnimation setTimingFunction:timingFunction];
145 [firstHalfAnimation setKeyPath:@"lineDashPhase"];
146 // Begin the lineDashPhase animation just short of the full arc length,
147 // otherwise the arc will be zero length at start.
148 NSArray* animationValues = @[ @(-(kArcLength - 0.4) * scaleFactor), @(0.0) ];
149 [firstHalfAnimation setValues:animationValues];
150 NSArray* keyTimes = @[ @(0.0), @(1.0) ];
151 [firstHalfAnimation setKeyTimes:keyTimes];
152 [firstHalfAnimation setDuration:kArcAnimationTime / 2.0];
153 [firstHalfAnimation setRemovedOnCompletion:NO];
154 [firstHalfAnimation setFillMode:kCAFillModeForwards];
156 // Create the second half of the arc animation, where it shrinks from full
157 // length back to a short block.
158 base::scoped_nsobject<CAKeyframeAnimation> secondHalfAnimation(
159 [[CAKeyframeAnimation alloc] init]);
160 [secondHalfAnimation setTimingFunction:timingFunction];
161 [secondHalfAnimation setKeyPath:@"lineDashPhase"];
162 // Stop the lineDashPhase animation just before it reaches the full arc
163 // length, otherwise the arc will be zero length at the end.
164 animationValues = @[ @(0.0), @((kArcLength - 0.3) * scaleFactor) ];
165 [secondHalfAnimation setValues:animationValues];
166 [secondHalfAnimation setKeyTimes:keyTimes];
167 [secondHalfAnimation setDuration:kArcAnimationTime / 2.0];
168 [secondHalfAnimation setRemovedOnCompletion:NO];
169 [secondHalfAnimation setFillMode:kCAFillModeForwards];
171 // Make four copies of the arc animations, to cover the four complete cycles
172 // of the full animation.
173 NSMutableArray* animations = [NSMutableArray array];
174 CGFloat beginTime = 0;
175 for (NSUInteger i = 0; i < 4; i++, beginTime += kArcAnimationTime) {
176 [firstHalfAnimation setBeginTime:beginTime];
177 [secondHalfAnimation setBeginTime:beginTime + kArcAnimationTime / 2.0];
178 [animations addObject:firstHalfAnimation];
179 [animations addObject:secondHalfAnimation];
180 firstHalfAnimation.reset([firstHalfAnimation copy]);
181 secondHalfAnimation.reset([secondHalfAnimation copy]);
184 // Create the rotation animation, which rotates the arc 360 degrees on each
185 // cycle. The animation also includes a separate 90 degree rotation in the
186 // opposite direction at the very end of each cycle. Ignoring the 360 degree
187 // rotation, each arc starts as a short block at degree 0 and ends as a short
188 // block at degree 270. Without a 90 degree rotation at the end of each cycle,
189 // the short block would appear to suddenly jump from 270 degrees to 360
190 // degrees. The full animation has to contain four of these -90 degree
191 // adjustments in order for the arc to return to its starting point, at which
192 // point the full animation can smoothly repeat.
193 CAKeyframeAnimation* rotationAnimation = [CAKeyframeAnimation animation];
194 [rotationAnimation setTimingFunction:
195 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
196 [rotationAnimation setKeyPath:@"transform.rotation"];
197 // Use a key frame animation to rotate 360 degrees on each cycle, and then
198 // jump back 90 degrees at the end of each cycle.
199 animationValues = @[ @(0.0), @(-1 * kDegrees360),
200 @(-1.0 * kDegrees360 + kDegrees90),
201 @(-2.0 * kDegrees360 + kDegrees90),
202 @(-2.0 * kDegrees360 + kDegrees180),
203 @(-3.0 * kDegrees360 + kDegrees180),
204 @(-3.0 * kDegrees360 + kDegrees270),
205 @(-4.0 * kDegrees360 + kDegrees270)];
206 [rotationAnimation setValues:animationValues];
207 keyTimes = @[ @(0.0), @(0.25), @(0.25), @(0.5), @(0.5), @(0.75), @(0.75),
209 [rotationAnimation setKeyTimes:keyTimes];
210 [rotationAnimation setDuration:kArcAnimationTime * 4.0];
211 [rotationAnimation setRemovedOnCompletion:NO];
212 [rotationAnimation setFillMode:kCAFillModeForwards];
213 [rotationAnimation setRepeatCount:HUGE_VALF];
214 [animations addObject:rotationAnimation];
216 // Use an animation group so that the animations are easier to manage, and to
217 // give them the best chance of firing synchronously.
218 CAAnimationGroup* group = [CAAnimationGroup animation];
219 [group setDuration:kArcAnimationTime * 4];
220 [group setRepeatCount:HUGE_VALF];
221 [group setFillMode:kCAFillModeForwards];
222 [group setRemovedOnCompletion:NO];
223 [group setAnimations:animations];
225 spinnerAnimation_.reset([group retain]);
228 - (void)updateAnimation:(NSNotification*)notification {
229 // Only animate the spinner if it's within a window, and that window is not
230 // currently minimized or being minimized.
231 if ([self window] && ![[self window] isMiniaturized] && ![self isHidden] &&
232 ![[notification name] isEqualToString:
233 NSWindowWillMiniaturizeNotification]) {
234 if (spinnerAnimation_.get() == nil) {
235 [self initializeAnimation];
237 // The spinner should never be animating at this point.
238 DCHECK(!isAnimating_);
240 [shapeLayer_ addAnimation:spinnerAnimation_.get() forKey:nil];
244 [shapeLayer_ removeAllAnimations];
245 isAnimating_ = false;