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