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_hover.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_painted_layer_delegates.h" | 12 #include "ui/views/animation/ink_drop_painted_layer_delegates.h" |
13 | 13 |
14 namespace views { | 14 namespace views { |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 // The opacity of the hover when it is visible. | 18 // The opacity of the hover when it is visible. |
19 const float kHoverVisibleOpacity = 0.128f; | 19 const float kHoverVisibleOpacity = 0.128f; |
20 | 20 |
21 // The opacity of the hover when it is not visible. | 21 // The opacity of the hover when it is not visible. |
22 const float kHiddenOpacity = 0.0f; | 22 const float kHiddenOpacity = 0.0f; |
23 | 23 |
24 } // namespace | 24 } // namespace |
25 | 25 |
26 InkDropHover::InkDropHover(const gfx::Size& size, | 26 InkDropHover::InkDropHover(const gfx::Size& size, |
27 int corner_radius, | 27 int corner_radius, |
28 const gfx::Point& center_point, | 28 const gfx::Point& center_point, |
29 SkColor color) | 29 SkColor color) |
30 : last_animation_initiated_was_fade_in_(false), | 30 : size_(size), |
31 explode_size_(size), | |
32 center_point_(center_point), | |
33 last_animation_initiated_was_fade_in_(false), | |
31 layer_delegate_( | 34 layer_delegate_( |
32 new RoundedRectangleLayerDelegate(color, size, corner_radius)), | 35 new RoundedRectangleLayerDelegate(color, size, corner_radius)), |
33 layer_(new ui::Layer()) { | 36 layer_(new ui::Layer()) { |
34 layer_->SetBounds(gfx::Rect(size)); | 37 layer_->SetBounds(gfx::Rect(size)); |
35 layer_->SetFillsBoundsOpaquely(false); | 38 layer_->SetFillsBoundsOpaquely(false); |
36 layer_->set_delegate(layer_delegate_.get()); | 39 layer_->set_delegate(layer_delegate_.get()); |
37 layer_->SetVisible(false); | 40 layer_->SetVisible(false); |
38 layer_->SetOpacity(kHoverVisibleOpacity); | 41 layer_->SetOpacity(kHoverVisibleOpacity); |
39 layer_->SetMasksToBounds(false); | 42 layer_->SetMasksToBounds(false); |
40 layer_->set_name("InkDropHover:layer"); | 43 layer_->set_name("InkDropHover:layer"); |
41 | |
42 gfx::Transform transform; | |
43 transform.Translate(center_point.x() - layer_->bounds().CenterPoint().x(), | |
44 center_point.y() - layer_->bounds().CenterPoint().y()); | |
45 layer_->SetTransform(transform); | |
46 } | 44 } |
47 | 45 |
48 InkDropHover::~InkDropHover() {} | 46 InkDropHover::~InkDropHover() {} |
49 | 47 |
50 bool InkDropHover::IsFadingInOrVisible() const { | 48 bool InkDropHover::IsFadingInOrVisible() const { |
51 return last_animation_initiated_was_fade_in_; | 49 return last_animation_initiated_was_fade_in_; |
52 } | 50 } |
53 | 51 |
54 void InkDropHover::FadeIn(const base::TimeDelta& duration) { | 52 void InkDropHover::FadeIn(const base::TimeDelta& duration) { |
55 layer_->SetOpacity(kHiddenOpacity); | 53 layer_->SetOpacity(kHiddenOpacity); |
56 layer_->SetVisible(true); | 54 layer_->SetVisible(true); |
57 AnimateFade(FADE_IN, duration); | 55 const gfx::Transform transform = CalculateTransform(size_); |
56 AnimateFade(FADE_IN, duration, transform, transform); | |
58 } | 57 } |
59 | 58 |
60 void InkDropHover::FadeOut(const base::TimeDelta& duration) { | 59 void InkDropHover::FadeOut(const base::TimeDelta& duration, bool explode) { |
61 AnimateFade(FADE_OUT, duration); | 60 AnimateFade(FADE_OUT, duration, CalculateTransform(size_), |
61 CalculateTransform(explode ? explode_size_ : size_)); | |
62 } | 62 } |
63 | 63 |
64 void InkDropHover::AnimateFade(HoverAnimationType animation_type, | 64 void InkDropHover::AnimateFade(HoverAnimationType animation_type, |
65 const base::TimeDelta& duration) { | 65 const base::TimeDelta& duration, |
66 const gfx::Transform& initial_transform, | |
67 const gfx::Transform& target_transform) { | |
66 last_animation_initiated_was_fade_in_ = animation_type == FADE_IN; | 68 last_animation_initiated_was_fade_in_ = animation_type == FADE_IN; |
67 | 69 |
70 layer_->SetTransform(initial_transform); | |
71 | |
68 // The |animation_observer| will be destroyed when the | 72 // The |animation_observer| will be destroyed when the |
69 // AnimationStartedCallback() returns true. | 73 // AnimationStartedCallback() returns true. |
70 ui::CallbackLayerAnimationObserver* animation_observer = | 74 ui::CallbackLayerAnimationObserver* animation_observer = |
71 new ui::CallbackLayerAnimationObserver( | 75 new ui::CallbackLayerAnimationObserver( |
72 base::Bind(&InkDropHover::AnimationEndedCallback, | 76 base::Bind(&InkDropHover::AnimationEndedCallback, |
73 base::Unretained(this), animation_type)); | 77 base::Unretained(this), animation_type)); |
74 | 78 |
75 ui::LayerAnimator* animator = layer_->GetAnimator(); | 79 ui::LayerAnimator* animator = layer_->GetAnimator(); |
76 ui::ScopedLayerAnimationSettings animation(animator); | 80 ui::ScopedLayerAnimationSettings animation(animator); |
77 animation.SetTweenType(gfx::Tween::EASE_IN_OUT); | 81 animation.SetTweenType(gfx::Tween::EASE_IN_OUT); |
78 animation.SetPreemptionStrategy( | 82 animation.SetPreemptionStrategy( |
79 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | 83 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); |
80 ui::LayerAnimationElement* animation_element = | 84 |
85 ui::LayerAnimationElement* opacity_element = | |
81 ui::LayerAnimationElement::CreateOpacityElement( | 86 ui::LayerAnimationElement::CreateOpacityElement( |
82 animation_type == FADE_IN ? kHoverVisibleOpacity : kHiddenOpacity, | 87 animation_type == FADE_IN ? kHoverVisibleOpacity : kHiddenOpacity, |
83 duration); | 88 duration); |
84 ui::LayerAnimationSequence* animation_sequence = | 89 ui::LayerAnimationSequence* opacity_sequence = |
85 new ui::LayerAnimationSequence(animation_element); | 90 new ui::LayerAnimationSequence(opacity_element); |
86 animation_sequence->AddObserver(animation_observer); | 91 opacity_sequence->AddObserver(animation_observer); |
87 | 92 |
88 animator->StartAnimation(animation_sequence); | 93 ui::LayerAnimationElement* transform_element = |
94 ui::LayerAnimationElement::CreateTransformElement(target_transform, | |
95 duration); | |
96 ui::LayerAnimationSequence* transform_sequence = | |
97 new ui::LayerAnimationSequence(transform_element); | |
98 transform_sequence->AddObserver(animation_observer); | |
99 | |
100 animator->StartAnimation(opacity_sequence); | |
varkha
2016/04/21 16:16:49
nit: Maybe move this above to have a full block of
bruthig
2016/04/21 21:14:04
Done.
| |
101 animator->StartAnimation(transform_sequence); | |
89 | 102 |
90 animation_observer->SetActive(); | 103 animation_observer->SetActive(); |
91 } | 104 } |
92 | 105 |
106 gfx::Transform InkDropHover::CalculateTransform(const gfx::SizeF& size) const { | |
107 gfx::Transform transform; | |
108 transform.Translate(center_point_.x(), center_point_.y()); | |
109 transform.Scale(size.width() / size_.width(), size.height() / size_.height()); | |
varkha
2016/04/21 16:16:49
This is one place I think could benefit from havin
bruthig
2016/04/21 21:14:04
Discussed offline. We will defer this as per othe
| |
110 transform.Translate(-layer_delegate_->GetCenterPoint().x(), | |
111 -layer_delegate_->GetCenterPoint().y()); | |
112 return transform; | |
113 } | |
114 | |
93 bool InkDropHover::AnimationEndedCallback( | 115 bool InkDropHover::AnimationEndedCallback( |
94 HoverAnimationType animation_type, | 116 HoverAnimationType animation_type, |
95 const ui::CallbackLayerAnimationObserver& observer) { | 117 const ui::CallbackLayerAnimationObserver& observer) { |
96 // AnimationEndedCallback() may be invoked when this is being destroyed and | 118 // AnimationEndedCallback() may be invoked when this is being destroyed and |
97 // |layer_| may be null. | 119 // |layer_| may be null. |
98 if (animation_type == FADE_OUT && layer_) | 120 if (animation_type == FADE_OUT && layer_) |
99 layer_->SetVisible(false); | 121 layer_->SetVisible(false); |
100 return true; | 122 return true; |
101 } | 123 } |
102 | 124 |
103 } // namespace views | 125 } // namespace views |
OLD | NEW |