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

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

Issue 1298513003: Implemented prototype for new ink drop specs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments from patch set 10. Created 5 years, 3 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>
8
7 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/logging.h"
11 #include "third_party/skia/include/core/SkColor.h"
12 #include "third_party/skia/include/core/SkPaint.h"
8 #include "ui/base/ui_base_switches.h" 13 #include "ui/base/ui_base_switches.h"
9 #include "ui/compositor/layer.h" 14 #include "ui/compositor/layer.h"
10 #include "ui/compositor/layer_animation_observer.h" 15 #include "ui/compositor/layer_animation_observer.h"
11 #include "ui/compositor/layer_animation_sequence.h" 16 #include "ui/compositor/layer_animation_sequence.h"
12 #include "ui/compositor/paint_recorder.h" 17 #include "ui/compositor/paint_recorder.h"
13 #include "ui/compositor/scoped_layer_animation_settings.h" 18 #include "ui/compositor/scoped_layer_animation_settings.h"
14 #include "ui/gfx/canvas.h" 19 #include "ui/gfx/canvas.h"
15 #include "ui/gfx/geometry/size.h" 20 #include "ui/gfx/transform_util.h"
16 #include "ui/views/animation/ink_drop_delegate.h"
17 #include "ui/views/view.h" 21 #include "ui/views/view.h"
18 22
19 namespace { 23 namespace {
20 24
21 // Animation constants 25 // The minimum scale factor to use when scaling rectangle layers. Smaller values
22 const float kMinimumScale = 0.1f; 26 // were causing visual anomalies.
23 const float kMinimumScaleCenteringOffset = 0.5f - kMinimumScale / 2.0f; 27 const float kMinimumRectScale = 0.0001f;
24 28
25 const int kHideAnimationDurationFastMs = 100; 29 // The minimum scale factor to use when scaling circle layers. Smaller values
26 const int kHideAnimationDurationSlowMs = 1000; 30 // were causing visual anomalies.
31 const float kMinimumCircleScale = 0.001f;
27 32
28 const int kShowInkDropAnimationDurationFastMs = 250; 33 // The ink drop color.
29 const int kShowInkDropAnimationDurationSlowMs = 750; 34 const SkColor kInkDropColor = SK_ColorBLACK;
30 35
31 const int kShowLongPressAnimationDurationFastMs = 250; 36 // The opacity of the ink drop when it is visible.
32 const int kShowLongPressAnimationDurationSlowMs = 2500; 37 const float kVisibleOpacity = 0.12f;
33 38
34 const int kRoundedRectCorners = 5; 39 // The opacity of the ink drop when it is not visible.
35 const int kCircleRadius = 30; 40 const float kHiddenOpacity = 0.0f;
36 41
37 const SkColor kInkDropColor = SK_ColorLTGRAY; 42 // Durations for the different InkDropState animations in milliseconds.
38 const SkColor kLongPressColor = SkColorSetRGB(182, 182, 182); 43 const int kHiddenStateAnimationDurationMs = 1;
44 const int kActionPendingStateAnimationDurationMs = 500;
45 const int kQuickActionStateAnimationDurationMs = 250;
46 const int kSlowActionPendingStateAnimationDurationMs = 500;
47 const int kSlowActionStateAnimationDurationMs = 250;
48 const int kActivatedStateAnimationDurationMs = 250;
49 const int kDeactivatedStateAnimationDurationMs = 250;
39 50
40 // Checks CommandLine switches to determine if the visual feedback should be 51 // A multiplicative factor used to slow down InkDropState animations.
41 // circular. 52 const int kSlowAnimationDurationFactor = 3;
42 bool UseCircularFeedback() {
43 static bool circular =
44 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
45 (::switches::kMaterialDesignInkDrop)) !=
46 ::switches::kMaterialDesignInkDropSquare;
47 return circular;
48 }
49 53
50 // Checks CommandLine switches to determine if the visual feedback should have 54 // Checks CommandLine switches to determine if the visual feedback should have
51 // a fast animations speed. 55 // a fast animations speed.
52 bool UseFastAnimations() { 56 bool UseFastAnimations() {
53 static bool fast = 57 static bool fast =
54 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 58 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
55 (::switches::kMaterialDesignInkDropAnimationSpeed)) != 59 (::switches::kMaterialDesignInkDropAnimationSpeed)) !=
56 ::switches::kMaterialDesignInkDropAnimationSpeedSlow; 60 ::switches::kMaterialDesignInkDropAnimationSpeedSlow;
57 return fast; 61 return fast;
58 } 62 }
59 63
64 // Returns the InkDropState animation duration for the given |state|.
65 base::TimeDelta GetAnimationDuration(views::InkDropState state) {
66 int duration = 0;
67 switch (state) {
68 case views::InkDropState::HIDDEN:
69 duration = kHiddenStateAnimationDurationMs;
70 break;
71 case views::InkDropState::ACTION_PENDING:
72 duration = kActionPendingStateAnimationDurationMs;
73 break;
74 case views::InkDropState::QUICK_ACTION:
75 duration = kQuickActionStateAnimationDurationMs;
76 break;
77 case views::InkDropState::SLOW_ACTION_PENDING:
78 duration = kSlowActionPendingStateAnimationDurationMs;
79 break;
80 case views::InkDropState::SLOW_ACTION:
81 duration = kSlowActionStateAnimationDurationMs;
82 break;
83 case views::InkDropState::ACTIVATED:
84 duration = kActivatedStateAnimationDurationMs;
85 break;
86 case views::InkDropState::DEACTIVATED:
87 duration = kDeactivatedStateAnimationDurationMs;
88 break;
89 }
90
91 return base::TimeDelta::FromMilliseconds(
92 (UseFastAnimations() ? 1 : kSlowAnimationDurationFactor) * duration);
93 }
94
95 // Calculates a Transform for a circle layer. The transform will be set up to
96 // translate the |drawn_center_point| to the origin, scale, and then translate
97 // to the target point defined by |target_center_x| and |target_center_y|.
98 gfx::Transform CalculateCircleTransform(const gfx::Point& drawn_center_point,
99 float scale,
100 float target_center_x,
101 float target_center_y) {
102 gfx::Transform transform;
103 transform.Translate(target_center_x, target_center_y);
104 transform.Scale(scale, scale);
105 transform.Translate(-drawn_center_point.x(), -drawn_center_point.y());
106 return transform;
107 }
108
109 // Calculates a Transform for a rectangle layer. The transform will be set up to
110 // translate the |drawn_center_point| to the origin and then scale by the
111 // |x_scale| and |y_scale| factors.
112 gfx::Transform CalculateRectTransform(const gfx::Point& drawn_center_point,
113 float x_scale,
114 float y_scale) {
115 gfx::Transform transform;
116 transform.Scale(x_scale, y_scale);
117 transform.Translate(-drawn_center_point.x(), -drawn_center_point.y());
118 return transform;
119 }
120
60 } // namespace 121 } // namespace
61 122
62 namespace views { 123 namespace views {
63 124
64 // An animation observer that should be set on animations of the provided 125 // Base ui::LayerDelegate stub that can be extended to paint shapes of a
65 // ui::Layer. Can be used to either start a hide animation, or to trigger one 126 // specific color.
66 // upon completion of the current animation. 127 class BasePaintedLayerDelegate : public ui::LayerDelegate {
67 //
68 // Sequential animations with PreemptionStrategy::ENQUEUE_NEW_ANIMATION cannot
69 // be used as the observed animation can complete before user input is received
70 // which determines if the hide animation should run.
71 class AppearAnimationObserver : public ui::LayerAnimationObserver {
72 public: 128 public:
73 // Will automatically start a hide animation of |layer| if |hide| is true. 129 ~BasePaintedLayerDelegate() override;
74 // Otherwise StartHideAnimation() or HideNowIfDoneOrOnceCompleted() must be 130
75 // called. 131 SkColor color() const { return color_; }
76 AppearAnimationObserver(ui::Layer* layer, bool hide); 132
77 ~AppearAnimationObserver() override; 133 // ui::LayerDelegate:
78 134 void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip) override;
79 // Returns true during both the appearing animation, and the hiding animation. 135 void OnDeviceScaleFactorChanged(float device_scale_factor) override;
80 bool IsAnimationActive(); 136 base::Closure PrepareForLayerBoundsChange() override;
81 137
82 // Starts a hide animation, preempting any current animations on |layer_|. 138 protected:
83 void StartHideAnimation(); 139 explicit BasePaintedLayerDelegate(SkColor color);
84
85 // Starts a hide animation if |layer_| is no longer animating. Otherwise the
86 // hide animation will be started once the current animation is completed.
87 void HideNowIfDoneOrOnceCompleted();
88
89 // Hides |background_layer| (without animation) after the current animation
90 // completes.
91 void SetBackgroundToHide(ui::Layer* background_layer);
92 140
93 private: 141 private:
94 // ui::ImplicitAnimationObserver: 142 // The color to paint.
95 void OnLayerAnimationEnded(ui::LayerAnimationSequence* sequence) override; 143 SkColor color_;
96 void OnLayerAnimationAborted(ui::LayerAnimationSequence* sequence) override; 144
97 void OnLayerAnimationScheduled( 145 DISALLOW_COPY_AND_ASSIGN(BasePaintedLayerDelegate);
98 ui::LayerAnimationSequence* sequence) override {}
99
100 bool RequiresNotificationWhenAnimatorDestroyed() const override;
101
102 // The ui::Layer being observed, which hide animations will be set on.
103 ui::Layer* layer_;
104
105 // Optional ui::Layer which will be hidden upon the completion of animating
106 // |layer_|
107 ui::Layer* background_layer_;
108
109 // If true the hide animation will immediately be scheduled upon completion of
110 // the observed animation.
111 bool hide_;
112
113 DISALLOW_COPY_AND_ASSIGN(AppearAnimationObserver);
114 }; 146 };
115 147
116 AppearAnimationObserver::AppearAnimationObserver(ui::Layer* layer, bool hide) 148 BasePaintedLayerDelegate::BasePaintedLayerDelegate(SkColor color)
117 : layer_(layer), background_layer_(nullptr), hide_(hide) {} 149 : color_(color) {}
118 150
119 AppearAnimationObserver::~AppearAnimationObserver() { 151 BasePaintedLayerDelegate::~BasePaintedLayerDelegate() {}
120 StopObserving(); 152
121 } 153 void BasePaintedLayerDelegate::OnDelegatedFrameDamage(
122 154 const gfx::Rect& damage_rect_in_dip) {}
123 bool AppearAnimationObserver::IsAnimationActive() { 155
124 // Initial animation ongoing 156 void BasePaintedLayerDelegate::OnDeviceScaleFactorChanged(
125 if (!attached_sequences().empty()) 157 float device_scale_factor) {}
126 return true; 158
127 // Maintain the animation until told to hide. 159 base::Closure BasePaintedLayerDelegate::PrepareForLayerBoundsChange() {
128 if (!hide_) 160 return base::Closure();
129 return true; 161 }
130 162
131 // Check the state of the triggered hide animation 163 // A BasePaintedLayerDelegate that paints a circle of a specified color and
132 return layer_->GetAnimator()->IsAnimatingProperty( 164 // radius.
133 ui::LayerAnimationElement::OPACITY) && 165 class CircleLayerDelegate : public BasePaintedLayerDelegate {
134 layer_->GetTargetOpacity() == 0.0f && 166 public:
135 layer_->GetAnimator()->IsAnimatingProperty( 167 CircleLayerDelegate(SkColor color, int radius);
136 ui::LayerAnimationElement::VISIBILITY) && 168 ~CircleLayerDelegate() override;
137 !layer_->GetTargetVisibility(); 169
138 } 170 int radius() const { return radius_; }
139 171
140 void AppearAnimationObserver::StartHideAnimation() { 172 // ui::LayerDelegate:
141 if (background_layer_) 173 void OnPaintLayer(const ui::PaintContext& context) override;
142 background_layer_->SetVisible(false); 174
143 if (!layer_->GetTargetVisibility()) 175 private:
176 // The radius of the circle.
177 int radius_;
178
179 DISALLOW_COPY_AND_ASSIGN(CircleLayerDelegate);
180 };
181
182 CircleLayerDelegate::CircleLayerDelegate(SkColor color, int radius)
183 : BasePaintedLayerDelegate(color), radius_(radius) {}
184
185 CircleLayerDelegate::~CircleLayerDelegate() {}
186
187 void CircleLayerDelegate::OnPaintLayer(const ui::PaintContext& context) {
188 SkPaint paint;
189 paint.setColor(color());
190 paint.setFlags(SkPaint::kAntiAlias_Flag);
191 paint.setStyle(SkPaint::kFill_Style);
192
193 ui::PaintRecorder recorder(context, gfx::Size(radius_, radius_));
194 gfx::Canvas* canvas = recorder.canvas();
195
196 gfx::Point center_point = gfx::Point(radius_, radius_);
197 canvas->DrawCircle(center_point, radius_, paint);
198 }
199
200 // A BasePaintedLayerDelegate that paints a rectangle of a specified color and
201 // size.
202 class RectangleLayerDelegate : public BasePaintedLayerDelegate {
203 public:
204 RectangleLayerDelegate(SkColor color, gfx::Size size);
205 ~RectangleLayerDelegate() override;
206
207 const gfx::Size& size() const { return size_; }
208
209 // ui::LayerDelegate:
210 void OnPaintLayer(const ui::PaintContext& context) override;
211
212 private:
213 // The size of the rectangle.
214 gfx::Size size_;
215
216 DISALLOW_COPY_AND_ASSIGN(RectangleLayerDelegate);
217 };
218
219 RectangleLayerDelegate::RectangleLayerDelegate(SkColor color, gfx::Size size)
220 : BasePaintedLayerDelegate(color), size_(size) {}
221
222 RectangleLayerDelegate::~RectangleLayerDelegate() {}
223
224 void RectangleLayerDelegate::OnPaintLayer(const ui::PaintContext& context) {
225 SkPaint paint;
226 paint.setColor(color());
227 paint.setFlags(SkPaint::kAntiAlias_Flag);
228 paint.setStyle(SkPaint::kFill_Style);
229
230 ui::PaintRecorder recorder(context, size_);
231 gfx::Canvas* canvas = recorder.canvas();
232 canvas->DrawRect(gfx::Rect(size_), paint);
233 }
234
235 InkDropAnimation::InkDropAnimation(const gfx::Size& large_size,
236 int large_corner_radius,
237 const gfx::Size& small_size,
238 int small_corner_radius)
239 : large_size_(large_size),
240 large_corner_radius_(large_corner_radius),
241 small_size_(small_size),
242 small_corner_radius_(small_corner_radius),
243 circle_layer_delegate_(new CircleLayerDelegate(
244 kInkDropColor,
245 std::min(large_size_.width(), large_size_.height()) / 2)),
246 rect_layer_delegate_(
247 new RectangleLayerDelegate(kInkDropColor, large_size_)),
248 root_layer_(new ui::Layer(ui::LAYER_NOT_DRAWN)),
249 ink_drop_state_(InkDropState::HIDDEN) {
250 for (int i = 0; i < PAINTED_SHAPE_COUNT; ++i)
251 AddPaintLayer(static_cast<PaintedShape>(i));
252
253 root_layer_->SetMasksToBounds(false);
254 root_layer_->SetBounds(gfx::Rect(large_size_));
255
256 ResetTransformsToMinSize();
257
258 SetOpacity(kHiddenOpacity);
259 }
260
261 InkDropAnimation::~InkDropAnimation() {}
262
263 void InkDropAnimation::AnimateToState(InkDropState ink_drop_state) {
264 if (ink_drop_state_ == ink_drop_state)
144 return; 265 return;
145 266
146 ui::ScopedLayerAnimationSettings animation(layer_->GetAnimator()); 267 if (ink_drop_state_ == InkDropState::HIDDEN) {
147 animation.SetTransitionDuration(base::TimeDelta::FromMilliseconds( 268 ResetTransformsToMinSize();
148 UseFastAnimations() ? kHideAnimationDurationFastMs 269 SetOpacity(kVisibleOpacity);
149 : kHideAnimationDurationSlowMs)); 270 }
150 animation.SetPreemptionStrategy( 271
151 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); 272 InkDropTransforms transforms;
152 layer_->SetOpacity(0.0f); 273
153 layer_->SetVisible(false); 274 // Must set the |ink_drop_state_| before handling the state change because
154 } 275 // some state changes make recursive calls to AnimateToState() and the last
155 276 // call should 'win'.
156 void AppearAnimationObserver::HideNowIfDoneOrOnceCompleted() { 277 ink_drop_state_ = ink_drop_state;
157 hide_ = true; 278
158 if (attached_sequences().empty()) 279 switch (ink_drop_state_) {
159 StartHideAnimation();
160 }
161
162 void AppearAnimationObserver::SetBackgroundToHide(ui::Layer* background_layer) {
163 background_layer_ = background_layer;
164 }
165
166 void AppearAnimationObserver::OnLayerAnimationEnded(
167 ui::LayerAnimationSequence* sequence) {
168 if (hide_)
169 StartHideAnimation();
170 }
171
172 void AppearAnimationObserver::OnLayerAnimationAborted(
173 ui::LayerAnimationSequence* sequence) {
174 if (hide_)
175 StartHideAnimation();
176 }
177
178 bool AppearAnimationObserver::RequiresNotificationWhenAnimatorDestroyed()
179 const {
180 // Ensures that OnImplicitAnimationsCompleted is called even if the observed
181 // animation is deleted. Allows for setting the proper state on |layer_|.
182 return true;
183 }
184
185 InkDropAnimation::InkDropAnimation()
186 : root_layer_(new ui::Layer(ui::LAYER_NOT_DRAWN)),
187 ink_drop_layer_(new ui::Layer()),
188 appear_animation_observer_(nullptr),
189 long_press_layer_(new ui::Layer()),
190 long_press_animation_observer_(nullptr),
191 ink_drop_bounds_(0, 0, 0, 0) {
192 ink_drop_delegate_.reset(new InkDropDelegate(ink_drop_layer_.get(),
193 kInkDropColor, kCircleRadius,
194 kRoundedRectCorners));
195 long_press_delegate_.reset(new InkDropDelegate(long_press_layer_.get(),
196 kLongPressColor, kCircleRadius,
197 kRoundedRectCorners));
198
199 SetupAnimationLayer(long_press_layer_.get(), long_press_delegate_.get());
200 SetupAnimationLayer(ink_drop_layer_.get(), ink_drop_delegate_.get());
201
202 root_layer_->Add(ink_drop_layer_.get());
203 root_layer_->Add(long_press_layer_.get());
204 }
205
206 InkDropAnimation::~InkDropAnimation() {}
207
208 void InkDropAnimation::AnimateToState(InkDropState state) {
209 // TODO(bruthig): Do not transition if we are already in |state| and restrict
210 // any state transition that don't make sense or wouldn't look visually
211 // appealing.
212 switch (state) {
213 case InkDropState::HIDDEN: 280 case InkDropState::HIDDEN:
214 AnimateHide(); 281 GetCurrentTansforms(&transforms);
282 AnimateToTransforms(transforms, kHiddenOpacity,
283 GetAnimationDuration(InkDropState::HIDDEN),
284 ui::LayerAnimator::ENQUEUE_NEW_ANIMATION);
215 break; 285 break;
216 case InkDropState::ACTION_PENDING: 286 case InkDropState::ACTION_PENDING:
217 AnimateTapDown(); 287 CalculateCircleTransforms(large_size_, &transforms);
288 AnimateToTransforms(transforms, kVisibleOpacity,
289 GetAnimationDuration(InkDropState::ACTION_PENDING),
290 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
218 break; 291 break;
219 case InkDropState::QUICK_ACTION: 292 case InkDropState::QUICK_ACTION:
220 AnimateTapDown(); 293 CalculateCircleTransforms(large_size_, &transforms);
221 AnimateHide(); 294 AnimateToTransforms(transforms, kHiddenOpacity,
295 GetAnimationDuration(InkDropState::QUICK_ACTION),
296 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
297 AnimateToState(InkDropState::HIDDEN);
298 break;
299 case InkDropState::SLOW_ACTION_PENDING:
300 CalculateRectTransforms(small_size_, small_corner_radius_, &transforms);
301 AnimateToTransforms(
302 transforms, kVisibleOpacity,
303 GetAnimationDuration(InkDropState::SLOW_ACTION_PENDING),
304 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
222 break; 305 break;
223 case InkDropState::SLOW_ACTION: 306 case InkDropState::SLOW_ACTION:
224 AnimateLongPress(); 307 CalculateRectTransforms(large_size_, large_corner_radius_, &transforms);
308 AnimateToTransforms(transforms, kHiddenOpacity,
309 GetAnimationDuration(InkDropState::SLOW_ACTION),
310 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
311 AnimateToState(InkDropState::HIDDEN);
225 break; 312 break;
226 case InkDropState::ACTIVATED: 313 case InkDropState::ACTIVATED:
227 AnimateLongPress(); 314 CalculateRectTransforms(small_size_, small_corner_radius_, &transforms);
315 AnimateToTransforms(transforms, kVisibleOpacity,
316 GetAnimationDuration(InkDropState::ACTIVATED),
317 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
318 break;
319 case InkDropState::DEACTIVATED:
320 CalculateRectTransforms(large_size_, large_corner_radius_, &transforms);
321 AnimateToTransforms(transforms, kHiddenOpacity,
322 GetAnimationDuration(InkDropState::DEACTIVATED),
323 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
324 AnimateToState(InkDropState::HIDDEN);
228 break; 325 break;
229 } 326 }
230 } 327 }
231 328
232 void InkDropAnimation::SetInkDropSize(const gfx::Size& size) { 329 void InkDropAnimation::AnimateToTransforms(
233 SetInkDropBounds(gfx::Rect(ink_drop_bounds_.origin(), size)); 330 const InkDropTransforms transforms,
234 } 331 float opacity,
235 332 base::TimeDelta duration,
236 gfx::Rect InkDropAnimation::GetInkDropBounds() const { 333 ui::LayerAnimator::PreemptionStrategy preemption_strategy) {
237 return ink_drop_bounds_; 334 ui::LayerAnimator* root_animator = root_layer_->GetAnimator();
238 } 335 ui::ScopedLayerAnimationSettings root_animation(root_animator);
239 336 root_animation.SetPreemptionStrategy(preemption_strategy);
240 void InkDropAnimation::SetInkDropBounds(const gfx::Rect& bounds) { 337 ui::LayerAnimationElement* root_element =
241 ink_drop_bounds_ = bounds; 338 ui::LayerAnimationElement::CreateOpacityElement(opacity, duration);
242 SetLayerBounds(ink_drop_layer_.get()); 339 ui::LayerAnimationSequence* root_sequence =
243 SetLayerBounds(long_press_layer_.get()); 340 new ui::LayerAnimationSequence(root_element);
244 } 341 root_animator->StartAnimation(root_sequence);
245 342
246 void InkDropAnimation::AnimateTapDown() { 343 for (int i = 0; i < PAINTED_SHAPE_COUNT; ++i) {
247 if ((appear_animation_observer_ && 344 ui::LayerAnimator* animator = painted_layers_[i]->GetAnimator();
248 appear_animation_observer_->IsAnimationActive()) || 345 ui::ScopedLayerAnimationSettings animation(animator);
249 (long_press_animation_observer_ && 346 animation.SetPreemptionStrategy(preemption_strategy);
250 long_press_animation_observer_->IsAnimationActive())) { 347 ui::LayerAnimationElement* element =
251 // Only one animation at a time. Subsequent tap downs are ignored until the 348 ui::LayerAnimationElement::CreateTransformElement(transforms[i],
252 // current animation completes. 349 duration);
253 return; 350 ui::LayerAnimationSequence* sequence =
351 new ui::LayerAnimationSequence(element);
352 animator->StartAnimation(sequence);
254 } 353 }
255 appear_animation_observer_.reset( 354 }
256 new AppearAnimationObserver(ink_drop_layer_.get(), false)); 355
257 AnimateShow(ink_drop_layer_.get(), appear_animation_observer_.get(), 356 void InkDropAnimation::ResetTransformsToMinSize() {
258 base::TimeDelta::FromMilliseconds( 357 InkDropTransforms transforms;
259 (UseFastAnimations() ? kShowInkDropAnimationDurationFastMs 358 // Using a size of 0x0 creates visual anomalies.
260 : kShowInkDropAnimationDurationSlowMs))); 359 CalculateCircleTransforms(gfx::Size(1, 1), &transforms);
261 } 360 SetTransforms(transforms);
262 361 }
263 void InkDropAnimation::AnimateHide() { 362
264 if (appear_animation_observer_ && 363 void InkDropAnimation::SetTransforms(const InkDropTransforms transforms) {
265 appear_animation_observer_->IsAnimationActive()) { 364 for (int i = 0; i < PAINTED_SHAPE_COUNT; ++i)
266 appear_animation_observer_->HideNowIfDoneOrOnceCompleted(); 365 painted_layers_[i]->SetTransform(transforms[i]);
267 } else if (long_press_animation_observer_) { 366 }
268 long_press_animation_observer_->HideNowIfDoneOrOnceCompleted(); 367
368 void InkDropAnimation::SetOpacity(float opacity) {
369 root_layer_->SetOpacity(opacity);
370 }
371
372 void InkDropAnimation::CalculateCircleTransforms(
373 const gfx::SizeF& size,
374 InkDropTransforms* transforms_out) const {
375 CalculateRectTransforms(size, std::min(size.width(), size.height()) / 2.0f,
376 transforms_out);
377 }
378
379 void InkDropAnimation::CalculateRectTransforms(
380 const gfx::SizeF& size,
381 float corner_radius,
382 InkDropTransforms* transforms_out) const {
383 DCHECK_GE(size.width() / 2.0f, corner_radius)
384 << "The circle's diameter should not be greater than the total width.";
385 DCHECK_GE(size.height() / 2.0f, corner_radius)
386 << "The circle's diameter should not be greater than the total height.";
387
388 // The shapes are drawn such that their center points are not at the origin.
389 // Thus we use the CalculateCircleTransform() and CalculateRectTransform()
390 // methods to calculate the complex Transforms.
391
392 const float circle_scale = std::max(
393 kMinimumCircleScale,
394 corner_radius / static_cast<float>(circle_layer_delegate_->radius()));
395
396 const float circle_target_x_offset = size.width() / 2.0f - corner_radius;
397 const float circle_target_y_offset = size.height() / 2.0f - corner_radius;
398
399 (*transforms_out)[TOP_LEFT_CIRCLE] = CalculateCircleTransform(
400 painted_layers_[TOP_LEFT_CIRCLE]->bounds().CenterPoint(), circle_scale,
401 -circle_target_x_offset, -circle_target_y_offset);
402
403 (*transforms_out)[TOP_RIGHT_CIRCLE] = CalculateCircleTransform(
404 painted_layers_[TOP_RIGHT_CIRCLE]->bounds().CenterPoint(), circle_scale,
405 circle_target_x_offset, -circle_target_y_offset);
406
407 (*transforms_out)[BOTTOM_RIGHT_CIRCLE] = CalculateCircleTransform(
408 painted_layers_[BOTTOM_RIGHT_CIRCLE]->bounds().CenterPoint(),
409 circle_scale, circle_target_x_offset, circle_target_y_offset);
410
411 (*transforms_out)[BOTTOM_LEFT_CIRCLE] = CalculateCircleTransform(
412 painted_layers_[BOTTOM_LEFT_CIRCLE]->bounds().CenterPoint(), circle_scale,
413 -circle_target_x_offset, circle_target_y_offset);
414
415 const float rect_delegate_width =
416 static_cast<float>(rect_layer_delegate_->size().width());
417 const float rect_delegate_height =
418 static_cast<float>(rect_layer_delegate_->size().height());
419
420 (*transforms_out)[HORIZONTAL_RECT] = CalculateRectTransform(
421 painted_layers_[HORIZONTAL_RECT]->bounds().CenterPoint(),
422 std::max(kMinimumRectScale, size.width() / rect_delegate_width),
423 std::max(kMinimumRectScale,
424 (size.height() - 2.0f * corner_radius) / rect_delegate_height));
425
426 (*transforms_out)[VERTICAL_RECT] = CalculateRectTransform(
427 painted_layers_[VERTICAL_RECT]->bounds().CenterPoint(),
428 std::max(kMinimumRectScale,
429 (size.width() - 2.0f * corner_radius) / rect_delegate_width),
430 std::max(kMinimumRectScale, size.height() / rect_delegate_height));
431 }
432
433 void InkDropAnimation::GetCurrentTansforms(
434 InkDropTransforms* transforms_out) const {
435 for (int i = 0; i < PAINTED_SHAPE_COUNT; ++i)
436 (*transforms_out)[i] = painted_layers_[i]->GetTargetTransform();
437 }
438
439 void InkDropAnimation::SetCenterPoint(const gfx::Point& center_point) {
440 gfx::Transform transform;
441 transform.Translate(center_point.x(), center_point.y());
442 root_layer_->SetTransform(transform);
443 }
444
445 void InkDropAnimation::AddPaintLayer(PaintedShape painted_shape) {
446 ui::LayerDelegate* delegate = nullptr;
447 switch (painted_shape) {
448 case TOP_LEFT_CIRCLE:
449 case TOP_RIGHT_CIRCLE:
450 case BOTTOM_RIGHT_CIRCLE:
451 case BOTTOM_LEFT_CIRCLE:
452 delegate = circle_layer_delegate_.get();
453 break;
454 case HORIZONTAL_RECT:
455 case VERTICAL_RECT:
456 delegate = rect_layer_delegate_.get();
457 break;
458 case PAINTED_SHAPE_COUNT:
459 NOTREACHED() << "PAINTED_SHAPE_COUNT is not an actual shape type.";
460 break;
269 } 461 }
270 } 462
271 463 ui::Layer* layer = new ui::Layer();
272 void InkDropAnimation::AnimateLongPress() { 464 root_layer_->Add(layer);
273 // Only one animation at a time. Subsequent long presses are ignored until the 465
274 // current animation completes. 466 layer->SetBounds(gfx::Rect(large_size_));
275 if (long_press_animation_observer_ &&
276 long_press_animation_observer_->IsAnimationActive()) {
277 return;
278 }
279 appear_animation_observer_.reset();
280 long_press_animation_observer_.reset(
281 new AppearAnimationObserver(long_press_layer_.get(), false));
282 long_press_animation_observer_->SetBackgroundToHide(ink_drop_layer_.get());
283 AnimateShow(long_press_layer_.get(), long_press_animation_observer_.get(),
284 base::TimeDelta::FromMilliseconds(
285 UseFastAnimations() ? kShowLongPressAnimationDurationFastMs
286 : kShowLongPressAnimationDurationSlowMs));
287 }
288
289 void InkDropAnimation::AnimateShow(ui::Layer* layer,
290 AppearAnimationObserver* observer,
291 base::TimeDelta duration) {
292 layer->SetVisible(true);
293 layer->SetOpacity(1.0f);
294
295 float start_x = ink_drop_bounds_.x() +
296 layer->bounds().width() * kMinimumScaleCenteringOffset;
297 float start_y = ink_drop_bounds_.y() +
298 layer->bounds().height() * kMinimumScaleCenteringOffset;
299
300 gfx::Transform initial_transform;
301 initial_transform.Translate(start_x, start_y);
302 initial_transform.Scale(kMinimumScale, kMinimumScale);
303 layer->SetTransform(initial_transform);
304
305 ui::LayerAnimator* animator = layer->GetAnimator();
306 ui::ScopedLayerAnimationSettings animation(animator);
307 animation.SetPreemptionStrategy(
308 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
309
310 gfx::Transform target_transform;
311 target_transform.Translate(ink_drop_bounds_.x(), ink_drop_bounds_.y());
312 ui::LayerAnimationElement* element =
313 ui::LayerAnimationElement::CreateTransformElement(target_transform,
314 duration);
315 ui::LayerAnimationSequence* sequence =
316 new ui::LayerAnimationSequence(element);
317 sequence->AddObserver(observer);
318 animator->StartAnimation(sequence);
319 }
320
321 void InkDropAnimation::SetLayerBounds(ui::Layer* layer) {
322 bool circle = UseCircularFeedback();
323 gfx::Size size = ink_drop_bounds_.size();
324 float circle_width = circle ? 2.0f * kCircleRadius : size.width();
325 float circle_height = circle ? 2.0f * kCircleRadius : size.height();
326 float circle_x = circle ? (size.width() - circle_width) * 0.5f : 0;
327 float circle_y = circle ? (size.height() - circle_height) * 0.5f : 0;
328 layer->SetBounds(gfx::Rect(circle_x, circle_y, circle_width, circle_height));
329 }
330
331 void InkDropAnimation::SetupAnimationLayer(ui::Layer* layer,
332 InkDropDelegate* delegate) {
333 layer->SetFillsBoundsOpaquely(false); 467 layer->SetFillsBoundsOpaquely(false);
334 layer->set_delegate(delegate); 468 layer->set_delegate(delegate);
335 layer->SetVisible(false); 469 layer->SetVisible(true);
336 layer->SetBounds(gfx::Rect()); 470 layer->SetOpacity(1.0);
337 delegate->set_should_render_circle(UseCircularFeedback()); 471 layer->SetMasksToBounds(false);
472
473 painted_layers_[painted_shape].reset(layer);
338 } 474 }
339 475
340 } // namespace views 476 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/animation/ink_drop_animation.h ('k') | ui/views/animation/ink_drop_animation_controller.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698