| 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" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 | 81 if (ink_drop_ripple_->OverridesHighlight()) { |
| 82 // state. (In the case of highlighting due to hover, we'll animate the | 82 // When deactivating and the host is focused, snap back to the highlight |
| 83 // highlight back in after a delay.) | 83 // state. (In the case of highlighting due to hover, we'll animate the |
| 84 if (ink_drop_state == views::InkDropState::DEACTIVATED && is_focused_) { | 84 // highlight back in after a delay.) |
| 85 ink_drop_ripple_->HideImmediately(); | 85 if (ink_drop_state == views::InkDropState::DEACTIVATED && is_focused_) { |
| 86 SetHighlight(true, base::TimeDelta(), false); | 86 ink_drop_ripple_->HideImmediately(); |
| 87 return; | 87 SetHighlight(true, base::TimeDelta(), false); |
| 88 } | 88 return; |
| 89 } |
| 89 | 90 |
| 90 if (ink_drop_state != views::InkDropState::HIDDEN) { | 91 if (ink_drop_state != views::InkDropState::HIDDEN) { |
| 91 SetHighlight(false, base::TimeDelta::FromMilliseconds( | 92 SetHighlight(false, base::TimeDelta::FromMilliseconds( |
| 92 kHighlightFadeOutBeforeRippleDurationMs), | 93 kHighlightFadeOutBeforeRippleDurationMs), |
| 93 true); | 94 true); |
| 95 } |
| 94 } | 96 } |
| 95 | 97 |
| 96 ink_drop_ripple_->AnimateToState(ink_drop_state); | 98 ink_drop_ripple_->AnimateToState(ink_drop_state); |
| 97 } | 99 } |
| 98 | 100 |
| 99 void InkDropImpl::SnapToActivated() { | 101 void InkDropImpl::SnapToActivated() { |
| 100 DestroyHiddenTargetedAnimations(); | 102 DestroyHiddenTargetedAnimations(); |
| 101 if (!ink_drop_ripple_) | 103 if (!ink_drop_ripple_) |
| 102 CreateInkDropRipple(); | 104 CreateInkDropRipple(); |
| 103 | 105 |
| 104 SetHighlight(false, base::TimeDelta(), false); | 106 if (ink_drop_ripple_->OverridesHighlight()) |
| 107 SetHighlight(false, base::TimeDelta(), false); |
| 105 | 108 |
| 106 ink_drop_ripple_->SnapToActivated(); | 109 ink_drop_ripple_->SnapToActivated(); |
| 107 } | 110 } |
| 108 | 111 |
| 109 void InkDropImpl::SetHovered(bool is_hovered) { | 112 void InkDropImpl::SetHovered(bool is_hovered) { |
| 110 is_hovered_ = is_hovered; | 113 is_hovered_ = is_hovered; |
| 111 SetHighlight(ShouldHighlight(), | 114 SetHighlight(ShouldHighlight(), |
| 112 ShouldHighlight() | 115 ShouldHighlight() |
| 113 ? base::TimeDelta::FromMilliseconds( | 116 ? base::TimeDelta::FromMilliseconds( |
| 114 kHighlightFadeInFromUserInputDurationMs) | 117 kHighlightFadeInFromUserInputDurationMs) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 126 if (ink_drop_ripple_ && | 129 if (ink_drop_ripple_ && |
| 127 (ink_drop_ripple_->target_ink_drop_state() == InkDropState::HIDDEN || | 130 (ink_drop_ripple_->target_ink_drop_state() == InkDropState::HIDDEN || |
| 128 ShouldAnimateToHidden(ink_drop_ripple_->target_ink_drop_state()))) { | 131 ShouldAnimateToHidden(ink_drop_ripple_->target_ink_drop_state()))) { |
| 129 DestroyInkDropRipple(); | 132 DestroyInkDropRipple(); |
| 130 } | 133 } |
| 131 } | 134 } |
| 132 | 135 |
| 133 void InkDropImpl::CreateInkDropRipple() { | 136 void InkDropImpl::CreateInkDropRipple() { |
| 134 DestroyInkDropRipple(); | 137 DestroyInkDropRipple(); |
| 135 ink_drop_ripple_ = ink_drop_host_->CreateInkDropRipple(); | 138 ink_drop_ripple_ = ink_drop_host_->CreateInkDropRipple(); |
| 136 ink_drop_ripple_->set_observer(this); | 139 if (ink_drop_ripple_->GetRootLayer()) { |
| 137 root_layer_->Add(ink_drop_ripple_->GetRootLayer()); | 140 ink_drop_ripple_->set_observer(this); |
| 141 root_layer_->Add(ink_drop_ripple_->GetRootLayer()); |
| 142 } |
| 138 AddRootLayerToHostIfNeeded(); | 143 AddRootLayerToHostIfNeeded(); |
| 139 } | 144 } |
| 140 | 145 |
| 141 void InkDropImpl::DestroyInkDropRipple() { | 146 void InkDropImpl::DestroyInkDropRipple() { |
| 142 if (!ink_drop_ripple_) | 147 if (!ink_drop_ripple_) |
| 143 return; | 148 return; |
| 144 root_layer_->Remove(ink_drop_ripple_->GetRootLayer()); | 149 if (ink_drop_ripple_->GetRootLayer()) |
| 150 root_layer_->Remove(ink_drop_ripple_->GetRootLayer()); |
| 145 ink_drop_ripple_.reset(); | 151 ink_drop_ripple_.reset(); |
| 146 RemoveRootLayerFromHostIfNeeded(); | 152 RemoveRootLayerFromHostIfNeeded(); |
| 147 } | 153 } |
| 148 | 154 |
| 149 void InkDropImpl::CreateInkDropHighlight() { | 155 void InkDropImpl::CreateInkDropHighlight() { |
| 150 DestroyInkDropHighlight(); | 156 DestroyInkDropHighlight(); |
| 151 | 157 |
| 152 highlight_ = ink_drop_host_->CreateInkDropHighlight(); | 158 highlight_ = ink_drop_host_->CreateInkDropHighlight(); |
| 153 if (!highlight_) | 159 if (!highlight_) |
| 154 return; | 160 return; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 | 198 |
| 193 void InkDropImpl::AnimationEnded(InkDropState ink_drop_state, | 199 void InkDropImpl::AnimationEnded(InkDropState ink_drop_state, |
| 194 InkDropAnimationEndedReason reason) { | 200 InkDropAnimationEndedReason reason) { |
| 195 if (reason != InkDropAnimationEndedReason::SUCCESS) | 201 if (reason != InkDropAnimationEndedReason::SUCCESS) |
| 196 return; | 202 return; |
| 197 if (ShouldAnimateToHidden(ink_drop_state)) { | 203 if (ShouldAnimateToHidden(ink_drop_state)) { |
| 198 ink_drop_ripple_->AnimateToState(views::InkDropState::HIDDEN); | 204 ink_drop_ripple_->AnimateToState(views::InkDropState::HIDDEN); |
| 199 } else if (ink_drop_state == views::InkDropState::HIDDEN) { | 205 } else if (ink_drop_state == views::InkDropState::HIDDEN) { |
| 200 // Re-highlight, as necessary. For hover, there's a delay; for focus, jump | 206 // Re-highlight, as necessary. For hover, there's a delay; for focus, jump |
| 201 // straight into the animation. | 207 // straight into the animation. |
| 202 if (is_focused_) | 208 if (!IsHighlightFadingInOrVisible()) { |
| 203 HighlightAfterRippleTimerFired(); | 209 if (is_focused_) |
| 204 else if (is_hovered_) | 210 HighlightAfterRippleTimerFired(); |
| 205 StartHighlightAfterRippleTimer(); | 211 else if (is_hovered_) |
| 212 StartHighlightAfterRippleTimer(); |
| 213 } |
| 206 | 214 |
| 207 // TODO(bruthig): Investigate whether creating and destroying | 215 // TODO(bruthig): Investigate whether creating and destroying |
| 208 // InkDropRipples is expensive and consider creating an | 216 // InkDropRipples is expensive and consider creating an |
| 209 // InkDropRipplePool. See www.crbug.com/522175. | 217 // InkDropRipplePool. See www.crbug.com/522175. |
| 210 DestroyInkDropRipple(); | 218 DestroyInkDropRipple(); |
| 211 } | 219 } |
| 212 } | 220 } |
| 213 | 221 |
| 214 // ----------------------------------------------------------------------------- | 222 // ----------------------------------------------------------------------------- |
| 215 // views::InkDropHighlightObserver: | 223 // views::InkDropHighlightObserver: |
| (...skipping 12 matching lines...) Expand all Loading... |
| 228 void InkDropImpl::SetHighlight(bool should_highlight, | 236 void InkDropImpl::SetHighlight(bool should_highlight, |
| 229 base::TimeDelta animation_duration, | 237 base::TimeDelta animation_duration, |
| 230 bool explode) { | 238 bool explode) { |
| 231 highlight_after_ripple_timer_.reset(); | 239 highlight_after_ripple_timer_.reset(); |
| 232 | 240 |
| 233 if (IsHighlightFadingInOrVisible() == should_highlight) | 241 if (IsHighlightFadingInOrVisible() == should_highlight) |
| 234 return; | 242 return; |
| 235 | 243 |
| 236 if (should_highlight) { | 244 if (should_highlight) { |
| 237 CreateInkDropHighlight(); | 245 CreateInkDropHighlight(); |
| 238 if (highlight_ && !(ink_drop_ripple_ && ink_drop_ripple_->IsVisible())) | 246 if (highlight_ && |
| 247 !(ink_drop_ripple_ && ink_drop_ripple_->IsVisible() && |
| 248 ink_drop_ripple_->OverridesHighlight())) { |
| 239 highlight_->FadeIn(animation_duration); | 249 highlight_->FadeIn(animation_duration); |
| 250 } |
| 240 } else { | 251 } else { |
| 241 highlight_->FadeOut(animation_duration, explode); | 252 highlight_->FadeOut(animation_duration, explode); |
| 242 } | 253 } |
| 243 } | 254 } |
| 244 | 255 |
| 245 bool InkDropImpl::ShouldHighlight() const { | 256 bool InkDropImpl::ShouldHighlight() const { |
| 246 return is_focused_ || is_hovered_; | 257 return is_focused_ || is_hovered_; |
| 247 } | 258 } |
| 248 | 259 |
| 249 void InkDropImpl::StartHighlightAfterRippleTimer() { | 260 void InkDropImpl::StartHighlightAfterRippleTimer() { |
| 250 highlight_after_ripple_timer_.reset(new base::OneShotTimer); | 261 highlight_after_ripple_timer_.reset(new base::OneShotTimer); |
| 251 highlight_after_ripple_timer_->Start( | 262 highlight_after_ripple_timer_->Start( |
| 252 FROM_HERE, | 263 FROM_HERE, |
| 253 base::TimeDelta::FromMilliseconds(kHoverFadeInAfterRippleDelayMs), | 264 base::TimeDelta::FromMilliseconds(kHoverFadeInAfterRippleDelayMs), |
| 254 base::Bind(&InkDropImpl::HighlightAfterRippleTimerFired, | 265 base::Bind(&InkDropImpl::HighlightAfterRippleTimerFired, |
| 255 base::Unretained(this))); | 266 base::Unretained(this))); |
| 256 } | 267 } |
| 257 | 268 |
| 258 void InkDropImpl::HighlightAfterRippleTimerFired() { | 269 void InkDropImpl::HighlightAfterRippleTimerFired() { |
| 259 SetHighlight(true, base::TimeDelta::FromMilliseconds( | 270 SetHighlight(true, base::TimeDelta::FromMilliseconds( |
| 260 kHighlightFadeInAfterRippleDurationMs), | 271 kHighlightFadeInAfterRippleDurationMs), |
| 261 true); | 272 true); |
| 262 highlight_after_ripple_timer_.reset(); | 273 highlight_after_ripple_timer_.reset(); |
| 263 } | 274 } |
| 264 | 275 |
| 265 } // namespace views | 276 } // namespace views |
| OLD | NEW |