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_animation_controller_impl.h" | 5 #include "ui/views/animation/ink_drop_animation_controller_impl.h" |
6 | 6 |
| 7 #include "base/timer/timer.h" |
| 8 #include "ui/compositor/layer.h" |
7 #include "ui/views/animation/ink_drop_animation.h" | 9 #include "ui/views/animation/ink_drop_animation.h" |
| 10 #include "ui/views/animation/ink_drop_consumer.h" |
8 #include "ui/views/animation/ink_drop_host.h" | 11 #include "ui/views/animation/ink_drop_host.h" |
| 12 #include "ui/views/animation/ink_drop_hover.h" |
9 | 13 |
10 namespace views { | 14 namespace views { |
11 | 15 |
| 16 namespace { |
| 17 |
| 18 // The duration, in milliseconds, of the hover state fade in animation when it |
| 19 // is triggered by user input. |
| 20 const int kHoverFadeInFromUserInputDurationInMs = 250; |
| 21 |
| 22 // The duration, in milliseconds, of the hover state fade out animation when it |
| 23 // is triggered by user input. |
| 24 const int kHoverFadeOutFromUserInputDurationInMs = 250; |
| 25 |
| 26 // The duration, in milliseconds, of the hover state fade in animation when it |
| 27 // is triggered by an ink drop ripple animation ending. |
| 28 const int kHoverFadeInAfterAnimationDurationInMs = 250; |
| 29 |
| 30 // The duration, in milliseconds, of the hover state fade out animation when it |
| 31 // is triggered by an ink drop ripple animation starting. |
| 32 const int kHoverFadeOutBeforeAnimationDurationInMs = 0; |
| 33 |
| 34 // The amount of time in milliseconds that |hover_| should delay after a ripple |
| 35 // animation before fading in. |
| 36 const int kHoverFadeInAfterAnimationDelayInMs = 1000; |
| 37 |
| 38 } // namespace |
| 39 |
12 InkDropAnimationControllerImpl::InkDropAnimationControllerImpl( | 40 InkDropAnimationControllerImpl::InkDropAnimationControllerImpl( |
13 InkDropHost* ink_drop_host) | 41 InkDropHost* ink_drop_host, |
14 : ink_drop_host_(ink_drop_host) {} | 42 InkDropConsumer* ink_drop_consumer) |
| 43 : ink_drop_host_(ink_drop_host), |
| 44 ink_drop_consumer_(ink_drop_consumer), |
| 45 ink_drop_large_corner_radius_(0), |
| 46 ink_drop_small_corner_radius_(0), |
| 47 root_layer_(new ui::Layer(ui::LAYER_NOT_DRAWN)), |
| 48 hover_after_animation_timer_(new base::OneShotTimer()) { |
| 49 ink_drop_host_->AddInkDropLayer(root_layer_.get()); |
| 50 } |
15 | 51 |
16 InkDropAnimationControllerImpl::~InkDropAnimationControllerImpl() { | 52 InkDropAnimationControllerImpl::~InkDropAnimationControllerImpl() { |
17 // Explicitly destroy the InkDropAnimation so that this still exists if | 53 // Explicitly destroy the InkDropAnimation so that this still exists if |
18 // views::InkDropAnimationObserver methods are called on this. | 54 // views::InkDropAnimationObserver methods are called on this. |
19 DestroyInkDropAnimation(); | 55 DestroyInkDropAnimation(); |
| 56 ink_drop_host_->RemoveInkDropLayer(root_layer_.get()); |
20 } | 57 } |
21 | 58 |
22 InkDropState InkDropAnimationControllerImpl::GetInkDropState() const { | 59 InkDropState InkDropAnimationControllerImpl::GetInkDropState() const { |
23 if (!ink_drop_animation_) | 60 if (!ink_drop_animation_) |
24 return InkDropState::HIDDEN; | 61 return InkDropState::HIDDEN; |
25 return ink_drop_animation_->ink_drop_state(); | 62 return ink_drop_animation_->ink_drop_state(); |
26 } | 63 } |
27 | 64 |
28 void InkDropAnimationControllerImpl::AnimateToState( | 65 void InkDropAnimationControllerImpl::AnimateToState( |
29 InkDropState ink_drop_state) { | 66 InkDropState ink_drop_state) { |
30 if (!ink_drop_animation_) | 67 if (!ink_drop_animation_) |
31 CreateInkDropAnimation(); | 68 CreateInkDropAnimation(); |
32 ink_drop_animation_->AnimateToState(ink_drop_state); | 69 ink_drop_animation_->AnimateToState(ink_drop_state); |
33 } | 70 } |
34 | 71 |
| 72 void InkDropAnimationControllerImpl::SetHovered(bool is_hovered) { |
| 73 SetHoveredInternal(is_hovered, |
| 74 is_hovered ? base::TimeDelta::FromMilliseconds( |
| 75 kHoverFadeInFromUserInputDurationInMs) |
| 76 : base::TimeDelta::FromMilliseconds( |
| 77 kHoverFadeOutFromUserInputDurationInMs)); |
| 78 } |
| 79 |
| 80 bool InkDropAnimationControllerImpl::IsHovered() const { |
| 81 return hover_ && hover_->IsVisible(); |
| 82 } |
| 83 |
35 gfx::Size InkDropAnimationControllerImpl::GetInkDropLargeSize() const { | 84 gfx::Size InkDropAnimationControllerImpl::GetInkDropLargeSize() const { |
36 return ink_drop_large_size_; | 85 return ink_drop_large_size_; |
37 } | 86 } |
38 | 87 |
39 void InkDropAnimationControllerImpl::SetInkDropSize(const gfx::Size& large_size, | 88 void InkDropAnimationControllerImpl::SetInkDropSize(const gfx::Size& large_size, |
40 int large_corner_radius, | 89 int large_corner_radius, |
41 const gfx::Size& small_size, | 90 const gfx::Size& small_size, |
42 int small_corner_radius) { | 91 int small_corner_radius) { |
43 // TODO(bruthig): Fix the ink drop animations to work for non-square sizes. | 92 // TODO(bruthig): Fix the ink drop animations to work for non-square sizes. |
44 DCHECK_EQ(large_size.width(), large_size.height()) | 93 DCHECK_EQ(large_size.width(), large_size.height()) |
45 << "The ink drop animation does not currently support non-square sizes."; | 94 << "The ink drop animation does not currently support non-square sizes."; |
46 DCHECK_EQ(small_size.width(), small_size.height()) | 95 DCHECK_EQ(small_size.width(), small_size.height()) |
47 << "The ink drop animation does not currently support non-square sizes."; | 96 << "The ink drop animation does not currently support non-square sizes."; |
48 ink_drop_large_size_ = large_size; | 97 ink_drop_large_size_ = large_size; |
49 ink_drop_large_corner_radius_ = large_corner_radius; | 98 ink_drop_large_corner_radius_ = large_corner_radius; |
50 ink_drop_small_size_ = small_size; | 99 ink_drop_small_size_ = small_size; |
51 ink_drop_small_corner_radius_ = small_corner_radius; | 100 ink_drop_small_corner_radius_ = small_corner_radius; |
52 ink_drop_animation_.reset(); | 101 |
| 102 DestroyInkDropAnimation(); |
| 103 DestroyInkDropHover(); |
53 } | 104 } |
54 | 105 |
55 void InkDropAnimationControllerImpl::SetInkDropCenter( | 106 void InkDropAnimationControllerImpl::SetInkDropCenter( |
56 const gfx::Point& center_point) { | 107 const gfx::Point& center_point) { |
57 ink_drop_center_ = center_point; | 108 ink_drop_center_ = center_point; |
58 if (ink_drop_animation_) | 109 if (ink_drop_animation_) |
59 ink_drop_animation_->SetCenterPoint(ink_drop_center_); | 110 ink_drop_animation_->SetCenterPoint(ink_drop_center_); |
| 111 if (hover_) |
| 112 hover_->SetCenterPoint(ink_drop_center_); |
| 113 } |
| 114 |
| 115 void InkDropAnimationControllerImpl::SetTimerForTest(base::Timer* timer) { |
| 116 hover_after_animation_timer_.reset(timer); |
60 } | 117 } |
61 | 118 |
62 void InkDropAnimationControllerImpl::CreateInkDropAnimation() { | 119 void InkDropAnimationControllerImpl::CreateInkDropAnimation() { |
63 DestroyInkDropAnimation(); | 120 DestroyInkDropAnimation(); |
64 | 121 |
65 ink_drop_animation_.reset(new InkDropAnimation( | 122 ink_drop_animation_.reset(new InkDropAnimation( |
66 ink_drop_large_size_, ink_drop_large_corner_radius_, ink_drop_small_size_, | 123 ink_drop_large_size_, ink_drop_large_corner_radius_, ink_drop_small_size_, |
67 ink_drop_small_corner_radius_)); | 124 ink_drop_small_corner_radius_)); |
68 | 125 |
69 ink_drop_animation_->AddObserver(this); | 126 ink_drop_animation_->AddObserver(this); |
70 ink_drop_animation_->SetCenterPoint(ink_drop_center_); | 127 ink_drop_animation_->SetCenterPoint(ink_drop_center_); |
71 ink_drop_host_->AddInkDropLayer(ink_drop_animation_->root_layer()); | 128 root_layer_->Add(ink_drop_animation_->root_layer()); |
72 } | 129 } |
73 | 130 |
74 void InkDropAnimationControllerImpl::DestroyInkDropAnimation() { | 131 void InkDropAnimationControllerImpl::DestroyInkDropAnimation() { |
75 if (!ink_drop_animation_) | 132 if (!ink_drop_animation_) |
76 return; | 133 return; |
77 ink_drop_host_->RemoveInkDropLayer(ink_drop_animation_->root_layer()); | 134 root_layer_->Remove(ink_drop_animation_->root_layer()); |
78 ink_drop_animation_->RemoveObserver(this); | 135 ink_drop_animation_->RemoveObserver(this); |
79 ink_drop_animation_.reset(); | 136 ink_drop_animation_.reset(); |
80 } | 137 } |
81 | 138 |
| 139 void InkDropAnimationControllerImpl::CreateInkDropHover() { |
| 140 DestroyInkDropHover(); |
| 141 |
| 142 hover_.reset( |
| 143 new InkDropHover(ink_drop_small_size_, ink_drop_small_corner_radius_)); |
| 144 hover_->SetCenterPoint(ink_drop_center_); |
| 145 root_layer_->Add(hover_->layer()); |
| 146 } |
| 147 |
| 148 void InkDropAnimationControllerImpl::DestroyInkDropHover() { |
| 149 if (!hover_) |
| 150 return; |
| 151 root_layer_->Remove(hover_->layer()); |
| 152 hover_.reset(); |
| 153 } |
| 154 |
82 void InkDropAnimationControllerImpl::InkDropAnimationStarted( | 155 void InkDropAnimationControllerImpl::InkDropAnimationStarted( |
83 InkDropState ink_drop_state) {} | 156 InkDropState ink_drop_state) { |
| 157 if (ink_drop_state != views::InkDropState::HIDDEN) { |
| 158 SetHoveredInternal(false, base::TimeDelta::FromMilliseconds( |
| 159 kHoverFadeOutBeforeAnimationDurationInMs)); |
| 160 } |
| 161 } |
84 | 162 |
85 void InkDropAnimationControllerImpl::InkDropAnimationEnded( | 163 void InkDropAnimationControllerImpl::InkDropAnimationEnded( |
86 InkDropState ink_drop_state, | 164 InkDropState ink_drop_state, |
87 InkDropAnimationEndedReason reason) { | 165 InkDropAnimationEndedReason reason) { |
88 if (reason != SUCCESS) | 166 if (reason != SUCCESS) |
89 return; | 167 return; |
90 switch (ink_drop_state) { | 168 switch (ink_drop_state) { |
91 case views::InkDropState::QUICK_ACTION: | 169 case views::InkDropState::QUICK_ACTION: |
92 case views::InkDropState::SLOW_ACTION: | 170 case views::InkDropState::SLOW_ACTION: |
93 case views::InkDropState::DEACTIVATED: | 171 case views::InkDropState::DEACTIVATED: |
94 ink_drop_animation_->AnimateToState(views::InkDropState::HIDDEN); | 172 ink_drop_animation_->AnimateToState(views::InkDropState::HIDDEN); |
95 break; | 173 break; |
96 case views::InkDropState::HIDDEN: | 174 case views::InkDropState::HIDDEN: |
| 175 StartHoverAfterAnimationTimer(); |
97 // TODO(bruthig): Investigate whether creating and destroying | 176 // TODO(bruthig): Investigate whether creating and destroying |
98 // InkDropAnimations is expensive and consider creating an | 177 // InkDropAnimations is expensive and consider creating an |
99 // InkDropAnimationPool. See www.crbug.com/522175. | 178 // InkDropAnimationPool. See www.crbug.com/522175. |
100 DestroyInkDropAnimation(); | 179 DestroyInkDropAnimation(); |
101 break; | 180 break; |
102 default: | 181 default: |
103 break; | 182 break; |
104 } | 183 } |
105 } | 184 } |
106 | 185 |
| 186 void InkDropAnimationControllerImpl::SetHoveredInternal( |
| 187 bool is_hovered, |
| 188 base::TimeDelta animation_duration) { |
| 189 StopHoverAfterAnimationTimer(); |
| 190 |
| 191 if (IsHovered() == is_hovered) |
| 192 return; |
| 193 |
| 194 if (is_hovered) { |
| 195 if (!hover_) |
| 196 CreateInkDropHover(); |
| 197 if (GetInkDropState() == views::InkDropState::HIDDEN) { |
| 198 hover_->FadeIn(animation_duration); |
| 199 } |
| 200 } else { |
| 201 hover_->FadeOut(animation_duration); |
| 202 } |
| 203 } |
| 204 |
| 205 void InkDropAnimationControllerImpl::StartHoverAfterAnimationTimer() { |
| 206 StopHoverAfterAnimationTimer(); |
| 207 |
| 208 hover_after_animation_timer_->Start( |
| 209 FROM_HERE, |
| 210 base::TimeDelta::FromMilliseconds(kHoverFadeInAfterAnimationDelayInMs), |
| 211 base::Bind(&InkDropAnimationControllerImpl::HoverAfterAnimationTimerFired, |
| 212 base::Unretained(this))); |
| 213 } |
| 214 |
| 215 void InkDropAnimationControllerImpl::StopHoverAfterAnimationTimer() { |
| 216 if (hover_after_animation_timer_->IsRunning()) |
| 217 hover_after_animation_timer_->Reset(); |
| 218 } |
| 219 |
| 220 void InkDropAnimationControllerImpl::HoverAfterAnimationTimerFired() { |
| 221 SetHoveredInternal(ink_drop_consumer_->ShouldShowInkDropHover(), |
| 222 base::TimeDelta::FromMilliseconds( |
| 223 kHoverFadeInAfterAnimationDurationInMs)); |
| 224 } |
| 225 |
107 } // namespace views | 226 } // namespace views |
OLD | NEW |