| 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_hover.h" | 5 #include "ui/views/animation/ink_drop_hover.h" |
| 6 | 6 |
| 7 #include "third_party/skia/include/core/SkColor.h" | 7 #include "third_party/skia/include/core/SkColor.h" |
| 8 #include "ui/compositor/callback_layer_animation_observer.h" | 8 #include "ui/compositor/callback_layer_animation_observer.h" |
| 9 #include "ui/compositor/layer.h" | 9 #include "ui/compositor/layer.h" |
| 10 #include "ui/compositor/layer_animation_sequence.h" | 10 #include "ui/compositor/layer_animation_sequence.h" |
| 11 #include "ui/compositor/scoped_layer_animation_settings.h" | 11 #include "ui/compositor/scoped_layer_animation_settings.h" |
| 12 #include "ui/views/animation/ink_drop_hover_observer.h" |
| 12 #include "ui/views/animation/ink_drop_painted_layer_delegates.h" | 13 #include "ui/views/animation/ink_drop_painted_layer_delegates.h" |
| 13 | 14 |
| 14 namespace views { | 15 namespace views { |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 // The opacity of the hover when it is visible. | 19 // The opacity of the hover when it is visible. |
| 19 const float kHoverVisibleOpacity = 0.128f; | 20 const float kHoverVisibleOpacity = 0.128f; |
| 20 | 21 |
| 21 // The opacity of the hover when it is not visible. | 22 // The opacity of the hover when it is not visible. |
| 22 const float kHiddenOpacity = 0.0f; | 23 const float kHiddenOpacity = 0.0f; |
| 23 | 24 |
| 24 } // namespace | 25 } // namespace |
| 25 | 26 |
| 26 InkDropHover::InkDropHover(const gfx::Size& size, | 27 InkDropHover::InkDropHover(const gfx::Size& size, |
| 27 int corner_radius, | 28 int corner_radius, |
| 28 const gfx::Point& center_point, | 29 const gfx::Point& center_point, |
| 29 SkColor color) | 30 SkColor color) |
| 30 : size_(size), | 31 : size_(size), |
| 31 explode_size_(size), | 32 explode_size_(size), |
| 32 center_point_(center_point), | 33 center_point_(center_point), |
| 33 last_animation_initiated_was_fade_in_(false), | 34 last_animation_initiated_was_fade_in_(false), |
| 34 layer_delegate_( | 35 layer_delegate_( |
| 35 new RoundedRectangleLayerDelegate(color, size, corner_radius)), | 36 new RoundedRectangleLayerDelegate(color, size, corner_radius)), |
| 36 layer_(new ui::Layer()) { | 37 layer_(new ui::Layer()), |
| 38 observer_(nullptr) { |
| 37 layer_->SetBounds(gfx::Rect(size)); | 39 layer_->SetBounds(gfx::Rect(size)); |
| 38 layer_->SetFillsBoundsOpaquely(false); | 40 layer_->SetFillsBoundsOpaquely(false); |
| 39 layer_->set_delegate(layer_delegate_.get()); | 41 layer_->set_delegate(layer_delegate_.get()); |
| 40 layer_->SetVisible(false); | 42 layer_->SetVisible(false); |
| 41 layer_->SetOpacity(kHoverVisibleOpacity); | 43 layer_->SetOpacity(kHoverVisibleOpacity); |
| 42 layer_->SetMasksToBounds(false); | 44 layer_->SetMasksToBounds(false); |
| 43 layer_->set_name("InkDropHover:layer"); | 45 layer_->set_name("InkDropHover:layer"); |
| 44 } | 46 } |
| 45 | 47 |
| 46 InkDropHover::~InkDropHover() {} | 48 InkDropHover::~InkDropHover() {} |
| 47 | 49 |
| 48 bool InkDropHover::IsFadingInOrVisible() const { | 50 bool InkDropHover::IsFadingInOrVisible() const { |
| 49 return last_animation_initiated_was_fade_in_; | 51 return last_animation_initiated_was_fade_in_; |
| 50 } | 52 } |
| 51 | 53 |
| 52 void InkDropHover::FadeIn(const base::TimeDelta& duration) { | 54 void InkDropHover::FadeIn(const base::TimeDelta& duration) { |
| 53 layer_->SetOpacity(kHiddenOpacity); | 55 layer_->SetOpacity(kHiddenOpacity); |
| 54 layer_->SetVisible(true); | 56 layer_->SetVisible(true); |
| 55 AnimateFade(FADE_IN, duration, size_, size_); | 57 AnimateFade(FADE_IN, duration, size_, size_); |
| 56 } | 58 } |
| 57 | 59 |
| 58 void InkDropHover::FadeOut(const base::TimeDelta& duration, bool explode) { | 60 void InkDropHover::FadeOut(const base::TimeDelta& duration, bool explode) { |
| 59 AnimateFade(FADE_OUT, duration, size_, explode ? explode_size_ : size_); | 61 AnimateFade(FADE_OUT, duration, size_, explode ? explode_size_ : size_); |
| 60 } | 62 } |
| 61 | 63 |
| 62 test::InkDropHoverTestApi* InkDropHover::GetTestApi() { | 64 test::InkDropHoverTestApi* InkDropHover::GetTestApi() { |
| 63 return nullptr; | 65 return nullptr; |
| 64 } | 66 } |
| 65 | 67 |
| 66 void InkDropHover::AnimateFade(HoverAnimationType animation_type, | 68 void InkDropHover::AnimateFade(AnimationType animation_type, |
| 67 const base::TimeDelta& duration, | 69 const base::TimeDelta& duration, |
| 68 const gfx::Size& initial_size, | 70 const gfx::Size& initial_size, |
| 69 const gfx::Size& target_size) { | 71 const gfx::Size& target_size) { |
| 70 last_animation_initiated_was_fade_in_ = animation_type == FADE_IN; | 72 last_animation_initiated_was_fade_in_ = animation_type == FADE_IN; |
| 71 | 73 |
| 72 layer_->SetTransform(CalculateTransform(initial_size)); | 74 layer_->SetTransform(CalculateTransform(initial_size)); |
| 73 | 75 |
| 74 // The |animation_observer| will be destroyed when the | 76 // The |animation_observer| will be destroyed when the |
| 75 // AnimationStartedCallback() returns true. | 77 // AnimationStartedCallback() returns true. |
| 76 ui::CallbackLayerAnimationObserver* animation_observer = | 78 ui::CallbackLayerAnimationObserver* animation_observer = |
| 77 new ui::CallbackLayerAnimationObserver( | 79 new ui::CallbackLayerAnimationObserver( |
| 80 base::Bind(&InkDropHover::AnimationStartedCallback, |
| 81 base::Unretained(this), animation_type), |
| 78 base::Bind(&InkDropHover::AnimationEndedCallback, | 82 base::Bind(&InkDropHover::AnimationEndedCallback, |
| 79 base::Unretained(this), animation_type)); | 83 base::Unretained(this), animation_type)); |
| 80 | 84 |
| 81 ui::LayerAnimator* animator = layer_->GetAnimator(); | 85 ui::LayerAnimator* animator = layer_->GetAnimator(); |
| 82 ui::ScopedLayerAnimationSettings animation(animator); | 86 ui::ScopedLayerAnimationSettings animation(animator); |
| 83 animation.SetTweenType(gfx::Tween::EASE_IN_OUT); | 87 animation.SetTweenType(gfx::Tween::EASE_IN_OUT); |
| 84 animation.SetPreemptionStrategy( | 88 animation.SetPreemptionStrategy( |
| 85 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | 89 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); |
| 86 | 90 |
| 87 ui::LayerAnimationElement* opacity_element = | 91 ui::LayerAnimationElement* opacity_element = |
| (...skipping 20 matching lines...) Expand all Loading... |
| 108 | 112 |
| 109 gfx::Transform InkDropHover::CalculateTransform(const gfx::Size& size) const { | 113 gfx::Transform InkDropHover::CalculateTransform(const gfx::Size& size) const { |
| 110 gfx::Transform transform; | 114 gfx::Transform transform; |
| 111 transform.Translate(center_point_.x(), center_point_.y()); | 115 transform.Translate(center_point_.x(), center_point_.y()); |
| 112 transform.Scale(size.width() / size_.width(), size.height() / size_.height()); | 116 transform.Scale(size.width() / size_.width(), size.height() / size_.height()); |
| 113 transform.Translate(-layer_delegate_->GetCenterPoint().x(), | 117 transform.Translate(-layer_delegate_->GetCenterPoint().x(), |
| 114 -layer_delegate_->GetCenterPoint().y()); | 118 -layer_delegate_->GetCenterPoint().y()); |
| 115 return transform; | 119 return transform; |
| 116 } | 120 } |
| 117 | 121 |
| 122 void InkDropHover::AnimationStartedCallback( |
| 123 AnimationType animation_type, |
| 124 const ui::CallbackLayerAnimationObserver& observer) { |
| 125 if (observer_) |
| 126 observer_->AnimationStarted(animation_type); |
| 127 } |
| 128 |
| 118 bool InkDropHover::AnimationEndedCallback( | 129 bool InkDropHover::AnimationEndedCallback( |
| 119 HoverAnimationType animation_type, | 130 AnimationType animation_type, |
| 120 const ui::CallbackLayerAnimationObserver& observer) { | 131 const ui::CallbackLayerAnimationObserver& observer) { |
| 121 // AnimationEndedCallback() may be invoked when this is being destroyed and | 132 // AnimationEndedCallback() may be invoked when this is being destroyed and |
| 122 // |layer_| may be null. | 133 // |layer_| may be null. |
| 123 if (animation_type == FADE_OUT && layer_) | 134 if (animation_type == FADE_OUT && layer_) |
| 124 layer_->SetVisible(false); | 135 layer_->SetVisible(false); |
| 136 |
| 137 if (observer_) { |
| 138 observer_->AnimationEnded(animation_type, |
| 139 observer.aborted_count() |
| 140 ? InkDropAnimationEndedReason::PRE_EMPTED |
| 141 : InkDropAnimationEndedReason::SUCCESS); |
| 142 } |
| 125 return true; | 143 return true; |
| 126 } | 144 } |
| 127 | 145 |
| 128 } // namespace views | 146 } // namespace views |
| OLD | NEW |