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

Side by Side Diff: chrome/browser/ui/cocoa/circular_activity_indicator_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: 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 "circular_activity_indicator_view.h"
6
7 #include "base/mac/scoped_cftyperef.h"
8 #include "base/mac/scoped_nsobject.h"
9
10 #define DEGREES_90 (M_PI / 2)
groby-ooo-7-16 2015/03/30 23:28:31 Please don't #define constants. Chromium customari
shrike 2015/03/31 07:05:53 Acknowledged.
11 #define DEGREES_180 (M_PI)
12 #define DEGREES_270 (3 * M_PI / 2)
13 #define DEGREES_360 (2 * M_PI)
14 #define DESIGN_WIDTH 28.0
15 #define ARC_RADIUS 12.5
16 #define ARC_LENGTH 58.9
17 #define ARC_STROKE_WIDTH 3.0
18 #define ARC_ANIMATION_TIME 1.333
19 #define ARC_START_ANGLE DEGREES_180
20 #define ARC_END_ANGLE (ARC_START_ANGLE + DEGREES_270)
21
22 @implementation CircularActivityIndicatorView
groby-ooo-7-16 2015/03/30 23:28:31 nit: I'd call it "SpinnerView" - that's the common
shrike 2015/03/31 07:05:53 But that's not what the designers call it (per the
groby-ooo-7-16 2015/03/31 23:07:01 Then do ignore me. I had no idea that's the offici
23
24 - (instancetype)initWithFrame:(NSRect)frame {
25 if (self = [super initWithFrame:frame]) {
26 [self setWantsLayer:YES];
27 }
28 return self;
29 }
30
31 - (void)dealloc {
32 [[NSNotificationCenter defaultCenter] removeObserver:self];
33 [super dealloc];
34 }
35
36 // Return a custom CALayer for the view (called from setWantsLayer:).
37 - (CALayer *)makeBackingLayer {
38 CGRect bounds = [self bounds];
39 // The circular activity indicator was designed to be |DESIGN_WIDTH| points
40 // wide. Compute the scale factor needed to scale design parameters like
41 // |RADIUS| so that the activity indicator scales to fit the view's bounds.
42 CGFloat scale_factor = bounds.size.width / DESIGN_WIDTH;
43
44 shape_layer_ = [CAShapeLayer layer];
45 [shape_layer_ setBounds:bounds];
46 [shape_layer_ setLineWidth:ARC_STROKE_WIDTH * scale_factor];
47 [shape_layer_ setLineCap:kCALineCapSquare];
48 [shape_layer_ setLineDashPattern:[NSArray arrayWithObject:
49 [NSNumber numberWithFloat:ARC_LENGTH * scale_factor]]];
50 [shape_layer_ setFillColor:NULL];
51
52 // Create the arc that, when stroked, creates the activity indicator.
53 base::ScopedCFTypeRef<CGMutablePathRef> shape_path(CGPathCreateMutable());
54 CGPathAddArc(shape_path, NULL, bounds.size.width / 2.0,
55 bounds.size.height / 2.0, ARC_RADIUS * scale_factor,
56 ARC_START_ANGLE, ARC_END_ANGLE, 0);
57 [shape_layer_ setPath:shape_path];
58
59 // Place |shape_layer_| in a parent layer so that it's easy to rotate
60 // |shape_layer_| around the center of the view.
61 CALayer* parent_layer = [CALayer layer];
62 [parent_layer setBounds:bounds];
63 [parent_layer addSublayer:shape_layer_];
64 [shape_layer_ setPosition:CGPointMake(bounds.size.width / 2.0,
65 bounds.size.height / 2.0)];
66
67 return parent_layer;
68 }
69
70 // The circular activity indicator animation consists of four cycles that it
71 // continually repeats. Each cycle consists of one complete rotation of the
72 // circular activity indicator's arc drawn in one of four colors (blue, red,
73 // yellow, or green). The arc's length also grows and shrinks over the course
74 // of each cycle, which we achieve by drawing the arc using a (solid) dashed
groby-ooo-7-16 2015/03/30 23:28:31 tiny nit: "we achieve" -> Please do clarify who "w
shrike 2015/03/31 07:05:53 Well, the "we" is actually me, but I didn't figure
groby-ooo-7-16 2015/03/31 23:07:01 There's a huge number of instances because it slip
75 // line pattern and animating the "phase" property.
groby-ooo-7-16 2015/03/30 23:28:30 I assume that's lineDashPhase?
shrike 2015/03/31 07:05:53 Eh. I've changed it, but the APIs that do the actu
76 - (void)initializeAnimation {
77 CGRect bounds = [self bounds];
78 CGFloat scale_factor = bounds.size.width / DESIGN_WIDTH;
79
80 // Create the first half of the arc animation, where it grows from a short
81 // block to its full length.
82 base::scoped_nsobject<CAMediaTimingFunction> timing_function(
83 [[CAMediaTimingFunction alloc] initWithControlPoints:0.4 :0.0 :0.2 :1]);
84 base::scoped_nsobject<CAKeyframeAnimation> first_half_animation(
85 [[CAKeyframeAnimation alloc] init]);
86 [first_half_animation setTimingFunction:timing_function];
87 [first_half_animation setKeyPath:@"lineDashPhase"];
88 NSMutableArray* animation_values = [NSMutableArray array];
89 [animation_values addObject:[NSNumber numberWithFloat:-58.5 * scale_factor]];
90 [animation_values addObject:[NSNumber numberWithFloat:0.0]];
91 [first_half_animation setValues:animation_values];
92 NSMutableArray* key_times = [NSMutableArray array];
93 [key_times addObject:[NSNumber numberWithFloat:0.0]];
94 [key_times addObject:[NSNumber numberWithFloat:1.0]];
95 [first_half_animation setKeyTimes:key_times];
96 [first_half_animation setDuration:ARC_ANIMATION_TIME / 2.0];
97 [first_half_animation setRemovedOnCompletion:NO];
98 [first_half_animation setFillMode:kCAFillModeForwards];
99
100 // Create the second half of the arc animation, where it shrinks from full
101 // length back to a short block.
102 base::scoped_nsobject<CAKeyframeAnimation> second_half_animation(
103 [[CAKeyframeAnimation alloc] init]);
104 [second_half_animation setTimingFunction:timing_function];
105 [second_half_animation setKeyPath:@"lineDashPhase"];
106 animation_values = [NSMutableArray array];
107 [animation_values addObject:[NSNumber numberWithFloat:0.0]];
108 [animation_values addObject:[NSNumber numberWithFloat:58.6 * scale_factor]];
groby-ooo-7-16 2015/03/30 23:28:30 What is 58.6?
shrike 2015/03/31 07:05:53 It's a value that's just short of the full arc len
groby-ooo-7-16 2015/03/31 23:07:01 Awesome, thanks. (The proximity to 57.3 - rad-2-de
109 [second_half_animation setValues:animation_values];
110 [second_half_animation setKeyTimes:key_times];
111 [second_half_animation setDuration:ARC_ANIMATION_TIME / 2.0];
112 [second_half_animation setRemovedOnCompletion:NO];
113 [second_half_animation setFillMode:kCAFillModeForwards];
114
115 // Make four copies of the arc animations, to cover the four complete cycles
116 // of the full animation.
117 NSMutableArray* animations = [NSMutableArray array];
118 NSUInteger i;
119 CGFloat begin_time = 0;
120 for (i = 0; i < 4; i++, begin_time += ARC_ANIMATION_TIME) {
121 [first_half_animation setBeginTime:begin_time];
122 [second_half_animation setBeginTime:begin_time + ARC_ANIMATION_TIME / 2.0];
123 [animations addObject:first_half_animation];
groby-ooo-7-16 2015/03/30 23:28:30 Out of curiosity: Why not [[[first_half_animation]
shrike 2015/03/31 07:05:53 I was thinking that the Chrome codebase frowned up
groby-ooo-7-16 2015/03/31 23:07:01 We're torn on autorelease :) Leaving this as-is i
124 [animations addObject:second_half_animation];
125 first_half_animation.reset([first_half_animation copy]);
126 second_half_animation.reset([second_half_animation copy]);
127 }
128
129 // Create the rotation animation, which rotates the arc 360 degrees on each
130 // cycle. The animation also includes a separate 90 degree rotation in the
131 // opposite direction at the very end of each cycle. Ignoring the 360 degree
132 // rotation, each arc starts as a short block at degree 0 and ends as a
133 // short block at degree 270. Without a 90 degree rotation at the end of each
134 // cycle, the short block would appear to suddenly jump from 270 degrees to
135 // 360 degrees.
136 CAKeyframeAnimation *rotation_animation = [CAKeyframeAnimation animation];
137 [rotation_animation setTimingFunction:
138 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
139 [rotation_animation setKeyPath:@"transform.rotation"];
140 animation_values = [NSMutableArray array];
141 // Use a key frame animation to rotate 360 degrees on each cycle, and then
142 // jump back 90 degrees at the end of each cycle.
143 [animation_values addObject:[NSNumber numberWithFloat:0.0]];
144 [animation_values addObject:[NSNumber numberWithFloat:-1 * DEGREES_360]];
145 [animation_values addObject:
146 [NSNumber numberWithFloat:-1 * DEGREES_360 + DEGREES_90]];
147 [animation_values addObject:
148 [NSNumber numberWithFloat:-2 * DEGREES_360 + DEGREES_90]];
149 [animation_values addObject:
150 [NSNumber numberWithFloat:-2 * DEGREES_360 + DEGREES_180]];
151 [animation_values addObject:
152 [NSNumber numberWithFloat:-3 * DEGREES_360 + DEGREES_180]];
153 [animation_values addObject:
154 [NSNumber numberWithFloat:-3 * DEGREES_360 + DEGREES_270]];
155 [animation_values addObject:
156 [NSNumber numberWithFloat:-4 * DEGREES_360 + DEGREES_270]];
157 [rotation_animation setValues:animation_values];
158 key_times = [NSMutableArray array];
159 [key_times addObject:[NSNumber numberWithFloat:0.0]];
160 [key_times addObject:[NSNumber numberWithFloat:0.25]];
161 [key_times addObject:[NSNumber numberWithFloat:0.25]];
162 [key_times addObject:[NSNumber numberWithFloat:0.5]];
163 [key_times addObject:[NSNumber numberWithFloat:0.5]];
164 [key_times addObject:[NSNumber numberWithFloat:0.75]];
165 [key_times addObject:[NSNumber numberWithFloat:0.75]];
166 [key_times addObject:[NSNumber numberWithFloat:1.0]];
167 [rotation_animation setKeyTimes:key_times];
168 [rotation_animation setDuration:ARC_ANIMATION_TIME * 4.0];
169 [rotation_animation setRemovedOnCompletion:NO];
170 [rotation_animation setFillMode:kCAFillModeForwards];
171 [rotation_animation setRepeatCount:HUGE_VALF];
172 [animations addObject:rotation_animation];
173
174 // Create a four-cycle long key frame animation to transition between
175 // successive colors at the end of each cycle.
176 CAKeyframeAnimation *color_animation = [CAKeyframeAnimation animation];
177 color_animation.timingFunction =
178 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
179 color_animation.keyPath = @"strokeColor";
180 CGColorRef blueColor = [[NSColor colorWithDeviceRed:62.0 / 255.0
groby-ooo-7-16 2015/03/30 23:28:30 It might be worth defining them as constants in th
shrike 2015/03/31 07:05:53 OK.
181 green:130.0 / 255.0
groby-ooo-7-16 2015/03/30 23:42:06 Sorry, forgot that -CGColor doesn't work before 10
shrike 2015/03/31 07:05:53 Done.
182 blue:241.0 / 255.0
183 alpha:1.0] CGColor];
184 CGColorRef redColor = [[NSColor colorWithDeviceRed:232.0 / 255.0
185 green:83.0 / 255.0
186 blue:51.0 / 255.0
187 alpha:1.0] CGColor];
188 CGColorRef yellowColor = [[NSColor colorWithDeviceRed:252.0 / 255.0
189 green:177.0 / 255.0
190 blue:42.0 / 255.0
191 alpha:1.0] CGColor];
192 CGColorRef greenColor = [[NSColor colorWithDeviceRed:19.0 / 255.0
193 green:140.0 / 255.0
194 blue:86.0 / 255.0
195 alpha:1.0] CGColor];
196 animation_values = [NSMutableArray array];
197 [animation_values addObject:(id)blueColor];
198 [animation_values addObject:(id)blueColor];
199 [animation_values addObject:(id)redColor];
200 [animation_values addObject:(id)redColor];
201 [animation_values addObject:(id)yellowColor];
202 [animation_values addObject:(id)yellowColor];
203 [animation_values addObject:(id)greenColor];
204 [animation_values addObject:(id)greenColor];
205 [animation_values addObject:(id)blueColor];
206 [color_animation setValues:animation_values];
207 key_times = [NSMutableArray array];
208 // Begin the transition bewtween colors a short time before the end of each
209 // cycle.
210 CGFloat transition_offset = 0.05;
groby-ooo-7-16 2015/03/30 23:28:31 Probably worth moving out as a constant.
shrike 2015/03/31 07:05:53 Done.
211 [key_times addObject:[NSNumber numberWithFloat:0.0]];
212 [key_times addObject:[NSNumber numberWithFloat:0.25 - transition_offset]];
213 [key_times addObject:[NSNumber numberWithFloat:0.25]];
214 [key_times addObject:[NSNumber numberWithFloat:0.50 - transition_offset]];
215 [key_times addObject:[NSNumber numberWithFloat:0.5]];
216 [key_times addObject:[NSNumber numberWithFloat:0.75 - transition_offset]];
217 [key_times addObject:[NSNumber numberWithFloat:0.75]];
218 [key_times addObject:[NSNumber numberWithFloat:0.999 - transition_offset]];
219 [key_times addObject:[NSNumber numberWithFloat:0.999]];
220 [color_animation setKeyTimes:key_times];
221 [color_animation setDuration:ARC_ANIMATION_TIME * 4.0];
222 [color_animation setRemovedOnCompletion:NO];
223 [color_animation setFillMode:kCAFillModeForwards];
224 [color_animation setRepeatCount:HUGE_VALF];
225 [animations addObject:color_animation];
226
227 // Use an animation group so that the animations are easier to manage, and to
228 // given them the best chance of firing to the same timing.
229 CAAnimationGroup* group = [CAAnimationGroup animation];
230 [group setDuration:ARC_ANIMATION_TIME * 4];
231 [group setRepeatCount:HUGE_VALF];
232 [group setFillMode:kCAFillModeForwards];
233 [group setRemovedOnCompletion:NO];
234 [group setAnimations:animations];
235
236 spinner_animation_.reset([group retain]);
237 }
238
239 - (void)updateAnimation:(NSNotification*)notification {
240 // Only animate the activity indicator if we are within a window, and that
241 // window is not currently minimized or being minimized.
242 if ([self window] && ![[self window] isMiniaturized] && ![self isHidden] &&
groby-ooo-7-16 2015/03/30 23:28:31 Do we need the [self window] check? (I assume it's
shrike 2015/03/31 07:05:53 It's more for the case where you remove the activi
groby-ooo-7-16 2015/03/31 23:07:01 Acknowledged.
243 ![[notification name] isEqualToString:
244 NSWindowWillMiniaturizeNotification]) {
245 if (spinner_animation_.get() == nil) {
246 [self initializeAnimation];
247 }
248 if (!is_animating_) {
249 [shape_layer_ addAnimation:spinner_animation_.get() forKey:nil];
250 is_animating_ = true;
251 } else {
252 }
253 } else {
254 [shape_layer_ removeAllAnimations];
255 is_animating_ = false;
256 }
257 }
258
259 // Register/unregister for window miniaturization event notifications so that
260 // we can stop the animation if the window is minaturized (i.e. not visible).
261 - (void)viewWillMoveToWindow:(NSWindow*)newWindow {
262 if ([self window]) {
263 [[NSNotificationCenter defaultCenter]
264 removeObserver:self
265 name:NSWindowWillMiniaturizeNotification
266 object:[self window]];
267 [[NSNotificationCenter defaultCenter]
268 removeObserver:self
269 name:NSWindowDidDeminiaturizeNotification
270 object:[self window]];
271 }
272
273 if (newWindow) {
274 [[NSNotificationCenter defaultCenter]
275 addObserver:self
276 selector:@selector(updateAnimation:)
277 name:NSWindowWillMiniaturizeNotification
278 object:newWindow];
279 [[NSNotificationCenter defaultCenter]
280 addObserver:self
281 selector:@selector(updateAnimation:)
282 name:NSWindowDidDeminiaturizeNotification
283 object:newWindow];
284 }
285 }
286
287 // Start or stop the animation whenever the view is added to or removed from a
288 // window.
289 - (void)viewDidMoveToWindow {
290 [self updateAnimation:nil];
291 }
292
293 // Start or stop the animation whenever the view is unhidden or hidden.
294 - (void)setHidden:(BOOL)flag
295 {
296 [super setHidden:flag];
297 [self updateAnimation:nil];
298 }
299
300
301 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698