| 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_highlight.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_highlight_observer.h" |
| 13 #include "ui/views/animation/ink_drop_painted_layer_delegates.h" | 13 #include "ui/views/animation/ink_drop_painted_layer_delegates.h" |
| 14 | 14 |
| 15 namespace views { | 15 namespace views { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 // The opacity of the hover when it is visible. | 19 // The opacity of the highlight when it is visible. |
| 20 const float kHoverVisibleOpacity = 0.128f; | 20 const float kHighlightVisibleOpacity = 0.128f; |
| 21 | 21 |
| 22 // The opacity of the hover when it is not visible. | 22 // The opacity of the highlight when it is not visible. |
| 23 const float kHiddenOpacity = 0.0f; | 23 const float kHiddenOpacity = 0.0f; |
| 24 | 24 |
| 25 } // namespace | 25 } // namespace |
| 26 | 26 |
| 27 std::string ToString(InkDropHover::AnimationType animation_type) { | 27 std::string ToString(InkDropHighlight::AnimationType animation_type) { |
| 28 switch (animation_type) { | 28 switch (animation_type) { |
| 29 case InkDropHover::FADE_IN: | 29 case InkDropHighlight::FADE_IN: |
| 30 return std::string("FADE_IN"); | 30 return std::string("FADE_IN"); |
| 31 case InkDropHover::FADE_OUT: | 31 case InkDropHighlight::FADE_OUT: |
| 32 return std::string("FADE_OUT"); | 32 return std::string("FADE_OUT"); |
| 33 } | 33 } |
| 34 NOTREACHED() | 34 NOTREACHED() |
| 35 << "Should never be reached but is necessary for some compilers."; | 35 << "Should never be reached but is necessary for some compilers."; |
| 36 return std::string("UNKNOWN"); | 36 return std::string("UNKNOWN"); |
| 37 } | 37 } |
| 38 | 38 |
| 39 InkDropHover::InkDropHover(const gfx::Size& size, | 39 InkDropHighlight::InkDropHighlight(const gfx::Size& size, |
| 40 int corner_radius, | 40 int corner_radius, |
| 41 const gfx::Point& center_point, | 41 const gfx::Point& center_point, |
| 42 SkColor color) | 42 SkColor color) |
| 43 : size_(size), | 43 : size_(size), |
| 44 explode_size_(size), | 44 explode_size_(size), |
| 45 center_point_(center_point), | 45 center_point_(center_point), |
| 46 last_animation_initiated_was_fade_in_(false), | 46 last_animation_initiated_was_fade_in_(false), |
| 47 layer_delegate_( | 47 layer_delegate_( |
| 48 new RoundedRectangleLayerDelegate(color, size, corner_radius)), | 48 new RoundedRectangleLayerDelegate(color, size, corner_radius)), |
| 49 layer_(new ui::Layer()), | 49 layer_(new ui::Layer()), |
| 50 observer_(nullptr) { | 50 observer_(nullptr) { |
| 51 layer_->SetBounds(gfx::Rect(size)); | 51 layer_->SetBounds(gfx::Rect(size)); |
| 52 layer_->SetFillsBoundsOpaquely(false); | 52 layer_->SetFillsBoundsOpaquely(false); |
| 53 layer_->set_delegate(layer_delegate_.get()); | 53 layer_->set_delegate(layer_delegate_.get()); |
| 54 layer_->SetVisible(false); | 54 layer_->SetVisible(false); |
| 55 layer_->SetOpacity(kHoverVisibleOpacity); | 55 layer_->SetOpacity(kHighlightVisibleOpacity); |
| 56 layer_->SetMasksToBounds(false); | 56 layer_->SetMasksToBounds(false); |
| 57 layer_->set_name("InkDropHover:layer"); | 57 layer_->set_name("InkDropHighlight:layer"); |
| 58 } | 58 } |
| 59 | 59 |
| 60 InkDropHover::~InkDropHover() {} | 60 InkDropHighlight::~InkDropHighlight() {} |
| 61 | 61 |
| 62 bool InkDropHover::IsFadingInOrVisible() const { | 62 bool InkDropHighlight::IsFadingInOrVisible() const { |
| 63 return last_animation_initiated_was_fade_in_; | 63 return last_animation_initiated_was_fade_in_; |
| 64 } | 64 } |
| 65 | 65 |
| 66 void InkDropHover::FadeIn(const base::TimeDelta& duration) { | 66 void InkDropHighlight::FadeIn(const base::TimeDelta& duration) { |
| 67 layer_->SetOpacity(kHiddenOpacity); | 67 layer_->SetOpacity(kHiddenOpacity); |
| 68 layer_->SetVisible(true); | 68 layer_->SetVisible(true); |
| 69 AnimateFade(FADE_IN, duration, size_, size_); | 69 AnimateFade(FADE_IN, duration, size_, size_); |
| 70 } | 70 } |
| 71 | 71 |
| 72 void InkDropHover::FadeOut(const base::TimeDelta& duration, bool explode) { | 72 void InkDropHighlight::FadeOut(const base::TimeDelta& duration, bool explode) { |
| 73 AnimateFade(FADE_OUT, duration, size_, explode ? explode_size_ : size_); | 73 AnimateFade(FADE_OUT, duration, size_, explode ? explode_size_ : size_); |
| 74 } | 74 } |
| 75 | 75 |
| 76 test::InkDropHoverTestApi* InkDropHover::GetTestApi() { | 76 test::InkDropHighlightTestApi* InkDropHighlight::GetTestApi() { |
| 77 return nullptr; | 77 return nullptr; |
| 78 } | 78 } |
| 79 | 79 |
| 80 void InkDropHover::AnimateFade(AnimationType animation_type, | 80 void InkDropHighlight::AnimateFade(AnimationType animation_type, |
| 81 const base::TimeDelta& duration, | 81 const base::TimeDelta& duration, |
| 82 const gfx::Size& initial_size, | 82 const gfx::Size& initial_size, |
| 83 const gfx::Size& target_size) { | 83 const gfx::Size& target_size) { |
| 84 last_animation_initiated_was_fade_in_ = animation_type == FADE_IN; | 84 last_animation_initiated_was_fade_in_ = animation_type == FADE_IN; |
| 85 | 85 |
| 86 layer_->SetTransform(CalculateTransform(initial_size)); | 86 layer_->SetTransform(CalculateTransform(initial_size)); |
| 87 | 87 |
| 88 // The |animation_observer| will be destroyed when the | 88 // The |animation_observer| will be destroyed when the |
| 89 // AnimationStartedCallback() returns true. | 89 // AnimationStartedCallback() returns true. |
| 90 ui::CallbackLayerAnimationObserver* animation_observer = | 90 ui::CallbackLayerAnimationObserver* animation_observer = |
| 91 new ui::CallbackLayerAnimationObserver( | 91 new ui::CallbackLayerAnimationObserver( |
| 92 base::Bind(&InkDropHover::AnimationStartedCallback, | 92 base::Bind(&InkDropHighlight::AnimationStartedCallback, |
| 93 base::Unretained(this), animation_type), | 93 base::Unretained(this), animation_type), |
| 94 base::Bind(&InkDropHover::AnimationEndedCallback, | 94 base::Bind(&InkDropHighlight::AnimationEndedCallback, |
| 95 base::Unretained(this), animation_type)); | 95 base::Unretained(this), animation_type)); |
| 96 | 96 |
| 97 ui::LayerAnimator* animator = layer_->GetAnimator(); | 97 ui::LayerAnimator* animator = layer_->GetAnimator(); |
| 98 ui::ScopedLayerAnimationSettings animation(animator); | 98 ui::ScopedLayerAnimationSettings animation(animator); |
| 99 animation.SetTweenType(gfx::Tween::EASE_IN_OUT); | 99 animation.SetTweenType(gfx::Tween::EASE_IN_OUT); |
| 100 animation.SetPreemptionStrategy( | 100 animation.SetPreemptionStrategy( |
| 101 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | 101 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); |
| 102 | 102 |
| 103 ui::LayerAnimationElement* opacity_element = | 103 ui::LayerAnimationElement* opacity_element = |
| 104 ui::LayerAnimationElement::CreateOpacityElement( | 104 ui::LayerAnimationElement::CreateOpacityElement( |
| 105 animation_type == FADE_IN ? kHoverVisibleOpacity : kHiddenOpacity, | 105 animation_type == FADE_IN ? kHighlightVisibleOpacity : kHiddenOpacity, |
| 106 duration); | 106 duration); |
| 107 ui::LayerAnimationSequence* opacity_sequence = | 107 ui::LayerAnimationSequence* opacity_sequence = |
| 108 new ui::LayerAnimationSequence(opacity_element); | 108 new ui::LayerAnimationSequence(opacity_element); |
| 109 opacity_sequence->AddObserver(animation_observer); | 109 opacity_sequence->AddObserver(animation_observer); |
| 110 animator->StartAnimation(opacity_sequence); | 110 animator->StartAnimation(opacity_sequence); |
| 111 | 111 |
| 112 if (initial_size != target_size) { | 112 if (initial_size != target_size) { |
| 113 ui::LayerAnimationElement* transform_element = | 113 ui::LayerAnimationElement* transform_element = |
| 114 ui::LayerAnimationElement::CreateTransformElement( | 114 ui::LayerAnimationElement::CreateTransformElement( |
| 115 CalculateTransform(target_size), duration); | 115 CalculateTransform(target_size), duration); |
| 116 ui::LayerAnimationSequence* transform_sequence = | 116 ui::LayerAnimationSequence* transform_sequence = |
| 117 new ui::LayerAnimationSequence(transform_element); | 117 new ui::LayerAnimationSequence(transform_element); |
| 118 transform_sequence->AddObserver(animation_observer); | 118 transform_sequence->AddObserver(animation_observer); |
| 119 animator->StartAnimation(transform_sequence); | 119 animator->StartAnimation(transform_sequence); |
| 120 } | 120 } |
| 121 | 121 |
| 122 animation_observer->SetActive(); | 122 animation_observer->SetActive(); |
| 123 } | 123 } |
| 124 | 124 |
| 125 gfx::Transform InkDropHover::CalculateTransform(const gfx::Size& size) const { | 125 gfx::Transform InkDropHighlight::CalculateTransform( |
| 126 const gfx::Size& size) const { |
| 126 gfx::Transform transform; | 127 gfx::Transform transform; |
| 127 transform.Translate(center_point_.x(), center_point_.y()); | 128 transform.Translate(center_point_.x(), center_point_.y()); |
| 128 transform.Scale(size.width() / size_.width(), size.height() / size_.height()); | 129 transform.Scale(size.width() / size_.width(), size.height() / size_.height()); |
| 129 transform.Translate(-layer_delegate_->GetCenterPoint().x(), | 130 transform.Translate(-layer_delegate_->GetCenterPoint().x(), |
| 130 -layer_delegate_->GetCenterPoint().y()); | 131 -layer_delegate_->GetCenterPoint().y()); |
| 131 return transform; | 132 return transform; |
| 132 } | 133 } |
| 133 | 134 |
| 134 void InkDropHover::AnimationStartedCallback( | 135 void InkDropHighlight::AnimationStartedCallback( |
| 135 AnimationType animation_type, | 136 AnimationType animation_type, |
| 136 const ui::CallbackLayerAnimationObserver& observer) { | 137 const ui::CallbackLayerAnimationObserver& observer) { |
| 137 if (observer_) | 138 if (observer_) |
| 138 observer_->AnimationStarted(animation_type); | 139 observer_->AnimationStarted(animation_type); |
| 139 } | 140 } |
| 140 | 141 |
| 141 bool InkDropHover::AnimationEndedCallback( | 142 bool InkDropHighlight::AnimationEndedCallback( |
| 142 AnimationType animation_type, | 143 AnimationType animation_type, |
| 143 const ui::CallbackLayerAnimationObserver& observer) { | 144 const ui::CallbackLayerAnimationObserver& observer) { |
| 144 // AnimationEndedCallback() may be invoked when this is being destroyed and | 145 // AnimationEndedCallback() may be invoked when this is being destroyed and |
| 145 // |layer_| may be null. | 146 // |layer_| may be null. |
| 146 if (animation_type == FADE_OUT && layer_) | 147 if (animation_type == FADE_OUT && layer_) |
| 147 layer_->SetVisible(false); | 148 layer_->SetVisible(false); |
| 148 | 149 |
| 149 if (observer_) { | 150 if (observer_) { |
| 150 observer_->AnimationEnded(animation_type, | 151 observer_->AnimationEnded(animation_type, |
| 151 observer.aborted_count() | 152 observer.aborted_count() |
| 152 ? InkDropAnimationEndedReason::PRE_EMPTED | 153 ? InkDropAnimationEndedReason::PRE_EMPTED |
| 153 : InkDropAnimationEndedReason::SUCCESS); | 154 : InkDropAnimationEndedReason::SUCCESS); |
| 154 } | 155 } |
| 155 return true; | 156 return true; |
| 156 } | 157 } |
| 157 | 158 |
| 158 } // namespace views | 159 } // namespace views |
| OLD | NEW |