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

Side by Side Diff: ui/views/animation/ink_drop_animation.cc

Issue 1422593003: Made material design ink drop QUICK_ACTION animation more visible. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Minor fixes after self-review. Created 5 years, 2 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
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 #include "ui/views/animation/ink_drop_animation.h" 5 #include "ui/views/animation/ink_drop_animation.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "third_party/skia/include/core/SkColor.h" 11 #include "third_party/skia/include/core/SkColor.h"
12 #include "ui/base/ui_base_switches.h" 12 #include "ui/base/ui_base_switches.h"
13 #include "ui/compositor/callback_layer_animation_observer.h" 13 #include "ui/compositor/callback_layer_animation_observer.h"
14 #include "ui/compositor/layer.h" 14 #include "ui/compositor/layer.h"
15 #include "ui/compositor/layer_animation_sequence.h" 15 #include "ui/compositor/layer_animation_sequence.h"
16 #include "ui/compositor/scoped_layer_animation_settings.h" 16 #include "ui/compositor/scoped_layer_animation_settings.h"
17 #include "ui/gfx/geometry/point3_f.h"
18 #include "ui/gfx/geometry/point_conversions.h"
19 #include "ui/gfx/geometry/point_f.h"
20 #include "ui/gfx/geometry/vector3d_f.h"
17 #include "ui/gfx/transform_util.h" 21 #include "ui/gfx/transform_util.h"
18 #include "ui/views/animation/ink_drop_animation_observer.h" 22 #include "ui/views/animation/ink_drop_animation_observer.h"
19 #include "ui/views/animation/ink_drop_painted_layer_delegates.h" 23 #include "ui/views/animation/ink_drop_painted_layer_delegates.h"
20 24
21 namespace { 25 namespace {
22 26
23 // The minimum scale factor to use when scaling rectangle layers. Smaller values 27 // The minimum scale factor to use when scaling rectangle layers. Smaller values
24 // were causing visual anomalies. 28 // were causing visual anomalies.
25 const float kMinimumRectScale = 0.0001f; 29 const float kMinimumRectScale = 0.0001f;
26 30
27 // The minimum scale factor to use when scaling circle layers. Smaller values 31 // The minimum scale factor to use when scaling circle layers. Smaller values
28 // were causing visual anomalies. 32 // were causing visual anomalies.
29 const float kMinimumCircleScale = 0.001f; 33 const float kMinimumCircleScale = 0.001f;
30 34
31 // The ink drop color. 35 // The ink drop color.
32 const SkColor kInkDropColor = SK_ColorBLACK; 36 const SkColor kInkDropColor = SK_ColorBLACK;
33 37
34 // The opacity of the ink drop when it is visible. 38 // The opacity of the ink drop when it is visible.
35 const float kVisibleOpacity = 0.14f; 39 const float kVisibleOpacity = 0.14f;
36 40
37 // The opacity of the ink drop when it is not visible. 41 // The opacity of the ink drop when it is not visible.
38 const float kHiddenOpacity = 0.0f; 42 const float kHiddenOpacity = 0.0f;
39 43
40 // Durations for the different InkDropState animations in milliseconds. 44 // The time taken to animate the ACTION_PENDING animation.
41 const int kHiddenStateAnimationDurationMs = 1; 45 const int kActionPendingAnimationDurationMs = 500;
42 const int kActionPendingStateAnimationDurationMs = 500; 46
43 const int kQuickActionStateAnimationDurationMs = 250; 47 // The time taken to animate the visible portion of the QUICK_ACTION animation.
44 const int kSlowActionPendingStateAnimationDurationMs = 500; 48 const int kQuickActionVisibleAnimationDurationMs = 200;
45 const int kSlowActionStateAnimationDurationMs = 250; 49
46 const int kActivatedStateAnimationDurationMs = 125; 50 // The time taken to animate the fade out portion of the QUICK_ACTION animation.
47 const int kDeactivatedStateAnimationDurationMs = 250; 51 const int kQuickActionFadeOutAnimationDurationMs = 100;
52
53 // The time taken to animate the SLOW_ACTION_PENDING animation.
54 const int kSlowActionPendingAnimationDurationMs = 100;
55
56 // The time taken to animate the visible portion of the SLOW_ACTION animation.
57 const int kSlowActionVisibleAnimationDurationMs = 150;
58
59 // The time taken to animate the fade out portion of the SLOW_ACTION animation.
60 const int kSlowActionFadeOutAnimationDurationMs = 100;
61
62 // The time taken to animate the ACTIVATED animation.
63 const int kActivatedAnimationDurationMs = 125;
64
65 // The time taken to animate the visible portion of the DEACTIVATED animation.
66 const int kDeactivatedVisibleAnimationDurationMs = 150;
67
68 // The time taken to animate the fade out portion of the SLOW_ACTION animation.
varkha 2015/10/22 18:33:08 s/SLOW_ACTION/DEACTIVATED?
bruthig 2015/11/04 21:51:02 Done.
69 const int kDeactivatedFadeOutAnimationDurationMs = 100;
48 70
49 // A multiplicative factor used to slow down InkDropState animations. 71 // A multiplicative factor used to slow down InkDropState animations.
50 const int kSlowAnimationDurationFactor = 3; 72 const int kSlowAnimationDurationFactor = 3;
51 73
52 // Checks CommandLine switches to determine if the visual feedback should have 74 // Checks CommandLine switches to determine if the visual feedback should have
53 // a fast animations speed. 75 // a fast animations speed.
54 bool UseFastAnimations() { 76 bool UseFastAnimations() {
55 static bool fast = 77 static bool fast =
56 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 78 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
57 (::switches::kMaterialDesignInkDropAnimationSpeed)) != 79 (::switches::kMaterialDesignInkDropAnimationSpeed)) !=
58 ::switches::kMaterialDesignInkDropAnimationSpeedSlow; 80 ::switches::kMaterialDesignInkDropAnimationSpeedSlow;
59 return fast; 81 return fast;
60 } 82 }
61 83
62 // Returns the InkDropState animation duration for the given |state|. 84 // All the sub animations that are used to animate each of the InkDropStates.
63 base::TimeDelta GetAnimationDuration(views::InkDropState state) { 85 enum InkDropSubAnimations {
varkha 2015/10/22 18:33:07 Should this enum be above the time constants since
bruthig 2015/11/04 21:51:02 Done.
86 // The ACTION_PENDING animation has only one sub animation which animates to a
87 // |large_size_| circle at visible opacity.
88 ACTION_PENDING,
89
90 // The QUICK_ACTION animation consists of the two sub animations:
91 // QUICK_ACTION_VISIBLE, QUICK_ACTION_FADE_OUT. The final frame of the
92 // animation is a |large_size_| circle at hidden opacity.
93
94 // The portion of the QUICK_ACTION animation that is at visible opacity.
95 QUICK_ACTION_VISIBLE,
varkha 2015/10/22 18:33:08 nit: White space after this line for consistency.
bruthig 2015/11/04 21:51:02 Done.
96 // The portion of the QUICK_ACTION animation that is at fading out to a hidden
97 // opacity.
98 QUICK_ACTION_FADE_OUT,
99
100 // The SLOW_ACTION_PENDING animation has only one sub animation which animates
101 // to a |small_size_| rounded rectangle at visible opacity.
102 SLOW_ACTION_PENDING,
103
104 // The SLOW_ACTION animation consists of the two sub animations:
105 // SLOW_ACTION_VISIBLE, SLOW_ACTION_FADE_OUT. The final frame of the
106 // animation is a |large_size_| rounded rectangle at hidden opacity.
107
108 // The portion of the SLOW_ACTION animation that is at visible opacity.
109 SLOW_ACTION_VISIBLE,
110
111 // The portion of the SLOW_ACTION animation that is fading out to a hidden
112 // opacity.
113 SLOW_ACTION_FADE_OUT,
114
115 // The ACTIVATED animation has only one sub animation which animates to a
116 // |small_size_| rounded rectangle at visible opacity.
117 ACTIVATED,
118
119 // The DEACTIVATED animation consists of the two sub animations:
120 // DEACTIVATED_VISIBLE, DEACTIVATED_FADE_OUT. The final frame of the
121 // animation is a |large_size_| rounded rectangle at hidden opacity.
122
123 // The portion of the DEACTIVATED animation that is at visible opacity.
124 DEACTIVATED_VISIBLE,
125
126 // The portion of the DEACTIVATED animation that is fading out to a hidden
127 // opacity.
128 DEACTIVATED_FADE_OUT,
129 };
130
131 // // Returns the InkDropState animation duration for the given |state|.
varkha 2015/10/22 18:33:08 nit: extra //.
bruthig 2015/11/04 21:51:02 Done.
132 base::TimeDelta GetAnimationDuration(InkDropSubAnimations state) {
64 int duration = 0; 133 int duration = 0;
65 switch (state) { 134 switch (state) {
66 case views::InkDropState::HIDDEN: 135 case ACTION_PENDING:
67 duration = kHiddenStateAnimationDurationMs; 136 duration = kActionPendingAnimationDurationMs;
68 break; 137 break;
69 case views::InkDropState::ACTION_PENDING: 138 case QUICK_ACTION_VISIBLE:
70 duration = kActionPendingStateAnimationDurationMs; 139 duration = kQuickActionVisibleAnimationDurationMs;
71 break; 140 break;
72 case views::InkDropState::QUICK_ACTION: 141 case QUICK_ACTION_FADE_OUT:
73 duration = kQuickActionStateAnimationDurationMs; 142 duration = kQuickActionFadeOutAnimationDurationMs;
74 break; 143 break;
75 case views::InkDropState::SLOW_ACTION_PENDING: 144 case SLOW_ACTION_PENDING:
76 duration = kSlowActionPendingStateAnimationDurationMs; 145 duration = kSlowActionPendingAnimationDurationMs;
77 break; 146 break;
78 case views::InkDropState::SLOW_ACTION: 147 case SLOW_ACTION_VISIBLE:
79 duration = kSlowActionStateAnimationDurationMs; 148 duration = kSlowActionVisibleAnimationDurationMs;
80 break; 149 break;
81 case views::InkDropState::ACTIVATED: 150 case SLOW_ACTION_FADE_OUT:
82 duration = kActivatedStateAnimationDurationMs; 151 duration = kSlowActionFadeOutAnimationDurationMs;
83 break; 152 break;
84 case views::InkDropState::DEACTIVATED: 153 case ACTIVATED:
85 duration = kDeactivatedStateAnimationDurationMs; 154 duration = kActivatedAnimationDurationMs;
155 break;
156 case DEACTIVATED_VISIBLE:
157 duration = kDeactivatedVisibleAnimationDurationMs;
158 break;
159 case DEACTIVATED_FADE_OUT:
160 duration = kDeactivatedFadeOutAnimationDurationMs;
86 break; 161 break;
87 } 162 }
88
89 return base::TimeDelta::FromMilliseconds( 163 return base::TimeDelta::FromMilliseconds(
90 (UseFastAnimations() ? 1 : kSlowAnimationDurationFactor) * duration); 164 (UseFastAnimations() ? 1 : kSlowAnimationDurationFactor) * duration);
91 } 165 }
92 166
93 // Calculates a Transform for a circle layer. The transform will be set up to 167 // Calculates a Transform for a circle layer. The transform will be set up to
94 // translate the |drawn_center_point| to the origin, scale, and then translate 168 // translate the |drawn_center_point| to the origin, scale, and then translate
95 // to the target point defined by |target_center_x| and |target_center_y|. 169 // to the target point defined by |target_center_x| and |target_center_y|.
96 gfx::Transform CalculateCircleTransform(const gfx::Point& drawn_center_point, 170 gfx::Transform CalculateCircleTransform(const gfx::Point& drawn_center_point,
97 float scale, 171 float scale,
98 float target_center_x, 172 float target_center_x,
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 247
174 void InkDropAnimation::SetCenterPoint(const gfx::Point& center_point) { 248 void InkDropAnimation::SetCenterPoint(const gfx::Point& center_point) {
175 gfx::Transform transform; 249 gfx::Transform transform;
176 transform.Translate(center_point.x(), center_point.y()); 250 transform.Translate(center_point.x(), center_point.y());
177 root_layer_->SetTransform(transform); 251 root_layer_->SetTransform(transform);
178 } 252 }
179 253
180 void InkDropAnimation::AnimateToStateInternal( 254 void InkDropAnimation::AnimateToStateInternal(
181 InkDropState ink_drop_state, 255 InkDropState ink_drop_state,
182 ui::LayerAnimationObserver* animation_observer) { 256 ui::LayerAnimationObserver* animation_observer) {
257 const InkDropState previous_ink_drop_state_ = ink_drop_state_;
183 ink_drop_state_ = ink_drop_state; 258 ink_drop_state_ = ink_drop_state;
184 259
185 if (ink_drop_state_ == InkDropState::HIDDEN) { 260 if (ink_drop_state_ == InkDropState::HIDDEN) {
186 // Animating to the HIDDEN state doesn't actually use any 261 // Animating to the HIDDEN state doesn't actually use any
187 // LayerAnimationSequences so we need to explicitly abort any running ones 262 // LayerAnimationSequences so we need to explicitly abort any running ones
188 // so that observers receive an InkDropAnimationEnded() event for the 263 // so that observers receive an InkDropAnimationEnded() event for the
189 // running animation prior to receiving an InkDropAnimationStarted() event 264 // running animation prior to receiving an InkDropAnimationStarted() event
190 // for the HIDDEN 'animation'. 265 // for the HIDDEN 'animation'.
191 AbortAllAnimations(); 266 AbortAllAnimations();
192 root_layer_->SetVisible(false);
193 SetStateToHidden(); 267 SetStateToHidden();
194 return; 268 return;
195 } 269 }
196 270
197 InkDropTransforms transforms; 271 InkDropTransforms transforms;
198 root_layer_->SetVisible(true); 272 root_layer_->SetVisible(true);
199 273
200 switch (ink_drop_state_) { 274 switch (ink_drop_state_) {
201 case InkDropState::HIDDEN: 275 case InkDropState::HIDDEN:
202 // This case is handled above in a short circuit return. 276 // This case is handled above in a short circuit return.
203 break; 277 break;
204 case InkDropState::ACTION_PENDING: 278 case InkDropState::ACTION_PENDING:
varkha 2015/10/22 18:33:08 General comment in this method. The code seems lik
bruthig 2015/11/04 21:51:02 I'm not so sure how to reduce the amount of code h
279 AnimateToOpacity(kVisibleOpacity, GetAnimationDuration(ACTION_PENDING),
280 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET,
281 animation_observer);
205 CalculateCircleTransforms(large_size_, &transforms); 282 CalculateCircleTransforms(large_size_, &transforms);
206 AnimateToTransforms(transforms, kVisibleOpacity, 283 AnimateToTransforms(transforms, GetAnimationDuration(ACTION_PENDING),
207 GetAnimationDuration(InkDropState::ACTION_PENDING),
208 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET, 284 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET,
209 animation_observer); 285 animation_observer);
210 break; 286 break;
211 case InkDropState::QUICK_ACTION: 287 case InkDropState::QUICK_ACTION: {
288 float visible_duration_ratio = 1.0f;
289
290 // If the previous state is ACTION_PENDING we don't want the QUICK_ACTION
291 // to take the full duration because the expanding ripple noticably
292 // changes speed.
293 if (previous_ink_drop_state_ == InkDropState::ACTION_PENDING) {
294 GetCurrentTransforms(&transforms);
295 visible_duration_ratio =
296 visible_duration_ratio -
297 CalculateDistanceEstimateToQuickAction(transforms);
298 }
299
300 const base::TimeDelta visible_duration =
301 GetAnimationDuration(QUICK_ACTION_VISIBLE) * visible_duration_ratio;
302
303 ui::LayerAnimator::PreemptionStrategy fade_out_preemption_strategy =
304 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET;
305
306 base::TimeDelta total_effective_duration =
307 GetAnimationDuration(QUICK_ACTION_FADE_OUT);
308
309 if (visible_duration.InMilliseconds() > 0) {
310 fade_out_preemption_strategy = ui::LayerAnimator::ENQUEUE_NEW_ANIMATION;
311 total_effective_duration += visible_duration;
312 AnimateToOpacity(kVisibleOpacity, visible_duration,
313 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET,
314 animation_observer);
315 }
316
317 AnimateToOpacity(kHiddenOpacity,
318 GetAnimationDuration(QUICK_ACTION_FADE_OUT),
319 fade_out_preemption_strategy, animation_observer);
212 CalculateCircleTransforms(large_size_, &transforms); 320 CalculateCircleTransforms(large_size_, &transforms);
213 AnimateToTransforms(transforms, kHiddenOpacity, 321 AnimateToTransforms(transforms, total_effective_duration,
214 GetAnimationDuration(InkDropState::QUICK_ACTION),
215 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET, 322 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET,
216 animation_observer); 323 animation_observer);
217 break; 324 break;
325 }
218 case InkDropState::SLOW_ACTION_PENDING: 326 case InkDropState::SLOW_ACTION_PENDING:
327 AnimateToOpacity(kVisibleOpacity,
328 GetAnimationDuration(SLOW_ACTION_PENDING),
329 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET,
330 animation_observer);
219 CalculateRectTransforms(small_size_, small_corner_radius_, &transforms); 331 CalculateRectTransforms(small_size_, small_corner_radius_, &transforms);
220 AnimateToTransforms( 332 AnimateToTransforms(transforms, GetAnimationDuration(SLOW_ACTION_PENDING),
221 transforms, kVisibleOpacity, 333 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET,
222 GetAnimationDuration(InkDropState::SLOW_ACTION_PENDING), 334 animation_observer);
223 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET,
224 animation_observer);
225 break; 335 break;
226 case InkDropState::SLOW_ACTION: 336 case InkDropState::SLOW_ACTION:
337 AnimateToOpacity(kVisibleOpacity,
338 GetAnimationDuration(SLOW_ACTION_VISIBLE),
339 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET,
340 animation_observer);
341 AnimateToOpacity(
342 kHiddenOpacity, GetAnimationDuration(SLOW_ACTION_FADE_OUT),
343 ui::LayerAnimator::ENQUEUE_NEW_ANIMATION, animation_observer);
227 CalculateRectTransforms(large_size_, large_corner_radius_, &transforms); 344 CalculateRectTransforms(large_size_, large_corner_radius_, &transforms);
228 AnimateToTransforms(transforms, kHiddenOpacity, 345 AnimateToTransforms(transforms,
229 GetAnimationDuration(InkDropState::SLOW_ACTION), 346 GetAnimationDuration(SLOW_ACTION_VISIBLE) +
347 GetAnimationDuration(SLOW_ACTION_FADE_OUT),
230 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET, 348 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET,
231 animation_observer); 349 animation_observer);
232 break; 350 break;
233 case InkDropState::ACTIVATED: 351 case InkDropState::ACTIVATED:
352 AnimateToOpacity(kVisibleOpacity, GetAnimationDuration(ACTIVATED),
353 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET,
354 animation_observer);
234 CalculateRectTransforms(small_size_, small_corner_radius_, &transforms); 355 CalculateRectTransforms(small_size_, small_corner_radius_, &transforms);
235 AnimateToTransforms(transforms, kVisibleOpacity, 356 AnimateToTransforms(transforms, GetAnimationDuration(ACTIVATED),
236 GetAnimationDuration(InkDropState::ACTIVATED),
237 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET, 357 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET,
238 animation_observer); 358 animation_observer);
239 break; 359 break;
240 case InkDropState::DEACTIVATED: 360 case InkDropState::DEACTIVATED:
361 AnimateToOpacity(kVisibleOpacity,
362 GetAnimationDuration(DEACTIVATED_VISIBLE),
363 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET,
364 animation_observer);
365 AnimateToOpacity(
366 kHiddenOpacity, GetAnimationDuration(DEACTIVATED_FADE_OUT),
367 ui::LayerAnimator::ENQUEUE_NEW_ANIMATION, animation_observer);
241 CalculateRectTransforms(large_size_, large_corner_radius_, &transforms); 368 CalculateRectTransforms(large_size_, large_corner_radius_, &transforms);
242 AnimateToTransforms(transforms, kHiddenOpacity, 369 AnimateToTransforms(transforms,
243 GetAnimationDuration(InkDropState::DEACTIVATED), 370 GetAnimationDuration(DEACTIVATED_VISIBLE) +
371 GetAnimationDuration(DEACTIVATED_FADE_OUT),
244 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET, 372 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET,
245 animation_observer); 373 animation_observer);
246 break; 374 break;
247 } 375 }
248 } 376 }
249 377
250 void InkDropAnimation::AnimateToTransforms( 378 void InkDropAnimation::AnimateToTransforms(
251 const InkDropTransforms transforms, 379 const InkDropTransforms transforms,
252 float opacity,
253 base::TimeDelta duration, 380 base::TimeDelta duration,
254 ui::LayerAnimator::PreemptionStrategy preemption_strategy, 381 ui::LayerAnimator::PreemptionStrategy preemption_strategy,
255 ui::LayerAnimationObserver* animation_observer) { 382 ui::LayerAnimationObserver* animation_observer) {
256 ui::LayerAnimator* root_animator = root_layer_->GetAnimator();
257 ui::ScopedLayerAnimationSettings root_animation(root_animator);
258 root_animation.SetPreemptionStrategy(preemption_strategy);
259 ui::LayerAnimationElement* root_element =
260 ui::LayerAnimationElement::CreateOpacityElement(opacity, duration);
261 ui::LayerAnimationSequence* root_sequence =
262 new ui::LayerAnimationSequence(root_element);
263
264 if (animation_observer)
265 root_sequence->AddObserver(animation_observer);
266
267 root_animator->StartAnimation(root_sequence);
268
269 for (int i = 0; i < PAINTED_SHAPE_COUNT; ++i) { 383 for (int i = 0; i < PAINTED_SHAPE_COUNT; ++i) {
270 ui::LayerAnimator* animator = painted_layers_[i]->GetAnimator(); 384 ui::LayerAnimator* animator = painted_layers_[i]->GetAnimator();
271 ui::ScopedLayerAnimationSettings animation(animator); 385 ui::ScopedLayerAnimationSettings animation(animator);
272 animation.SetPreemptionStrategy(preemption_strategy); 386 animation.SetPreemptionStrategy(preemption_strategy);
273 ui::LayerAnimationElement* element = 387 ui::LayerAnimationElement* element =
274 ui::LayerAnimationElement::CreateTransformElement(transforms[i], 388 ui::LayerAnimationElement::CreateTransformElement(transforms[i],
275 duration); 389 duration);
276 ui::LayerAnimationSequence* sequence = 390 ui::LayerAnimationSequence* sequence =
277 new ui::LayerAnimationSequence(element); 391 new ui::LayerAnimationSequence(element);
278 392
279 if (animation_observer) 393 if (animation_observer)
280 sequence->AddObserver(animation_observer); 394 sequence->AddObserver(animation_observer);
281 395
282 animator->StartAnimation(sequence); 396 animator->StartAnimation(sequence);
283 } 397 }
284 } 398 }
285 399
286 void InkDropAnimation::SetStateToHidden() { 400 void InkDropAnimation::SetStateToHidden() {
287 InkDropTransforms transforms; 401 InkDropTransforms transforms;
288 // Using a size of 0x0 creates visual anomalies. 402 // Using a size of 0x0 creates visual anomalies.
289 CalculateCircleTransforms(gfx::Size(1, 1), &transforms); 403 CalculateCircleTransforms(gfx::Size(1, 1), &transforms);
290 SetTransforms(transforms); 404 SetTransforms(transforms);
291 SetOpacity(kHiddenOpacity); 405 SetOpacity(kHiddenOpacity);
406 root_layer_->SetVisible(false);
varkha 2015/10/22 18:33:07 Just a question: did it matter that this is now th
bruthig 2015/11/04 21:51:02 Nope
292 } 407 }
293 408
294 void InkDropAnimation::SetTransforms(const InkDropTransforms transforms) { 409 void InkDropAnimation::SetTransforms(const InkDropTransforms transforms) {
295 for (int i = 0; i < PAINTED_SHAPE_COUNT; ++i) 410 for (int i = 0; i < PAINTED_SHAPE_COUNT; ++i)
296 painted_layers_[i]->SetTransform(transforms[i]); 411 painted_layers_[i]->SetTransform(transforms[i]);
297 } 412 }
298 413
299 void InkDropAnimation::SetOpacity(float opacity) { 414 void InkDropAnimation::SetOpacity(float opacity) {
300 root_layer_->SetOpacity(opacity); 415 root_layer_->SetOpacity(opacity);
301 } 416 }
302 417
418 void InkDropAnimation::AnimateToOpacity(
419 float opacity,
420 base::TimeDelta duration,
421 ui::LayerAnimator::PreemptionStrategy preemption_strategy,
422 ui::LayerAnimationObserver* animation_observer) {
423 ui::LayerAnimator* animator = root_layer_->GetAnimator();
424 ui::ScopedLayerAnimationSettings animation_settings(animator);
425 animation_settings.SetPreemptionStrategy(preemption_strategy);
426 ui::LayerAnimationElement* animation_element =
427 ui::LayerAnimationElement::CreateOpacityElement(opacity, duration);
428 ui::LayerAnimationSequence* animation_sequence =
429 new ui::LayerAnimationSequence(animation_element);
430
431 if (animation_observer)
432 animation_sequence->AddObserver(animation_observer);
433
434 animator->StartAnimation(animation_sequence);
435 }
436
303 void InkDropAnimation::CalculateCircleTransforms( 437 void InkDropAnimation::CalculateCircleTransforms(
304 const gfx::Size& size, 438 const gfx::Size& size,
305 InkDropTransforms* transforms_out) const { 439 InkDropTransforms* transforms_out) const {
306 CalculateRectTransforms(size, std::min(size.width(), size.height()) / 2.0f, 440 CalculateRectTransforms(size, std::min(size.width(), size.height()) / 2.0f,
307 transforms_out); 441 transforms_out);
308 } 442 }
309 443
310 void InkDropAnimation::CalculateRectTransforms( 444 void InkDropAnimation::CalculateRectTransforms(
311 const gfx::Size& size, 445 const gfx::Size& size,
312 float corner_radius, 446 float corner_radius,
313 InkDropTransforms* transforms_out) const { 447 InkDropTransforms* transforms_out) const {
314 DCHECK_GE(size.width() / 2.0f, corner_radius) 448 DCHECK_GE(size.width() / 2.0f, corner_radius)
315 << "The circle's diameter should not be greater than the total width."; 449 << "The circle's diameter should not be greater than the total width.";
316 DCHECK_GE(size.height() / 2.0f, corner_radius) 450 DCHECK_GE(size.height() / 2.0f, corner_radius)
317 << "The circle's diameter should not be greater than the total height."; 451 << "The circle's diameter should not be greater than the total height.";
318 452
319 // The shapes are drawn such that their center points are not at the origin. 453 // The shapes are drawn such that their center points are not at the origin.
320 // Thus we use the CalculateCircleTransform() and CalculateRectTransform() 454 // Thus we use the CalculateCircleTransform() and CalculateRectTransform()
321 // methods to calculate the complex Transforms. 455 // methods to calculate the complex Transforms.
322 456
323 const float circle_scale = std::max( 457 const float circle_scale = std::max(
324 kMinimumCircleScale, 458 kMinimumCircleScale,
325 corner_radius / static_cast<float>(circle_layer_delegate_->radius())); 459 corner_radius / static_cast<float>(circle_layer_delegate_->radius()));
326 460
327 const float circle_target_x_offset = size.width() / 2.0f - corner_radius; 461 const float circle_target_x_offset = size.width() / 2.0f - corner_radius;
328 const float circle_target_y_offset = size.height() / 2.0f - corner_radius; 462 const float circle_target_y_offset = size.height() / 2.0f - corner_radius;
329 463
330 (*transforms_out)[TOP_LEFT_CIRCLE] = CalculateCircleTransform( 464 (*transforms_out)[TOP_LEFT_CIRCLE] = CalculateCircleTransform(
331 painted_layers_[TOP_LEFT_CIRCLE]->bounds().CenterPoint(), circle_scale, 465 ToRoundedPoint(circle_layer_delegate_->GetCenterPoint()), circle_scale,
332 -circle_target_x_offset, -circle_target_y_offset); 466 -circle_target_x_offset, -circle_target_y_offset);
333 467
334 (*transforms_out)[TOP_RIGHT_CIRCLE] = CalculateCircleTransform( 468 (*transforms_out)[TOP_RIGHT_CIRCLE] = CalculateCircleTransform(
335 painted_layers_[TOP_RIGHT_CIRCLE]->bounds().CenterPoint(), circle_scale, 469 ToRoundedPoint(circle_layer_delegate_->GetCenterPoint()), circle_scale,
336 circle_target_x_offset, -circle_target_y_offset); 470 circle_target_x_offset, -circle_target_y_offset);
337 471
338 (*transforms_out)[BOTTOM_RIGHT_CIRCLE] = CalculateCircleTransform( 472 (*transforms_out)[BOTTOM_RIGHT_CIRCLE] = CalculateCircleTransform(
339 painted_layers_[BOTTOM_RIGHT_CIRCLE]->bounds().CenterPoint(), 473 ToRoundedPoint(circle_layer_delegate_->GetCenterPoint()), circle_scale,
340 circle_scale, circle_target_x_offset, circle_target_y_offset); 474 circle_target_x_offset, circle_target_y_offset);
341 475
342 (*transforms_out)[BOTTOM_LEFT_CIRCLE] = CalculateCircleTransform( 476 (*transforms_out)[BOTTOM_LEFT_CIRCLE] = CalculateCircleTransform(
343 painted_layers_[BOTTOM_LEFT_CIRCLE]->bounds().CenterPoint(), circle_scale, 477 ToRoundedPoint(circle_layer_delegate_->GetCenterPoint()), circle_scale,
344 -circle_target_x_offset, circle_target_y_offset); 478 -circle_target_x_offset, circle_target_y_offset);
345 479
346 const float rect_delegate_width = 480 const float rect_delegate_width =
347 static_cast<float>(rect_layer_delegate_->size().width()); 481 static_cast<float>(rect_layer_delegate_->size().width());
348 const float rect_delegate_height = 482 const float rect_delegate_height =
349 static_cast<float>(rect_layer_delegate_->size().height()); 483 static_cast<float>(rect_layer_delegate_->size().height());
350 484
351 (*transforms_out)[HORIZONTAL_RECT] = CalculateRectTransform( 485 (*transforms_out)[HORIZONTAL_RECT] = CalculateRectTransform(
352 painted_layers_[HORIZONTAL_RECT]->bounds().CenterPoint(), 486 ToRoundedPoint(rect_layer_delegate_->GetCenterPoint()),
353 std::max(kMinimumRectScale, size.width() / rect_delegate_width), 487 std::max(kMinimumRectScale, size.width() / rect_delegate_width),
354 std::max(kMinimumRectScale, 488 std::max(kMinimumRectScale,
355 (size.height() - 2.0f * corner_radius) / rect_delegate_height)); 489 (size.height() - 2.0f * corner_radius) / rect_delegate_height));
356 490
357 (*transforms_out)[VERTICAL_RECT] = CalculateRectTransform( 491 (*transforms_out)[VERTICAL_RECT] = CalculateRectTransform(
358 painted_layers_[VERTICAL_RECT]->bounds().CenterPoint(), 492 ToRoundedPoint(rect_layer_delegate_->GetCenterPoint()),
359 std::max(kMinimumRectScale, 493 std::max(kMinimumRectScale,
360 (size.width() - 2.0f * corner_radius) / rect_delegate_width), 494 (size.width() - 2.0f * corner_radius) / rect_delegate_width),
361 std::max(kMinimumRectScale, size.height() / rect_delegate_height)); 495 std::max(kMinimumRectScale, size.height() / rect_delegate_height));
362 } 496 }
363 497
364 void InkDropAnimation::GetCurrentTansforms( 498 void InkDropAnimation::GetCurrentTransforms(
365 InkDropTransforms* transforms_out) const { 499 InkDropTransforms* transforms_out) const {
366 for (int i = 0; i < PAINTED_SHAPE_COUNT; ++i) 500 for (int i = 0; i < PAINTED_SHAPE_COUNT; ++i)
367 (*transforms_out)[i] = painted_layers_[i]->GetTargetTransform(); 501 (*transforms_out)[i] = painted_layers_[i]->transform();
368 } 502 }
369 503
370 void InkDropAnimation::AddPaintLayer(PaintedShape painted_shape) { 504 void InkDropAnimation::AddPaintLayer(PaintedShape painted_shape) {
371 ui::LayerDelegate* delegate = nullptr; 505 ui::LayerDelegate* delegate = nullptr;
372 switch (painted_shape) { 506 switch (painted_shape) {
373 case TOP_LEFT_CIRCLE: 507 case TOP_LEFT_CIRCLE:
374 case TOP_RIGHT_CIRCLE: 508 case TOP_RIGHT_CIRCLE:
375 case BOTTOM_RIGHT_CIRCLE: 509 case BOTTOM_RIGHT_CIRCLE:
376 case BOTTOM_LEFT_CIRCLE: 510 case BOTTOM_LEFT_CIRCLE:
377 delegate = circle_layer_delegate_.get(); 511 delegate = circle_layer_delegate_.get();
(...skipping 19 matching lines...) Expand all
397 531
398 painted_layers_[painted_shape].reset(layer); 532 painted_layers_[painted_shape].reset(layer);
399 } 533 }
400 534
401 void InkDropAnimation::AbortAllAnimations() { 535 void InkDropAnimation::AbortAllAnimations() {
402 root_layer_->GetAnimator()->AbortAllAnimations(); 536 root_layer_->GetAnimator()->AbortAllAnimations();
403 for (int i = 0; i < PAINTED_SHAPE_COUNT; ++i) 537 for (int i = 0; i < PAINTED_SHAPE_COUNT; ++i)
404 painted_layers_[i]->GetAnimator()->AbortAllAnimations(); 538 painted_layers_[i]->GetAnimator()->AbortAllAnimations();
405 } 539 }
406 540
541 float InkDropAnimation::CalculateDistanceEstimateToQuickAction(
542 const InkDropTransforms& transforms) const {
543 gfx::Point3F circle_center_point =
544 gfx::Point3F(circle_layer_delegate_->GetCenterPoint());
545
546 gfx::Point3F circle_top_point(
547 circle_center_point.x(),
548 circle_center_point.y() - circle_layer_delegate_->radius(), 0);
549
550 transforms[TOP_LEFT_CIRCLE].TransformPoint(&circle_center_point);
551 transforms[TOP_LEFT_CIRCLE].TransformPoint(&circle_top_point);
552
553 // Calculate the ratio of how far the transformed circle's center point is
554 // from the destination compared to how far it can be.
555 const float center_point_distance_estimate =
556 1.0f -
557 gfx::Vector3dF(circle_center_point.x(), circle_center_point.y(), 0)
558 .Length() /
varkha 2015/10/22 18:33:08 nit: indentation looks a bit odd here.
bruthig 2015/11/04 21:51:02 This is the result of 'git cl format'.
559 (gfx::Vector3dF(static_cast<float>(large_size_.width()),
560 static_cast<float>(large_size_.height()), 0)
561 .Length() /
562 2.0f);
563
564 // Calculate the ratio of how far the transformed circle's top most point is
varkha 2015/10/22 18:33:08 nit: topmost.
bruthig 2015/11/04 21:51:02 Done.
565 // from the destination compared to how far it can be.
566 const float top_point_distance_estimate = fabs(
varkha 2015/10/22 18:33:08 When is the argument of fabs ever negative?
bruthig 2015/11/04 21:51:02 circle_top_point.y() is expected to be negative si
567 circle_top_point.y() / static_cast<float>(large_size_.height()) / 2.0f);
varkha 2015/10/22 18:33:07 Are the brackets correct here? Is it not y/(h/2) s
bruthig 2015/11/04 21:51:02 Done.
568
569 return std::min(center_point_distance_estimate, top_point_distance_estimate);
570 }
571
407 void InkDropAnimation::AnimationStartedCallback( 572 void InkDropAnimation::AnimationStartedCallback(
408 InkDropState ink_drop_state, 573 InkDropState ink_drop_state,
409 const ui::CallbackLayerAnimationObserver& observer) { 574 const ui::CallbackLayerAnimationObserver& observer) {
410 FOR_EACH_OBSERVER(InkDropAnimationObserver, observers_, 575 FOR_EACH_OBSERVER(InkDropAnimationObserver, observers_,
411 InkDropAnimationStarted(ink_drop_state)); 576 InkDropAnimationStarted(ink_drop_state));
412 } 577 }
413 578
414 bool InkDropAnimation::AnimationEndedCallback( 579 bool InkDropAnimation::AnimationEndedCallback(
415 InkDropState ink_drop_state, 580 InkDropState ink_drop_state,
416 const ui::CallbackLayerAnimationObserver& observer) { 581 const ui::CallbackLayerAnimationObserver& observer) {
417 FOR_EACH_OBSERVER( 582 FOR_EACH_OBSERVER(
418 InkDropAnimationObserver, observers_, 583 InkDropAnimationObserver, observers_,
419 InkDropAnimationEnded(ink_drop_state, 584 InkDropAnimationEnded(ink_drop_state,
420 observer.aborted_count() 585 observer.aborted_count()
421 ? InkDropAnimationObserver::PRE_EMPTED 586 ? InkDropAnimationObserver::PRE_EMPTED
422 : InkDropAnimationObserver::SUCCESS)); 587 : InkDropAnimationObserver::SUCCESS));
423 return true; 588 return true;
424 } 589 }
425 590
426 } // namespace views 591 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/animation/ink_drop_animation.h ('k') | ui/views/animation/ink_drop_painted_layer_delegates.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698