| 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/auto_reset.h" | 7 #include "base/auto_reset.h" |
| 8 #include "base/timer/timer.h" | 8 #include "base/timer/timer.h" |
| 9 #include "ui/compositor/layer.h" | 9 #include "ui/compositor/layer.h" |
| 10 #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" | 11 #include "ui/views/animation/ink_drop_hover.h" |
| 12 #include "ui/views/animation/square_ink_drop_animation.h" | 12 #include "ui/views/animation/square_ink_drop_ripple.h" |
| 13 | 13 |
| 14 namespace views { | 14 namespace views { |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 // The duration, in milliseconds, of the hover state fade in animation when it | 18 // The duration, in milliseconds, of the hover state fade in animation when it |
| 19 // is triggered by user input. | 19 // is triggered by user input. |
| 20 const int kHoverFadeInFromUserInputDurationInMs = 250; | 20 const int kHoverFadeInFromUserInputDurationInMs = 250; |
| 21 | 21 |
| 22 // The duration, in milliseconds, of the hover state fade out animation when it | 22 // The duration, in milliseconds, of the hover state fade out animation when it |
| 23 // is triggered by user input. | 23 // is triggered by user input. |
| 24 const int kHoverFadeOutFromUserInputDurationInMs = 250; | 24 const int kHoverFadeOutFromUserInputDurationInMs = 250; |
| 25 | 25 |
| 26 // The duration, in milliseconds, of the hover state fade in animation when it | 26 // The duration, in milliseconds, of the hover state fade in animation when it |
| 27 // is triggered by an ink drop ripple animation ending. | 27 // is triggered by an ink drop ripple animation ending. |
| 28 const int kHoverFadeInAfterAnimationDurationInMs = 250; | 28 const int kHoverFadeInAfterRippleDurationInMs = 250; |
| 29 | 29 |
| 30 // The duration, in milliseconds, of the hover state fade out animation when it | 30 // The duration, in milliseconds, of the hover state fade out animation when it |
| 31 // is triggered by an ink drop ripple animation starting. | 31 // is triggered by an ink drop ripple animation starting. |
| 32 const int kHoverFadeOutBeforeAnimationDurationInMs = 120; | 32 const int kHoverFadeOutBeforeRippleDurationInMs = 120; |
| 33 | 33 |
| 34 // The amount of time in milliseconds that |hover_| should delay after a ripple | 34 // The amount of time in milliseconds that |hover_| should delay after a ripple |
| 35 // animation before fading in. | 35 // animation before fading in. |
| 36 const int kHoverFadeInAfterAnimationDelayInMs = 1000; | 36 const int kHoverFadeInAfterRippleDelayInMs = 1000; |
| 37 | 37 |
| 38 // Returns true if an ink drop with the given |ink_drop_state| should | 38 // Returns true if an ink drop with the given |ink_drop_state| should |
| 39 // automatically transition to the InkDropState::HIDDEN state. | 39 // automatically transition to the InkDropState::HIDDEN state. |
| 40 bool ShouldAnimateToHidden(InkDropState ink_drop_state) { | 40 bool ShouldAnimateToHidden(InkDropState ink_drop_state) { |
| 41 switch (ink_drop_state) { | 41 switch (ink_drop_state) { |
| 42 case views::InkDropState::ACTION_TRIGGERED: | 42 case views::InkDropState::ACTION_TRIGGERED: |
| 43 case views::InkDropState::ALTERNATE_ACTION_TRIGGERED: | 43 case views::InkDropState::ALTERNATE_ACTION_TRIGGERED: |
| 44 case views::InkDropState::DEACTIVATED: | 44 case views::InkDropState::DEACTIVATED: |
| 45 return true; | 45 return true; |
| 46 default: | 46 default: |
| 47 return false; | 47 return false; |
| 48 } | 48 } |
| 49 } | 49 } |
| 50 | 50 |
| 51 } // namespace | 51 } // namespace |
| 52 | 52 |
| 53 InkDropAnimationControllerImpl::InkDropAnimationControllerImpl( | 53 InkDropAnimationControllerImpl::InkDropAnimationControllerImpl( |
| 54 InkDropHost* ink_drop_host) | 54 InkDropHost* ink_drop_host) |
| 55 : ink_drop_host_(ink_drop_host), | 55 : ink_drop_host_(ink_drop_host), |
| 56 root_layer_(new ui::Layer(ui::LAYER_NOT_DRAWN)), | 56 root_layer_(new ui::Layer(ui::LAYER_NOT_DRAWN)), |
| 57 root_layer_added_to_host_(false), | 57 root_layer_added_to_host_(false), |
| 58 is_hovered_(false), | 58 is_hovered_(false), |
| 59 hover_after_animation_timer_(nullptr) { | 59 hover_after_ripple_timer_(nullptr) { |
| 60 root_layer_->set_name("InkDropAnimationControllerImpl:RootLayer"); | 60 root_layer_->set_name("InkDropAnimationControllerImpl:RootLayer"); |
| 61 } | 61 } |
| 62 | 62 |
| 63 InkDropAnimationControllerImpl::~InkDropAnimationControllerImpl() { | 63 InkDropAnimationControllerImpl::~InkDropAnimationControllerImpl() { |
| 64 // Explicitly destroy the InkDropAnimation so that this still exists if | 64 // Explicitly destroy the InkDropRipple so that this still exists if |
| 65 // views::InkDropAnimationObserver methods are called on this. | 65 // views::InkDropRippleObserver methods are called on this. |
| 66 DestroyInkDropAnimation(); | 66 DestroyInkDropRipple(); |
| 67 DestroyInkDropHover(); | 67 DestroyInkDropHover(); |
| 68 } | 68 } |
| 69 | 69 |
| 70 InkDropState InkDropAnimationControllerImpl::GetTargetInkDropState() const { | 70 InkDropState InkDropAnimationControllerImpl::GetTargetInkDropState() const { |
| 71 if (!ink_drop_animation_) | 71 if (!ink_drop_ripple_) |
| 72 return InkDropState::HIDDEN; | 72 return InkDropState::HIDDEN; |
| 73 return ink_drop_animation_->target_ink_drop_state(); | 73 return ink_drop_ripple_->target_ink_drop_state(); |
| 74 } | 74 } |
| 75 | 75 |
| 76 bool InkDropAnimationControllerImpl::IsVisible() const { | 76 bool InkDropAnimationControllerImpl::IsVisible() const { |
| 77 return ink_drop_animation_ && ink_drop_animation_->IsVisible(); | 77 return ink_drop_ripple_ && ink_drop_ripple_->IsVisible(); |
| 78 } | 78 } |
| 79 | 79 |
| 80 void InkDropAnimationControllerImpl::AnimateToState( | 80 void InkDropAnimationControllerImpl::AnimateToState( |
| 81 InkDropState ink_drop_state) { | 81 InkDropState ink_drop_state) { |
| 82 DestroyHiddenTargetedAnimations(); | 82 DestroyHiddenTargetedAnimations(); |
| 83 if (!ink_drop_animation_) | 83 if (!ink_drop_ripple_) |
| 84 CreateInkDropAnimation(); | 84 CreateInkDropRipple(); |
| 85 | 85 |
| 86 if (ink_drop_state != views::InkDropState::HIDDEN) { | 86 if (ink_drop_state != views::InkDropState::HIDDEN) { |
| 87 SetHoveredInternal(false, base::TimeDelta::FromMilliseconds( | 87 SetHoveredInternal(false, base::TimeDelta::FromMilliseconds( |
| 88 kHoverFadeOutBeforeAnimationDurationInMs), | 88 kHoverFadeOutBeforeRippleDurationInMs), |
| 89 true); | 89 true); |
| 90 } | 90 } |
| 91 | 91 |
| 92 ink_drop_animation_->AnimateToState(ink_drop_state); | 92 ink_drop_ripple_->AnimateToState(ink_drop_state); |
| 93 } | 93 } |
| 94 | 94 |
| 95 void InkDropAnimationControllerImpl::SnapToActivated() { | 95 void InkDropAnimationControllerImpl::SnapToActivated() { |
| 96 DestroyHiddenTargetedAnimations(); | 96 DestroyHiddenTargetedAnimations(); |
| 97 if (!ink_drop_animation_) | 97 if (!ink_drop_ripple_) |
| 98 CreateInkDropAnimation(); | 98 CreateInkDropRipple(); |
| 99 | 99 |
| 100 SetHoveredInternal(false, base::TimeDelta(), false); | 100 SetHoveredInternal(false, base::TimeDelta(), false); |
| 101 | 101 |
| 102 ink_drop_animation_->SnapToActivated(); | 102 ink_drop_ripple_->SnapToActivated(); |
| 103 } | 103 } |
| 104 | 104 |
| 105 void InkDropAnimationControllerImpl::SetHovered(bool is_hovered) { | 105 void InkDropAnimationControllerImpl::SetHovered(bool is_hovered) { |
| 106 is_hovered_ = is_hovered; | 106 is_hovered_ = is_hovered; |
| 107 SetHoveredInternal(is_hovered, | 107 SetHoveredInternal(is_hovered, |
| 108 is_hovered ? base::TimeDelta::FromMilliseconds( | 108 is_hovered ? base::TimeDelta::FromMilliseconds( |
| 109 kHoverFadeInFromUserInputDurationInMs) | 109 kHoverFadeInFromUserInputDurationInMs) |
| 110 : base::TimeDelta::FromMilliseconds( | 110 : base::TimeDelta::FromMilliseconds( |
| 111 kHoverFadeOutFromUserInputDurationInMs), | 111 kHoverFadeOutFromUserInputDurationInMs), |
| 112 false); | 112 false); |
| 113 } | 113 } |
| 114 | 114 |
| 115 void InkDropAnimationControllerImpl::DestroyHiddenTargetedAnimations() { | 115 void InkDropAnimationControllerImpl::DestroyHiddenTargetedAnimations() { |
| 116 if (ink_drop_animation_ && | 116 if (ink_drop_ripple_ && |
| 117 (ink_drop_animation_->target_ink_drop_state() == InkDropState::HIDDEN || | 117 (ink_drop_ripple_->target_ink_drop_state() == InkDropState::HIDDEN || |
| 118 ShouldAnimateToHidden(ink_drop_animation_->target_ink_drop_state()))) { | 118 ShouldAnimateToHidden(ink_drop_ripple_->target_ink_drop_state()))) { |
| 119 DestroyInkDropAnimation(); | 119 DestroyInkDropRipple(); |
| 120 } | 120 } |
| 121 } | 121 } |
| 122 | 122 |
| 123 void InkDropAnimationControllerImpl::CreateInkDropAnimation() { | 123 void InkDropAnimationControllerImpl::CreateInkDropRipple() { |
| 124 DestroyInkDropAnimation(); | 124 DestroyInkDropRipple(); |
| 125 ink_drop_animation_ = ink_drop_host_->CreateInkDropAnimation(); | 125 ink_drop_ripple_ = ink_drop_host_->CreateInkDropRipple(); |
| 126 ink_drop_animation_->set_observer(this); | 126 ink_drop_ripple_->set_observer(this); |
| 127 root_layer_->Add(ink_drop_animation_->GetRootLayer()); | 127 root_layer_->Add(ink_drop_ripple_->GetRootLayer()); |
| 128 AddRootLayerToHostIfNeeded(); | 128 AddRootLayerToHostIfNeeded(); |
| 129 } | 129 } |
| 130 | 130 |
| 131 void InkDropAnimationControllerImpl::DestroyInkDropAnimation() { | 131 void InkDropAnimationControllerImpl::DestroyInkDropRipple() { |
| 132 if (!ink_drop_animation_) | 132 if (!ink_drop_ripple_) |
| 133 return; | 133 return; |
| 134 root_layer_->Remove(ink_drop_animation_->GetRootLayer()); | 134 root_layer_->Remove(ink_drop_ripple_->GetRootLayer()); |
| 135 ink_drop_animation_.reset(); | 135 ink_drop_ripple_.reset(); |
| 136 RemoveRootLayerFromHostIfNeeded(); | 136 RemoveRootLayerFromHostIfNeeded(); |
| 137 } | 137 } |
| 138 | 138 |
| 139 void InkDropAnimationControllerImpl::CreateInkDropHover() { | 139 void InkDropAnimationControllerImpl::CreateInkDropHover() { |
| 140 DestroyInkDropHover(); | 140 DestroyInkDropHover(); |
| 141 | 141 |
| 142 hover_ = ink_drop_host_->CreateInkDropHover(); | 142 hover_ = ink_drop_host_->CreateInkDropHover(); |
| 143 if (!hover_) | 143 if (!hover_) |
| 144 return; | 144 return; |
| 145 hover_->set_observer(this); | 145 hover_->set_observer(this); |
| 146 root_layer_->Add(hover_->layer()); | 146 root_layer_->Add(hover_->layer()); |
| 147 AddRootLayerToHostIfNeeded(); | 147 AddRootLayerToHostIfNeeded(); |
| 148 } | 148 } |
| 149 | 149 |
| 150 void InkDropAnimationControllerImpl::DestroyInkDropHover() { | 150 void InkDropAnimationControllerImpl::DestroyInkDropHover() { |
| 151 if (!hover_) | 151 if (!hover_) |
| 152 return; | 152 return; |
| 153 root_layer_->Remove(hover_->layer()); | 153 root_layer_->Remove(hover_->layer()); |
| 154 hover_->set_observer(nullptr); | 154 hover_->set_observer(nullptr); |
| 155 hover_.reset(); | 155 hover_.reset(); |
| 156 RemoveRootLayerFromHostIfNeeded(); | 156 RemoveRootLayerFromHostIfNeeded(); |
| 157 } | 157 } |
| 158 | 158 |
| 159 void InkDropAnimationControllerImpl::AddRootLayerToHostIfNeeded() { | 159 void InkDropAnimationControllerImpl::AddRootLayerToHostIfNeeded() { |
| 160 DCHECK(hover_ || ink_drop_animation_); | 160 DCHECK(hover_ || ink_drop_ripple_); |
| 161 if (!root_layer_added_to_host_) { | 161 if (!root_layer_added_to_host_) { |
| 162 root_layer_added_to_host_ = true; | 162 root_layer_added_to_host_ = true; |
| 163 ink_drop_host_->AddInkDropLayer(root_layer_.get()); | 163 ink_drop_host_->AddInkDropLayer(root_layer_.get()); |
| 164 } | 164 } |
| 165 } | 165 } |
| 166 | 166 |
| 167 void InkDropAnimationControllerImpl::RemoveRootLayerFromHostIfNeeded() { | 167 void InkDropAnimationControllerImpl::RemoveRootLayerFromHostIfNeeded() { |
| 168 if (root_layer_added_to_host_ && !hover_ && !ink_drop_animation_) { | 168 if (root_layer_added_to_host_ && !hover_ && !ink_drop_ripple_) { |
| 169 root_layer_added_to_host_ = false; | 169 root_layer_added_to_host_ = false; |
| 170 ink_drop_host_->RemoveInkDropLayer(root_layer_.get()); | 170 ink_drop_host_->RemoveInkDropLayer(root_layer_.get()); |
| 171 } | 171 } |
| 172 } | 172 } |
| 173 | 173 |
| 174 bool InkDropAnimationControllerImpl::IsHoverFadingInOrVisible() const { | 174 bool InkDropAnimationControllerImpl::IsHoverFadingInOrVisible() const { |
| 175 return hover_ && hover_->IsFadingInOrVisible(); | 175 return hover_ && hover_->IsFadingInOrVisible(); |
| 176 } | 176 } |
| 177 | 177 |
| 178 // ----------------------------------------------------------------------------- | 178 // ----------------------------------------------------------------------------- |
| 179 // views::InkDropAnimationObserver: | 179 // views::InkDropRippleObserver: |
| 180 | 180 |
| 181 void InkDropAnimationControllerImpl::AnimationStarted( | 181 void InkDropAnimationControllerImpl::AnimationStarted( |
| 182 InkDropState ink_drop_state) {} | 182 InkDropState ink_drop_state) {} |
| 183 | 183 |
| 184 void InkDropAnimationControllerImpl::AnimationEnded( | 184 void InkDropAnimationControllerImpl::AnimationEnded( |
| 185 InkDropState ink_drop_state, | 185 InkDropState ink_drop_state, |
| 186 InkDropAnimationEndedReason reason) { | 186 InkDropAnimationEndedReason reason) { |
| 187 if (reason != InkDropAnimationEndedReason::SUCCESS) | 187 if (reason != InkDropAnimationEndedReason::SUCCESS) |
| 188 return; | 188 return; |
| 189 if (ShouldAnimateToHidden(ink_drop_state)) { | 189 if (ShouldAnimateToHidden(ink_drop_state)) { |
| 190 ink_drop_animation_->AnimateToState(views::InkDropState::HIDDEN); | 190 ink_drop_ripple_->AnimateToState(views::InkDropState::HIDDEN); |
| 191 } else if (ink_drop_state == views::InkDropState::HIDDEN) { | 191 } else if (ink_drop_state == views::InkDropState::HIDDEN) { |
| 192 if (is_hovered_) | 192 if (is_hovered_) |
| 193 StartHoverAfterAnimationTimer(); | 193 StartHoverAfterRippleTimer(); |
| 194 // TODO(bruthig): Investigate whether creating and destroying | 194 // TODO(bruthig): Investigate whether creating and destroying |
| 195 // InkDropAnimations is expensive and consider creating an | 195 // InkDropRipples is expensive and consider creating an |
| 196 // InkDropAnimationPool. See www.crbug.com/522175. | 196 // InkDropRipplePool. See www.crbug.com/522175. |
| 197 DestroyInkDropAnimation(); | 197 DestroyInkDropRipple(); |
| 198 } | 198 } |
| 199 } | 199 } |
| 200 | 200 |
| 201 // ----------------------------------------------------------------------------- | 201 // ----------------------------------------------------------------------------- |
| 202 // views::InkDropHoverObserver: | 202 // views::InkDropHoverObserver: |
| 203 | 203 |
| 204 void InkDropAnimationControllerImpl::AnimationStarted( | 204 void InkDropAnimationControllerImpl::AnimationStarted( |
| 205 InkDropHover::AnimationType animation_type) {} | 205 InkDropHover::AnimationType animation_type) {} |
| 206 | 206 |
| 207 void InkDropAnimationControllerImpl::AnimationEnded( | 207 void InkDropAnimationControllerImpl::AnimationEnded( |
| 208 InkDropHover::AnimationType animation_type, | 208 InkDropHover::AnimationType animation_type, |
| 209 InkDropAnimationEndedReason reason) { | 209 InkDropAnimationEndedReason reason) { |
| 210 if (animation_type == InkDropHover::FADE_OUT && | 210 if (animation_type == InkDropHover::FADE_OUT && |
| 211 reason == InkDropAnimationEndedReason::SUCCESS) { | 211 reason == InkDropAnimationEndedReason::SUCCESS) { |
| 212 DestroyInkDropHover(); | 212 DestroyInkDropHover(); |
| 213 } | 213 } |
| 214 } | 214 } |
| 215 | 215 |
| 216 void InkDropAnimationControllerImpl::SetHoveredInternal( | 216 void InkDropAnimationControllerImpl::SetHoveredInternal( |
| 217 bool is_hovered, | 217 bool is_hovered, |
| 218 base::TimeDelta animation_duration, | 218 base::TimeDelta animation_duration, |
| 219 bool explode) { | 219 bool explode) { |
| 220 StopHoverAfterAnimationTimer(); | 220 StopHoverAfterRippleTimer(); |
| 221 | 221 |
| 222 if (IsHoverFadingInOrVisible() == is_hovered) | 222 if (IsHoverFadingInOrVisible() == is_hovered) |
| 223 return; | 223 return; |
| 224 | 224 |
| 225 if (is_hovered) { | 225 if (is_hovered) { |
| 226 CreateInkDropHover(); | 226 CreateInkDropHover(); |
| 227 if (hover_ && !IsVisible()) | 227 if (hover_ && !IsVisible()) |
| 228 hover_->FadeIn(animation_duration); | 228 hover_->FadeIn(animation_duration); |
| 229 } else { | 229 } else { |
| 230 hover_->FadeOut(animation_duration, explode); | 230 hover_->FadeOut(animation_duration, explode); |
| 231 } | 231 } |
| 232 } | 232 } |
| 233 | 233 |
| 234 void InkDropAnimationControllerImpl::StartHoverAfterAnimationTimer() { | 234 void InkDropAnimationControllerImpl::StartHoverAfterRippleTimer() { |
| 235 StopHoverAfterAnimationTimer(); | 235 StopHoverAfterRippleTimer(); |
| 236 | 236 |
| 237 if (!hover_after_animation_timer_) | 237 if (!hover_after_ripple_timer_) |
| 238 hover_after_animation_timer_.reset(new base::OneShotTimer); | 238 hover_after_ripple_timer_.reset(new base::OneShotTimer); |
| 239 | 239 |
| 240 hover_after_animation_timer_->Start( | 240 hover_after_ripple_timer_->Start( |
| 241 FROM_HERE, | 241 FROM_HERE, |
| 242 base::TimeDelta::FromMilliseconds(kHoverFadeInAfterAnimationDelayInMs), | 242 base::TimeDelta::FromMilliseconds(kHoverFadeInAfterRippleDelayInMs), |
| 243 base::Bind(&InkDropAnimationControllerImpl::HoverAfterAnimationTimerFired, | 243 base::Bind(&InkDropAnimationControllerImpl::HoverAfterRippleTimerFired, |
| 244 base::Unretained(this))); | 244 base::Unretained(this))); |
| 245 } | 245 } |
| 246 | 246 |
| 247 void InkDropAnimationControllerImpl::StopHoverAfterAnimationTimer() { | 247 void InkDropAnimationControllerImpl::StopHoverAfterRippleTimer() { |
| 248 if (hover_after_animation_timer_) | 248 if (hover_after_ripple_timer_) |
| 249 hover_after_animation_timer_.reset(); | 249 hover_after_ripple_timer_.reset(); |
| 250 } | 250 } |
| 251 | 251 |
| 252 void InkDropAnimationControllerImpl::HoverAfterAnimationTimerFired() { | 252 void InkDropAnimationControllerImpl::HoverAfterRippleTimerFired() { |
| 253 SetHoveredInternal(true, base::TimeDelta::FromMilliseconds( | 253 SetHoveredInternal(true, base::TimeDelta::FromMilliseconds( |
| 254 kHoverFadeInAfterAnimationDurationInMs), | 254 kHoverFadeInAfterRippleDurationInMs), |
| 255 true); | 255 true); |
| 256 hover_after_animation_timer_.reset(); | 256 hover_after_ripple_timer_.reset(); |
| 257 } | 257 } |
| 258 | 258 |
| 259 } // namespace views | 259 } // namespace views |
| OLD | NEW |