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_painted_layer_delegates.h" |
| 13 |
| 14 namespace views { |
| 15 |
| 16 namespace { |
| 17 |
| 18 // The opacity of the hover when it is visible. |
| 19 const float kHoverVisibleOpacity = 0.08f; |
| 20 |
| 21 // The opacity of the hover when it is not visible. |
| 22 const float kHiddenOpacity = 0.0f; |
| 23 |
| 24 // The hover color. |
| 25 const SkColor kHoverColor = SK_ColorBLACK; |
| 26 |
| 27 } // namespace |
| 28 |
| 29 InkDropHover::InkDropHover(const gfx::Size& size, int corner_radius) |
| 30 : layer_delegate_( |
| 31 new RoundedRectangleLayerDelegate(kHoverColor, size, corner_radius)), |
| 32 layer_(new ui::Layer()) { |
| 33 layer_->SetBounds(gfx::Rect(size)); |
| 34 layer_->SetFillsBoundsOpaquely(false); |
| 35 layer_->set_delegate(layer_delegate_.get()); |
| 36 layer_->SetVisible(false); |
| 37 layer_->SetOpacity(kHoverVisibleOpacity); |
| 38 layer_->SetMasksToBounds(false); |
| 39 layer_->set_name("InkDropHover:layer"); |
| 40 SetCenterPoint(gfx::Rect(size).CenterPoint()); |
| 41 } |
| 42 |
| 43 InkDropHover::~InkDropHover() {} |
| 44 |
| 45 bool InkDropHover::IsVisible() const { |
| 46 return layer_->visible(); |
| 47 } |
| 48 |
| 49 void InkDropHover::FadeIn(const base::TimeDelta& duration) { |
| 50 layer_->SetOpacity(kHiddenOpacity); |
| 51 layer_->SetVisible(true); |
| 52 AnimateFade(FADE_IN, duration); |
| 53 } |
| 54 |
| 55 void InkDropHover::FadeOut(const base::TimeDelta& duration) { |
| 56 AnimateFade(FADE_OUT, duration); |
| 57 } |
| 58 |
| 59 void InkDropHover::AnimateFade(HoverAnimationType animation_type, |
| 60 const base::TimeDelta& duration) { |
| 61 // The |animation_observer| will be destroyed when the |
| 62 // AnimationStartedCallback() returns true. |
| 63 ui::CallbackLayerAnimationObserver* animation_observer = |
| 64 new ui::CallbackLayerAnimationObserver( |
| 65 base::Bind(&InkDropHover::AnimationEndedCallback, |
| 66 base::Unretained(this), animation_type)); |
| 67 |
| 68 ui::LayerAnimator* animator = layer_->GetAnimator(); |
| 69 ui::ScopedLayerAnimationSettings animation(animator); |
| 70 animation.SetPreemptionStrategy( |
| 71 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); |
| 72 ui::LayerAnimationElement* animation_element = |
| 73 ui::LayerAnimationElement::CreateOpacityElement( |
| 74 animation_type == FADE_IN ? kHoverVisibleOpacity : kHiddenOpacity, |
| 75 duration); |
| 76 ui::LayerAnimationSequence* animation_sequence = |
| 77 new ui::LayerAnimationSequence(animation_element); |
| 78 animation_sequence->AddObserver(animation_observer); |
| 79 |
| 80 animator->StartAnimation(animation_sequence); |
| 81 |
| 82 animation_observer->SetActive(); |
| 83 } |
| 84 |
| 85 void InkDropHover::SetCenterPoint(const gfx::Point& center_point) { |
| 86 gfx::Transform transform; |
| 87 transform.Translate(center_point.x() - layer_->bounds().CenterPoint().x(), |
| 88 center_point.y() - layer_->bounds().CenterPoint().y()); |
| 89 layer_->SetTransform(transform); |
| 90 } |
| 91 |
| 92 bool InkDropHover::AnimationEndedCallback( |
| 93 HoverAnimationType animation_type, |
| 94 const ui::CallbackLayerAnimationObserver& observer) { |
| 95 // AnimationEndedCallback() may be invoked when this is being destroyed and |
| 96 // |layer_| may be null. |
| 97 if (animation_type == FADE_OUT && layer_) |
| 98 layer_->SetVisible(false); |
| 99 return true; |
| 100 } |
| 101 |
| 102 } // namespace views |
OLD | NEW |