Chromium Code Reviews| Index: ui/views/animation/ink_drop_hover.cc |
| diff --git a/ui/views/animation/ink_drop_hover.cc b/ui/views/animation/ink_drop_hover.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f4ee7455bf458c506e485d22248a40e10e59bc92 |
| --- /dev/null |
| +++ b/ui/views/animation/ink_drop_hover.cc |
| @@ -0,0 +1,114 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ui/views/animation/ink_drop_hover.h" |
| + |
| +#include "third_party/skia/include/core/SkColor.h" |
| +#include "ui/compositor/callback_layer_animation_observer.h" |
| +#include "ui/compositor/layer.h" |
| +#include "ui/compositor/layer_animation_sequence.h" |
| +#include "ui/compositor/scoped_layer_animation_settings.h" |
| +#include "ui/views/animation/ink_drop_painted_layer_delegates.h" |
| + |
| +namespace views { |
| + |
| +namespace { |
| + |
| +// The opacity of the hover when it is visible. |
| +const float kHoverVisibleOpacity = 0.08f; |
| + |
| +// The opacity of the hover when it is not visible. |
| +const float kHiddenOpacity = 0.0f; |
| + |
| +// The hover color. |
| +const SkColor kHoverColor = SK_ColorBLACK; |
| + |
| +} // namespace |
| + |
| +InkDropHover::InkDropHover(const gfx::Size& size, int corner_radius) |
| + : is_hovered_(false), |
| + layer_delegate_( |
| + new RoundedRectangleLayerDelegate(kHoverColor, size, corner_radius)), |
| + layer_(new ui::Layer()) { |
| + layer_->SetBounds(gfx::Rect(size)); |
| + layer_->SetFillsBoundsOpaquely(false); |
| + layer_->set_delegate(layer_delegate_.get()); |
| + layer_->SetVisible(false); |
| + layer_->SetOpacity(kHoverVisibleOpacity); |
| + layer_->SetMasksToBounds(false); |
| + SetCenterPoint(gfx::Rect(size).CenterPoint()); |
| +} |
| + |
| +InkDropHover::~InkDropHover() {} |
| + |
| +void InkDropHover::FadeIn(const base::TimeDelta& duration) { |
| + layer_->SetOpacity(kHiddenOpacity); |
| + layer_->SetVisible(true); |
| + AnimateFade(FADE_IN, duration); |
| +} |
| + |
| +void InkDropHover::FadeOut(const base::TimeDelta& duration) { |
| + AnimateFade(FADE_OUT, duration); |
| +} |
| + |
| +void InkDropHover::AnimateFade(HoverAnimation hover_animation, |
| + const base::TimeDelta& duration) { |
| + ui::CallbackLayerAnimationObserver* animation_observer = |
|
tdanderson
2015/10/15 14:46:54
Do you really want |animation_observer| and |anima
bruthig
2015/10/15 21:49:29
I've added a comment about the |animation_observer
|
| + new ui::CallbackLayerAnimationObserver( |
| + base::Bind(&InkDropHover::AnimationStartedCallback, |
| + base::Unretained(this), hover_animation), |
| + base::Bind(&InkDropHover::AnimationEndedCallback, |
| + base::Unretained(this), hover_animation)); |
| + |
| + ui::LayerAnimator* animator = layer_->GetAnimator(); |
| + ui::ScopedLayerAnimationSettings animation(animator); |
| + animation.SetPreemptionStrategy( |
| + ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); |
|
tdanderson
2015/10/15 14:46:54
from in person conversation: should this be preemp
bruthig
2015/10/15 21:49:29
I double checked and IMMEDIATELY_ANIMATE_TO_NEW_TA
|
| + ui::LayerAnimationElement* animation_element = |
| + ui::LayerAnimationElement::CreateOpacityElement( |
| + hover_animation == FADE_IN ? kHoverVisibleOpacity : kHiddenOpacity, |
| + duration); |
| + ui::LayerAnimationSequence* animation_sequence = |
| + new ui::LayerAnimationSequence(animation_element); |
| + animation_sequence->AddObserver(animation_observer); |
| + |
| + animator->StartAnimation(animation_sequence); |
| + |
| + animation_observer->SetActive(); |
| +} |
| + |
| +void InkDropHover::SetCenterPoint(const gfx::Point& center_point) { |
| + gfx::Transform transform; |
| + transform.Translate(center_point.x() - layer_->bounds().CenterPoint().x(), |
| + center_point.y() - layer_->bounds().CenterPoint().y()); |
| + layer_->SetTransform(transform); |
| +} |
| + |
| +void InkDropHover::AnimationStartedCallback( |
| + HoverAnimation hover_animation, |
| + const ui::CallbackLayerAnimationObserver& observer) { |
| + switch (hover_animation) { |
| + case FADE_IN: |
| + is_hovered_ = true; |
| + break; |
| + case FADE_OUT: |
| + break; |
| + } |
| +} |
| + |
| +bool InkDropHover::AnimationEndedCallback( |
| + HoverAnimation hover_animation, |
| + const ui::CallbackLayerAnimationObserver& observer) { |
| + switch (hover_animation) { |
| + case FADE_IN: |
| + break; |
| + case FADE_OUT: |
| + is_hovered_ = false; |
| + layer_->SetVisible(false); |
| + break; |
| + } |
| + return true; |
| +} |
| + |
| +} // namespace views |