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); |
| 138 AddRootLayerToHostIfNeeded(); | 141 root_layer_->Add(ink_drop_ripple_->GetRootLayer()); |
| 142 AddRootLayerToHostIfNeeded(); | |
| 143 } | |
| 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_); |
| 177 DCHECK(!root_layer_->children().empty()); | |
| 171 if (!root_layer_added_to_host_) { | 178 if (!root_layer_added_to_host_) { |
| 172 root_layer_added_to_host_ = true; | 179 root_layer_added_to_host_ = true; |
| 173 ink_drop_host_->AddInkDropLayer(root_layer_.get()); | 180 ink_drop_host_->AddInkDropLayer(root_layer_.get()); |
| 174 } | 181 } |
| 175 } | 182 } |
| 176 | 183 |
| 177 void InkDropImpl::RemoveRootLayerFromHostIfNeeded() { | 184 void InkDropImpl::RemoveRootLayerFromHostIfNeeded() { |
| 178 if (root_layer_added_to_host_ && !highlight_ && !ink_drop_ripple_) { | 185 if (root_layer_added_to_host_ && root_layer_->children().empty()) { |
| 179 root_layer_added_to_host_ = false; | 186 root_layer_added_to_host_ = false; |
| 180 ink_drop_host_->RemoveInkDropLayer(root_layer_.get()); | 187 ink_drop_host_->RemoveInkDropLayer(root_layer_.get()); |
| 181 } | 188 } |
| 182 } | 189 } |
| 183 | 190 |
| 184 bool InkDropImpl::IsHighlightFadingInOrVisible() const { | 191 bool InkDropImpl::IsHighlightFadingInOrVisible() const { |
| 185 return highlight_ && highlight_->IsFadingInOrVisible(); | 192 return highlight_ && highlight_->IsFadingInOrVisible(); |
| 186 } | 193 } |
| 187 | 194 |
| 188 // ----------------------------------------------------------------------------- | 195 // ----------------------------------------------------------------------------- |
| 189 // views::InkDropRippleObserver: | 196 // views::InkDropRippleObserver: |
| 190 | 197 |
| 191 void InkDropImpl::AnimationStarted(InkDropState ink_drop_state) {} | 198 void InkDropImpl::AnimationStarted(InkDropState ink_drop_state) {} |
| 192 | 199 |
| 193 void InkDropImpl::AnimationEnded(InkDropState ink_drop_state, | 200 void InkDropImpl::AnimationEnded(InkDropState ink_drop_state, |
| 194 InkDropAnimationEndedReason reason) { | 201 InkDropAnimationEndedReason reason) { |
| 195 if (reason != InkDropAnimationEndedReason::SUCCESS) | 202 if (reason != InkDropAnimationEndedReason::SUCCESS) |
| 196 return; | 203 return; |
| 197 if (ShouldAnimateToHidden(ink_drop_state)) { | 204 if (ShouldAnimateToHidden(ink_drop_state)) { |
| 198 ink_drop_ripple_->AnimateToState(views::InkDropState::HIDDEN); | 205 ink_drop_ripple_->AnimateToState(views::InkDropState::HIDDEN); |
| 199 } else if (ink_drop_state == views::InkDropState::HIDDEN) { | 206 } else if (ink_drop_state == views::InkDropState::HIDDEN) { |
| 200 // Re-highlight, as necessary. For hover, there's a delay; for focus, jump | 207 // Re-highlight, as necessary. For hover, there's a delay; for focus, jump |
| 201 // straight into the animation. | 208 // straight into the animation. |
| 202 if (is_focused_) | 209 if (!IsHighlightFadingInOrVisible()) { |
| 203 HighlightAfterRippleTimerFired(); | 210 if (is_focused_) |
| 204 else if (is_hovered_) | 211 HighlightAfterRippleTimerFired(); |
| 205 StartHighlightAfterRippleTimer(); | 212 else if (is_hovered_) |
| 213 StartHighlightAfterRippleTimer(); | |
| 214 } | |
| 206 | 215 |
| 207 // TODO(bruthig): Investigate whether creating and destroying | 216 // TODO(bruthig): Investigate whether creating and destroying |
| 208 // InkDropRipples is expensive and consider creating an | 217 // InkDropRipples is expensive and consider creating an |
| 209 // InkDropRipplePool. See www.crbug.com/522175. | 218 // InkDropRipplePool. See www.crbug.com/522175. |
| 210 DestroyInkDropRipple(); | 219 DestroyInkDropRipple(); |
| 211 } | 220 } |
| 212 } | 221 } |
| 213 | 222 |
| 214 // ----------------------------------------------------------------------------- | 223 // ----------------------------------------------------------------------------- |
| 215 // views::InkDropHighlightObserver: | 224 // views::InkDropHighlightObserver: |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 228 void InkDropImpl::SetHighlight(bool should_highlight, | 237 void InkDropImpl::SetHighlight(bool should_highlight, |
| 229 base::TimeDelta animation_duration, | 238 base::TimeDelta animation_duration, |
| 230 bool explode) { | 239 bool explode) { |
| 231 highlight_after_ripple_timer_.reset(); | 240 highlight_after_ripple_timer_.reset(); |
| 232 | 241 |
| 233 if (IsHighlightFadingInOrVisible() == should_highlight) | 242 if (IsHighlightFadingInOrVisible() == should_highlight) |
| 234 return; | 243 return; |
| 235 | 244 |
| 236 if (should_highlight) { | 245 if (should_highlight) { |
| 237 CreateInkDropHighlight(); | 246 CreateInkDropHighlight(); |
| 238 if (highlight_ && !(ink_drop_ripple_ && ink_drop_ripple_->IsVisible())) | 247 if (highlight_ && |
| 248 !(ink_drop_ripple_ && ink_drop_ripple_->IsVisible() && | |
| 249 ink_drop_ripple_->OverridesHighlight())) { | |
|
bruthig
2016/09/08 15:56:04
Can this have a test?
Evan Stade
2016/09/08 19:17:32
added to HighlightCanCoexistWithRipple
| |
| 239 highlight_->FadeIn(animation_duration); | 250 highlight_->FadeIn(animation_duration); |
| 251 } | |
| 240 } else { | 252 } else { |
| 241 highlight_->FadeOut(animation_duration, explode); | 253 highlight_->FadeOut(animation_duration, explode); |
| 242 } | 254 } |
| 243 } | 255 } |
| 244 | 256 |
| 245 bool InkDropImpl::ShouldHighlight() const { | 257 bool InkDropImpl::ShouldHighlight() const { |
| 246 return is_focused_ || is_hovered_; | 258 return is_focused_ || is_hovered_; |
| 247 } | 259 } |
| 248 | 260 |
| 249 void InkDropImpl::StartHighlightAfterRippleTimer() { | 261 void InkDropImpl::StartHighlightAfterRippleTimer() { |
| 250 highlight_after_ripple_timer_.reset(new base::OneShotTimer); | 262 highlight_after_ripple_timer_.reset(new base::OneShotTimer); |
| 251 highlight_after_ripple_timer_->Start( | 263 highlight_after_ripple_timer_->Start( |
| 252 FROM_HERE, | 264 FROM_HERE, |
| 253 base::TimeDelta::FromMilliseconds(kHoverFadeInAfterRippleDelayMs), | 265 base::TimeDelta::FromMilliseconds(kHoverFadeInAfterRippleDelayMs), |
| 254 base::Bind(&InkDropImpl::HighlightAfterRippleTimerFired, | 266 base::Bind(&InkDropImpl::HighlightAfterRippleTimerFired, |
| 255 base::Unretained(this))); | 267 base::Unretained(this))); |
| 256 } | 268 } |
| 257 | 269 |
| 258 void InkDropImpl::HighlightAfterRippleTimerFired() { | 270 void InkDropImpl::HighlightAfterRippleTimerFired() { |
| 259 SetHighlight(true, base::TimeDelta::FromMilliseconds( | 271 SetHighlight(true, base::TimeDelta::FromMilliseconds( |
| 260 kHighlightFadeInAfterRippleDurationMs), | 272 kHighlightFadeInAfterRippleDurationMs), |
| 261 true); | 273 true); |
| 262 highlight_after_ripple_timer_.reset(); | 274 highlight_after_ripple_timer_.reset(); |
| 263 } | 275 } |
| 264 | 276 |
| 265 } // namespace views | 277 } // namespace views |
| OLD | NEW |