| 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_impl.h" | 5 #include "ui/views/animation/ink_drop_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_highlight.h" |
| 10 #include "ui/views/animation/ink_drop_host.h" | 11 #include "ui/views/animation/ink_drop_host.h" |
| 11 #include "ui/views/animation/ink_drop_hover.h" | |
| 12 #include "ui/views/animation/square_ink_drop_ripple.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 highlight state fade in animation when |
| 19 // is triggered by user input. | 19 // it is triggered by user input. |
| 20 const int kHoverFadeInFromUserInputDurationInMs = 250; | 20 const int kHighlightFadeInFromUserInputDurationInMs = 250; |
| 21 | 21 |
| 22 // The duration, in milliseconds, of the hover state fade out animation when it | 22 // The duration, in milliseconds, of the highlight state fade out animation when |
| 23 // is triggered by user input. | 23 // it is triggered by user input. |
| 24 const int kHoverFadeOutFromUserInputDurationInMs = 250; | 24 const int kHighlightFadeOutFromUserInputDurationInMs = 250; |
| 25 | 25 |
| 26 // The duration, in milliseconds, of the hover state fade in animation when it | 26 // The duration, in milliseconds, of the highlight state fade in animation when |
| 27 // is triggered by an ink drop ripple animation ending. | 27 // it is triggered by an ink drop ripple animation ending. |
| 28 const int kHoverFadeInAfterRippleDurationInMs = 250; | 28 const int kHighlightFadeInAfterRippleDurationInMs = 250; |
| 29 | 29 |
| 30 // The duration, in milliseconds, of the hover state fade out animation when it | 30 // The duration, in milliseconds, of the highlight state fade out animation when |
| 31 // is triggered by an ink drop ripple animation starting. | 31 // it is triggered by an ink drop ripple animation starting. |
| 32 const int kHoverFadeOutBeforeRippleDurationInMs = 120; | 32 const int kHighlightFadeOutBeforeRippleDurationInMs = 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 |highlight_| should delay after a |
| 35 // animation before fading in. | 35 // ripple animation before fading in. |
| 36 const int kHoverFadeInAfterRippleDelayInMs = 1000; | 36 const int kHighlightFadeInAfterRippleDelayInMs = 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 InkDropImpl::InkDropImpl(InkDropHost* ink_drop_host) | 53 InkDropImpl::InkDropImpl(InkDropHost* ink_drop_host) |
| 54 : ink_drop_host_(ink_drop_host), | 54 : ink_drop_host_(ink_drop_host), |
| 55 root_layer_(new ui::Layer(ui::LAYER_NOT_DRAWN)), | 55 root_layer_(new ui::Layer(ui::LAYER_NOT_DRAWN)), |
| 56 root_layer_added_to_host_(false), | 56 root_layer_added_to_host_(false), |
| 57 is_hovered_(false), | 57 is_hovered_(false), |
| 58 is_focused_(false), | 58 is_focused_(false), |
| 59 hover_after_ripple_timer_(nullptr) { | 59 highlight_after_ripple_timer_(nullptr) { |
| 60 root_layer_->set_name("InkDropImpl:RootLayer"); | 60 root_layer_->set_name("InkDropImpl:RootLayer"); |
| 61 } | 61 } |
| 62 | 62 |
| 63 InkDropImpl::~InkDropImpl() { | 63 InkDropImpl::~InkDropImpl() { |
| 64 // Explicitly destroy the InkDropRipple so that this still exists if | 64 // Explicitly destroy the InkDropRipple so that this still exists if |
| 65 // views::InkDropRippleObserver methods are called on this. | 65 // views::InkDropRippleObserver methods are called on this. |
| 66 DestroyInkDropRipple(); | 66 DestroyInkDropRipple(); |
| 67 DestroyInkDropHover(); | 67 DestroyInkDropHighlight(); |
| 68 } | 68 } |
| 69 | 69 |
| 70 InkDropState InkDropImpl::GetTargetInkDropState() const { | 70 InkDropState InkDropImpl::GetTargetInkDropState() const { |
| 71 if (!ink_drop_ripple_) | 71 if (!ink_drop_ripple_) |
| 72 return InkDropState::HIDDEN; | 72 return InkDropState::HIDDEN; |
| 73 return ink_drop_ripple_->target_ink_drop_state(); | 73 return ink_drop_ripple_->target_ink_drop_state(); |
| 74 } | 74 } |
| 75 | 75 |
| 76 bool InkDropImpl::IsVisible() const { | 76 bool InkDropImpl::IsVisible() const { |
| 77 return ink_drop_ripple_ && ink_drop_ripple_->IsVisible(); | 77 return ink_drop_ripple_ && ink_drop_ripple_->IsVisible(); |
| 78 } | 78 } |
| 79 | 79 |
| 80 void InkDropImpl::AnimateToState(InkDropState ink_drop_state) { | 80 void InkDropImpl::AnimateToState(InkDropState ink_drop_state) { |
| 81 DestroyHiddenTargetedAnimations(); | 81 DestroyHiddenTargetedAnimations(); |
| 82 if (!ink_drop_ripple_) | 82 if (!ink_drop_ripple_) |
| 83 CreateInkDropRipple(); | 83 CreateInkDropRipple(); |
| 84 | 84 |
| 85 if (ink_drop_state != views::InkDropState::HIDDEN) { | 85 if (ink_drop_state != views::InkDropState::HIDDEN) { |
| 86 SetHighlight(false, base::TimeDelta::FromMilliseconds( | 86 SetHighlight(false, base::TimeDelta::FromMilliseconds( |
| 87 kHoverFadeOutBeforeRippleDurationInMs), | 87 kHighlightFadeOutBeforeRippleDurationInMs), |
| 88 true); | 88 true); |
| 89 } | 89 } |
| 90 | 90 |
| 91 ink_drop_ripple_->AnimateToState(ink_drop_state); | 91 ink_drop_ripple_->AnimateToState(ink_drop_state); |
| 92 } | 92 } |
| 93 | 93 |
| 94 void InkDropImpl::SnapToActivated() { | 94 void InkDropImpl::SnapToActivated() { |
| 95 DestroyHiddenTargetedAnimations(); | 95 DestroyHiddenTargetedAnimations(); |
| 96 if (!ink_drop_ripple_) | 96 if (!ink_drop_ripple_) |
| 97 CreateInkDropRipple(); | 97 CreateInkDropRipple(); |
| 98 | 98 |
| 99 SetHighlight(false, base::TimeDelta(), false); | 99 SetHighlight(false, base::TimeDelta(), false); |
| 100 | 100 |
| 101 ink_drop_ripple_->SnapToActivated(); | 101 ink_drop_ripple_->SnapToActivated(); |
| 102 } | 102 } |
| 103 | 103 |
| 104 void InkDropImpl::SetHovered(bool is_hovered) { | 104 void InkDropImpl::SetHovered(bool is_hovered) { |
| 105 is_hovered_ = is_hovered; | 105 is_hovered_ = is_hovered; |
| 106 SetHighlight(ShouldHighlight(), | 106 SetHighlight(ShouldHighlight(), |
| 107 ShouldHighlight() ? base::TimeDelta::FromMilliseconds( | 107 ShouldHighlight() |
| 108 kHoverFadeInFromUserInputDurationInMs) | 108 ? base::TimeDelta::FromMilliseconds( |
| 109 : base::TimeDelta::FromMilliseconds( | 109 kHighlightFadeInFromUserInputDurationInMs) |
| 110 kHoverFadeOutFromUserInputDurationInMs), | 110 : base::TimeDelta::FromMilliseconds( |
| 111 kHighlightFadeOutFromUserInputDurationInMs), |
| 111 false); | 112 false); |
| 112 } | 113 } |
| 113 | 114 |
| 114 void InkDropImpl::SetFocused(bool is_focused) { | 115 void InkDropImpl::SetFocused(bool is_focused) { |
| 115 is_focused_ = is_focused; | 116 is_focused_ = is_focused; |
| 116 SetHighlight(ShouldHighlight(), base::TimeDelta(), false); | 117 SetHighlight(ShouldHighlight(), base::TimeDelta(), false); |
| 117 } | 118 } |
| 118 | 119 |
| 119 void InkDropImpl::DestroyHiddenTargetedAnimations() { | 120 void InkDropImpl::DestroyHiddenTargetedAnimations() { |
| 120 if (ink_drop_ripple_ && | 121 if (ink_drop_ripple_ && |
| (...skipping 12 matching lines...) Expand all Loading... |
| 133 } | 134 } |
| 134 | 135 |
| 135 void InkDropImpl::DestroyInkDropRipple() { | 136 void InkDropImpl::DestroyInkDropRipple() { |
| 136 if (!ink_drop_ripple_) | 137 if (!ink_drop_ripple_) |
| 137 return; | 138 return; |
| 138 root_layer_->Remove(ink_drop_ripple_->GetRootLayer()); | 139 root_layer_->Remove(ink_drop_ripple_->GetRootLayer()); |
| 139 ink_drop_ripple_.reset(); | 140 ink_drop_ripple_.reset(); |
| 140 RemoveRootLayerFromHostIfNeeded(); | 141 RemoveRootLayerFromHostIfNeeded(); |
| 141 } | 142 } |
| 142 | 143 |
| 143 void InkDropImpl::CreateInkDropHover() { | 144 void InkDropImpl::CreateInkDropHighlight() { |
| 144 DestroyInkDropHover(); | 145 DestroyInkDropHighlight(); |
| 145 | 146 |
| 146 hover_ = ink_drop_host_->CreateInkDropHover(); | 147 highlight_ = ink_drop_host_->CreateInkDropHighlight(); |
| 147 if (!hover_) | 148 if (!highlight_) |
| 148 return; | 149 return; |
| 149 hover_->set_observer(this); | 150 highlight_->set_observer(this); |
| 150 root_layer_->Add(hover_->layer()); | 151 root_layer_->Add(highlight_->layer()); |
| 151 AddRootLayerToHostIfNeeded(); | 152 AddRootLayerToHostIfNeeded(); |
| 152 } | 153 } |
| 153 | 154 |
| 154 void InkDropImpl::DestroyInkDropHover() { | 155 void InkDropImpl::DestroyInkDropHighlight() { |
| 155 if (!hover_) | 156 if (!highlight_) |
| 156 return; | 157 return; |
| 157 root_layer_->Remove(hover_->layer()); | 158 root_layer_->Remove(highlight_->layer()); |
| 158 hover_->set_observer(nullptr); | 159 highlight_->set_observer(nullptr); |
| 159 hover_.reset(); | 160 highlight_.reset(); |
| 160 RemoveRootLayerFromHostIfNeeded(); | 161 RemoveRootLayerFromHostIfNeeded(); |
| 161 } | 162 } |
| 162 | 163 |
| 163 void InkDropImpl::AddRootLayerToHostIfNeeded() { | 164 void InkDropImpl::AddRootLayerToHostIfNeeded() { |
| 164 DCHECK(hover_ || ink_drop_ripple_); | 165 DCHECK(highlight_ || ink_drop_ripple_); |
| 165 if (!root_layer_added_to_host_) { | 166 if (!root_layer_added_to_host_) { |
| 166 root_layer_added_to_host_ = true; | 167 root_layer_added_to_host_ = true; |
| 167 ink_drop_host_->AddInkDropLayer(root_layer_.get()); | 168 ink_drop_host_->AddInkDropLayer(root_layer_.get()); |
| 168 } | 169 } |
| 169 } | 170 } |
| 170 | 171 |
| 171 void InkDropImpl::RemoveRootLayerFromHostIfNeeded() { | 172 void InkDropImpl::RemoveRootLayerFromHostIfNeeded() { |
| 172 if (root_layer_added_to_host_ && !hover_ && !ink_drop_ripple_) { | 173 if (root_layer_added_to_host_ && !highlight_ && !ink_drop_ripple_) { |
| 173 root_layer_added_to_host_ = false; | 174 root_layer_added_to_host_ = false; |
| 174 ink_drop_host_->RemoveInkDropLayer(root_layer_.get()); | 175 ink_drop_host_->RemoveInkDropLayer(root_layer_.get()); |
| 175 } | 176 } |
| 176 } | 177 } |
| 177 | 178 |
| 178 bool InkDropImpl::IsHoverFadingInOrVisible() const { | 179 bool InkDropImpl::IsHighlightFadingInOrVisible() const { |
| 179 return hover_ && hover_->IsFadingInOrVisible(); | 180 return highlight_ && highlight_->IsFadingInOrVisible(); |
| 180 } | 181 } |
| 181 | 182 |
| 182 // ----------------------------------------------------------------------------- | 183 // ----------------------------------------------------------------------------- |
| 183 // views::InkDropRippleObserver: | 184 // views::InkDropRippleObserver: |
| 184 | 185 |
| 185 void InkDropImpl::AnimationStarted(InkDropState ink_drop_state) {} | 186 void InkDropImpl::AnimationStarted(InkDropState ink_drop_state) {} |
| 186 | 187 |
| 187 void InkDropImpl::AnimationEnded(InkDropState ink_drop_state, | 188 void InkDropImpl::AnimationEnded(InkDropState ink_drop_state, |
| 188 InkDropAnimationEndedReason reason) { | 189 InkDropAnimationEndedReason reason) { |
| 189 if (reason != InkDropAnimationEndedReason::SUCCESS) | 190 if (reason != InkDropAnimationEndedReason::SUCCESS) |
| 190 return; | 191 return; |
| 191 if (ShouldAnimateToHidden(ink_drop_state)) { | 192 if (ShouldAnimateToHidden(ink_drop_state)) { |
| 192 ink_drop_ripple_->AnimateToState(views::InkDropState::HIDDEN); | 193 ink_drop_ripple_->AnimateToState(views::InkDropState::HIDDEN); |
| 193 } else if (ink_drop_state == views::InkDropState::HIDDEN) { | 194 } else if (ink_drop_state == views::InkDropState::HIDDEN) { |
| 194 if (is_hovered_) | 195 if (is_hovered_) |
| 195 StartHoverAfterRippleTimer(); | 196 StartHighlightAfterRippleTimer(); |
| 196 // TODO(bruthig): Investigate whether creating and destroying | 197 // TODO(bruthig): Investigate whether creating and destroying |
| 197 // InkDropRipples is expensive and consider creating an | 198 // InkDropRipples is expensive and consider creating an |
| 198 // InkDropRipplePool. See www.crbug.com/522175. | 199 // InkDropRipplePool. See www.crbug.com/522175. |
| 199 DestroyInkDropRipple(); | 200 DestroyInkDropRipple(); |
| 200 } | 201 } |
| 201 } | 202 } |
| 202 | 203 |
| 203 // ----------------------------------------------------------------------------- | 204 // ----------------------------------------------------------------------------- |
| 204 // views::InkDropHoverObserver: | 205 // views::InkDropHighlightObserver: |
| 205 | 206 |
| 206 void InkDropImpl::AnimationStarted(InkDropHover::AnimationType animation_type) { | 207 void InkDropImpl::AnimationStarted( |
| 207 } | 208 InkDropHighlight::AnimationType animation_type) {} |
| 208 | 209 |
| 209 void InkDropImpl::AnimationEnded(InkDropHover::AnimationType animation_type, | 210 void InkDropImpl::AnimationEnded(InkDropHighlight::AnimationType animation_type, |
| 210 InkDropAnimationEndedReason reason) { | 211 InkDropAnimationEndedReason reason) { |
| 211 if (animation_type == InkDropHover::FADE_OUT && | 212 if (animation_type == InkDropHighlight::FADE_OUT && |
| 212 reason == InkDropAnimationEndedReason::SUCCESS) { | 213 reason == InkDropAnimationEndedReason::SUCCESS) { |
| 213 DestroyInkDropHover(); | 214 DestroyInkDropHighlight(); |
| 214 } | 215 } |
| 215 } | 216 } |
| 216 | 217 |
| 217 void InkDropImpl::SetHighlight(bool should_highlight, | 218 void InkDropImpl::SetHighlight(bool should_highlight, |
| 218 base::TimeDelta animation_duration, | 219 base::TimeDelta animation_duration, |
| 219 bool explode) { | 220 bool explode) { |
| 220 StopHoverAfterRippleTimer(); | 221 StopHighlightAfterRippleTimer(); |
| 221 | 222 |
| 222 if (IsHoverFadingInOrVisible() == should_highlight) | 223 if (IsHighlightFadingInOrVisible() == should_highlight) |
| 223 return; | 224 return; |
| 224 | 225 |
| 225 if (should_highlight) { | 226 if (should_highlight) { |
| 226 CreateInkDropHover(); | 227 CreateInkDropHighlight(); |
| 227 if (hover_ && !IsVisible()) | 228 if (highlight_ && !IsVisible()) |
| 228 hover_->FadeIn(animation_duration); | 229 highlight_->FadeIn(animation_duration); |
| 229 } else { | 230 } else { |
| 230 hover_->FadeOut(animation_duration, explode); | 231 highlight_->FadeOut(animation_duration, explode); |
| 231 } | 232 } |
| 232 } | 233 } |
| 233 | 234 |
| 234 bool InkDropImpl::ShouldHighlight() const { | 235 bool InkDropImpl::ShouldHighlight() const { |
| 235 return is_focused_ || is_hovered_; | 236 return is_focused_ || is_hovered_; |
| 236 } | 237 } |
| 237 | 238 |
| 238 void InkDropImpl::StartHoverAfterRippleTimer() { | 239 void InkDropImpl::StartHighlightAfterRippleTimer() { |
| 239 StopHoverAfterRippleTimer(); | 240 StopHighlightAfterRippleTimer(); |
| 240 | 241 |
| 241 if (!hover_after_ripple_timer_) | 242 if (!highlight_after_ripple_timer_) |
| 242 hover_after_ripple_timer_.reset(new base::OneShotTimer); | 243 highlight_after_ripple_timer_.reset(new base::OneShotTimer); |
| 243 | 244 |
| 244 hover_after_ripple_timer_->Start( | 245 highlight_after_ripple_timer_->Start( |
| 245 FROM_HERE, | 246 FROM_HERE, |
| 246 base::TimeDelta::FromMilliseconds(kHoverFadeInAfterRippleDelayInMs), | 247 base::TimeDelta::FromMilliseconds(kHighlightFadeInAfterRippleDelayInMs), |
| 247 base::Bind(&InkDropImpl::HoverAfterRippleTimerFired, | 248 base::Bind(&InkDropImpl::HighlightAfterRippleTimerFired, |
| 248 base::Unretained(this))); | 249 base::Unretained(this))); |
| 249 } | 250 } |
| 250 | 251 |
| 251 void InkDropImpl::StopHoverAfterRippleTimer() { | 252 void InkDropImpl::StopHighlightAfterRippleTimer() { |
| 252 if (hover_after_ripple_timer_) | 253 if (highlight_after_ripple_timer_) |
| 253 hover_after_ripple_timer_.reset(); | 254 highlight_after_ripple_timer_.reset(); |
| 254 } | 255 } |
| 255 | 256 |
| 256 void InkDropImpl::HoverAfterRippleTimerFired() { | 257 void InkDropImpl::HighlightAfterRippleTimerFired() { |
| 257 SetHighlight(true, base::TimeDelta::FromMilliseconds( | 258 SetHighlight(true, base::TimeDelta::FromMilliseconds( |
| 258 kHoverFadeInAfterRippleDurationInMs), | 259 kHighlightFadeInAfterRippleDurationInMs), |
| 259 true); | 260 true); |
| 260 hover_after_ripple_timer_.reset(); | 261 highlight_after_ripple_timer_.reset(); |
| 261 } | 262 } |
| 262 | 263 |
| 263 } // namespace views | 264 } // namespace views |
| OLD | NEW |