Chromium Code Reviews| 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; |
| 155 highlight_->set_observer(this); | 161 highlight_->set_observer(this); |
| 156 root_layer_->Add(highlight_->layer()); | 162 root_layer_->Add(highlight_->layer()); |
| 157 AddRootLayerToHostIfNeeded(); | 163 AddRootLayerToHostIfNeeded(); |
| 158 } | 164 } |
| 159 | 165 |
| 160 void InkDropImpl::DestroyInkDropHighlight() { | 166 void InkDropImpl::DestroyInkDropHighlight() { |
| 161 if (!highlight_) | 167 if (!highlight_) |
| 162 return; | 168 return; |
| 163 root_layer_->Remove(highlight_->layer()); | 169 root_layer_->Remove(highlight_->layer()); |
| 164 highlight_->set_observer(nullptr); | 170 highlight_->set_observer(nullptr); |
| 165 highlight_.reset(); | 171 highlight_.reset(); |
| 166 RemoveRootLayerFromHostIfNeeded(); | 172 RemoveRootLayerFromHostIfNeeded(); |
| 167 } | 173 } |
| 168 | 174 |
| 169 void InkDropImpl::AddRootLayerToHostIfNeeded() { | 175 void InkDropImpl::AddRootLayerToHostIfNeeded() { |
| 170 DCHECK(highlight_ || ink_drop_ripple_); | 176 DCHECK(highlight_ || ink_drop_ripple_); |
| 171 if (!root_layer_added_to_host_) { | 177 if (!root_layer_added_to_host_) { |
| 172 root_layer_added_to_host_ = true; | 178 root_layer_added_to_host_ = true; |
| 173 ink_drop_host_->AddInkDropLayer(root_layer_.get()); | 179 ink_drop_host_->AddInkDropLayer(root_layer_.get()); |
|
bruthig
2016/08/25 17:45:29
Perhaps this should only add the |root_layer_| to
Evan Stade
2016/08/26 23:36:15
But we'll need to do that for highlight anyway.
bruthig
2016/08/31 22:22:29
I don't think this method should make assumptions
Evan Stade
2016/09/01 01:19:15
If both highlight and ripple are layerless, then t
bruthig
2016/09/01 15:53:57
Yeah
| |
| 174 } | 180 } |
| 175 } | 181 } |
| 176 | 182 |
| 177 void InkDropImpl::RemoveRootLayerFromHostIfNeeded() { | 183 void InkDropImpl::RemoveRootLayerFromHostIfNeeded() { |
| 178 if (root_layer_added_to_host_ && !highlight_ && !ink_drop_ripple_) { | 184 if (root_layer_added_to_host_ && !highlight_ && !ink_drop_ripple_) { |
|
bruthig
2016/08/25 17:45:29
This should check if the ink drop layer exists ins
Evan Stade
2016/08/26 23:36:15
That would break the case where we destroy the hig
bruthig
2016/08/31 22:22:29
Sorry I wasn't clear on that. The following re-wr
Evan Stade
2016/09/01 01:19:15
I don't want to support an empty ripple and highli
bruthig
2016/09/01 15:53:57
Consider the following case:
1. SetHover(true)
-
Evan Stade
2016/09/07 23:46:19
Done.
| |
| 179 root_layer_added_to_host_ = false; | 185 root_layer_added_to_host_ = false; |
| 180 ink_drop_host_->RemoveInkDropLayer(root_layer_.get()); | 186 ink_drop_host_->RemoveInkDropLayer(root_layer_.get()); |
| 181 } | 187 } |
| 182 } | 188 } |
| 183 | 189 |
| 184 bool InkDropImpl::IsHighlightFadingInOrVisible() const { | 190 bool InkDropImpl::IsHighlightFadingInOrVisible() const { |
| 185 return highlight_ && highlight_->IsFadingInOrVisible(); | 191 return highlight_ && highlight_->IsFadingInOrVisible(); |
| 186 } | 192 } |
| 187 | 193 |
| 188 // ----------------------------------------------------------------------------- | 194 // ----------------------------------------------------------------------------- |
| 189 // views::InkDropRippleObserver: | 195 // views::InkDropRippleObserver: |
| 190 | 196 |
| 191 void InkDropImpl::AnimationStarted(InkDropState ink_drop_state) {} | 197 void InkDropImpl::AnimationStarted(InkDropState ink_drop_state) {} |
| 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 (ink_drop_ripple_->OverridesHighlight()) { |
|
bruthig
2016/08/25 17:45:29
I'm wondering if this should check |ink_drop_highl
Evan Stade
2016/08/26 23:36:15
Done.
| |
| 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_ && !(ink_drop_ripple_ && ink_drop_ripple_->IsVisible())) |
|
bruthig
2016/08/25 17:45:29
This clause is currently preventing the highlight
Evan Stade
2016/08/26 23:36:15
Done.
| |
| 239 highlight_->FadeIn(animation_duration); | 247 highlight_->FadeIn(animation_duration); |
| 240 } else { | 248 } else { |
| 241 highlight_->FadeOut(animation_duration, explode); | 249 highlight_->FadeOut(animation_duration, explode); |
| 242 } | 250 } |
| 243 } | 251 } |
| 244 | 252 |
| 245 bool InkDropImpl::ShouldHighlight() const { | 253 bool InkDropImpl::ShouldHighlight() const { |
| 246 return is_focused_ || is_hovered_; | 254 return is_focused_ || is_hovered_; |
| 247 } | 255 } |
| 248 | 256 |
| 249 void InkDropImpl::StartHighlightAfterRippleTimer() { | 257 void InkDropImpl::StartHighlightAfterRippleTimer() { |
| 250 highlight_after_ripple_timer_.reset(new base::OneShotTimer); | 258 highlight_after_ripple_timer_.reset(new base::OneShotTimer); |
| 251 highlight_after_ripple_timer_->Start( | 259 highlight_after_ripple_timer_->Start( |
| 252 FROM_HERE, | 260 FROM_HERE, |
| 253 base::TimeDelta::FromMilliseconds(kHoverFadeInAfterRippleDelayMs), | 261 base::TimeDelta::FromMilliseconds(kHoverFadeInAfterRippleDelayMs), |
| 254 base::Bind(&InkDropImpl::HighlightAfterRippleTimerFired, | 262 base::Bind(&InkDropImpl::HighlightAfterRippleTimerFired, |
| 255 base::Unretained(this))); | 263 base::Unretained(this))); |
| 256 } | 264 } |
| 257 | 265 |
| 258 void InkDropImpl::HighlightAfterRippleTimerFired() { | 266 void InkDropImpl::HighlightAfterRippleTimerFired() { |
| 259 SetHighlight(true, base::TimeDelta::FromMilliseconds( | 267 SetHighlight(true, base::TimeDelta::FromMilliseconds( |
| 260 kHighlightFadeInAfterRippleDurationMs), | 268 kHighlightFadeInAfterRippleDurationMs), |
| 261 true); | 269 true); |
| 262 highlight_after_ripple_timer_.reset(); | 270 highlight_after_ripple_timer_.reset(); |
| 263 } | 271 } |
| 264 | 272 |
| 265 } // namespace views | 273 } // namespace views |
| OLD | NEW |