Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(206)

Side by Side Diff: ui/views/animation/ink_drop_animation_controller_impl.cc

Issue 1963193002: Ink Drop cleanup. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed tdanderson@ nit from patch set 1. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | ui/views/animation/ink_drop_factory_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "ui/views/animation/ink_drop_animation_controller_impl.h"
6
7 #include "base/auto_reset.h"
8 #include "base/timer/timer.h"
9 #include "ui/compositor/layer.h"
10 #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"
13
14 namespace views {
15
16 namespace {
17
18 // The duration, in milliseconds, of the hover state fade in animation when it
19 // is triggered by user input.
20 const int kHoverFadeInFromUserInputDurationInMs = 250;
21
22 // The duration, in milliseconds, of the hover state fade out animation when it
23 // is triggered by user input.
24 const int kHoverFadeOutFromUserInputDurationInMs = 250;
25
26 // The duration, in milliseconds, of the hover state fade in animation when it
27 // is triggered by an ink drop ripple animation ending.
28 const int kHoverFadeInAfterRippleDurationInMs = 250;
29
30 // The duration, in milliseconds, of the hover state fade out animation when it
31 // is triggered by an ink drop ripple animation starting.
32 const int kHoverFadeOutBeforeRippleDurationInMs = 120;
33
34 // The amount of time in milliseconds that |hover_| should delay after a ripple
35 // animation before fading in.
36 const int kHoverFadeInAfterRippleDelayInMs = 1000;
37
38 // Returns true if an ink drop with the given |ink_drop_state| should
39 // automatically transition to the InkDropState::HIDDEN state.
40 bool ShouldAnimateToHidden(InkDropState ink_drop_state) {
41 switch (ink_drop_state) {
42 case views::InkDropState::ACTION_TRIGGERED:
43 case views::InkDropState::ALTERNATE_ACTION_TRIGGERED:
44 case views::InkDropState::DEACTIVATED:
45 return true;
46 default:
47 return false;
48 }
49 }
50
51 } // namespace
52
53 InkDropAnimationControllerImpl::InkDropAnimationControllerImpl(
54 InkDropHost* ink_drop_host)
55 : ink_drop_host_(ink_drop_host),
56 root_layer_(new ui::Layer(ui::LAYER_NOT_DRAWN)),
57 root_layer_added_to_host_(false),
58 is_hovered_(false),
59 hover_after_ripple_timer_(nullptr) {
60 root_layer_->set_name("InkDropAnimationControllerImpl:RootLayer");
61 }
62
63 InkDropAnimationControllerImpl::~InkDropAnimationControllerImpl() {
64 // Explicitly destroy the InkDropRipple so that this still exists if
65 // views::InkDropRippleObserver methods are called on this.
66 DestroyInkDropRipple();
67 DestroyInkDropHover();
68 }
69
70 InkDropState InkDropAnimationControllerImpl::GetTargetInkDropState() const {
71 if (!ink_drop_ripple_)
72 return InkDropState::HIDDEN;
73 return ink_drop_ripple_->target_ink_drop_state();
74 }
75
76 bool InkDropAnimationControllerImpl::IsVisible() const {
77 return ink_drop_ripple_ && ink_drop_ripple_->IsVisible();
78 }
79
80 void InkDropAnimationControllerImpl::AnimateToState(
81 InkDropState ink_drop_state) {
82 DestroyHiddenTargetedAnimations();
83 if (!ink_drop_ripple_)
84 CreateInkDropRipple();
85
86 if (ink_drop_state != views::InkDropState::HIDDEN) {
87 SetHoveredInternal(false, base::TimeDelta::FromMilliseconds(
88 kHoverFadeOutBeforeRippleDurationInMs),
89 true);
90 }
91
92 ink_drop_ripple_->AnimateToState(ink_drop_state);
93 }
94
95 void InkDropAnimationControllerImpl::SnapToActivated() {
96 DestroyHiddenTargetedAnimations();
97 if (!ink_drop_ripple_)
98 CreateInkDropRipple();
99
100 SetHoveredInternal(false, base::TimeDelta(), false);
101
102 ink_drop_ripple_->SnapToActivated();
103 }
104
105 void InkDropAnimationControllerImpl::SetHovered(bool is_hovered) {
106 is_hovered_ = is_hovered;
107 SetHoveredInternal(is_hovered,
108 is_hovered ? base::TimeDelta::FromMilliseconds(
109 kHoverFadeInFromUserInputDurationInMs)
110 : base::TimeDelta::FromMilliseconds(
111 kHoverFadeOutFromUserInputDurationInMs),
112 false);
113 }
114
115 void InkDropAnimationControllerImpl::DestroyHiddenTargetedAnimations() {
116 if (ink_drop_ripple_ &&
117 (ink_drop_ripple_->target_ink_drop_state() == InkDropState::HIDDEN ||
118 ShouldAnimateToHidden(ink_drop_ripple_->target_ink_drop_state()))) {
119 DestroyInkDropRipple();
120 }
121 }
122
123 void InkDropAnimationControllerImpl::CreateInkDropRipple() {
124 DestroyInkDropRipple();
125 ink_drop_ripple_ = ink_drop_host_->CreateInkDropRipple();
126 ink_drop_ripple_->set_observer(this);
127 root_layer_->Add(ink_drop_ripple_->GetRootLayer());
128 AddRootLayerToHostIfNeeded();
129 }
130
131 void InkDropAnimationControllerImpl::DestroyInkDropRipple() {
132 if (!ink_drop_ripple_)
133 return;
134 root_layer_->Remove(ink_drop_ripple_->GetRootLayer());
135 ink_drop_ripple_.reset();
136 RemoveRootLayerFromHostIfNeeded();
137 }
138
139 void InkDropAnimationControllerImpl::CreateInkDropHover() {
140 DestroyInkDropHover();
141
142 hover_ = ink_drop_host_->CreateInkDropHover();
143 if (!hover_)
144 return;
145 hover_->set_observer(this);
146 root_layer_->Add(hover_->layer());
147 AddRootLayerToHostIfNeeded();
148 }
149
150 void InkDropAnimationControllerImpl::DestroyInkDropHover() {
151 if (!hover_)
152 return;
153 root_layer_->Remove(hover_->layer());
154 hover_->set_observer(nullptr);
155 hover_.reset();
156 RemoveRootLayerFromHostIfNeeded();
157 }
158
159 void InkDropAnimationControllerImpl::AddRootLayerToHostIfNeeded() {
160 DCHECK(hover_ || ink_drop_ripple_);
161 if (!root_layer_added_to_host_) {
162 root_layer_added_to_host_ = true;
163 ink_drop_host_->AddInkDropLayer(root_layer_.get());
164 }
165 }
166
167 void InkDropAnimationControllerImpl::RemoveRootLayerFromHostIfNeeded() {
168 if (root_layer_added_to_host_ && !hover_ && !ink_drop_ripple_) {
169 root_layer_added_to_host_ = false;
170 ink_drop_host_->RemoveInkDropLayer(root_layer_.get());
171 }
172 }
173
174 bool InkDropAnimationControllerImpl::IsHoverFadingInOrVisible() const {
175 return hover_ && hover_->IsFadingInOrVisible();
176 }
177
178 // -----------------------------------------------------------------------------
179 // views::InkDropRippleObserver:
180
181 void InkDropAnimationControllerImpl::AnimationStarted(
182 InkDropState ink_drop_state) {}
183
184 void InkDropAnimationControllerImpl::AnimationEnded(
185 InkDropState ink_drop_state,
186 InkDropAnimationEndedReason reason) {
187 if (reason != InkDropAnimationEndedReason::SUCCESS)
188 return;
189 if (ShouldAnimateToHidden(ink_drop_state)) {
190 ink_drop_ripple_->AnimateToState(views::InkDropState::HIDDEN);
191 } else if (ink_drop_state == views::InkDropState::HIDDEN) {
192 if (is_hovered_)
193 StartHoverAfterRippleTimer();
194 // TODO(bruthig): Investigate whether creating and destroying
195 // InkDropRipples is expensive and consider creating an
196 // InkDropRipplePool. See www.crbug.com/522175.
197 DestroyInkDropRipple();
198 }
199 }
200
201 // -----------------------------------------------------------------------------
202 // views::InkDropHoverObserver:
203
204 void InkDropAnimationControllerImpl::AnimationStarted(
205 InkDropHover::AnimationType animation_type) {}
206
207 void InkDropAnimationControllerImpl::AnimationEnded(
208 InkDropHover::AnimationType animation_type,
209 InkDropAnimationEndedReason reason) {
210 if (animation_type == InkDropHover::FADE_OUT &&
211 reason == InkDropAnimationEndedReason::SUCCESS) {
212 DestroyInkDropHover();
213 }
214 }
215
216 void InkDropAnimationControllerImpl::SetHoveredInternal(
217 bool is_hovered,
218 base::TimeDelta animation_duration,
219 bool explode) {
220 StopHoverAfterRippleTimer();
221
222 if (IsHoverFadingInOrVisible() == is_hovered)
223 return;
224
225 if (is_hovered) {
226 CreateInkDropHover();
227 if (hover_ && !IsVisible())
228 hover_->FadeIn(animation_duration);
229 } else {
230 hover_->FadeOut(animation_duration, explode);
231 }
232 }
233
234 void InkDropAnimationControllerImpl::StartHoverAfterRippleTimer() {
235 StopHoverAfterRippleTimer();
236
237 if (!hover_after_ripple_timer_)
238 hover_after_ripple_timer_.reset(new base::OneShotTimer);
239
240 hover_after_ripple_timer_->Start(
241 FROM_HERE,
242 base::TimeDelta::FromMilliseconds(kHoverFadeInAfterRippleDelayInMs),
243 base::Bind(&InkDropAnimationControllerImpl::HoverAfterRippleTimerFired,
244 base::Unretained(this)));
245 }
246
247 void InkDropAnimationControllerImpl::StopHoverAfterRippleTimer() {
248 if (hover_after_ripple_timer_)
249 hover_after_ripple_timer_.reset();
250 }
251
252 void InkDropAnimationControllerImpl::HoverAfterRippleTimerFired() {
253 SetHoveredInternal(true, base::TimeDelta::FromMilliseconds(
254 kHoverFadeInAfterRippleDurationInMs),
255 true);
256 hover_after_ripple_timer_.reset();
257 }
258
259 } // namespace views
OLDNEW
« no previous file with comments | « no previous file | ui/views/animation/ink_drop_factory_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698