Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: chrome/browser/ui/cocoa/spinner_view.mm

Issue 1048733004: Add a Material Design Circular Activity Indicator (Spinner) view for Mac. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Change to mono-color, rounded endings per UX. Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #import "chrome/browser/ui/cocoa/spinner_view.h"
6
7 #import <QuartzCore/QuartzCore.h>
8
9 #include "base/mac/scoped_cftyperef.h"
10 #include "skia/ext/skia_utils_mac.h"
11
12 namespace {
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.
25 }
26
27 @interface SpinnerView () {
28 base::scoped_nsobject<CAAnimationGroup> spinnerAnimation_;
29 CAShapeLayer* shapeLayer_; // Weak.
30 }
31 @end
32
33
34 @implementation SpinnerView
35
36 - (instancetype)initWithFrame:(NSRect)frame {
37 if (self = [super initWithFrame:frame]) {
38 [self setWantsLayer:YES];
39 }
40 return self;
41 }
42
43 - (void)dealloc {
44 [[NSNotificationCenter defaultCenter] removeObserver:self];
45 [super dealloc];
46 }
47
48 // Overridden to return a custom CALayer for the view (called from
49 // setWantsLayer:).
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;
56
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);
66
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];
73
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)];
81
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];
125 }
126
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;
137
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 NSMutableArray* animationValues = [NSMutableArray array];
147 // Begin the lineDashPhase animation just short of the full arc length,
148 // otherwise the arc will be zero length at start.
149 [animationValues addObject:@(-(kArcLength - 0.4) * scaleFactor)];
150 [animationValues addObject:@(0.0)];
151 [firstHalfAnimation setValues:animationValues];
152 NSArray* keyTimes = @[ @(0.0), @(1.0) ];
153 [firstHalfAnimation setKeyTimes:keyTimes];
154 [firstHalfAnimation setDuration:kArcAnimationTime / 2.0];
155 [firstHalfAnimation setRemovedOnCompletion:NO];
156 [firstHalfAnimation setFillMode:kCAFillModeForwards];
157
158 // Create the second half of the arc animation, where it shrinks from full
159 // length back to a short block.
160 base::scoped_nsobject<CAKeyframeAnimation> secondHalfAnimation(
161 [[CAKeyframeAnimation alloc] init]);
162 [secondHalfAnimation setTimingFunction:timingFunction];
163 [secondHalfAnimation setKeyPath:@"lineDashPhase"];
164 animationValues = [NSMutableArray array];
165 [animationValues addObject:@(0.0)];
166 // Stop the lineDashPhase animation just before it reaches the full arc
167 // length, otherwise the arc will be zero length at the end.
168 [animationValues addObject:@((kArcLength - 0.3) * scaleFactor)];
169 [secondHalfAnimation setValues:animationValues];
170 [secondHalfAnimation setKeyTimes:keyTimes];
171 [secondHalfAnimation setDuration:kArcAnimationTime / 2.0];
172 [secondHalfAnimation setRemovedOnCompletion:NO];
173 [secondHalfAnimation setFillMode:kCAFillModeForwards];
174
175 // Make four copies of the arc animations, to cover the four complete cycles
176 // of the full animation.
177 NSMutableArray* animations = [NSMutableArray array];
178 CGFloat beginTime = 0;
179 for (NSUInteger i = 0; i < 4; i++, beginTime += kArcAnimationTime) {
180 [firstHalfAnimation setBeginTime:beginTime];
181 [secondHalfAnimation setBeginTime:beginTime + kArcAnimationTime / 2.0];
182 [animations addObject:firstHalfAnimation];
183 [animations addObject:secondHalfAnimation];
184 firstHalfAnimation.reset([firstHalfAnimation copy]);
185 secondHalfAnimation.reset([secondHalfAnimation copy]);
186 }
187
188 // Create the rotation animation, which rotates the arc 360 degrees on each
189 // cycle. The animation also includes a separate 90 degree rotation in the
190 // opposite direction at the very end of each cycle. Ignoring the 360 degree
191 // rotation, each arc starts as a short block at degree 0 and ends as a short
192 // block at degree 270. Without a 90 degree rotation at the end of each cycle,
193 // the short block would appear to suddenly jump from 270 degrees to 360
194 // degrees. The full animation has to contain four of these -90 degree
195 // adjustments in order for the arc to return to its starting point, at which
196 // point the full animation can smoothly repeat.
197 CAKeyframeAnimation* rotationAnimation = [CAKeyframeAnimation animation];
198 [rotationAnimation setTimingFunction:
199 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
200 [rotationAnimation setKeyPath:@"transform.rotation"];
201 animationValues = [NSMutableArray array];
202 // Use a key frame animation to rotate 360 degrees on each cycle, and then
203 // jump back 90 degrees at the end of each cycle.
204 [animationValues addObject:@(0.0)];
205 [animationValues addObject:@(-1 * kDegrees360)];
206 [animationValues addObject:@(-1.0 * kDegrees360 + kDegrees90)];
207 [animationValues addObject:@(-2.0 * kDegrees360 + kDegrees90)];
208 [animationValues addObject:@(-2.0 * kDegrees360 + kDegrees180)];
209 [animationValues addObject:@(-3.0 * kDegrees360 + kDegrees180)];
210 [animationValues addObject:@(-3.0 * kDegrees360 + kDegrees270)];
211 [animationValues addObject:@(-4.0 * kDegrees360 + kDegrees270)];
212 [rotationAnimation setValues:animationValues];
213 keyTimes = @[ @(0.0), @(0.25), @(0.25), @(0.5), @(0.5), @(0.75), @(0.75),
214 @(1.0)];
215 [rotationAnimation setKeyTimes:keyTimes];
216 [rotationAnimation setDuration:kArcAnimationTime * 4.0];
217 [rotationAnimation setRemovedOnCompletion:NO];
218 [rotationAnimation setFillMode:kCAFillModeForwards];
219 [rotationAnimation setRepeatCount:HUGE_VALF];
220 [animations addObject:rotationAnimation];
221
222 // Use an animation group so that the animations are easier to manage, and to
223 // give them the best chance of firing synchronously.
224 CAAnimationGroup* group = [CAAnimationGroup animation];
225 [group setDuration:kArcAnimationTime * 4];
226 [group setRepeatCount:HUGE_VALF];
227 [group setFillMode:kCAFillModeForwards];
228 [group setRemovedOnCompletion:NO];
229 [group setAnimations:animations];
230
231 spinnerAnimation_.reset([group retain]);
232 }
233
234 - (void)updateAnimation:(NSNotification*)notification {
235 // Only animate the spinner if it's within a window, and that window is not
236 // currently minimized or being minimized.
237 if ([self window] && ![[self window] isMiniaturized] && ![self isHidden] &&
238 ![[notification name] isEqualToString:
239 NSWindowWillMiniaturizeNotification]) {
240 if (spinnerAnimation_.get() == nil) {
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;
252 }
253 }
254
255 @end
OLDNEW
« no previous file with comments | « chrome/browser/ui/cocoa/spinner_view.h ('k') | chrome/browser/ui/cocoa/spinner_view_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698