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 SetCenterPoint(gfx::Rect(size).CenterPoint()); |
| 40 } |
| 41 |
| 42 InkDropHover::~InkDropHover() {} |
| 43 |
| 44 bool InkDropHover::IsVisible() const { |
| 45 return layer_->visible(); |
| 46 } |
| 47 |
| 48 void InkDropHover::FadeIn(const base::TimeDelta& duration) { |
| 49 layer_->SetOpacity(kHiddenOpacity); |
| 50 layer_->SetVisible(true); |
| 51 AnimateFade(FADE_IN, duration); |
| 52 } |
| 53 |
| 54 void InkDropHover::FadeOut(const base::TimeDelta& duration) { |
| 55 AnimateFade(FADE_OUT, duration); |
| 56 } |
| 57 |
| 58 void InkDropHover::AnimateFade(HoverAnimationType animation_type, |
| 59 const base::TimeDelta& duration) { |
| 60 // The |animation_observer| will be destroyed when the |
| 61 // AnimationStartedCallback() returns true. |
| 62 ui::CallbackLayerAnimationObserver* animation_observer = |
| 63 new ui::CallbackLayerAnimationObserver( |
| 64 base::Bind(&InkDropHover::AnimationEndedCallback, |
| 65 base::Unretained(this), animation_type)); |
| 66 |
| 67 ui::LayerAnimator* animator = layer_->GetAnimator(); |
| 68 ui::ScopedLayerAnimationSettings animation(animator); |
| 69 animation.SetPreemptionStrategy( |
| 70 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); |
| 71 ui::LayerAnimationElement* animation_element = |
| 72 ui::LayerAnimationElement::CreateOpacityElement( |
| 73 animation_type == FADE_IN ? kHoverVisibleOpacity : kHiddenOpacity, |
| 74 duration); |
| 75 ui::LayerAnimationSequence* animation_sequence = |
| 76 new ui::LayerAnimationSequence(animation_element); |
| 77 animation_sequence->AddObserver(animation_observer); |
| 78 |
| 79 animator->StartAnimation(animation_sequence); |
| 80 |
| 81 animation_observer->SetActive(); |
| 82 } |
| 83 |
| 84 void InkDropHover::SetCenterPoint(const gfx::Point& center_point) { |
| 85 gfx::Transform transform; |
| 86 transform.Translate(center_point.x() - layer_->bounds().CenterPoint().x(), |
| 87 center_point.y() - layer_->bounds().CenterPoint().y()); |
| 88 layer_->SetTransform(transform); |
| 89 } |
| 90 |
| 91 bool InkDropHover::AnimationEndedCallback( |
| 92 HoverAnimationType animation_type, |
| 93 const ui::CallbackLayerAnimationObserver& observer) { |
| 94 switch (animation_type) { |
| 95 case FADE_IN: |
| 96 break; |
| 97 case FADE_OUT: |
| 98 layer_->SetVisible(false); |
| 99 break; |
| 100 } |
| 101 return true; |
| 102 } |
| 103 |
| 104 } // namespace views |
OLD | NEW |