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

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

Powered by Google App Engine
This is Rietveld 408576698