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

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: Added varkha@'s behavioural changes Created 5 years, 1 month 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.09f;
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 // All the sub animations that are used to animate each of the InkDropStates.
41 const int kHiddenStateAnimationDurationMs = 1; 45 // These are used to get time durations with
42 const int kActionPendingStateAnimationDurationMs = 500; 46 // GetAnimationDuration(InkDropSubAnimations). Note that in general a sub
43 const int kQuickActionStateAnimationDurationMs = 250; 47 // animation defines the duration for either a transformation animation or an
44 const int kSlowActionPendingStateAnimationDurationMs = 500; 48 // opacity animation but there are some exceptions where an entire InkDropState
45 const int kSlowActionStateAnimationDurationMs = 250; 49 // animation consists of only 1 sub animation and it defines the duration for
46 const int kActivatedStateAnimationDurationMs = 125; 50 // both the transformation and opacity animations.
47 const int kDeactivatedStateAnimationDurationMs = 250; 51 enum InkDropSubAnimations {
52 // Multi-purpose sub animation that is used for all InkDropState animations
53 // that fade out. It defines the portion of the animation where the ripple
54 // shape should be in it's final form and should pause at full opacity before
55 // fading out.
56 OPACITY_PAUSE_BEFORE_FADE_OUT,
57
58 // HIDDEN sub animations.
59
60 // The HIDDEN animation consists of a single sub animation that is fading out
61 // to a hidden opacity.
62 HIDDEN_FADE_OUT,
63
64 // ACTION_PENDING sub animations.
65
66 // The ACTION_PENDING sub animation that fades in to the visible opacity.
67 ACTION_PENDING_FADE_IN,
68
69 // The ACTION_PENDING sub animation that transforms the shape to a
70 // |large_size_| circle.
71 ACTION_PENDING_TRANSFORM,
72
73 // QUICK_ACTION sub animations.
74
75 // The QUICK_ACTION sub animation that is fading in to a visible opacity.
76 QUICK_ACTION_FADE_IN,
77
78 // The QUICK_ACTION sub animation that is fading out to a hidden opacity.
79 QUICK_ACTION_FADE_OUT,
80
81 // The QUICK_ACTION sub animation that transforms the shape to a |large_size_|
82 // circle.
83 QUICK_ACTION_TRANSFORM,
84
85 // The SLOW_ACTION_PENDING animation has only one sub animation which animates
86 // to a |small_size_| rounded rectangle at visible opacity.
87 SLOW_ACTION_PENDING,
88
89 // SLOW_ACTION sub animations.
90
91 // The SLOW_ACTION sub animation that is fading out to a hidden opacity.
92 SLOW_ACTION_FADE_OUT,
93
94 // The SLOW_ACTION sub animation that transforms the shape to a |large_size_|
95 // rounded rectangle.
96 SLOW_ACTION_TRANSFORM,
97
98 // ACTIVATED sub animations.
99
100 // The ACTIVATED sub animation that is fading in to a visible opacity.
101 ACTIVATED_FADE_IN,
102
103 // The ACTIVATED sub animation that transforms the shape to a |small_size_|
104 // rounded rectangle.
105 ACTIVATED_TRANSFORM,
106
107 // DEACTIVATED sub animations.
108
109 // The DEACTIVATED sub animation that is fading out to a hidden opacity.
110 DEACTIVATED_FADE_OUT,
111
112 // The DEACTIVATED sub animation that transforms the shape to a |large_size_|
113 // rounded rectangle.
114 DEACTIVATED_TRANSFORM,
115 };
116
117 // Duration constants for InkDropStateSubAnimations. See the
118 // InkDropStateSubAnimations enum documentation for more info.
119 const int kOpacityPauseBeforeFadeOutDurationMs = 100;
120 const int kHiddenFadeOutAnimationDurationMs = 100;
121 const int kActionPendingFadeInAnimationDurationMs = 100;
122 const int kActionPendingTransformAnimationDurationMs = 300;
123 const int kQuickActionFadeInAnimationDurationMs = 100;
124 const int kQuickActionFadeOutAnimationDurationMs = 100;
125 const int kQuickActionTransformAnimationDurationMs = 300;
126 const int kSlowActionPendingAnimationDurationMs = 100;
127 const int kSlowActionFadeOutAnimationDurationMs = 100;
128 const int kSlowActionTransformAnimationDurationMs = 250;
129 const int kActivatedFadeInAnimationDurationMs = 75;
130 const int kActivatedTransformAnimationDurationMs = 150;
131 const int kDeactivatedFadeOutAnimationDurationMs = 100;
132 const int kDeactivatedTransformAnimationDurationMs = 100;
48 133
49 // A multiplicative factor used to slow down InkDropState animations. 134 // A multiplicative factor used to slow down InkDropState animations.
50 const int kSlowAnimationDurationFactor = 3; 135 const int kSlowAnimationDurationFactor = 3;
51 136
52 // Checks CommandLine switches to determine if the visual feedback should have 137 // Checks CommandLine switches to determine if the visual feedback should have
53 // a fast animations speed. 138 // a fast animations speed.
54 bool UseFastAnimations() { 139 bool UseFastAnimations() {
55 static bool fast = 140 static bool fast =
56 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 141 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
57 (::switches::kMaterialDesignInkDropAnimationSpeed)) != 142 (::switches::kMaterialDesignInkDropAnimationSpeed)) !=
58 ::switches::kMaterialDesignInkDropAnimationSpeedSlow; 143 ::switches::kMaterialDesignInkDropAnimationSpeedSlow;
59 return fast; 144 return fast;
60 } 145 }
61 146
62 // Returns the InkDropState animation duration for the given |state|. 147 // Returns the InkDropState sub animation duration for the given |state|.
63 base::TimeDelta GetAnimationDuration(views::InkDropState state) { 148 base::TimeDelta GetAnimationDuration(InkDropSubAnimations state) {
64 int duration = 0; 149 int duration = 0;
65 switch (state) { 150 switch (state) {
66 case views::InkDropState::HIDDEN: 151 case OPACITY_PAUSE_BEFORE_FADE_OUT:
67 duration = kHiddenStateAnimationDurationMs; 152 duration = kOpacityPauseBeforeFadeOutDurationMs;
68 break; 153 break;
69 case views::InkDropState::ACTION_PENDING: 154 case HIDDEN_FADE_OUT:
70 duration = kActionPendingStateAnimationDurationMs; 155 duration = kHiddenFadeOutAnimationDurationMs;
varkha 2015/11/06 00:00:27 nit: Maybe drop Animation from all of those for co
bruthig 2015/11/11 18:11:20 Done.
71 break; 156 break;
72 case views::InkDropState::QUICK_ACTION: 157 case ACTION_PENDING_FADE_IN:
73 duration = kQuickActionStateAnimationDurationMs; 158 duration = kActionPendingFadeInAnimationDurationMs;
74 break; 159 break;
75 case views::InkDropState::SLOW_ACTION_PENDING: 160 case ACTION_PENDING_TRANSFORM:
76 duration = kSlowActionPendingStateAnimationDurationMs; 161 duration = kActionPendingTransformAnimationDurationMs;
77 break; 162 break;
78 case views::InkDropState::SLOW_ACTION: 163 case QUICK_ACTION_FADE_IN:
79 duration = kSlowActionStateAnimationDurationMs; 164 duration = kQuickActionFadeInAnimationDurationMs;
80 break; 165 break;
81 case views::InkDropState::ACTIVATED: 166 case QUICK_ACTION_FADE_OUT:
82 duration = kActivatedStateAnimationDurationMs; 167 duration = kQuickActionFadeOutAnimationDurationMs;
83 break; 168 break;
84 case views::InkDropState::DEACTIVATED: 169 case QUICK_ACTION_TRANSFORM:
85 duration = kDeactivatedStateAnimationDurationMs; 170 duration = kQuickActionTransformAnimationDurationMs;
171 break;
172 case SLOW_ACTION_PENDING:
173 duration = kSlowActionPendingAnimationDurationMs;
174 break;
175 case SLOW_ACTION_TRANSFORM:
176 duration = kSlowActionTransformAnimationDurationMs;
177 break;
178 case SLOW_ACTION_FADE_OUT:
179 duration = kSlowActionFadeOutAnimationDurationMs;
180 break;
181 case ACTIVATED_FADE_IN:
182 duration = kActivatedFadeInAnimationDurationMs;
183 break;
184 case ACTIVATED_TRANSFORM:
185 duration = kActivatedTransformAnimationDurationMs;
186 break;
187 case DEACTIVATED_FADE_OUT:
188 duration = kDeactivatedFadeOutAnimationDurationMs;
189 break;
190 case DEACTIVATED_TRANSFORM:
191 duration = kDeactivatedTransformAnimationDurationMs;
86 break; 192 break;
87 } 193 }
88
89 return base::TimeDelta::FromMilliseconds( 194 return base::TimeDelta::FromMilliseconds(
90 (UseFastAnimations() ? 1 : kSlowAnimationDurationFactor) * duration); 195 (UseFastAnimations() ? 1 : kSlowAnimationDurationFactor) * duration);
91 } 196 }
92 197
93 // Calculates a Transform for a circle layer. The transform will be set up to 198 // 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 199 // 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|. 200 // to the target point defined by |target_center_x| and |target_center_y|.
96 gfx::Transform CalculateCircleTransform(const gfx::Point& drawn_center_point, 201 gfx::Transform CalculateCircleTransform(const gfx::Point& drawn_center_point,
97 float scale, 202 float scale,
98 float target_center_x, 203 float target_center_x,
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 278
174 void InkDropAnimation::SetCenterPoint(const gfx::Point& center_point) { 279 void InkDropAnimation::SetCenterPoint(const gfx::Point& center_point) {
175 gfx::Transform transform; 280 gfx::Transform transform;
176 transform.Translate(center_point.x(), center_point.y()); 281 transform.Translate(center_point.x(), center_point.y());
177 root_layer_->SetTransform(transform); 282 root_layer_->SetTransform(transform);
178 } 283 }
179 284
180 void InkDropAnimation::AnimateToStateInternal( 285 void InkDropAnimation::AnimateToStateInternal(
181 InkDropState ink_drop_state, 286 InkDropState ink_drop_state,
182 ui::LayerAnimationObserver* animation_observer) { 287 ui::LayerAnimationObserver* animation_observer) {
288 if (ink_drop_state_ == InkDropState::HIDDEN) {
289 // The HIDDEN state animations might still be in progress and we don't want
290 // to start the new ink drop sequence from that shape so snap the state to
291 // hidden.
292 AbortAllAnimations();
293 SetStateToHidden();
294 }
295
183 ink_drop_state_ = ink_drop_state; 296 ink_drop_state_ = ink_drop_state;
184 297
185 if (ink_drop_state_ == InkDropState::HIDDEN) {
186 // Animating to the HIDDEN state doesn't actually use any
187 // LayerAnimationSequences so we need to explicitly abort any running ones
188 // so that observers receive an InkDropAnimationEnded() event for the
189 // running animation prior to receiving an InkDropAnimationStarted() event
190 // for the HIDDEN 'animation'.
191 AbortAllAnimations();
192 root_layer_->SetVisible(false);
193 SetStateToHidden();
194 return;
195 }
196
197 InkDropTransforms transforms; 298 InkDropTransforms transforms;
198 root_layer_->SetVisible(true); 299 root_layer_->SetVisible(true);
199 300
200 switch (ink_drop_state_) { 301 switch (ink_drop_state_) {
201 case InkDropState::HIDDEN: 302 case InkDropState::HIDDEN:
202 // This case is handled above in a short circuit return. 303 // Only the opacity is animated so we have to explicitly abort the other
304 // running animations so that the LayerAnimationObservers are notified in
305 // the proper order.
306 AbortAllAnimations();
varkha 2015/11/06 00:00:27 Why is this necessary given that we have called it
bruthig 2015/11/11 18:11:20 The call to AbortAllAnimations() above is guarded
307 if (GetCurrentOpacity() != kHiddenOpacity) {
308 AnimateToOpacity(kHiddenOpacity, GetAnimationDuration(HIDDEN_FADE_OUT),
309 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET,
310 gfx::Tween::EASE_IN_OUT, animation_observer);
311 }
203 break; 312 break;
204 case InkDropState::ACTION_PENDING: 313 case InkDropState::ACTION_PENDING:
314 AnimateToOpacity(kVisibleOpacity,
315 GetAnimationDuration(ACTION_PENDING_FADE_IN),
316 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET,
317 gfx::Tween::EASE_IN, animation_observer);
205 CalculateCircleTransforms(large_size_, &transforms); 318 CalculateCircleTransforms(large_size_, &transforms);
206 AnimateToTransforms(transforms, kVisibleOpacity, 319 AnimateToTransforms(transforms,
207 GetAnimationDuration(InkDropState::ACTION_PENDING), 320 GetAnimationDuration(ACTION_PENDING_TRANSFORM),
208 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET, 321 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET,
209 animation_observer); 322 gfx::Tween::EASE_OUT, animation_observer);
210 break; 323 break;
211 case InkDropState::QUICK_ACTION: 324 case InkDropState::QUICK_ACTION: {
325 const float fade_in_ratio = 1.0f - GetCurrentOpacity() / kVisibleOpacity;
326 const base::TimeDelta fade_in_duration =
327 GetAnimationDuration(QUICK_ACTION_FADE_IN) * fade_in_ratio;
328
329 const base::TimeDelta fade_out_duration =
330 GetAnimationDuration(QUICK_ACTION_FADE_OUT);
331
332 GetCurrentTransforms(&transforms);
333 const float transformation_ratio =
334 1.0f - CalculateDistanceEstimateToQuickAction(transforms);
335 const base::TimeDelta transformation_duration =
336 GetAnimationDuration(QUICK_ACTION_TRANSFORM) * transformation_ratio;
337
338 const base::TimeDelta visible_duration =
339 transformation_duration - fade_in_duration +
340 GetAnimationDuration(OPACITY_PAUSE_BEFORE_FADE_OUT);
341
342 if (fade_in_duration.InMilliseconds() > 0) {
343 AnimateToOpacity(kVisibleOpacity, fade_in_duration,
344 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET,
345 gfx::Tween::EASE_IN, animation_observer);
346 }
347
348 AnimateToOpacity(kVisibleOpacity, visible_duration,
349 ui::LayerAnimator::ENQUEUE_NEW_ANIMATION,
350 gfx::Tween::LINEAR, animation_observer);
351 AnimateToOpacity(kHiddenOpacity, fade_out_duration,
352 ui::LayerAnimator::ENQUEUE_NEW_ANIMATION,
353 gfx::Tween::EASE_OUT, animation_observer);
212 CalculateCircleTransforms(large_size_, &transforms); 354 CalculateCircleTransforms(large_size_, &transforms);
213 AnimateToTransforms(transforms, kHiddenOpacity, 355 AnimateToTransforms(transforms, transformation_duration,
214 GetAnimationDuration(InkDropState::QUICK_ACTION),
215 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET, 356 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET,
216 animation_observer); 357 gfx::Tween::EASE_OUT, animation_observer);
217 break; 358 break;
359 }
218 case InkDropState::SLOW_ACTION_PENDING: 360 case InkDropState::SLOW_ACTION_PENDING:
361 AnimateToOpacity(kVisibleOpacity,
362 GetAnimationDuration(SLOW_ACTION_PENDING),
363 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET,
364 gfx::Tween::EASE_IN, animation_observer);
219 CalculateRectTransforms(small_size_, small_corner_radius_, &transforms); 365 CalculateRectTransforms(small_size_, small_corner_radius_, &transforms);
220 AnimateToTransforms( 366 AnimateToTransforms(transforms, GetAnimationDuration(SLOW_ACTION_PENDING),
221 transforms, kVisibleOpacity, 367 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET,
222 GetAnimationDuration(InkDropState::SLOW_ACTION_PENDING), 368 gfx::Tween::EASE_IN_OUT, animation_observer);
223 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET,
224 animation_observer);
225 break; 369 break;
226 case InkDropState::SLOW_ACTION: 370 case InkDropState::SLOW_ACTION:
371 AnimateToOpacity(kVisibleOpacity,
372 GetAnimationDuration(SLOW_ACTION_TRANSFORM) +
373 GetAnimationDuration(OPACITY_PAUSE_BEFORE_FADE_OUT),
374 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET,
375 gfx::Tween::EASE_IN, animation_observer);
376 AnimateToOpacity(kHiddenOpacity,
377 GetAnimationDuration(SLOW_ACTION_FADE_OUT),
378 ui::LayerAnimator::ENQUEUE_NEW_ANIMATION,
379 gfx::Tween::EASE_IN_OUT, animation_observer);
227 CalculateRectTransforms(large_size_, large_corner_radius_, &transforms); 380 CalculateRectTransforms(large_size_, large_corner_radius_, &transforms);
228 AnimateToTransforms(transforms, kHiddenOpacity, 381 AnimateToTransforms(transforms,
229 GetAnimationDuration(InkDropState::SLOW_ACTION), 382 GetAnimationDuration(SLOW_ACTION_TRANSFORM),
230 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET, 383 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET,
231 animation_observer); 384 gfx::Tween::EASE_IN_OUT, animation_observer);
232 break; 385 break;
233 case InkDropState::ACTIVATED: 386 case InkDropState::ACTIVATED:
387 AnimateToOpacity(kVisibleOpacity, GetAnimationDuration(ACTIVATED_FADE_IN),
388 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET,
389 gfx::Tween::EASE_IN, animation_observer);
234 CalculateRectTransforms(small_size_, small_corner_radius_, &transforms); 390 CalculateRectTransforms(small_size_, small_corner_radius_, &transforms);
235 AnimateToTransforms(transforms, kVisibleOpacity, 391 AnimateToTransforms(transforms, GetAnimationDuration(ACTIVATED_TRANSFORM),
236 GetAnimationDuration(InkDropState::ACTIVATED),
237 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET, 392 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET,
238 animation_observer); 393 gfx::Tween::EASE_OUT, animation_observer);
239 break; 394 break;
240 case InkDropState::DEACTIVATED: 395 case InkDropState::DEACTIVATED:
396 AnimateToOpacity(kVisibleOpacity,
397 GetAnimationDuration(DEACTIVATED_TRANSFORM) +
398 GetAnimationDuration(OPACITY_PAUSE_BEFORE_FADE_OUT),
399 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET,
400 gfx::Tween::EASE_IN, animation_observer);
401 AnimateToOpacity(kHiddenOpacity,
402 GetAnimationDuration(DEACTIVATED_FADE_OUT),
403 ui::LayerAnimator::ENQUEUE_NEW_ANIMATION,
404 gfx::Tween::EASE_IN_OUT, animation_observer);
241 CalculateRectTransforms(large_size_, large_corner_radius_, &transforms); 405 CalculateRectTransforms(large_size_, large_corner_radius_, &transforms);
242 AnimateToTransforms(transforms, kHiddenOpacity, 406 AnimateToTransforms(transforms,
243 GetAnimationDuration(InkDropState::DEACTIVATED), 407 GetAnimationDuration(DEACTIVATED_TRANSFORM),
244 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET, 408 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET,
245 animation_observer); 409 gfx::Tween::EASE_IN_OUT, animation_observer);
246 break; 410 break;
247 } 411 }
248 } 412 }
249 413
250 void InkDropAnimation::AnimateToTransforms( 414 void InkDropAnimation::AnimateToTransforms(
251 const InkDropTransforms transforms, 415 const InkDropTransforms transforms,
252 float opacity,
253 base::TimeDelta duration, 416 base::TimeDelta duration,
254 ui::LayerAnimator::PreemptionStrategy preemption_strategy, 417 ui::LayerAnimator::PreemptionStrategy preemption_strategy,
418 gfx::Tween::Type tween,
255 ui::LayerAnimationObserver* animation_observer) { 419 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) { 420 for (int i = 0; i < PAINTED_SHAPE_COUNT; ++i) {
270 ui::LayerAnimator* animator = painted_layers_[i]->GetAnimator(); 421 ui::LayerAnimator* animator = painted_layers_[i]->GetAnimator();
271 ui::ScopedLayerAnimationSettings animation(animator); 422 ui::ScopedLayerAnimationSettings animation(animator);
272 animation.SetPreemptionStrategy(preemption_strategy); 423 animation.SetPreemptionStrategy(preemption_strategy);
424 animation.SetTweenType(tween);
273 ui::LayerAnimationElement* element = 425 ui::LayerAnimationElement* element =
274 ui::LayerAnimationElement::CreateTransformElement(transforms[i], 426 ui::LayerAnimationElement::CreateTransformElement(transforms[i],
275 duration); 427 duration);
276 ui::LayerAnimationSequence* sequence = 428 ui::LayerAnimationSequence* sequence =
277 new ui::LayerAnimationSequence(element); 429 new ui::LayerAnimationSequence(element);
278 430
279 if (animation_observer) 431 if (animation_observer)
280 sequence->AddObserver(animation_observer); 432 sequence->AddObserver(animation_observer);
281 433
282 animator->StartAnimation(sequence); 434 animator->StartAnimation(sequence);
283 } 435 }
284 } 436 }
285 437
286 void InkDropAnimation::SetStateToHidden() { 438 void InkDropAnimation::SetStateToHidden() {
287 InkDropTransforms transforms; 439 InkDropTransforms transforms;
288 // Using a size of 0x0 creates visual anomalies. 440 // Using a size of 0x0 creates visual anomalies.
289 CalculateCircleTransforms(gfx::Size(1, 1), &transforms); 441 CalculateCircleTransforms(gfx::Size(1, 1), &transforms);
290 SetTransforms(transforms); 442 SetTransforms(transforms);
291 SetOpacity(kHiddenOpacity); 443 SetOpacity(kHiddenOpacity);
444 root_layer_->SetVisible(false);
292 } 445 }
293 446
294 void InkDropAnimation::SetTransforms(const InkDropTransforms transforms) { 447 void InkDropAnimation::SetTransforms(const InkDropTransforms transforms) {
295 for (int i = 0; i < PAINTED_SHAPE_COUNT; ++i) 448 for (int i = 0; i < PAINTED_SHAPE_COUNT; ++i)
296 painted_layers_[i]->SetTransform(transforms[i]); 449 painted_layers_[i]->SetTransform(transforms[i]);
297 } 450 }
298 451
452 float InkDropAnimation::GetCurrentOpacity() const {
453 return root_layer_->opacity();
454 }
455
299 void InkDropAnimation::SetOpacity(float opacity) { 456 void InkDropAnimation::SetOpacity(float opacity) {
300 root_layer_->SetOpacity(opacity); 457 root_layer_->SetOpacity(opacity);
301 } 458 }
302 459
460 void InkDropAnimation::AnimateToOpacity(
461 float opacity,
462 base::TimeDelta duration,
463 ui::LayerAnimator::PreemptionStrategy preemption_strategy,
464 gfx::Tween::Type tween,
465 ui::LayerAnimationObserver* animation_observer) {
466 ui::LayerAnimator* animator = root_layer_->GetAnimator();
467 ui::ScopedLayerAnimationSettings animation_settings(animator);
468 animation_settings.SetPreemptionStrategy(preemption_strategy);
469 animation_settings.SetTweenType(tween);
470 ui::LayerAnimationElement* animation_element =
471 ui::LayerAnimationElement::CreateOpacityElement(opacity, duration);
472 ui::LayerAnimationSequence* animation_sequence =
473 new ui::LayerAnimationSequence(animation_element);
474
475 if (animation_observer)
476 animation_sequence->AddObserver(animation_observer);
477
478 animator->StartAnimation(animation_sequence);
479 }
480
303 void InkDropAnimation::CalculateCircleTransforms( 481 void InkDropAnimation::CalculateCircleTransforms(
304 const gfx::Size& size, 482 const gfx::Size& size,
305 InkDropTransforms* transforms_out) const { 483 InkDropTransforms* transforms_out) const {
306 CalculateRectTransforms(size, std::min(size.width(), size.height()) / 2.0f, 484 CalculateRectTransforms(size, std::min(size.width(), size.height()) / 2.0f,
307 transforms_out); 485 transforms_out);
308 } 486 }
309 487
310 void InkDropAnimation::CalculateRectTransforms( 488 void InkDropAnimation::CalculateRectTransforms(
311 const gfx::Size& size, 489 const gfx::Size& size,
312 float corner_radius, 490 float corner_radius,
313 InkDropTransforms* transforms_out) const { 491 InkDropTransforms* transforms_out) const {
314 DCHECK_GE(size.width() / 2.0f, corner_radius) 492 DCHECK_GE(size.width() / 2.0f, corner_radius)
315 << "The circle's diameter should not be greater than the total width."; 493 << "The circle's diameter should not be greater than the total width.";
316 DCHECK_GE(size.height() / 2.0f, corner_radius) 494 DCHECK_GE(size.height() / 2.0f, corner_radius)
317 << "The circle's diameter should not be greater than the total height."; 495 << "The circle's diameter should not be greater than the total height.";
318 496
319 // The shapes are drawn such that their center points are not at the origin. 497 // The shapes are drawn such that their center points are not at the origin.
320 // Thus we use the CalculateCircleTransform() and CalculateRectTransform() 498 // Thus we use the CalculateCircleTransform() and CalculateRectTransform()
321 // methods to calculate the complex Transforms. 499 // methods to calculate the complex Transforms.
322 500
323 const float circle_scale = std::max( 501 const float circle_scale = std::max(
324 kMinimumCircleScale, 502 kMinimumCircleScale,
325 corner_radius / static_cast<float>(circle_layer_delegate_->radius())); 503 corner_radius / static_cast<float>(circle_layer_delegate_->radius()));
326 504
327 const float circle_target_x_offset = size.width() / 2.0f - corner_radius; 505 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; 506 const float circle_target_y_offset = size.height() / 2.0f - corner_radius;
329 507
330 (*transforms_out)[TOP_LEFT_CIRCLE] = CalculateCircleTransform( 508 (*transforms_out)[TOP_LEFT_CIRCLE] = CalculateCircleTransform(
331 painted_layers_[TOP_LEFT_CIRCLE]->bounds().CenterPoint(), circle_scale, 509 ToRoundedPoint(circle_layer_delegate_->GetCenterPoint()), circle_scale,
332 -circle_target_x_offset, -circle_target_y_offset); 510 -circle_target_x_offset, -circle_target_y_offset);
333 511
334 (*transforms_out)[TOP_RIGHT_CIRCLE] = CalculateCircleTransform( 512 (*transforms_out)[TOP_RIGHT_CIRCLE] = CalculateCircleTransform(
335 painted_layers_[TOP_RIGHT_CIRCLE]->bounds().CenterPoint(), circle_scale, 513 ToRoundedPoint(circle_layer_delegate_->GetCenterPoint()), circle_scale,
336 circle_target_x_offset, -circle_target_y_offset); 514 circle_target_x_offset, -circle_target_y_offset);
337 515
338 (*transforms_out)[BOTTOM_RIGHT_CIRCLE] = CalculateCircleTransform( 516 (*transforms_out)[BOTTOM_RIGHT_CIRCLE] = CalculateCircleTransform(
339 painted_layers_[BOTTOM_RIGHT_CIRCLE]->bounds().CenterPoint(), 517 ToRoundedPoint(circle_layer_delegate_->GetCenterPoint()), circle_scale,
340 circle_scale, circle_target_x_offset, circle_target_y_offset); 518 circle_target_x_offset, circle_target_y_offset);
341 519
342 (*transforms_out)[BOTTOM_LEFT_CIRCLE] = CalculateCircleTransform( 520 (*transforms_out)[BOTTOM_LEFT_CIRCLE] = CalculateCircleTransform(
343 painted_layers_[BOTTOM_LEFT_CIRCLE]->bounds().CenterPoint(), circle_scale, 521 ToRoundedPoint(circle_layer_delegate_->GetCenterPoint()), circle_scale,
344 -circle_target_x_offset, circle_target_y_offset); 522 -circle_target_x_offset, circle_target_y_offset);
345 523
346 const float rect_delegate_width = 524 const float rect_delegate_width =
347 static_cast<float>(rect_layer_delegate_->size().width()); 525 static_cast<float>(rect_layer_delegate_->size().width());
348 const float rect_delegate_height = 526 const float rect_delegate_height =
349 static_cast<float>(rect_layer_delegate_->size().height()); 527 static_cast<float>(rect_layer_delegate_->size().height());
350 528
351 (*transforms_out)[HORIZONTAL_RECT] = CalculateRectTransform( 529 (*transforms_out)[HORIZONTAL_RECT] = CalculateRectTransform(
352 painted_layers_[HORIZONTAL_RECT]->bounds().CenterPoint(), 530 ToRoundedPoint(rect_layer_delegate_->GetCenterPoint()),
353 std::max(kMinimumRectScale, size.width() / rect_delegate_width), 531 std::max(kMinimumRectScale, size.width() / rect_delegate_width),
354 std::max(kMinimumRectScale, 532 std::max(kMinimumRectScale,
355 (size.height() - 2.0f * corner_radius) / rect_delegate_height)); 533 (size.height() - 2.0f * corner_radius) / rect_delegate_height));
356 534
357 (*transforms_out)[VERTICAL_RECT] = CalculateRectTransform( 535 (*transforms_out)[VERTICAL_RECT] = CalculateRectTransform(
358 painted_layers_[VERTICAL_RECT]->bounds().CenterPoint(), 536 ToRoundedPoint(rect_layer_delegate_->GetCenterPoint()),
359 std::max(kMinimumRectScale, 537 std::max(kMinimumRectScale,
360 (size.width() - 2.0f * corner_radius) / rect_delegate_width), 538 (size.width() - 2.0f * corner_radius) / rect_delegate_width),
361 std::max(kMinimumRectScale, size.height() / rect_delegate_height)); 539 std::max(kMinimumRectScale, size.height() / rect_delegate_height));
362 } 540 }
363 541
364 void InkDropAnimation::GetCurrentTansforms( 542 void InkDropAnimation::GetCurrentTransforms(
365 InkDropTransforms* transforms_out) const { 543 InkDropTransforms* transforms_out) const {
366 for (int i = 0; i < PAINTED_SHAPE_COUNT; ++i) 544 for (int i = 0; i < PAINTED_SHAPE_COUNT; ++i)
367 (*transforms_out)[i] = painted_layers_[i]->GetTargetTransform(); 545 (*transforms_out)[i] = painted_layers_[i]->transform();
368 } 546 }
369 547
370 void InkDropAnimation::AddPaintLayer(PaintedShape painted_shape) { 548 void InkDropAnimation::AddPaintLayer(PaintedShape painted_shape) {
371 ui::LayerDelegate* delegate = nullptr; 549 ui::LayerDelegate* delegate = nullptr;
372 switch (painted_shape) { 550 switch (painted_shape) {
373 case TOP_LEFT_CIRCLE: 551 case TOP_LEFT_CIRCLE:
374 case TOP_RIGHT_CIRCLE: 552 case TOP_RIGHT_CIRCLE:
375 case BOTTOM_RIGHT_CIRCLE: 553 case BOTTOM_RIGHT_CIRCLE:
376 case BOTTOM_LEFT_CIRCLE: 554 case BOTTOM_LEFT_CIRCLE:
377 delegate = circle_layer_delegate_.get(); 555 delegate = circle_layer_delegate_.get();
(...skipping 19 matching lines...) Expand all
397 575
398 painted_layers_[painted_shape].reset(layer); 576 painted_layers_[painted_shape].reset(layer);
399 } 577 }
400 578
401 void InkDropAnimation::AbortAllAnimations() { 579 void InkDropAnimation::AbortAllAnimations() {
402 root_layer_->GetAnimator()->AbortAllAnimations(); 580 root_layer_->GetAnimator()->AbortAllAnimations();
403 for (int i = 0; i < PAINTED_SHAPE_COUNT; ++i) 581 for (int i = 0; i < PAINTED_SHAPE_COUNT; ++i)
404 painted_layers_[i]->GetAnimator()->AbortAllAnimations(); 582 painted_layers_[i]->GetAnimator()->AbortAllAnimations();
405 } 583 }
406 584
585 float InkDropAnimation::CalculateDistanceEstimateToQuickAction(
586 const InkDropTransforms& transforms) const {
587 gfx::Point3F circle_center_point =
588 gfx::Point3F(circle_layer_delegate_->GetCenterPoint());
589
590 gfx::Point3F circle_top_point(
591 circle_center_point.x(),
592 circle_center_point.y() - circle_layer_delegate_->radius(), 0);
593
594 transforms[TOP_LEFT_CIRCLE].TransformPoint(&circle_center_point);
595 transforms[TOP_LEFT_CIRCLE].TransformPoint(&circle_top_point);
596
597 // Calculate the ratio of how far the transformed circle's center point is
598 // from the destination compared to how far it can be.
599 const float center_point_distance_estimate =
600 1.0f -
601 gfx::Vector3dF(circle_center_point.x(), circle_center_point.y(), 0)
602 .Length() /
603 (gfx::Vector3dF(large_size_.width(), large_size_.height(), 0)
604 .Length() /
605 2.0f);
606
607 // Calculate the ratio of how far the transformed circle's topmost point is
608 // from the destination compared to how far it can be.
609 const float top_point_distance_estimate =
610 -circle_top_point.y() / (large_size_.height() / 2.0f);
611
612 return std::min(center_point_distance_estimate, top_point_distance_estimate);
613 }
614
407 void InkDropAnimation::AnimationStartedCallback( 615 void InkDropAnimation::AnimationStartedCallback(
408 InkDropState ink_drop_state, 616 InkDropState ink_drop_state,
409 const ui::CallbackLayerAnimationObserver& observer) { 617 const ui::CallbackLayerAnimationObserver& observer) {
410 FOR_EACH_OBSERVER(InkDropAnimationObserver, observers_, 618 FOR_EACH_OBSERVER(InkDropAnimationObserver, observers_,
411 InkDropAnimationStarted(ink_drop_state)); 619 InkDropAnimationStarted(ink_drop_state));
412 } 620 }
413 621
414 bool InkDropAnimation::AnimationEndedCallback( 622 bool InkDropAnimation::AnimationEndedCallback(
415 InkDropState ink_drop_state, 623 InkDropState ink_drop_state,
416 const ui::CallbackLayerAnimationObserver& observer) { 624 const ui::CallbackLayerAnimationObserver& observer) {
625 if (ink_drop_state == InkDropState::HIDDEN)
626 SetStateToHidden();
627
417 FOR_EACH_OBSERVER( 628 FOR_EACH_OBSERVER(
418 InkDropAnimationObserver, observers_, 629 InkDropAnimationObserver, observers_,
419 InkDropAnimationEnded(ink_drop_state, 630 InkDropAnimationEnded(ink_drop_state,
420 observer.aborted_count() 631 observer.aborted_count()
421 ? InkDropAnimationObserver::PRE_EMPTED 632 ? InkDropAnimationObserver::PRE_EMPTED
422 : InkDropAnimationObserver::SUCCESS)); 633 : InkDropAnimationObserver::SUCCESS));
423 return true; 634 return true;
424 } 635 }
425 636
426 } // namespace views 637 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698