Chromium Code Reviews| OLD | NEW |
|---|---|
| 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_controller_impl.h" | 5 #include "ui/views/animation/ink_drop_animation.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "ui/base/ui_base_switches.h" | 8 #include "ui/base/ui_base_switches.h" |
| 9 #include "ui/compositor/layer.h" | 9 #include "ui/compositor/layer.h" |
| 10 #include "ui/compositor/layer_animation_observer.h" | 10 #include "ui/compositor/layer_animation_observer.h" |
| 11 #include "ui/compositor/layer_animation_sequence.h" | 11 #include "ui/compositor/layer_animation_sequence.h" |
| 12 #include "ui/compositor/paint_recorder.h" | 12 #include "ui/compositor/paint_recorder.h" |
| 13 #include "ui/compositor/scoped_layer_animation_settings.h" | 13 #include "ui/compositor/scoped_layer_animation_settings.h" |
| 14 #include "ui/events/event.h" | |
| 15 #include "ui/gfx/canvas.h" | 14 #include "ui/gfx/canvas.h" |
| 16 #include "ui/gfx/geometry/size.h" | 15 #include "ui/gfx/geometry/size.h" |
| 17 #include "ui/views/animation/ink_drop_delegate.h" | 16 #include "ui/views/animation/ink_drop_delegate.h" |
| 18 #include "ui/views/animation/ink_drop_host.h" | |
| 19 #include "ui/views/view.h" | 17 #include "ui/views/view.h" |
| 20 | 18 |
| 21 namespace { | 19 namespace { |
| 22 | 20 |
| 23 // Animation constants | 21 // Animation constants |
| 24 const float kMinimumScale = 0.1f; | 22 const float kMinimumScale = 0.1f; |
| 25 const float kMinimumScaleCenteringOffset = 0.5f - kMinimumScale / 2.0f; | 23 const float kMinimumScaleCenteringOffset = 0.5f - kMinimumScale / 2.0f; |
| 26 | 24 |
| 27 const int kHideAnimationDurationFastMs = 100; | 25 const int kHideAnimationDurationFastMs = 100; |
| 28 const int kHideAnimationDurationSlowMs = 1000; | 26 const int kHideAnimationDurationSlowMs = 1000; |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 177 StartHideAnimation(); | 175 StartHideAnimation(); |
| 178 } | 176 } |
| 179 | 177 |
| 180 bool AppearAnimationObserver::RequiresNotificationWhenAnimatorDestroyed() | 178 bool AppearAnimationObserver::RequiresNotificationWhenAnimatorDestroyed() |
| 181 const { | 179 const { |
| 182 // Ensures that OnImplicitAnimationsCompleted is called even if the observed | 180 // Ensures that OnImplicitAnimationsCompleted is called even if the observed |
| 183 // animation is deleted. Allows for setting the proper state on |layer_|. | 181 // animation is deleted. Allows for setting the proper state on |layer_|. |
| 184 return true; | 182 return true; |
| 185 } | 183 } |
| 186 | 184 |
| 187 InkDropAnimationControllerImpl::InkDropAnimationControllerImpl( | 185 InkDropAnimation::InkDropAnimation() |
| 188 InkDropHost* ink_drop_host) | 186 : root_layer_(new ui::Layer(ui::LAYER_NOT_DRAWN)), |
| 189 : ink_drop_host_(ink_drop_host), | |
| 190 root_layer_(new ui::Layer(ui::LAYER_NOT_DRAWN)), | |
| 191 ink_drop_layer_(new ui::Layer()), | 187 ink_drop_layer_(new ui::Layer()), |
| 192 appear_animation_observer_(nullptr), | 188 appear_animation_observer_(nullptr), |
| 193 long_press_layer_(new ui::Layer()), | 189 long_press_layer_(new ui::Layer()), |
| 194 long_press_animation_observer_(nullptr), | 190 long_press_animation_observer_(nullptr), |
| 195 ink_drop_bounds_(0, 0, 0, 0) { | 191 ink_drop_bounds_(0, 0, 0, 0) { |
| 196 ink_drop_delegate_.reset(new InkDropDelegate(ink_drop_layer_.get(), | 192 ink_drop_delegate_.reset(new InkDropDelegate(ink_drop_layer_.get(), |
| 197 kInkDropColor, kCircleRadius, | 193 kInkDropColor, kCircleRadius, |
| 198 kRoundedRectCorners)); | 194 kRoundedRectCorners)); |
| 199 long_press_delegate_.reset(new InkDropDelegate(long_press_layer_.get(), | 195 long_press_delegate_.reset(new InkDropDelegate(long_press_layer_.get(), |
| 200 kLongPressColor, kCircleRadius, | 196 kLongPressColor, kCircleRadius, |
| 201 kRoundedRectCorners)); | 197 kRoundedRectCorners)); |
| 202 | 198 |
| 203 SetupAnimationLayer(long_press_layer_.get(), long_press_delegate_.get()); | 199 SetupAnimationLayer(long_press_layer_.get(), long_press_delegate_.get()); |
| 204 SetupAnimationLayer(ink_drop_layer_.get(), ink_drop_delegate_.get()); | 200 SetupAnimationLayer(ink_drop_layer_.get(), ink_drop_delegate_.get()); |
| 205 | 201 |
| 206 root_layer_->Add(ink_drop_layer_.get()); | 202 root_layer_->Add(ink_drop_layer_.get()); |
| 207 root_layer_->Add(long_press_layer_.get()); | 203 root_layer_->Add(long_press_layer_.get()); |
| 208 | |
| 209 // TODO(bruthig): Change this to only be called before the ink drop becomes | |
| 210 // active. See www.crbug.com/522175. | |
| 211 ink_drop_host_->AddInkDropLayer(root_layer_.get()); | |
| 212 } | 204 } |
| 213 | 205 |
| 214 InkDropAnimationControllerImpl::~InkDropAnimationControllerImpl() { | 206 InkDropAnimation::~InkDropAnimation() {} |
| 215 // TODO(bruthig): Change this to be called when the ink drop becomes hidden. | 207 |
| 216 // See www.crbug.com/522175. | 208 ui::Layer* InkDropAnimation::GetRootLayer() { |
|
jonross
2015/08/19 21:41:01
This is just a simple accessor.
Change to:
ui::L
bruthig
2015/08/20 12:16:21
Done.
| |
| 217 ink_drop_host_->RemoveInkDropLayer(root_layer_.get()); | 209 return root_layer_.get(); |
| 218 } | 210 } |
| 219 | 211 |
| 220 void InkDropAnimationControllerImpl::AnimateToState(InkDropState state) { | 212 void InkDropAnimation::AnimateToState(InkDropState state) { |
| 221 // TODO(bruthig): Do not transition if we are already in |state| and restrict | 213 // TODO(bruthig): Do not transition if we are already in |state| and restrict |
| 222 // any state transition that don't make sense or wouldn't look visually | 214 // any state transition that don't make sense or wouldn't look visually |
| 223 // appealing. | 215 // appealing. |
| 224 switch (state) { | 216 switch (state) { |
| 225 case InkDropState::HIDDEN: | 217 case InkDropState::HIDDEN: |
| 226 AnimateHide(); | 218 AnimateHide(); |
| 227 break; | 219 break; |
| 228 case InkDropState::ACTION_PENDING: | 220 case InkDropState::ACTION_PENDING: |
| 229 AnimateTapDown(); | 221 AnimateTapDown(); |
| 230 break; | 222 break; |
| 231 case InkDropState::QUICK_ACTION: | 223 case InkDropState::QUICK_ACTION: |
| 232 AnimateTapDown(); | 224 AnimateTapDown(); |
| 233 AnimateHide(); | 225 AnimateHide(); |
| 234 break; | 226 break; |
| 235 case InkDropState::SLOW_ACTION: | 227 case InkDropState::SLOW_ACTION: |
| 236 AnimateLongPress(); | 228 AnimateLongPress(); |
| 237 break; | 229 break; |
| 238 case InkDropState::ACTIVATED: | 230 case InkDropState::ACTIVATED: |
| 239 AnimateLongPress(); | 231 AnimateLongPress(); |
| 240 break; | 232 break; |
| 241 } | 233 } |
| 242 } | 234 } |
| 243 | 235 |
| 244 void InkDropAnimationControllerImpl::SetInkDropSize(const gfx::Size& size) { | 236 void InkDropAnimation::SetInkDropSize(const gfx::Size& size) { |
| 245 SetInkDropBounds(gfx::Rect(ink_drop_bounds_.origin(), size)); | 237 SetInkDropBounds(gfx::Rect(ink_drop_bounds_.origin(), size)); |
| 246 } | 238 } |
| 247 | 239 |
| 248 gfx::Rect InkDropAnimationControllerImpl::GetInkDropBounds() const { | 240 gfx::Rect InkDropAnimation::GetInkDropBounds() const { |
| 249 return ink_drop_bounds_; | 241 return ink_drop_bounds_; |
| 250 } | 242 } |
| 251 | 243 |
| 252 void InkDropAnimationControllerImpl::SetInkDropBounds(const gfx::Rect& bounds) { | 244 void InkDropAnimation::SetInkDropBounds(const gfx::Rect& bounds) { |
| 253 ink_drop_bounds_ = bounds; | 245 ink_drop_bounds_ = bounds; |
| 254 SetLayerBounds(ink_drop_layer_.get()); | 246 SetLayerBounds(ink_drop_layer_.get()); |
| 255 SetLayerBounds(long_press_layer_.get()); | 247 SetLayerBounds(long_press_layer_.get()); |
| 256 } | 248 } |
| 257 | 249 |
| 258 void InkDropAnimationControllerImpl::AnimateTapDown() { | 250 void InkDropAnimation::AnimateTapDown() { |
| 259 if ((appear_animation_observer_ && | 251 if ((appear_animation_observer_ && |
| 260 appear_animation_observer_->IsAnimationActive()) || | 252 appear_animation_observer_->IsAnimationActive()) || |
| 261 (long_press_animation_observer_ && | 253 (long_press_animation_observer_ && |
| 262 long_press_animation_observer_->IsAnimationActive())) { | 254 long_press_animation_observer_->IsAnimationActive())) { |
| 263 // Only one animation at a time. Subsequent tap downs are ignored until the | 255 // Only one animation at a time. Subsequent tap downs are ignored until the |
| 264 // current animation completes. | 256 // current animation completes. |
| 265 return; | 257 return; |
| 266 } | 258 } |
| 267 appear_animation_observer_.reset( | 259 appear_animation_observer_.reset( |
| 268 new AppearAnimationObserver(ink_drop_layer_.get(), false)); | 260 new AppearAnimationObserver(ink_drop_layer_.get(), false)); |
| 269 AnimateShow(ink_drop_layer_.get(), appear_animation_observer_.get(), | 261 AnimateShow(ink_drop_layer_.get(), appear_animation_observer_.get(), |
| 270 base::TimeDelta::FromMilliseconds( | 262 base::TimeDelta::FromMilliseconds( |
| 271 (UseFastAnimations() ? kShowInkDropAnimationDurationFastMs | 263 (UseFastAnimations() ? kShowInkDropAnimationDurationFastMs |
| 272 : kShowInkDropAnimationDurationSlowMs))); | 264 : kShowInkDropAnimationDurationSlowMs))); |
| 273 } | 265 } |
| 274 | 266 |
| 275 void InkDropAnimationControllerImpl::AnimateHide() { | 267 void InkDropAnimation::AnimateHide() { |
| 276 if (appear_animation_observer_ && | 268 if (appear_animation_observer_ && |
| 277 appear_animation_observer_->IsAnimationActive()) { | 269 appear_animation_observer_->IsAnimationActive()) { |
| 278 appear_animation_observer_->HideNowIfDoneOrOnceCompleted(); | 270 appear_animation_observer_->HideNowIfDoneOrOnceCompleted(); |
| 279 } else if (long_press_animation_observer_) { | 271 } else if (long_press_animation_observer_) { |
| 280 long_press_animation_observer_->HideNowIfDoneOrOnceCompleted(); | 272 long_press_animation_observer_->HideNowIfDoneOrOnceCompleted(); |
| 281 } | 273 } |
| 282 } | 274 } |
| 283 | 275 |
| 284 void InkDropAnimationControllerImpl::AnimateLongPress() { | 276 void InkDropAnimation::AnimateLongPress() { |
| 285 // Only one animation at a time. Subsequent long presses are ignored until the | 277 // Only one animation at a time. Subsequent long presses are ignored until the |
| 286 // current animation completes. | 278 // current animation completes. |
| 287 if (long_press_animation_observer_ && | 279 if (long_press_animation_observer_ && |
| 288 long_press_animation_observer_->IsAnimationActive()) { | 280 long_press_animation_observer_->IsAnimationActive()) { |
| 289 return; | 281 return; |
| 290 } | 282 } |
| 291 appear_animation_observer_.reset(); | 283 appear_animation_observer_.reset(); |
| 292 long_press_animation_observer_.reset( | 284 long_press_animation_observer_.reset( |
| 293 new AppearAnimationObserver(long_press_layer_.get(), false)); | 285 new AppearAnimationObserver(long_press_layer_.get(), false)); |
| 294 long_press_animation_observer_->SetBackgroundToHide(ink_drop_layer_.get()); | 286 long_press_animation_observer_->SetBackgroundToHide(ink_drop_layer_.get()); |
| 295 AnimateShow(long_press_layer_.get(), long_press_animation_observer_.get(), | 287 AnimateShow(long_press_layer_.get(), long_press_animation_observer_.get(), |
| 296 base::TimeDelta::FromMilliseconds( | 288 base::TimeDelta::FromMilliseconds( |
| 297 UseFastAnimations() ? kShowLongPressAnimationDurationFastMs | 289 UseFastAnimations() ? kShowLongPressAnimationDurationFastMs |
| 298 : kShowLongPressAnimationDurationSlowMs)); | 290 : kShowLongPressAnimationDurationSlowMs)); |
| 299 } | 291 } |
| 300 | 292 |
| 301 void InkDropAnimationControllerImpl::AnimateShow( | 293 void InkDropAnimation::AnimateShow(ui::Layer* layer, |
| 302 ui::Layer* layer, | 294 AppearAnimationObserver* observer, |
| 303 AppearAnimationObserver* observer, | 295 base::TimeDelta duration) { |
| 304 base::TimeDelta duration) { | |
| 305 layer->SetVisible(true); | 296 layer->SetVisible(true); |
| 306 layer->SetOpacity(1.0f); | 297 layer->SetOpacity(1.0f); |
| 307 | 298 |
| 308 float start_x = ink_drop_bounds_.x() + | 299 float start_x = ink_drop_bounds_.x() + |
| 309 layer->bounds().width() * kMinimumScaleCenteringOffset; | 300 layer->bounds().width() * kMinimumScaleCenteringOffset; |
| 310 float start_y = ink_drop_bounds_.y() + | 301 float start_y = ink_drop_bounds_.y() + |
| 311 layer->bounds().height() * kMinimumScaleCenteringOffset; | 302 layer->bounds().height() * kMinimumScaleCenteringOffset; |
| 312 | 303 |
| 313 gfx::Transform initial_transform; | 304 gfx::Transform initial_transform; |
| 314 initial_transform.Translate(start_x, start_y); | 305 initial_transform.Translate(start_x, start_y); |
| 315 initial_transform.Scale(kMinimumScale, kMinimumScale); | 306 initial_transform.Scale(kMinimumScale, kMinimumScale); |
| 316 layer->SetTransform(initial_transform); | 307 layer->SetTransform(initial_transform); |
| 317 | 308 |
| 318 ui::LayerAnimator* animator = layer->GetAnimator(); | 309 ui::LayerAnimator* animator = layer->GetAnimator(); |
| 319 ui::ScopedLayerAnimationSettings animation(animator); | 310 ui::ScopedLayerAnimationSettings animation(animator); |
| 320 animation.SetPreemptionStrategy( | 311 animation.SetPreemptionStrategy( |
| 321 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | 312 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); |
| 322 | 313 |
| 323 gfx::Transform target_transform; | 314 gfx::Transform target_transform; |
| 324 target_transform.Translate(ink_drop_bounds_.x(), ink_drop_bounds_.y()); | 315 target_transform.Translate(ink_drop_bounds_.x(), ink_drop_bounds_.y()); |
| 325 ui::LayerAnimationElement* element = | 316 ui::LayerAnimationElement* element = |
| 326 ui::LayerAnimationElement::CreateTransformElement(target_transform, | 317 ui::LayerAnimationElement::CreateTransformElement(target_transform, |
| 327 duration); | 318 duration); |
| 328 ui::LayerAnimationSequence* sequence = | 319 ui::LayerAnimationSequence* sequence = |
| 329 new ui::LayerAnimationSequence(element); | 320 new ui::LayerAnimationSequence(element); |
| 330 sequence->AddObserver(observer); | 321 sequence->AddObserver(observer); |
| 331 animator->StartAnimation(sequence); | 322 animator->StartAnimation(sequence); |
| 332 } | 323 } |
| 333 | 324 |
| 334 void InkDropAnimationControllerImpl::SetLayerBounds(ui::Layer* layer) { | 325 void InkDropAnimation::SetLayerBounds(ui::Layer* layer) { |
| 335 bool circle = UseCircularFeedback(); | 326 bool circle = UseCircularFeedback(); |
| 336 gfx::Size size = ink_drop_bounds_.size(); | 327 gfx::Size size = ink_drop_bounds_.size(); |
| 337 float circle_width = circle ? 2.0f * kCircleRadius : size.width(); | 328 float circle_width = circle ? 2.0f * kCircleRadius : size.width(); |
| 338 float circle_height = circle ? 2.0f * kCircleRadius : size.height(); | 329 float circle_height = circle ? 2.0f * kCircleRadius : size.height(); |
| 339 float circle_x = circle ? (size.width() - circle_width) * 0.5f : 0; | 330 float circle_x = circle ? (size.width() - circle_width) * 0.5f : 0; |
| 340 float circle_y = circle ? (size.height() - circle_height) * 0.5f : 0; | 331 float circle_y = circle ? (size.height() - circle_height) * 0.5f : 0; |
| 341 layer->SetBounds(gfx::Rect(circle_x, circle_y, circle_width, circle_height)); | 332 layer->SetBounds(gfx::Rect(circle_x, circle_y, circle_width, circle_height)); |
| 342 } | 333 } |
| 343 | 334 |
| 344 void InkDropAnimationControllerImpl::SetupAnimationLayer( | 335 void InkDropAnimation::SetupAnimationLayer(ui::Layer* layer, |
| 345 ui::Layer* layer, | 336 InkDropDelegate* delegate) { |
| 346 InkDropDelegate* delegate) { | |
| 347 layer->SetFillsBoundsOpaquely(false); | 337 layer->SetFillsBoundsOpaquely(false); |
| 348 layer->set_delegate(delegate); | 338 layer->set_delegate(delegate); |
| 349 layer->SetVisible(false); | 339 layer->SetVisible(false); |
| 350 layer->SetBounds(gfx::Rect()); | 340 layer->SetBounds(gfx::Rect()); |
| 351 delegate->set_should_render_circle(UseCircularFeedback()); | 341 delegate->set_should_render_circle(UseCircularFeedback()); |
| 352 } | 342 } |
| 353 | 343 |
| 354 } // namespace views | 344 } // namespace views |
| OLD | NEW |