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