| 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_highlight.h" |
| 11 #include "ui/views/animation/ink_drop_host.h" | 11 #include "ui/views/animation/ink_drop_host.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 highlight state fade in animation when | 18 // The duration, in milliseconds, of the highlight state fade in animation when |
| 19 // it is triggered by user input. | 19 // it is triggered by user input. |
| 20 const int kHighlightFadeInFromUserInputDurationInMs = 250; | 20 const int kHighlightFadeInFromUserInputDurationMs = 250; |
| 21 | 21 |
| 22 // The duration, in milliseconds, of the highlight state fade out animation when | 22 // The duration, in milliseconds, of the highlight state fade out animation when |
| 23 // it is triggered by user input. | 23 // it is triggered by user input. |
| 24 const int kHighlightFadeOutFromUserInputDurationInMs = 250; | 24 const int kHighlightFadeOutFromUserInputDurationMs = 250; |
| 25 | 25 |
| 26 // The duration, in milliseconds, of the highlight state fade in animation when | 26 // The duration, in milliseconds, of the highlight state fade in animation when |
| 27 // it is triggered by an ink drop ripple animation ending. | 27 // it is triggered by an ink drop ripple animation ending. |
| 28 const int kHighlightFadeInAfterRippleDurationInMs = 250; | 28 const int kHighlightFadeInAfterRippleDurationMs = 250; |
| 29 | 29 |
| 30 // The duration, in milliseconds, of the highlight state fade out animation when | 30 // The duration, in milliseconds, of the highlight state fade out animation when |
| 31 // it is triggered by an ink drop ripple animation starting. | 31 // it is triggered by an ink drop ripple animation starting. |
| 32 const int kHighlightFadeOutBeforeRippleDurationInMs = 120; | 32 const int kHighlightFadeOutBeforeRippleDurationMs = 120; |
| 33 | 33 |
| 34 // The amount of time in milliseconds that |highlight_| should delay after a | 34 // The amount of time in milliseconds that |highlight_| should delay after a |
| 35 // ripple animation before fading in. | 35 // ripple animation before fading in, for highlight due to mouse hover. |
| 36 const int kHighlightFadeInAfterRippleDelayInMs = 1000; | 36 const int kHoverFadeInAfterRippleDelayMs = 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: |
| (...skipping 24 matching lines...) Expand all Loading... |
| 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 void InkDropImpl::AnimateToState(InkDropState ink_drop_state) { | 76 void InkDropImpl::AnimateToState(InkDropState ink_drop_state) { |
| 77 DestroyHiddenTargetedAnimations(); | 77 DestroyHiddenTargetedAnimations(); |
| 78 if (!ink_drop_ripple_) | 78 if (!ink_drop_ripple_) |
| 79 CreateInkDropRipple(); | 79 CreateInkDropRipple(); |
| 80 | 80 |
| 81 // When deactivating and the host is focused, snap back to the highlight |
| 82 // state. (In the case of highlighting due to hover, we'll animate the |
| 83 // highlight back in after a delay.) |
| 84 if (ink_drop_state == views::InkDropState::DEACTIVATED && is_focused_) { |
| 85 ink_drop_ripple_->HideImmediately(); |
| 86 SetHighlight(true, base::TimeDelta(), false); |
| 87 return; |
| 88 } |
| 89 |
| 81 if (ink_drop_state != views::InkDropState::HIDDEN) { | 90 if (ink_drop_state != views::InkDropState::HIDDEN) { |
| 82 SetHighlight(false, base::TimeDelta::FromMilliseconds( | 91 SetHighlight(false, base::TimeDelta::FromMilliseconds( |
| 83 kHighlightFadeOutBeforeRippleDurationInMs), | 92 kHighlightFadeOutBeforeRippleDurationMs), |
| 84 true); | 93 true); |
| 85 } | 94 } |
| 86 | 95 |
| 87 ink_drop_ripple_->AnimateToState(ink_drop_state); | 96 ink_drop_ripple_->AnimateToState(ink_drop_state); |
| 88 } | 97 } |
| 89 | 98 |
| 90 void InkDropImpl::SnapToActivated() { | 99 void InkDropImpl::SnapToActivated() { |
| 91 DestroyHiddenTargetedAnimations(); | 100 DestroyHiddenTargetedAnimations(); |
| 92 if (!ink_drop_ripple_) | 101 if (!ink_drop_ripple_) |
| 93 CreateInkDropRipple(); | 102 CreateInkDropRipple(); |
| 94 | 103 |
| 95 SetHighlight(false, base::TimeDelta(), false); | 104 SetHighlight(false, base::TimeDelta(), false); |
| 96 | 105 |
| 97 ink_drop_ripple_->SnapToActivated(); | 106 ink_drop_ripple_->SnapToActivated(); |
| 98 } | 107 } |
| 99 | 108 |
| 100 void InkDropImpl::SetHovered(bool is_hovered) { | 109 void InkDropImpl::SetHovered(bool is_hovered) { |
| 101 is_hovered_ = is_hovered; | 110 is_hovered_ = is_hovered; |
| 102 SetHighlight(ShouldHighlight(), | 111 SetHighlight(ShouldHighlight(), |
| 103 ShouldHighlight() | 112 ShouldHighlight() |
| 104 ? base::TimeDelta::FromMilliseconds( | 113 ? base::TimeDelta::FromMilliseconds( |
| 105 kHighlightFadeInFromUserInputDurationInMs) | 114 kHighlightFadeInFromUserInputDurationMs) |
| 106 : base::TimeDelta::FromMilliseconds( | 115 : base::TimeDelta::FromMilliseconds( |
| 107 kHighlightFadeOutFromUserInputDurationInMs), | 116 kHighlightFadeOutFromUserInputDurationMs), |
| 108 false); | 117 false); |
| 109 } | 118 } |
| 110 | 119 |
| 111 void InkDropImpl::SetFocused(bool is_focused) { | 120 void InkDropImpl::SetFocused(bool is_focused) { |
| 112 is_focused_ = is_focused; | 121 is_focused_ = is_focused; |
| 113 SetHighlight(ShouldHighlight(), base::TimeDelta(), false); | 122 SetHighlight(ShouldHighlight(), base::TimeDelta(), false); |
| 114 } | 123 } |
| 115 | 124 |
| 116 void InkDropImpl::DestroyHiddenTargetedAnimations() { | 125 void InkDropImpl::DestroyHiddenTargetedAnimations() { |
| 117 if (ink_drop_ripple_ && | 126 if (ink_drop_ripple_ && |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 | 190 |
| 182 void InkDropImpl::AnimationStarted(InkDropState ink_drop_state) {} | 191 void InkDropImpl::AnimationStarted(InkDropState ink_drop_state) {} |
| 183 | 192 |
| 184 void InkDropImpl::AnimationEnded(InkDropState ink_drop_state, | 193 void InkDropImpl::AnimationEnded(InkDropState ink_drop_state, |
| 185 InkDropAnimationEndedReason reason) { | 194 InkDropAnimationEndedReason reason) { |
| 186 if (reason != InkDropAnimationEndedReason::SUCCESS) | 195 if (reason != InkDropAnimationEndedReason::SUCCESS) |
| 187 return; | 196 return; |
| 188 if (ShouldAnimateToHidden(ink_drop_state)) { | 197 if (ShouldAnimateToHidden(ink_drop_state)) { |
| 189 ink_drop_ripple_->AnimateToState(views::InkDropState::HIDDEN); | 198 ink_drop_ripple_->AnimateToState(views::InkDropState::HIDDEN); |
| 190 } else if (ink_drop_state == views::InkDropState::HIDDEN) { | 199 } else if (ink_drop_state == views::InkDropState::HIDDEN) { |
| 191 if (is_hovered_) | 200 // Re-highlight, as necessary. For hover, there's a delay; for focus, jump |
| 201 // straight into the animation. |
| 202 if (is_focused_) |
| 203 HighlightAfterRippleTimerFired(); |
| 204 else if (is_hovered_) |
| 192 StartHighlightAfterRippleTimer(); | 205 StartHighlightAfterRippleTimer(); |
| 206 |
| 193 // TODO(bruthig): Investigate whether creating and destroying | 207 // TODO(bruthig): Investigate whether creating and destroying |
| 194 // InkDropRipples is expensive and consider creating an | 208 // InkDropRipples is expensive and consider creating an |
| 195 // InkDropRipplePool. See www.crbug.com/522175. | 209 // InkDropRipplePool. See www.crbug.com/522175. |
| 196 DestroyInkDropRipple(); | 210 DestroyInkDropRipple(); |
| 197 } | 211 } |
| 198 } | 212 } |
| 199 | 213 |
| 200 // ----------------------------------------------------------------------------- | 214 // ----------------------------------------------------------------------------- |
| 201 // views::InkDropHighlightObserver: | 215 // views::InkDropHighlightObserver: |
| 202 | 216 |
| 203 void InkDropImpl::AnimationStarted( | 217 void InkDropImpl::AnimationStarted( |
| 204 InkDropHighlight::AnimationType animation_type) {} | 218 InkDropHighlight::AnimationType animation_type) {} |
| 205 | 219 |
| 206 void InkDropImpl::AnimationEnded(InkDropHighlight::AnimationType animation_type, | 220 void InkDropImpl::AnimationEnded(InkDropHighlight::AnimationType animation_type, |
| 207 InkDropAnimationEndedReason reason) { | 221 InkDropAnimationEndedReason reason) { |
| 208 if (animation_type == InkDropHighlight::FADE_OUT && | 222 if (animation_type == InkDropHighlight::FADE_OUT && |
| 209 reason == InkDropAnimationEndedReason::SUCCESS) { | 223 reason == InkDropAnimationEndedReason::SUCCESS) { |
| 210 DestroyInkDropHighlight(); | 224 DestroyInkDropHighlight(); |
| 211 } | 225 } |
| 212 } | 226 } |
| 213 | 227 |
| 214 void InkDropImpl::SetHighlight(bool should_highlight, | 228 void InkDropImpl::SetHighlight(bool should_highlight, |
| 215 base::TimeDelta animation_duration, | 229 base::TimeDelta animation_duration, |
| 216 bool explode) { | 230 bool explode) { |
| 217 StopHighlightAfterRippleTimer(); | 231 highlight_after_ripple_timer_.reset(); |
| 218 | 232 |
| 219 if (IsHighlightFadingInOrVisible() == should_highlight) | 233 if (IsHighlightFadingInOrVisible() == should_highlight) |
| 220 return; | 234 return; |
| 221 | 235 |
| 222 if (should_highlight) { | 236 if (should_highlight) { |
| 223 CreateInkDropHighlight(); | 237 CreateInkDropHighlight(); |
| 224 if (highlight_ && !(ink_drop_ripple_ && ink_drop_ripple_->IsVisible())) | 238 if (highlight_ && !(ink_drop_ripple_ && ink_drop_ripple_->IsVisible())) |
| 225 highlight_->FadeIn(animation_duration); | 239 highlight_->FadeIn(animation_duration); |
| 226 } else { | 240 } else { |
| 227 highlight_->FadeOut(animation_duration, explode); | 241 highlight_->FadeOut(animation_duration, explode); |
| 228 } | 242 } |
| 229 } | 243 } |
| 230 | 244 |
| 231 bool InkDropImpl::ShouldHighlight() const { | 245 bool InkDropImpl::ShouldHighlight() const { |
| 232 return is_focused_ || is_hovered_; | 246 return is_focused_ || is_hovered_; |
| 233 } | 247 } |
| 234 | 248 |
| 235 void InkDropImpl::StartHighlightAfterRippleTimer() { | 249 void InkDropImpl::StartHighlightAfterRippleTimer() { |
| 236 StopHighlightAfterRippleTimer(); | 250 highlight_after_ripple_timer_.reset(new base::OneShotTimer); |
| 237 | |
| 238 if (!highlight_after_ripple_timer_) | |
| 239 highlight_after_ripple_timer_.reset(new base::OneShotTimer); | |
| 240 | |
| 241 highlight_after_ripple_timer_->Start( | 251 highlight_after_ripple_timer_->Start( |
| 242 FROM_HERE, | 252 FROM_HERE, |
| 243 base::TimeDelta::FromMilliseconds(kHighlightFadeInAfterRippleDelayInMs), | 253 base::TimeDelta::FromMilliseconds(kHoverFadeInAfterRippleDelayMs), |
| 244 base::Bind(&InkDropImpl::HighlightAfterRippleTimerFired, | 254 base::Bind(&InkDropImpl::HighlightAfterRippleTimerFired, |
| 245 base::Unretained(this))); | 255 base::Unretained(this))); |
| 246 } | 256 } |
| 247 | 257 |
| 248 void InkDropImpl::StopHighlightAfterRippleTimer() { | |
| 249 if (highlight_after_ripple_timer_) | |
| 250 highlight_after_ripple_timer_.reset(); | |
| 251 } | |
| 252 | |
| 253 void InkDropImpl::HighlightAfterRippleTimerFired() { | 258 void InkDropImpl::HighlightAfterRippleTimerFired() { |
| 254 SetHighlight(true, base::TimeDelta::FromMilliseconds( | 259 SetHighlight(true, base::TimeDelta::FromMilliseconds( |
| 255 kHighlightFadeInAfterRippleDurationInMs), | 260 kHighlightFadeInAfterRippleDurationMs), |
| 256 true); | 261 true); |
| 257 highlight_after_ripple_timer_.reset(); | 262 highlight_after_ripple_timer_.reset(); |
| 258 } | 263 } |
| 259 | 264 |
| 260 } // namespace views | 265 } // namespace views |
| OLD | NEW |